commit16
This commit is contained in:
@@ -101,7 +101,9 @@ export class PortalApiError extends Error {
|
||||
? 'O registo solicitado já não está disponível.'
|
||||
: status === 422
|
||||
? 'Verifique os campos assinalados.'
|
||||
: 'Não foi possível comunicar com o portal. Tente novamente.',
|
||||
: status === 419
|
||||
? 'A sessão não pôde ser validada. Actualize a página e tente novamente.'
|
||||
: 'Não foi possível comunicar com o portal. Tente novamente.',
|
||||
);
|
||||
this.status = status;
|
||||
this.errors = errors;
|
||||
@@ -122,10 +124,15 @@ export async function portalRequest<T>(
|
||||
typeof document === 'undefined'
|
||||
? undefined
|
||||
: document.cookie
|
||||
.split('; ')
|
||||
.split(';')
|
||||
.map((value) => value.trim())
|
||||
.find((value) => value.startsWith('XSRF-TOKEN='))
|
||||
?.slice(11);
|
||||
if (token) headers.set('X-XSRF-TOKEN', decodeURIComponent(token));
|
||||
try {
|
||||
if (token) headers.set('X-XSRF-TOKEN', decodeURIComponent(token));
|
||||
} catch {
|
||||
throw new PortalApiError(419);
|
||||
}
|
||||
}
|
||||
const response = await fetch(url, {
|
||||
...options,
|
||||
@@ -136,7 +143,7 @@ export async function portalRequest<T>(
|
||||
if (!response.ok || body === null) {
|
||||
throw new PortalApiError(
|
||||
response.status,
|
||||
response.status === 422 ? body?.errors : undefined,
|
||||
response.status === 422 ? (body?.errors ?? {}) : undefined,
|
||||
);
|
||||
}
|
||||
return body as T;
|
||||
|
||||
Reference in New Issue
Block a user