commit16
This commit is contained in:
@@ -174,12 +174,21 @@ export function money(r: Opportunity) {
|
||||
: `${new Intl.NumberFormat('pt-MZ', { maximumFractionDigits: 2 }).format(Number(r.estimated_investment_amount))} ${r.estimated_investment_currency}`;
|
||||
}
|
||||
export async function api(url: string, body?: unknown) {
|
||||
const token = decodeURIComponent(
|
||||
document.cookie
|
||||
.split('; ')
|
||||
.find((c) => c.startsWith('XSRF-TOKEN='))
|
||||
?.slice(11) ?? '',
|
||||
);
|
||||
const encodedToken =
|
||||
body === undefined || typeof document === 'undefined'
|
||||
? ''
|
||||
: (document.cookie
|
||||
.split('; ')
|
||||
.find((c) => c.startsWith('XSRF-TOKEN='))
|
||||
?.slice(11) ?? '');
|
||||
let token = '';
|
||||
try {
|
||||
token = decodeURIComponent(encodedToken);
|
||||
} catch {
|
||||
throw new Error(
|
||||
'A sessão não pôde ser validada. Actualize a página e tente novamente.',
|
||||
);
|
||||
}
|
||||
const formData =
|
||||
typeof FormData !== 'undefined' && body instanceof FormData;
|
||||
const response = await fetch(url, {
|
||||
@@ -194,10 +203,14 @@ export async function api(url: string, body?: unknown) {
|
||||
? {}
|
||||
: { body: formData ? body : JSON.stringify(body) }),
|
||||
});
|
||||
const data = await response.json();
|
||||
if (!response.ok)
|
||||
const data = await response.json().catch(() => null);
|
||||
if (!response.ok || (data === null && response.status !== 204))
|
||||
throw new Error(
|
||||
data.message ?? 'Não foi possível concluir a operação.',
|
||||
typeof data?.message === 'string'
|
||||
? data.message
|
||||
: response.status === 419
|
||||
? 'A sessão expirou. Actualize a página e tente novamente.'
|
||||
: 'Não foi possível concluir a operação. Tente novamente.',
|
||||
);
|
||||
return data;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user