forked from alan2207/bulletproof-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-client.ts
More file actions
32 lines (27 loc) · 761 Bytes
/
api-client.ts
File metadata and controls
32 lines (27 loc) · 761 Bytes
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
import Axios, { InternalAxiosRequestConfig } from 'axios';
import { env } from '@/config/env';
import { useNotifications } from '@/stores/notifications';
function authRequestInterceptor(config: InternalAxiosRequestConfig) {
if (config.headers) {
config.headers.Accept = 'application/json';
}
return config;
}
export const api = Axios.create({
baseURL: env.API_URL,
});
api.interceptors.request.use(authRequestInterceptor);
api.interceptors.response.use(
(response) => {
return response.data;
},
(error) => {
const message = error.response?.data?.message || error.message;
useNotifications.getState().addNotification({
type: 'error',
title: 'Error',
message,
});
return Promise.reject(error);
},
);