Files
InternetProg_MorozovDV_PIbd-22/html/mvc/api/client.js

17 lines
816 B
JavaScript

const BASE_URL = "http://localhost:5174"; // Порт json-server
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();
}
export const getAllItems = (entity) => request(entity);
export const getItem = (entity, id) => request(`${entity}/${id}`);
export const createItem = (entity, data) => request(entity, "POST", data);
export const updateItem = (entity, id, data) => request(`${entity}/${id}`, "PATCH", data);
export const deleteItem = (entity, id) => request(`${entity}/${id}`, "DELETE");