2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-11 18:02:32 +03:00

Make axios instantiable

This commit is contained in:
Nick Uraltsev
2015-10-03 09:38:16 -07:00
parent 4f732e8caa
commit b10874fa67
5 changed files with 194 additions and 42 deletions
+40 -18
View File
@@ -5,7 +5,21 @@ var utils = require('./utils');
var dispatchRequest = require('./core/dispatchRequest');
var InterceptorManager = require('./core/InterceptorManager');
var axios = module.exports = function (config) {
function Axios (defaultConfig) {
this.defaultConfig = utils.merge({
headers: {},
timeout: defaults.timeout,
transformRequest: defaults.transformRequest,
transformResponse: defaults.transformResponse
}, defaultConfig);
this.interceptors = {
request: new InterceptorManager(),
response: new InterceptorManager()
};
}
Axios.prototype.request = function (config) {
// Allow for axios('example/url'[, config]) a la fetch API
if (typeof config === 'string') {
config = utils.merge({
@@ -13,13 +27,7 @@ var axios = module.exports = function (config) {
}, arguments[1]);
}
config = utils.merge({
method: 'get',
headers: {},
timeout: defaults.timeout,
transformRequest: defaults.transformRequest,
transformResponse: defaults.transformResponse
}, config);
config = utils.merge(this.defaultConfig, { method: 'get' }, config);
// Don't allow overriding defaults.withCredentials
config.withCredentials = config.withCredentials || defaults.withCredentials;
@@ -28,11 +36,11 @@ var axios = module.exports = function (config) {
var chain = [dispatchRequest, undefined];
var promise = Promise.resolve(config);
axios.interceptors.request.forEach(function (interceptor) {
this.interceptors.request.forEach(function (interceptor) {
chain.unshift(interceptor.fulfilled, interceptor.rejected);
});
axios.interceptors.response.forEach(function (interceptor) {
this.interceptors.response.forEach(function (interceptor) {
chain.push(interceptor.fulfilled, interceptor.rejected);
});
@@ -43,6 +51,14 @@ var axios = module.exports = function (config) {
return promise;
};
var defaultInstance = new Axios();
var axios = module.exports = bind(Axios.prototype.request, defaultInstance);
axios.createNew = function (defaultConfig) {
return new Axios(defaultConfig);
};
// Expose defaults
axios.defaults = defaults;
@@ -53,36 +69,42 @@ axios.all = function (promises) {
axios.spread = require('./helpers/spread');
// Expose interceptors
axios.interceptors = {
request: new InterceptorManager(),
response: new InterceptorManager()
};
axios.interceptors = defaultInstance.interceptors;
// Provide aliases for supported request methods
(function () {
function createShortMethods() {
utils.forEach(arguments, function (method) {
axios[method] = function (url, config) {
return axios(utils.merge(config || {}, {
Axios.prototype[method] = function (url, config) {
return this.request(utils.merge(config || {}, {
method: method,
url: url
}));
};
axios[method] = bind(Axios.prototype[method], defaultInstance);
});
}
function createShortMethodsWithData() {
utils.forEach(arguments, function (method) {
axios[method] = function (url, data, config) {
return axios(utils.merge(config || {}, {
Axios.prototype[method] = function (url, data, config) {
return this.request(utils.merge(config || {}, {
method: method,
url: url,
data: data
}));
};
axios[method] = bind(Axios.prototype[method], defaultInstance);
});
}
createShortMethods('delete', 'get', 'head');
createShortMethodsWithData('post', 'put', 'patch');
})();
// Helpers
function bind (fn, thisArg) {
return function () {
return fn.apply(thisArg, Array.prototype.slice.call(arguments));
};
}