ceiba-scores/scripts/fetchData.js

46 lines
1.1 KiB
JavaScript

import fs from "fs/promises";
const apiUrl = "https://admin.ceiba-conseil.com";
async function fetchJSONApi(path) {
const url = `${apiUrl}${path}`;
const options = {
method: "GET",
headers: {
"content-type": "application/json",
},
};
console.log(`fetchJSONApi: ${url}`);
const response = await fetch(url, options);
if (!response.ok) {
const errors = await response.json();
throw errors.errors[0].message;
}
return response.json();
}
async function fetchData() {
const fields = [
"*",
"translations.*",
"questions.sort",
"questions.questions_id.*",
"questions.questions_id.translations.*",
"questions.questions_id.answers.*",
"questions.questions_id.answers.answers_id.*",
"questions.questions_id.answers.answers_id.translations.*",
"results.*",
"results.results_id.*",
"results.results_id.translations.*",
];
const url = `/items/scores?${fields
.map((item) => `fields[]=${item}`)
.join("&")}`;
const data = await fetchJSONApi(url);
await fs.writeFile("./src/data.json", JSON.stringify(data.data), "utf8");
}
fetchData();