mirror of
https://github.com/tenrok/axios.git
synced 2026-06-23 20:40: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 Promise = require('es6-promise').Promise;
|
||||||
var defaults = require('./defaults');
|
var defaults = require('./defaults');
|
||||||
var utils = require('./utils');
|
var utils = require('./utils');
|
||||||
|
var InterceptorManager = require('./helpers/InterceptorManager');
|
||||||
|
|
||||||
var axios = module.exports = function axios(config) {
|
var axios = module.exports = function axios(config) {
|
||||||
config = utils.merge({
|
config = utils.merge({
|
||||||
@@ -13,7 +14,7 @@ var axios = module.exports = function axios(config) {
|
|||||||
// Don't allow overriding defaults.withCredentials
|
// Don't allow overriding defaults.withCredentials
|
||||||
config.withCredentials = config.withCredentials || defaults.withCredentials;
|
config.withCredentials = config.withCredentials || defaults.withCredentials;
|
||||||
|
|
||||||
var serverRequest = function (config) {
|
function dispatchRequest(config) {
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
try {
|
try {
|
||||||
// For browsers use XHR adapter
|
// 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);
|
console.warn('For more information about usage see ' + docs);
|
||||||
}
|
}
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
}
|
};
|
||||||
|
|
||||||
var chain = [serverRequest, undefined];
|
// Hook up interceptors middleware
|
||||||
|
var chain = [dispatchRequest, undefined];
|
||||||
var promise = Promise.resolve(config);
|
var promise = Promise.resolve(config);
|
||||||
|
|
||||||
utils.forEach(axios.interceptors.request.handlers, function (interceptor) {
|
axios.interceptors.request.forEach(function (interceptor) {
|
||||||
chain.unshift(interceptor.request, interceptor.requestError);
|
chain.unshift(interceptor.fulfilled, interceptor.rejected);
|
||||||
});
|
});
|
||||||
|
|
||||||
utils.forEach(axios.interceptors.response.handlers, function (interceptor) {
|
axios.interceptors.response.forEach(function (interceptor) {
|
||||||
chain.push(interceptor.response, interceptor.responseError);
|
chain.push(interceptor.fulfilled, interceptor.rejected);
|
||||||
});
|
});
|
||||||
|
|
||||||
while (chain.length) {
|
while (chain.length) {
|
||||||
var thenFn = chain.shift();
|
promise = promise.then(chain.shift(), chain.shift());
|
||||||
var rejectFn = chain.shift();
|
|
||||||
|
|
||||||
promise = promise.then(thenFn, rejectFn);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Provide alias for success
|
// Provide alias for success
|
||||||
@@ -93,20 +92,10 @@ axios.all = function (promises) {
|
|||||||
};
|
};
|
||||||
axios.spread = require('./helpers/spread');
|
axios.spread = require('./helpers/spread');
|
||||||
|
|
||||||
// interceptors
|
// Expose interceptors
|
||||||
axios.interceptors = {
|
axios.interceptors = {
|
||||||
request: {
|
request: new InterceptorManager(),
|
||||||
handlers: [],
|
response: new InterceptorManager()
|
||||||
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 });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Provide aliases for supported request methods
|
// Provide aliases for supported request methods
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
var utils = require('../utils');
|
||||||
|
|
||||||
|
function InterceptorManager() {
|
||||||
|
this.handlers = [];
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a new interceptor to the stack
|
||||||
|
*
|
||||||
|
* @param {Function} fulfilled The function to handle `then` for a `Promise`
|
||||||
|
* @param {Function} rejected The function to handle `reject` for a `Promise`
|
||||||
|
*
|
||||||
|
* @return {Number} An ID used to remove interceptor later
|
||||||
|
*/
|
||||||
|
InterceptorManager.prototype.use = function (fulfilled, rejected) {
|
||||||
|
this.handlers.push({
|
||||||
|
fulfilled: fulfilled,
|
||||||
|
rejected: rejected
|
||||||
|
});
|
||||||
|
return this.handlers.length - 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove an interceptor from the stack
|
||||||
|
*
|
||||||
|
* @param {Number} id The ID that was returned by `use`
|
||||||
|
*/
|
||||||
|
InterceptorManager.prototype.eject = function (id) {
|
||||||
|
if (this.handlers[id]) {
|
||||||
|
this.handlers[id] = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Iterate over all the registered interceptors
|
||||||
|
*
|
||||||
|
* This method is particularly useful for skipping over any
|
||||||
|
* interceptors that may have become `null` calling `remove`.
|
||||||
|
*
|
||||||
|
* @param {Function} fn The function to call for each interceptor
|
||||||
|
*/
|
||||||
|
InterceptorManager.prototype.forEach = function (fn) {
|
||||||
|
utils.forEach(this.handlers, function (h) {
|
||||||
|
if (h !== null) {
|
||||||
|
fn(h);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = InterceptorManager;
|
||||||
|
|
||||||
@@ -296,4 +296,50 @@ describe('interceptors', function () {
|
|||||||
expect(response.data).toBe('OK123');
|
expect(response.data).toBe('OK123');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should allow removing interceptors', function () {
|
||||||
|
var request, response, intercept;
|
||||||
|
|
||||||
|
runs(function () {
|
||||||
|
axios.interceptors.response.use(function (data) {
|
||||||
|
data.data = data.data + '1';
|
||||||
|
return data;
|
||||||
|
});
|
||||||
|
intercept = axios.interceptors.response.use(function (data) {
|
||||||
|
data.data = data.data + '2';
|
||||||
|
return data;
|
||||||
|
});
|
||||||
|
axios.interceptors.response.use(function (data) {
|
||||||
|
data.data = data.data + '3';
|
||||||
|
return data;
|
||||||
|
});
|
||||||
|
|
||||||
|
axios.interceptors.response.eject(intercept);
|
||||||
|
|
||||||
|
axios({
|
||||||
|
url: '/foo'
|
||||||
|
}).then(function (data) {
|
||||||
|
response = data;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
waitsFor(function () {
|
||||||
|
return request = jasmine.Ajax.requests.mostRecent();
|
||||||
|
}, 'waiting for the request', 100);
|
||||||
|
|
||||||
|
runs(function () {
|
||||||
|
request.response({
|
||||||
|
status: 200,
|
||||||
|
responseText: 'OK'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
waitsFor(function () {
|
||||||
|
return response;
|
||||||
|
}, 'waiting for the response', 100);
|
||||||
|
|
||||||
|
runs(function () {
|
||||||
|
expect(response.data).toBe('OK13');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user