designtopack/src/stores/api.js

69 lines
1.6 KiB
JavaScript
Raw Normal View History

2024-07-10 18:43:46 +02:00
import { defineStore } from "pinia";
export const useApiStore = defineStore("counter", () => {
2024-07-11 12:42:29 +02:00
async function fetchPageData() {
const isHomePage = window.location.pathname === "/";
const url = isHomePage
? `${window.location.href}home.json`
: `${window.location.href}.json`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error(
"Une erreur s'est produite lors de la récupération des données :",
error
);
throw error;
}
}
function fetchDataThroughKQL() {
2024-07-10 18:43:46 +02:00
const api = "/api/query";
const username = import.meta.env.VITE_USERNAME;
const password = import.meta.env.VITE_PASSWORD;
const token = btoa(`${username}:${password}`);
const headers = {
Authorization: `Basic ${token}`,
};
const request = {
method: "post",
body: JSON.stringify({
query: `page('home')`,
select: {
testImages: {
query: "page.testImages.toFiles",
select: {
url: true,
},
},
blocks: {
query: "page.testBlocks.toBlocks",
},
},
}),
headers,
};
fetch(api, request)
.then((response) => response.json())
.then((response) => {
console.log(response);
})
.catch((error) => {
console.log(error);
});
}
2024-07-11 12:42:29 +02:00
return { fetchDataThroughKQL, fetchPageData };
2024-07-10 18:43:46 +02:00
});