panels-origin/web-panel/composables/api-response.ts
Cursor Agent b58791aea4
refactor(core): finish RPC migration for budget, excel import, and docs
- Replace remaining budget.ts prepare() with fn_budget_replace and fn_project_update
- Migrate excel.ts findExisting, document store, assign, and catalogs to RPC
- Add 015-rpc-documents.sql for project/company/worker document metadata
- Document RPC envelope conventions in db/README.md
- Add http_errors_test and front api-response composables

Co-authored-by: alberto.martinez <alberto.martinez@mrdev.mx>
2026-09-04 00:01:57 +00:00

59 lines
1.6 KiB
TypeScript

export type FrontRpcCode = "VALIDATION" | "NETWORK" | "INTERNAL";
export type FrontResponse<T = unknown> = {
ok: boolean;
code: FrontRpcCode | string;
layer: "front";
message: string;
context?: Record<string, unknown>;
data?: T | null;
errors?: Record<string, string> | string[] | null;
};
export function buildFrontError(
message: string,
code: FrontRpcCode = "VALIDATION",
context?: Record<string, unknown>,
errors?: Record<string, string> | 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;
}
/** Muestra el mensaje detallado de la API o front sin reescribirlo. */
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;
}