|
1 |
| -/* eslint-disable @typescript-eslint/no-explicit-any */ |
2 |
| -import axios, { AxiosError, type AxiosRequestConfig, type AxiosResponse } from 'axios' |
| 1 | +export * from './axios.client' |
3 | 2 |
|
4 |
| -const axiosClient = axios.create({ |
5 |
| - baseURL: import.meta.env.VITE_API_URL |
6 |
| -}) |
7 |
| - |
8 |
| -axiosClient.interceptors.request.use((config) => { |
9 |
| - config.headers['Content-Type'] = 'application/json' |
10 |
| - config.headers['Access-Control-Allow-Origin'] = '*' |
11 |
| - // X-CSRFToken: FGWp2aF83aGv8UNqTUzKom4RwMfsYo5kSqzdHahcsi495vgsyYnzBEgY9OohXcWr |
12 |
| - |
13 |
| - const accessToken = localStorage.getItem('accessToken') |
14 |
| - if (accessToken) { |
15 |
| - config.headers.Authorization = `Bearer ${accessToken}` |
16 |
| - } |
17 |
| - |
18 |
| - return config |
19 |
| -}) |
20 |
| - |
21 |
| -axiosClient.interceptors.response.use( |
22 |
| - (response) => response, |
23 |
| - async (error: AxiosError) => { |
24 |
| - const resp = error.response as any |
25 |
| - const respErrorCode = resp?.status ?? 500 |
26 |
| - if (respErrorCode === 401) { |
27 |
| - localStorage.removeItem('accessToken') |
28 |
| - localStorage.removeItem('refreshToken') |
29 |
| - window.location.href = '/login' |
30 |
| - } |
31 |
| - const respErrorMessage = resp?.data?.error ?? resp?.data?.message ?? 'UNKNOWN_ERROR' |
32 |
| - |
33 |
| - throw new Error(respErrorMessage) |
34 |
| - } |
35 |
| -) |
36 |
| - |
37 |
| -function $post<T = any, R = AxiosResponse<T>, D = any>( |
38 |
| - url: string, |
39 |
| - data?: D, |
40 |
| - config?: AxiosRequestConfig<D> |
41 |
| -): Promise<R> { |
42 |
| - try { |
43 |
| - return axiosClient.post<T, R>(url, data, config) |
44 |
| - } catch (error) { |
45 |
| - return Promise.reject(error) |
46 |
| - } |
47 |
| -} |
48 |
| - |
49 |
| -function $get<T = any, R = AxiosResponse<T>, D = any>( |
50 |
| - url: string, |
51 |
| - config?: AxiosRequestConfig<D> |
52 |
| -): Promise<R> { |
53 |
| - try { |
54 |
| - return axiosClient.get<T, R>(url, config) |
55 |
| - } catch (error) { |
56 |
| - return Promise.reject(error) |
57 |
| - } |
58 |
| -} |
59 |
| - |
60 |
| -function $put<T = any, R = AxiosResponse<T>, D = any>( |
61 |
| - url: string, |
62 |
| - data?: D, |
63 |
| - config?: AxiosRequestConfig<D> |
64 |
| -): Promise<R> { |
65 |
| - try { |
66 |
| - return axiosClient.put<T, R>(url, data, config) |
67 |
| - } catch (error) { |
68 |
| - return Promise.reject(error) |
69 |
| - } |
70 |
| -} |
71 |
| - |
72 |
| -function $patch<T = any, R = AxiosResponse<T>, D = any>( |
73 |
| - url: string, |
74 |
| - data?: D, |
75 |
| - config?: AxiosRequestConfig<D> |
76 |
| -): Promise<R> { |
77 |
| - try { |
78 |
| - return axiosClient.patch<T, R>(url, data, config) |
79 |
| - } catch (error) { |
80 |
| - return Promise.reject(error) |
81 |
| - } |
82 |
| -} |
83 |
| - |
84 |
| -function $delete<T = any, R = AxiosResponse<T>, D = any>( |
85 |
| - url: string, |
86 |
| - config?: AxiosRequestConfig<D> |
87 |
| -): Promise<R> { |
88 |
| - try { |
89 |
| - return axiosClient.delete<T, R>(url, config) |
90 |
| - } catch (error) { |
91 |
| - return Promise.reject(error) |
92 |
| - } |
93 |
| -} |
94 |
| - |
95 |
| -export { $get, $post, $put, $patch, $delete } |
| 3 | +export * from './auth' |
0 commit comments