-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaxios_utils.js
More file actions
35 lines (30 loc) · 1.06 KB
/
axios_utils.js
File metadata and controls
35 lines (30 loc) · 1.06 KB
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
const axios = require('axios');
const instance = axios.create();
// Add a request interceptor
instance.interceptors.request.use((settings) => {
// Do something before request is sent
if (!settings.timeout) {
settings.timeout = 101000; // Set default timeout of 101 seconds, Cloudflare default is 100
}
return settings;
}, (error) => {
// Do something with request error
return Promise.reject(error);
});
// Add a response interceptor
instance.interceptors.response.use((response) => {
// Any status code that lie within the range of 2xx cause this function to trigger
// Do something with response data
response.statusCode = response.status;
response.statusMessage = response.statusText;
return response;
}, (error) => {
// Any status codes that falls outside the range of 2xx cause this function to trigger
// Do something with response error
if (error.response) {
error.response.statusCode = error.response.status;
error.response.statusMessage = error.response.statusText;
}
return Promise.reject(error);
});
module.exports = instance;