mirror of
https://github.com/tenrok/axios.git
synced 2026-06-23 20:40:40 +03:00
fix: added a option to choose between legacy and the new request/response interceptor ordering
* test: add request interceptor tests for legacy and ordered execution * feat: add legacy interceptor request/response ordering option --------- Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
@@ -615,6 +615,9 @@ These are the available config options for making requests. Only the `url` is re
|
|||||||
|
|
||||||
// throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts
|
// throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts
|
||||||
clarifyTimeoutError: false,
|
clarifyTimeoutError: false,
|
||||||
|
|
||||||
|
// use the legacy interceptor request/response ordering
|
||||||
|
legacyInterceptorReqResOrdering: true, // default
|
||||||
},
|
},
|
||||||
|
|
||||||
env: {
|
env: {
|
||||||
|
|||||||
@@ -302,6 +302,7 @@ declare namespace axios {
|
|||||||
silentJSONParsing?: boolean;
|
silentJSONParsing?: boolean;
|
||||||
forcedJSONParsing?: boolean;
|
forcedJSONParsing?: boolean;
|
||||||
clarifyTimeoutError?: boolean;
|
clarifyTimeoutError?: boolean;
|
||||||
|
legacyInterceptorReqResOrdering?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface GenericAbortSignal {
|
interface GenericAbortSignal {
|
||||||
|
|||||||
Vendored
+1
@@ -332,6 +332,7 @@ export interface TransitionalOptions {
|
|||||||
silentJSONParsing?: boolean;
|
silentJSONParsing?: boolean;
|
||||||
forcedJSONParsing?: boolean;
|
forcedJSONParsing?: boolean;
|
||||||
clarifyTimeoutError?: boolean;
|
clarifyTimeoutError?: boolean;
|
||||||
|
legacyInterceptorReqResOrdering?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface GenericAbortSignal {
|
export interface GenericAbortSignal {
|
||||||
|
|||||||
+10
-1
@@ -8,6 +8,7 @@ import mergeConfig from './mergeConfig.js';
|
|||||||
import buildFullPath from './buildFullPath.js';
|
import buildFullPath from './buildFullPath.js';
|
||||||
import validator from '../helpers/validator.js';
|
import validator from '../helpers/validator.js';
|
||||||
import AxiosHeaders from './AxiosHeaders.js';
|
import AxiosHeaders from './AxiosHeaders.js';
|
||||||
|
import transitionalDefaults from '../defaults/transitional.js';
|
||||||
|
|
||||||
const validators = validator.validators;
|
const validators = validator.validators;
|
||||||
|
|
||||||
@@ -80,7 +81,8 @@ class Axios {
|
|||||||
validator.assertOptions(transitional, {
|
validator.assertOptions(transitional, {
|
||||||
silentJSONParsing: validators.transitional(validators.boolean),
|
silentJSONParsing: validators.transitional(validators.boolean),
|
||||||
forcedJSONParsing: validators.transitional(validators.boolean),
|
forcedJSONParsing: validators.transitional(validators.boolean),
|
||||||
clarifyTimeoutError: validators.transitional(validators.boolean)
|
clarifyTimeoutError: validators.transitional(validators.boolean),
|
||||||
|
legacyInterceptorReqResOrdering: validators.transitional(validators.boolean)
|
||||||
}, false);
|
}, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,7 +141,14 @@ class Axios {
|
|||||||
|
|
||||||
synchronousRequestInterceptors = synchronousRequestInterceptors && interceptor.synchronous;
|
synchronousRequestInterceptors = synchronousRequestInterceptors && interceptor.synchronous;
|
||||||
|
|
||||||
|
const transitional = config.transitional || transitionalDefaults;
|
||||||
|
const legacyInterceptorReqResOrdering = transitional && transitional.legacyInterceptorReqResOrdering;
|
||||||
|
|
||||||
|
if (legacyInterceptorReqResOrdering) {
|
||||||
requestInterceptorChain.unshift(interceptor.fulfilled, interceptor.rejected);
|
requestInterceptorChain.unshift(interceptor.fulfilled, interceptor.rejected);
|
||||||
|
} else {
|
||||||
|
requestInterceptorChain.push(interceptor.fulfilled, interceptor.rejected);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const responseInterceptorChain = [];
|
const responseInterceptorChain = [];
|
||||||
|
|||||||
@@ -3,5 +3,6 @@
|
|||||||
export default {
|
export default {
|
||||||
silentJSONParsing: true,
|
silentJSONParsing: true,
|
||||||
forcedJSONParsing: true,
|
forcedJSONParsing: true,
|
||||||
clarifyTimeoutError: false
|
clarifyTimeoutError: false,
|
||||||
|
legacyInterceptorReqResOrdering: true
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -91,6 +91,63 @@ describe('interceptors', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should execute request interceptor in legacy order', function (done) {
|
||||||
|
let sequence = '';
|
||||||
|
axios.interceptors.request.use(function (config) {
|
||||||
|
sequence += '1';
|
||||||
|
return config;
|
||||||
|
});
|
||||||
|
|
||||||
|
axios.interceptors.request.use(function (config) {
|
||||||
|
sequence += '2';
|
||||||
|
return config;
|
||||||
|
});
|
||||||
|
|
||||||
|
axios.interceptors.request.use(function (config) {
|
||||||
|
sequence += '3';
|
||||||
|
return config;
|
||||||
|
});
|
||||||
|
|
||||||
|
axios({
|
||||||
|
url: '/foo',
|
||||||
|
});
|
||||||
|
|
||||||
|
getAjaxRequest().then(function (request) {
|
||||||
|
expect(sequence).toBe('321');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should execute request interceptor in order', function (done) {
|
||||||
|
let sequence = '';
|
||||||
|
axios.interceptors.request.use(function (config) {
|
||||||
|
sequence += '1';
|
||||||
|
return config;
|
||||||
|
});
|
||||||
|
|
||||||
|
axios.interceptors.request.use(function (config) {
|
||||||
|
sequence += '2';
|
||||||
|
return config;
|
||||||
|
});
|
||||||
|
|
||||||
|
axios.interceptors.request.use(function (config) {
|
||||||
|
sequence += '3';
|
||||||
|
return config;
|
||||||
|
});
|
||||||
|
|
||||||
|
axios({
|
||||||
|
url: '/foo',
|
||||||
|
transitional: {
|
||||||
|
legacyInterceptorReqResOrdering: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
getAjaxRequest().then(function (request) {
|
||||||
|
expect(sequence).toBe('123');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('runs the interceptor if runWhen function is provided and resolves to true', function (done) {
|
it('runs the interceptor if runWhen function is provided and resolves to true', function (done) {
|
||||||
function onGetCall(config) {
|
function onGetCall(config) {
|
||||||
return config.method === 'get';
|
return config.method === 'get';
|
||||||
|
|||||||
Reference in New Issue
Block a user