Actualizar frontend/src/lib/api.js

This commit is contained in:
2026-10-07 12:53:11 -05:00
parent 526723faa7
commit f68926f078

View File

@@ -1,5 +1,23 @@
// Cliente del backend FastAPI de Leads (puerto 8001).
const BASE_URL = import.meta.env.VITE_API_URL || "https://dashboardapi-leads.escueladerefrigeracion.lat";
// Cliente del backend FastAPI de Leads.
// 1) Si existe VITE_API_URL (archivo .env del frontend), se usa esa.
// 2) Si no, se decide por el host desde el que se abrió la página:
// localhost -> backend local http://localhost:8001
// cualquier otro -> API publicada del servidor
const API_LOCAL = "http://localhost:8001";
const API_SERVIDOR = "https://api-leads.escueladerefrigeracion.lat";
function _baseUrlPorDefecto() {
if (typeof window === "undefined") return API_LOCAL;
const h = window.location.hostname;
return h === "localhost" || h === "127.0.0.1" || h === "::1"
? API_LOCAL
: API_SERVIDOR;
}
const BASE_URL = (
import.meta.env.VITE_API_URL || _baseUrlPorDefecto()
).replace(/\/+$/, "");
// ── Cache en memoria (frontend) con TTL 15 min y "stale-while-revalidate" ──
// Guarda la respuesta de cada GET (por path+params). Al volver a pedir:
// - si hay dato vigente (< 15 min) -> lo devuelve al instante, SIN fetch.