mirror of
https://github.com/tenrok/axios.git
synced 2026-06-11 18:02:32 +03:00
Initial interceptor implementation.
This commit is contained in:
+50
-14
@@ -13,20 +13,22 @@ var axios = module.exports = function axios(config) {
|
||||
// Don't allow overriding defaults.withCredentials
|
||||
config.withCredentials = config.withCredentials || defaults.withCredentials;
|
||||
|
||||
var promise = new Promise(function (resolve, reject) {
|
||||
try {
|
||||
// For browsers use XHR adapter
|
||||
if (typeof window !== 'undefined') {
|
||||
require('./adapters/xhr')(resolve, reject, config);
|
||||
var serverRequest = function (config) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
try {
|
||||
// For browsers use XHR adapter
|
||||
if (typeof window !== 'undefined') {
|
||||
require('./adapters/xhr')(resolve, reject, config);
|
||||
}
|
||||
// For node use HTTP adapter
|
||||
else if (typeof process !== 'undefined') {
|
||||
require('./adapters/http')(resolve, reject, config);
|
||||
}
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
// For node use HTTP adapter
|
||||
else if (typeof process !== 'undefined') {
|
||||
require('./adapters/http')(resolve, reject, config);
|
||||
}
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
function deprecatedMethod(method, instead, docs) {
|
||||
try {
|
||||
@@ -41,6 +43,24 @@ var axios = module.exports = function axios(config) {
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
var chain = [serverRequest, undefined];
|
||||
var promise = Promise.resolve(config);
|
||||
|
||||
utils.forEach(axios.interceptors.request.handlers, function (interceptor) {
|
||||
chain.unshift(interceptor.request, interceptor.requestError);
|
||||
});
|
||||
|
||||
utils.forEach(axios.interceptors.response.handlers, function (interceptor) {
|
||||
chain.push(interceptor.response, interceptor.responseError);
|
||||
});
|
||||
|
||||
while (chain.length) {
|
||||
var thenFn = chain.shift();
|
||||
var rejectFn = chain.shift();
|
||||
|
||||
promise = promise.then(thenFn, rejectFn);
|
||||
}
|
||||
|
||||
// Provide alias for success
|
||||
promise.success = function success(fn) {
|
||||
deprecatedMethod('success', 'then', 'https://github.com/mzabriskie/axios/blob/master/README.md#response-api');
|
||||
@@ -73,6 +93,22 @@ axios.all = function (promises) {
|
||||
};
|
||||
axios.spread = require('./helpers/spread');
|
||||
|
||||
// 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 });
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Provide aliases for supported request methods
|
||||
createShortMethods('delete', 'get', 'head');
|
||||
createShortMethodsWithData('post', 'put', 'patch');
|
||||
@@ -98,4 +134,4 @@ function createShortMethodsWithData() {
|
||||
}));
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user