From f68926f07815a334bb2370499f6cf223ebea0ea8 Mon Sep 17 00:00:00 2001 From: Administrator Date: Wed, 7 Oct 2026 12:53:11 -0500 Subject: [PATCH] Actualizar frontend/src/lib/api.js --- frontend/src/lib/api.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/frontend/src/lib/api.js b/frontend/src/lib/api.js index 3a4c329..1b06eb0 100644 --- a/frontend/src/lib/api.js +++ b/frontend/src/lib/api.js @@ -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. @@ -196,4 +214,4 @@ export const api = { borrarCampania: (id) => post("/api/campanias/borrar", { id }, ["/api/campanias", "/api/leads", "/api/vendedores", "/api/roas", "/api/otros-general", "/api/programa"]), -}; +}; \ No newline at end of file