Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/axios/lib/core/Axios.js
1126 views
1
'use strict';
2
3
var utils = require('./../utils');
4
var buildURL = require('../helpers/buildURL');
5
var InterceptorManager = require('./InterceptorManager');
6
var dispatchRequest = require('./dispatchRequest');
7
var mergeConfig = require('./mergeConfig');
8
var validator = require('../helpers/validator');
9
10
var validators = validator.validators;
11
/**
12
* Create a new instance of Axios
13
*
14
* @param {Object} instanceConfig The default config for the instance
15
*/
16
function Axios(instanceConfig) {
17
this.defaults = instanceConfig;
18
this.interceptors = {
19
request: new InterceptorManager(),
20
response: new InterceptorManager()
21
};
22
}
23
24
/**
25
* Dispatch a request
26
*
27
* @param {Object} config The config specific for this request (merged with this.defaults)
28
*/
29
Axios.prototype.request = function request(config) {
30
/*eslint no-param-reassign:0*/
31
// Allow for axios('example/url'[, config]) a la fetch API
32
if (typeof config === 'string') {
33
config = arguments[1] || {};
34
config.url = arguments[0];
35
} else {
36
config = config || {};
37
}
38
39
config = mergeConfig(this.defaults, config);
40
41
// Set config.method
42
if (config.method) {
43
config.method = config.method.toLowerCase();
44
} else if (this.defaults.method) {
45
config.method = this.defaults.method.toLowerCase();
46
} else {
47
config.method = 'get';
48
}
49
50
var transitional = config.transitional;
51
52
if (transitional !== undefined) {
53
validator.assertOptions(transitional, {
54
silentJSONParsing: validators.transitional(validators.boolean),
55
forcedJSONParsing: validators.transitional(validators.boolean),
56
clarifyTimeoutError: validators.transitional(validators.boolean)
57
}, false);
58
}
59
60
// filter out skipped interceptors
61
var requestInterceptorChain = [];
62
var synchronousRequestInterceptors = true;
63
this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
64
if (typeof interceptor.runWhen === 'function' && interceptor.runWhen(config) === false) {
65
return;
66
}
67
68
synchronousRequestInterceptors = synchronousRequestInterceptors && interceptor.synchronous;
69
70
requestInterceptorChain.unshift(interceptor.fulfilled, interceptor.rejected);
71
});
72
73
var responseInterceptorChain = [];
74
this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
75
responseInterceptorChain.push(interceptor.fulfilled, interceptor.rejected);
76
});
77
78
var promise;
79
80
if (!synchronousRequestInterceptors) {
81
var chain = [dispatchRequest, undefined];
82
83
Array.prototype.unshift.apply(chain, requestInterceptorChain);
84
chain = chain.concat(responseInterceptorChain);
85
86
promise = Promise.resolve(config);
87
while (chain.length) {
88
promise = promise.then(chain.shift(), chain.shift());
89
}
90
91
return promise;
92
}
93
94
95
var newConfig = config;
96
while (requestInterceptorChain.length) {
97
var onFulfilled = requestInterceptorChain.shift();
98
var onRejected = requestInterceptorChain.shift();
99
try {
100
newConfig = onFulfilled(newConfig);
101
} catch (error) {
102
onRejected(error);
103
break;
104
}
105
}
106
107
try {
108
promise = dispatchRequest(newConfig);
109
} catch (error) {
110
return Promise.reject(error);
111
}
112
113
while (responseInterceptorChain.length) {
114
promise = promise.then(responseInterceptorChain.shift(), responseInterceptorChain.shift());
115
}
116
117
return promise;
118
};
119
120
Axios.prototype.getUri = function getUri(config) {
121
config = mergeConfig(this.defaults, config);
122
return buildURL(config.url, config.params, config.paramsSerializer).replace(/^\?/, '');
123
};
124
125
// Provide aliases for supported request methods
126
utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData(method) {
127
/*eslint func-names:0*/
128
Axios.prototype[method] = function(url, config) {
129
return this.request(mergeConfig(config || {}, {
130
method: method,
131
url: url,
132
data: (config || {}).data
133
}));
134
};
135
});
136
137
utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
138
/*eslint func-names:0*/
139
Axios.prototype[method] = function(url, data, config) {
140
return this.request(mergeConfig(config || {}, {
141
method: method,
142
url: url,
143
data: data
144
}));
145
};
146
});
147
148
module.exports = Axios;
149
150