Agent-Logs-Url: https://github.com/pdf114514/CarReservation/sessions/c0a4b7dc-228e-4e7d-a985-61b9a17de159 Co-authored-by: pdf114514 <57948770+pdf114514@users.noreply.github.com>
34 lines
1.3 KiB
JavaScript
34 lines
1.3 KiB
JavaScript
const API_BASE = (import.meta.env.VITE_API_BASE_URL ?? '') + '/api';
|
|
|
|
async function request(path, options = {}) {
|
|
const hasBody = options.body !== undefined;
|
|
const res = await fetch(`${API_BASE}${path}`, {
|
|
credentials: 'include',
|
|
headers: {
|
|
...(hasBody ? { 'Content-Type': 'application/json' } : {}),
|
|
...(options.headers ?? {}),
|
|
},
|
|
...options,
|
|
});
|
|
if (!res.ok) {
|
|
const err = await res.json().catch(() => ({ error: 'Unknown error' }));
|
|
throw new Error(err.error || `HTTP ${res.status}`);
|
|
}
|
|
return res.json();
|
|
}
|
|
|
|
export const api = {
|
|
// Cars
|
|
getCars: () => request('/cars'),
|
|
createCar: (data) => request('/cars', { method: 'POST', body: JSON.stringify(data) }),
|
|
updateCar: (id, data) => request(`/cars/${id}`, { method: 'PUT', body: JSON.stringify(data) }),
|
|
deleteCar: (id) => request(`/cars/${id}`, { method: 'DELETE' }),
|
|
reorderCars: (ids) => request('/cars/reorder', { method: 'PUT', body: JSON.stringify({ ids }) }),
|
|
|
|
// Reservations
|
|
getReservations: () => request('/reservations'),
|
|
createReservation: (data) => request('/reservations', { method: 'POST', body: JSON.stringify(data) }),
|
|
updateReservation: (id, data) => request(`/reservations/${id}`, { method: 'PUT', body: JSON.stringify(data) }),
|
|
deleteReservation: (id) => request(`/reservations/${id}`, { method: 'DELETE' }),
|
|
};
|