fix and update
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user