22 lines
778 B
JavaScript
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;
|