-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom-backend-task.js
37 lines (33 loc) · 1.01 KB
/
custom-backend-task.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
// custom-backend-task.js
import fs from "node:fs";
import fetch from "make-fetch-happen";
async function fetchAndCachePlaceCalData(config, context) {
let placeCalData = null;
const cacheDir = `${context.cwd}/.cache/`;
const cachePath = `${context.cwd}/.cache/${config.collection}.json`;
if (!fs.existsSync(cacheDir)) {
fs.mkdirSync(cacheDir, (error, data) => {
console.log("DATAEO", data);
console.log("ERREO", error);
return;
});
}
if (fs.existsSync(cachePath)) {
placeCalData = JSON.parse(fs.readFileSync(cachePath, 'utf8'));
} else {
const response = await fetch(config.url, {
"method": "POST",
"headers": { "content-type": "application/json" },
"body": JSON.stringify({ "query": config.query.query })
});
if (response) {
const collectionJson = await response.json();
fs.writeFileSync(cachePath, JSON.stringify(collectionJson));
placeCalData = collectionJson;
}
}
return placeCalData;
}
export {
fetchAndCachePlaceCalData
};