-
Notifications
You must be signed in to change notification settings - Fork 0
/
CacheApiCall.ts
48 lines (45 loc) · 1.39 KB
/
CacheApiCall.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
const generateKey = <T extends object>(path: string, obj: T): string => {
const objectKeys = Object.keys(obj)
.sort((a, b) => a.localeCompare(b))
.map((key) => {
const keyItem = key as keyof T;
if (typeof obj[keyItem] === "object") {
return `${key}:${generateKey(path, obj[keyItem] as object)}`;
}
return `${key}:${obj[keyItem]}`;
})
.join("&");
return path + objectKeys;
};
type MapValue = { result: object; date: number };
function cachedApiCall(limit: number) {
const map = new Map<string, MapValue>();
return async function <T extends object>(
path: Parameters<typeof fetch>[0],
config?: T
) {
const key = generateKey(path.toString(), config ?? {});
if (map.has(key)) {
if (Math.abs(map.get(key)!.date - Date.now()) < limit) {
return map.get(key)!.result;
}
}
const result = await fetch(path).then((res) => res.json());
map.set(key, { result, date: Date.now() });
return result;
};
}
const call = cachedApiCall(3000);
call("https://jsonplaceholder.typicode.com/todos/1", { keyword: "dev" }).then(
(a) => console.log(a)
);
setTimeout(() => {
call("https://jsonplaceholder.typicode.com/todos/1", {
keyword: "dev1",
}).then((a) => console.log(a));
}, 2500);
setTimeout(() => {
call("https://jsonplaceholder.typicode.com/todos/1", {
keyword: "dev1",
}).then((a) => console.log(a));
}, 4000);