клиент

This commit is contained in:
2025-04-16 22:13:40 +04:00
parent 5e341c99a2
commit 7cd561e86b

40
components/api/client.js Normal file
View File

@@ -0,0 +1,40 @@
const URL = "http://localhost:5174/";
const makeRequest = async (path, params, vars, method = "GET", data = null) => {
try {
const requestParams = params ? `?${params}` : "";
const pathVariables = vars ? `/${vars}` : "";
const options = { method };
const hasBody = (method === "POST" || method === "PUT") && data;
if (hasBody) {
options.headers = { "Content-Type": "application/json;charset=utf-8" };
options.body = JSON.stringify(data);
}
const response = await fetch(`${URL}${path}${pathVariables}${requestParams}`, options);
if (!response.ok) {
throw new Error(`Response status: ${response?.status}`);
}
const json = await response.json();
console.debug(path, json);
return json;
}
catch (error) {
if (error instanceof SyntaxError) {
throw new Error("There was a SyntaxError", error);
}
else {
throw new Error("There was an error", error);
}
}
};
export const getAllItems = (path, params) => makeRequest(path, params);
export const getItem = (path, id) => makeRequest(path, null, id);
export const createItem = (path, data) => makeRequest(path, null, null, "POST", data);
export const updateItem = (path, id, data) => makeRequest(path, null, id, "PUT", data);
export const deleteItem = (path, id) => makeRequest(path, null, id, "DELETE");