-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaxiosProvider.ts
37 lines (32 loc) · 1.29 KB
/
axiosProvider.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
36
37
// eslint-disable-next-line no-restricted-imports
import axios from 'axios';
import { APIError } from 'src/types/apiError';
/**
* Here we create the axios instance that should be used whenever you need to do an API call.
* Do not import axios from the original package in your code.
*/
/**
* You can configure the axios defaults here
* https://axios-http.com/docs/config_defaults
*
* The calls to the backend API are done via the frontend proxy.
* We do this to avoid exposing our backend to the public and overcome CORS issues.
* Check build/serverOptions.ts to see how the proxy is configured.
* Calls go like this: http://localhost:5000/backend/account -> http://localhost:5001/account
*/
const axiosInstance = axios.create({
baseURL: '/backend',
});
/**
* You can add interceptors if you have some common logic that needs to run on every request or response.
* https://axios-http.com/docs/interceptors
*
* The following interceptor convert AxiosError to APIError because we don't want to expose axios objects outside.
*/
axiosInstance.interceptors.response.use(null, (error) => {
const finalError = axios.isAxiosError(error)
? new APIError({ status: error.response?.status, message: error.message })
: error;
return Promise.reject(finalError);
});
export { axiosInstance as axios };