forked from alan2207/bulletproof-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
axios.ts
35 lines (30 loc) · 865 Bytes
/
axios.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import Axios, { AxiosRequestConfig } from 'axios';
import { API_URL } from '@/config';
import { useNotificationStore } from '@/stores/notifications';
import storage from '@/utils/storage';
function authRequestInterceptor(config: AxiosRequestConfig) {
const token = storage.getToken();
if (token) {
config.headers.authorization = `${token}`;
}
config.headers.Accept = 'application/json';
return config;
}
export const axios = Axios.create({
baseURL: API_URL,
});
axios.interceptors.request.use(authRequestInterceptor);
axios.interceptors.response.use(
(response) => {
return response.data;
},
(error) => {
const message = error.response?.data?.message || error.message;
useNotificationStore.getState().addNotification({
type: 'error',
title: 'Error',
message,
});
return Promise.reject(error);
}
);