https://dev.kronoz.win/r/marzneshin.git
1
2import { FetchOptions, ofetch } from 'ofetch';
3import { useAuth } from '@marzneshin/modules/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;