// ====================================================== // API CORE REQUEST // Funzione centrale che gestisce TUTTE le chiamate API // - Applica BASE_URL // - Applica token // - Gestisce errori // - Converte automaticamente in JSON // ====================================================== async function apiRequest(method, path, body = null) { // Se BASE_URL non è ancora stato caricato da config.js if (!Admin.BASE_URL) { console.error("BASE_URL non ancora inizializzato"); throw new Error("BASE_URL non inizializzato"); } // Opzioni comuni a tutte le richieste const options = { method, headers: { "Authorization": "Bearer " + Admin.token, "Content-Type": "application/json" } }; // Se c'è un body, lo serializziamo if (body) { options.body = JSON.stringify(body); } // Eseguiamo la richiesta const res = await fetch(`${Admin.BASE_URL}${path}`, options); // Gestione errori centralizzata if (!res.ok) { const text = await res.text(); throw new Error(`Errore API ${res.status}: ${text}`); } // Ritorna direttamente il JSON return res.json(); } // ====================================================== // API GET // Richiesta GET semplice // ====================================================== function apiGet(path) { return apiRequest("GET", path); } // ====================================================== // API POST // Richiesta POST con body opzionale // ====================================================== function apiPost(path, body = null) { return apiRequest("POST", path, body); } // ====================================================== // API DELETE // Richiesta DELETE semplice // ====================================================== function apiDelete(path) { return apiRequest("DELETE", path); }