// Cliente HTTP del API Gateway de FRASA (Express + Prisma) const API_URL = import.meta.env.VITE_API_URL ?? 'http://192.168.10.123:3000/api' // TODO produccion: '/api' same-origin detras de NGINX /** * Error HTTP del API (respuesta con status >= 400, p. ej. validación o 404). * NO amerita fallback offline: el servidor respondió y su respuesta manda. */ export class ErrorApi extends Error { constructor( public readonly status: number, message: string, ) { super(message) this.name = 'ErrorApi' } } /** * fetch solo rechaza (TypeError) cuando falla la red; una respuesta HTTP de * error resuelve normalmente. El fallback offline solo aplica al primer caso. */ export function esErrorDeRed(err: unknown): boolean { return err instanceof TypeError } /** Llama al API Gateway; lanza ErrorApi con el mensaje del servidor si el status no es 2xx */ export async function request(ruta: string, init?: RequestInit): Promise { const res = await fetch(`${API_URL}${ruta}`, { headers: { 'Content-Type': 'application/json' }, ...init, }) if (!res.ok) { let mensaje = `Error ${res.status} al llamar ${ruta}` try { const cuerpo = (await res.json()) as { error?: string } if (typeof cuerpo.error === 'string') mensaje = cuerpo.error } catch { // cuerpo no JSON: se queda el mensaje genérico } throw new ErrorApi(res.status, mensaje) } return (await res.json()) as T }