mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Adding support for removing interceptors
This commit is contained in:
+13
-24
@@ -1,6 +1,7 @@
|
||||
var Promise = require('es6-promise').Promise;
|
||||
var defaults = require('./defaults');
|
||||
var utils = require('./utils');
|
||||
var InterceptorManager = require('./helpers/InterceptorManager');
|
||||
|
||||
var axios = module.exports = function axios(config) {
|
||||
config = utils.merge({
|
||||
@@ -13,7 +14,7 @@ var axios = module.exports = function axios(config) {
|
||||
// Don't allow overriding defaults.withCredentials
|
||||
config.withCredentials = config.withCredentials || defaults.withCredentials;
|
||||
|
||||
var serverRequest = function (config) {
|
||||
function dispatchRequest(config) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
try {
|
||||
// For browsers use XHR adapter
|
||||
@@ -41,24 +42,22 @@ var axios = module.exports = function axios(config) {
|
||||
console.warn('For more information about usage see ' + docs);
|
||||
}
|
||||
} catch (e) {}
|
||||
}
|
||||
};
|
||||
|
||||
var chain = [serverRequest, undefined];
|
||||
// Hook up interceptors middleware
|
||||
var chain = [dispatchRequest, undefined];
|
||||
var promise = Promise.resolve(config);
|
||||
|
||||
utils.forEach(axios.interceptors.request.handlers, function (interceptor) {
|
||||
chain.unshift(interceptor.request, interceptor.requestError);
|
||||
axios.interceptors.request.forEach(function (interceptor) {
|
||||
chain.unshift(interceptor.fulfilled, interceptor.rejected);
|
||||
});
|
||||
|
||||
utils.forEach(axios.interceptors.response.handlers, function (interceptor) {
|
||||
chain.push(interceptor.response, interceptor.responseError);
|
||||
axios.interceptors.response.forEach(function (interceptor) {
|
||||
chain.push(interceptor.fulfilled, interceptor.rejected);
|
||||
});
|
||||
|
||||
while (chain.length) {
|
||||
var thenFn = chain.shift();
|
||||
var rejectFn = chain.shift();
|
||||
|
||||
promise = promise.then(thenFn, rejectFn);
|
||||
promise = promise.then(chain.shift(), chain.shift());
|
||||
}
|
||||
|
||||
// Provide alias for success
|
||||
@@ -93,20 +92,10 @@ axios.all = function (promises) {
|
||||
};
|
||||
axios.spread = require('./helpers/spread');
|
||||
|
||||
// interceptors
|
||||
// Expose interceptors
|
||||
axios.interceptors = {
|
||||
request: {
|
||||
handlers: [],
|
||||
use: function (thenFn, rejectFn) {
|
||||
axios.interceptors.request.handlers.push({ request: thenFn, requestError: rejectFn });
|
||||
}
|
||||
},
|
||||
response: {
|
||||
handlers: [],
|
||||
use: function (thenFn, rejectFn) {
|
||||
axios.interceptors.response.handlers.push({ response: thenFn, responseError: rejectFn });
|
||||
}
|
||||
}
|
||||
request: new InterceptorManager(),
|
||||
response: new InterceptorManager()
|
||||
};
|
||||
|
||||
// Provide aliases for supported request methods
|
||||
|
||||
Reference in New Issue
Block a user