fix and update

This commit is contained in:
lendry
2026-06-29 18:14:00 +03:00
parent 71dfeda873
commit 5a220917dc
4 changed files with 169 additions and 70 deletions

View File

@@ -612,16 +612,21 @@ export function buildSystemSettingPayload(setting: Pick<SystemSetting, 'key' | '
export async function apiFetch<T>(path: string, options: RequestInit = {}, token?: string | null, allowRetry = true): Promise<T> {
const resolvedToken = token ?? (typeof window !== 'undefined' ? window.localStorage.getItem(AUTH_TOKEN_KEY) : null);
const method = (options.method ?? 'GET').toUpperCase();
const hasBody = options.body !== undefined && options.body !== null;
const headers: Record<string, string> = {
...(hasBody || method === 'POST' || method === 'PUT' || method === 'PATCH'
? { 'Content-Type': 'application/json' }
: {}),
...(resolvedToken ? { Authorization: `Bearer ${resolvedToken}` } : {}),
...(options.headers as Record<string, string> | undefined)
};
let response: Response;
try {
response = await fetch(`${getApiUrl()}${path}`, {
...options,
headers: {
'Content-Type': 'application/json',
...(resolvedToken ? { Authorization: `Bearer ${resolvedToken}` } : {}),
...options.headers
}
headers
});
} catch (error) {
throw mapFetchError(error);