提交 9e3830cf 编写于 作者: M mzabriskie

Adding support for removing interceptors

上级 7b05902b
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
......
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');
});
});
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');
});
});
});
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册