Files
anotherboobsdirectory/packages/frontend/src/services/api.js
Nicola Malizia 41da3553aa basic profile
2025-10-10 19:22:01 +02:00

48 lines
1.5 KiB
JavaScript

const API_BASE_URL = import.meta.env.VITE_API_URL || '/api';
const handleResponse = async (response) => {
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || 'Si è verificato un errore');
}
return response.json();
};
export const getCelebrities = async () => {
const response = await fetch(`${API_BASE_URL}/celebrities/`);
return handleResponse(response);
};
export const getCelebrityById = async (id) => {
const response = await fetch(`${API_BASE_URL}/celebrities/${id}`);
return handleResponse(response);
};
export const createCelebrity = async (celebrityData) => {
const response = await fetch(`${API_BASE_URL}/celebrities/`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(celebrityData),
});
return handleResponse(response);
};
export const updateCelebrity = async (id, celebrityData) => {
const response = await fetch(`${API_BASE_URL}/celebrities/${id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(celebrityData),
});
return handleResponse(response);
};
export const deleteCelebrity = async (id) => {
const response = await fetch(`${API_BASE_URL}/celebrities/${id}`, {
method: 'DELETE',
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || 'Errore durante l\'eliminazione');
}
return response.json(); // O un messaggio di successo
};