fix(pos): handle expired API sessions and add receipt preview close button

This commit is contained in:
Deploy
2026-08-26 07:05:02 +02:00
parent b6af852b54
commit 290ced173b
14 changed files with 52 additions and 17 deletions

View File

@@ -12,6 +12,15 @@ async function apiFetch<T>(path: string, init?: RequestInit): Promise<T> {
...(init?.headers ?? {}),
},
});
if (res.status === 401) {
if (typeof window !== 'undefined') {
window.location.href = '/login';
}
throw Object.assign(new Error('Sesión caducada, identifícate de nuevo'), {
status: 401,
code: 'UNAUTHORIZED',
});
}
if (!res.ok) {
const err = (await res.json().catch(() => ({ message: res.statusText }))) as {
message?: string;
@@ -22,6 +31,15 @@ async function apiFetch<T>(path: string, init?: RequestInit): Promise<T> {
code: err.error?.code,
});
}
// Defensive: if a proxy/misconfiguration ever returns HTML on a successful
// response, surface a readable message instead of a JSON parse crash.
const contentType = res.headers.get('content-type') ?? '';
if (!contentType.includes('application/json')) {
throw Object.assign(new Error('Respuesta inesperada del servidor (sesión o proxy)'), {
status: res.status,
code: 'INVALID_RESPONSE',
});
}
return res.json() as Promise<T>;
}