mirror of
https://origin.cursor.com/mrdevmx/panels.git
synced 2026-10-09 13:23:17 +00:00
El Excel Neodata por partida sí se leía (A001–A009, 9 semanas), pero
la API devolvía { ok, data } sin envelope y el panel no desembalaba
data: el diálogo mostraba 0 y «Sin match». Ahora se aplana la
respuesta y el preview lista las partidas encontradas.
Co-authored-by: alberto.martinez <alberto.martinez@mrdev.mx>
82 lines
2.3 KiB
TypeScript
82 lines
2.3 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;
|
|
data?: unknown;
|
|
} {
|
|
return !!value && typeof value === "object"
|
|
&& "ok" in value && "message" in value && "layer" in value;
|
|
}
|
|
|
|
/** Normaliza respuestas `{ ok, data }` (con o sin envelope RPC) al shape plano que esperan las pantallas. */
|
|
export function unwrapApiPayload<T>(body: unknown): T {
|
|
if (!body || typeof body !== "object") return body as T;
|
|
const rec = body as Record<string, unknown>;
|
|
const data = rec.data;
|
|
const canSpread = "ok" in rec
|
|
&& data != null
|
|
&& typeof data === "object"
|
|
&& !Array.isArray(data);
|
|
if (!isApiEnvelope(body) && !canSpread) return body as T;
|
|
const spread = canSpread ? data as Record<string, unknown> : {};
|
|
return {
|
|
...spread,
|
|
data,
|
|
ok: rec.ok,
|
|
code: rec.code,
|
|
message: rec.message,
|
|
status: rec.status,
|
|
layer: rec.layer,
|
|
} as T;
|
|
}
|
|
|
|
/** 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;
|
|
}
|