Files
InternetProg_MorozovDV_PIbd-22/html/mvc/api/client.js
2025-05-16 02:30:48 +04:00

22 lines
778 B
JavaScript

const BASE_URL = "http://localhost:5174";
async function request(path, method = "GET", body = null) {
const options = { method, headers: { "Content-Type": "application/json" } };
if (body) options.body = JSON.stringify(body);
const response = await fetch(`${BASE_URL}/${path}`, options);
if (!response.ok) {
throw new Error(`Ошибка запроса: ${response.status}`);
}
return response.json();
}
const api = {
getAll: (entity) => request(entity),
getById: (entity, id) => request(`${entity}/${id}`),
create: (entity, data) => request(entity, "POST", data),
update: (entity, id, data) => request(`${entity}/${id}`, "PATCH", data),
delete: (entity, id) => request(`${entity}/${id}`, "DELETE"),
};
export default api;