This repository has been archived on 2026-01-21. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Acad-IA/src/lib/api.ts
T

35 lines
1.0 KiB
TypeScript

// api.ts
const API_BASE =
(import.meta.env.VITE_API_BASE?.replace(/\/$/, "")) ||
"https://genesis-engine.apps.lci.ulsa.mx"; // 👈 tu Bun.serve real
export async function postAPI<T=any>(path: string, body: any): Promise<T> {
const url = `${API_BASE}${path}`;
const r = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
// Si necesitas cookies, agrega: credentials: "include",
body: JSON.stringify(body),
});
const ct = r.headers.get("content-type") || "";
const isHTML = ct.includes("text/html");
const text = await r.text();
if (!r.ok) {
throw new Error(
isHTML
? "El servidor respondió HTML (probable 404 del dashboard/puerto 3000). Revisa API_BASE y el puerto."
: text
);
}
// Si el server devolvió JSON correctamente:
if (ct.includes("application/json")) return JSON.parse(text) as T;
// Último recurso: intenta parsear
try { return JSON.parse(text) as T } catch {
throw new Error("Respuesta no-JSON desde la API.");
}
}