80 lines
2.1 KiB
JavaScript
80 lines
2.1 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
var defaults = require('./../defaults');
|
||
|
var utils = require('./../utils');
|
||
|
var InterceptorManager = require('./InterceptorManager');
|
||
|
var dispatchRequest = require('./dispatchRequest');
|
||
|
|
||
|
/**
|
||
|
* Create a new instance of Axios
|
||
|
*
|
||
|
* @param {Object} instanceConfig The default config for the instance
|
||
|
*/
|
||
|
function Axios(instanceConfig) {
|
||
|
this.defaults = instanceConfig;
|
||
|
this.interceptors = {
|
||
|
request: new InterceptorManager(),
|
||
|
response: new InterceptorManager()
|
||
|
};
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Dispatch a request
|
||
|
*
|
||
|
* @param {Object} config The config specific for this request (merged with this.defaults)
|
||
|
*/
|
||
|
Axios.prototype.request = function request(config) {
|
||
|
/*eslint no-param-reassign:0*/
|
||
|
// 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(defaults, {method: 'get'}, this.defaults, config);
|
||
|
config.method = config.method.toLowerCase();
|
||
|
|
||
|
// Hook up interceptors middleware
|
||
|
var chain = [dispatchRequest, undefined];
|
||
|
var promise = Promise.resolve(config);
|
||
|
|
||
|
this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
|
||
|
chain.unshift(interceptor.fulfilled, interceptor.rejected);
|
||
|
});
|
||
|
|
||
|
this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
|
||
|
chain.push(interceptor.fulfilled, interceptor.rejected);
|
||
|
});
|
||
|
|
||
|
while (chain.length) {
|
||
|
promise = promise.then(chain.shift(), chain.shift());
|
||
|
}
|
||
|
|
||
|
return promise;
|
||
|
};
|
||
|
|
||
|
// Provide aliases for supported request methods
|
||
|
utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData(method) {
|
||
|
/*eslint func-names:0*/
|
||
|
Axios.prototype[method] = function(url, config) {
|
||
|
return this.request(utils.merge(config || {}, {
|
||
|
method: method,
|
||
|
url: url
|
||
|
}));
|
||
|
};
|
||
|
});
|
||
|
|
||
|
utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
|
||
|
/*eslint func-names:0*/
|
||
|
Axios.prototype[method] = function(url, data, config) {
|
||
|
return this.request(utils.merge(config || {}, {
|
||
|
method: method,
|
||
|
url: url,
|
||
|
data: data
|
||
|
}));
|
||
|
};
|
||
|
});
|
||
|
|
||
|
module.exports = Axios;
|