82 lines
2.1 KiB
JavaScript
82 lines
2.1 KiB
JavaScript
import config from "../src/config.json" with { type: "json" };
|
|
|
|
const url = `http://localhost:${config.port}/api/v1`;
|
|
|
|
const apiCookie = async (endpoint, request) => {
|
|
const response = await fetch(`${url}/${endpoint}`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(request),
|
|
});
|
|
|
|
const responseCookie = response.headers.getSetCookie().at(0) ?? "";
|
|
|
|
return [await response.json(), responseCookie];
|
|
};
|
|
|
|
const apiToken = async (endpoint, cookie) => {
|
|
const response = await fetch(`${url}/${endpoint}`, {
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Cookie: cookie,
|
|
},
|
|
});
|
|
|
|
return await response.json();
|
|
};
|
|
|
|
const apiGet = async (endpoint, token) => {
|
|
const response = await fetch(`${url}/${endpoint}`, {
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
});
|
|
|
|
return await response.json();
|
|
};
|
|
|
|
const apiPost = async (endpoint, request, token) => {
|
|
const response = await fetch(`${url}/${endpoint}`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
body: JSON.stringify(request),
|
|
});
|
|
|
|
return await response.json();
|
|
};
|
|
|
|
const apiPatch = async (endpoint, request, token) => {
|
|
const response = await fetch(`${url}/${endpoint}`, {
|
|
method: "PATCH",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
body: JSON.stringify(request),
|
|
});
|
|
|
|
return await response.json();
|
|
};
|
|
|
|
const apiDelete = async (endpoint, request, token) => {
|
|
const response = await fetch(`${url}/${endpoint}`, {
|
|
method: "DELETE",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
body: JSON.stringify(request),
|
|
});
|
|
|
|
return await response.json();
|
|
};
|
|
|
|
export { apiCookie, apiToken, apiGet, apiPost, apiPatch, apiDelete };
|