gitscope
← marzneshin / v0.5.4 / dashboard/src/utils/fetch.ts
https://dev.kronoz.win/r/marzneshin.git
dashboard/src/utils/fetch.ts · TypeScript
Blame Raw
 1
 2import { FetchOptions, ofetch } from 'ofetch';
 3import { useAuth } from '@marzneshin/features/auth';
 4
 5export const $fetch = ofetch.create({
 6    baseURL: import.meta.env.VITE_BASE_API,
 7});
 8
 9export const fetcher = <T = any>(
10    url: string,
11    ops: FetchOptions<'json'> = {}
12) => {
13    const token = useAuth.getState().getAuthToken();
14    if (token) {
15        ops['headers'] = {
16            ...(ops?.headers || {}),
17            Authorization: `Bearer ${token}`,
18        };
19    }
20    return $fetch<T>(url, ops);
21};
22
23export const fetch = fetcher;