gitscope
← marzneshin / v0.1.1 / app/dashboard/src/service/http.ts
https://dev.kronoz.win/r/marzneshin.git
app/dashboard/src/service/http.ts · TypeScript
Blame Raw
 1import { FetchOptions, $fetch as ohMyFetch } from 'ohmyfetch';
 2import { getAuthToken } from 'service/auth-storage';
 3
 4export const $fetch = ohMyFetch.create({
 5  baseURL: import.meta.env.VITE_BASE_API,
 6});
 7
 8export const fetcher = <T = any>(
 9  url: string,
10  ops: FetchOptions<'json'> = {}
11) => {
12  const token = getAuthToken();
13  if (token) {
14    ops['headers'] = {
15      ...(ops?.headers || {}),
16      Authorization: `Bearer ${getAuthToken()}`,
17    };
18  }
19  return $fetch<T>(url, ops);
20};
21
22export const fetch = fetcher;
23
24export const fetchAdminLoader = () => {
25  return fetch('/admins', {
26    headers: {
27      Authorization: `Bearer ${getAuthToken()}`,
28    },
29  });
30};