This commit is contained in:
2026-11-02 20:31:15 +02:00
parent 8ddfa93f45
commit 1da273f1f3
131 changed files with 14802 additions and 526 deletions
+22 -9
View File
@@ -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;
}