-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (43 loc) · 977 Bytes
/
index.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
const axios = require('axios');
const encodeUrl = require('encodeurl');
const Ata = axios.create();
Ata.interceptors.request.use(
function(config) {
config.url = encodeUrl(config.url);
if (config.proxy && typeof config.proxy === 'string') {
const [host, port] = config.proxy.split(':');
config.proxy = {
host,
port
};
}
return config;
},
function(error) {
return Promise.reject(error);
}
);
/**
* Fork: [axios-module](https://github.com/nuxt-community/axios-module)
*/
const shortcut = Object.create(null);
const requestMethods = [
'request',
'delete',
'get',
'head',
'options',
'post',
'put',
'patch'
];
for (const method of requestMethods) {
shortcut['$' + method] = function() {
return this[method].apply(this, arguments).then(res => res && res.data);
};
}
for (const key in shortcut) {
Ata[key] = shortcut[key].bind(Ata);
}
module.exports = Ata;
module.exports.default = Ata;