export type FrontRpcCode = "VALIDATION" | "NETWORK" | "INTERNAL"; export type FrontResponse = { ok: boolean; code: FrontRpcCode | string; layer: "front"; message: string; context?: Record; data?: T | null; errors?: Record | string[] | null; }; export function buildFrontError( message: string, code: FrontRpcCode = "VALIDATION", context?: Record, errors?: Record | string[] | null, ): FrontResponse { return { ok: false, code, layer: "front", message, context, data: null, errors: errors ?? null, }; } export function buildFrontNetworkError(method: string, path: string): FrontResponse { return buildFrontError( `No se pudo conectar con el servidor en ${method} ${path} — verifique su conexión`, "NETWORK", { route: `${method} ${path}` }, ); } export function isApiEnvelope(value: unknown): value is { ok: boolean; code: string; status?: number; layer: string; message: string; } { return !!value && typeof value === "object" && "ok" in value && "message" in value && "layer" in value; } export function displayErrorMessage(value: unknown, fallback = "Ocurrió un error"): string { if (isApiEnvelope(value)) return value.message; if (value && typeof value === "object" && "message" in value) { return String((value as { message: unknown }).message); } if (value && typeof value === "object" && "error" in value) { return String((value as { error: unknown }).error); } return fallback; }