-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfetchMockData.js
46 lines (41 loc) · 1.03 KB
/
fetchMockData.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import https from "https";
import fs from "fs";
import chalk from "chalk";
import chalkTemplate from "chalk-template";
const messages = {
success: (jsonPath) => chalkTemplate`
{green.bold Loaded JSON file}
{bold ${jsonPath}}
`,
};
const fetchMockData = (jsonPath) =>
new Promise((resolve, reject) => {
if (jsonPath.includes("http")) {
https
.get(jsonPath, (res) => {
let body = "";
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", async () => {
try {
await resolve(body);
} catch (error) {
reject(error.message);
}
});
})
.on("error", (error) => {
reject(error.message);
});
} else {
fs.readFile(jsonPath, async (error, data) => {
if (error) throw error;
await resolve(data);
});
}
}).then((data) => {
console.log(messages.success(jsonPath));
return data;
});
export default fetchMockData;