mirror of
https://github.com/tenrok/axios.git
synced 2026-05-18 12:39:44 +03:00
89 lines
2.2 KiB
JavaScript
89 lines
2.2 KiB
JavaScript
'use strict';
|
|
|
|
var defaults = require('./defaults');
|
|
var utils = require('./utils');
|
|
var dispatchRequest = require('./core/dispatchRequest');
|
|
var InterceptorManager = require('./core/InterceptorManager');
|
|
|
|
var axios = module.exports = function (config) {
|
|
// Allow for axios('example/url'[, config]) a la fetch API
|
|
if (typeof config === 'string') {
|
|
config = utils.merge({
|
|
url: arguments[0]
|
|
}, arguments[1]);
|
|
}
|
|
|
|
config = utils.merge({
|
|
method: 'get',
|
|
headers: {},
|
|
timeout: defaults.timeout,
|
|
transformRequest: defaults.transformRequest,
|
|
transformResponse: defaults.transformResponse
|
|
}, config);
|
|
|
|
// Don't allow overriding defaults.withCredentials
|
|
config.withCredentials = config.withCredentials || defaults.withCredentials;
|
|
|
|
// Hook up interceptors middleware
|
|
var chain = [dispatchRequest, undefined];
|
|
var promise = Promise.resolve(config);
|
|
|
|
axios.interceptors.request.forEach(function (interceptor) {
|
|
chain.unshift(interceptor.fulfilled, interceptor.rejected);
|
|
});
|
|
|
|
axios.interceptors.response.forEach(function (interceptor) {
|
|
chain.push(interceptor.fulfilled, interceptor.rejected);
|
|
});
|
|
|
|
while (chain.length) {
|
|
promise = promise.then(chain.shift(), chain.shift());
|
|
}
|
|
|
|
return promise;
|
|
};
|
|
|
|
// Expose defaults
|
|
axios.defaults = defaults;
|
|
|
|
// Expose all/spread
|
|
axios.all = function (promises) {
|
|
return Promise.all(promises);
|
|
};
|
|
axios.spread = require('./helpers/spread');
|
|
|
|
// Expose interceptors
|
|
axios.interceptors = {
|
|
request: new InterceptorManager(),
|
|
response: new InterceptorManager()
|
|
};
|
|
|
|
// Provide aliases for supported request methods
|
|
(function () {
|
|
function createShortMethods() {
|
|
utils.forEach(arguments, function (method) {
|
|
axios[method] = function (url, config) {
|
|
return axios(utils.merge(config || {}, {
|
|
method: method,
|
|
url: url
|
|
}));
|
|
};
|
|
});
|
|
}
|
|
|
|
function createShortMethodsWithData() {
|
|
utils.forEach(arguments, function (method) {
|
|
axios[method] = function (url, data, config) {
|
|
return axios(utils.merge(config || {}, {
|
|
method: method,
|
|
url: url,
|
|
data: data
|
|
}));
|
|
};
|
|
});
|
|
}
|
|
|
|
createShortMethods('delete', 'get', 'head');
|
|
createShortMethodsWithData('post', 'put', 'patch');
|
|
})();
|