diff --git a/src/services/api.js b/src/services/api.js index ffabf30..8f868f4 100644 --- a/src/services/api.js +++ b/src/services/api.js @@ -1,49 +1,66 @@ -// src/services/api.js +const BASE_URL = import.meta.env.VITE_API_BASE_URL || ''; -const apiCall = async (url, body = {}) => { - const response = await fetch(url, { - method: 'POST', +const apiCall = async (url, body = null) => { + // Configuration de base pour l'appel réseau + const options = { + method: body ? 'POST' : 'GET', // Devient GET s'il n'y a pas de body (ex: getClientProfile) headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', }, - credentials: 'include', // Garde bien ça pour envoyer le cookie de session - body: JSON.stringify(body) - }); + credentials: 'include', // CRUCIAL : Maintient la session active avec FOSSBilling + }; + if (body) { + options.body = JSON.stringify(body); + } + + const response = await fetch(url, options); const data = await response.json(); + + // Interception des erreurs de l'API FOSSBilling if (data.error) throw new Error(data.error.message); + return data.result; }; +// ========================================== +// ROUTES FOSSBILLING NATIVES (BACKTICKS INTÉGRÉS) +// ========================================== + export const loginClient = (email, password) => - apiCall('/api/guest/client/login', { email, password }); + apiCall(`${BASE_URL}/api/guest/client/login`, { email, password }); export const getClientProfile = () => - apiCall('/api/client/profile/get'); + apiCall(`${BASE_URL}/api/client/profile/get`); // Récupère la liste des services/commandes du client export const getClientOrders = () => - apiCall('/api/client/order/get_list'); + apiCall(`${BASE_URL}/api/client/order/get_list`); // Récupère les détails techniques du service rattaché à une commande export const getOrderService = (order_id) => - apiCall('/api/client/order/service', { id: order_id }); + apiCall(`${BASE_URL}/api/client/order/service`, { id: order_id }); + +// ========================================== +// ROUTES PERSONNALISÉES (CUSTOM API) +// ========================================== -// Fonction d'inscription unifiee pour créer un compte client et commander un service en une seule étape export const registerUnifiedClient = async (email, username, password, firstName, lastName) => { try { - const response = await fetch('https://web.gise.be/custom_api/signup.php', { + // Utilisation des backticks ici aussi ! + const response = await fetch(`${BASE_URL}/custom_api/signup.php`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email: email, username: username, password: password, - first_name: firstName, // L'étiquette est "first_name", le contenu est ta variable JS "firstName" - last_name: lastName // L'étiquette est "last_name", le contenu est ta variable JS "lastName" + first_name: firstName, + last_name: lastName }) }); + const data = await response.json(); if (data.error) throw new Error(data.error.message); return data.result;