-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.2-fs_fetch.ts
59 lines (54 loc) · 1.46 KB
/
1.2-fs_fetch.ts
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
47
48
49
50
51
52
53
54
55
56
57
58
59
import { readFileSync, writeFileSync } from "fs";
// bun i -D @types/bun (Bun)
// pnpm i -D @types/node (Node.js w/ pnpm)
/*
1. Reading config file
*/
const config = JSON.parse(readFileSync("2-config.json", "utf8"));
// Sample Promise
console.log("Promise not yet created.");
const textEventually = new Promise<string>((resolve) => {
setTimeout(() => resolve("Promise done!"), 1000);
});
console.log("Promise created!");
console.log(await textEventually);
/*
2. Fetching website with GET request
*/
const response = await fetch("https://dummyjson.com/products").then((res) =>
res.json()
);
/*
3. Computing results (List reduce, map, filter, sort, flatMap)
*/
const results = {
totalStock: response.products.reduce(
(sum, product) => sum + product.stock,
0
),
products: response.products
.map((x) => ({
...x,
discountedPrice: (x.price * (100 - x.discountPercentage)) / 100,
}))
.filter((x) => x.discountedPrice >= config.minPrice)
.sort((a, b) => a[config.sort] - b[config.sort]),
cases: response.products.flatMap((x) => {
switch (x.category) {
case "laptops":
return x.title + " case (Silver)";
case "smartphones":
return [
x.title + " case (White)",
x.title + " case (Blue)",
x.title + " case (Purple)",
];
default:
return [];
}
}),
};
/*
4. Writing results to file
*/
writeFileSync("2-results.json", JSON.stringify(results, null, 4));