56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
import config from "../src/config.json" with { type: "json" };
|
|
|
|
const url = `http://localhost:${config.port}/api/v1`;
|
|
|
|
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 { apiGet, apiPost, apiPatch, apiDelete };
|