Merge pull request #1995 from automatisch/support-async-before-request
feat(http-client): support async beforeRequest interceptors
This commit is contained in:
@@ -54,12 +54,14 @@ export function createInstance(customConfig = {}, { requestInterceptor, response
|
|||||||
// not always we have custom request interceptors
|
// not always we have custom request interceptors
|
||||||
if (requestInterceptor) {
|
if (requestInterceptor) {
|
||||||
instance.interceptors.request.use(
|
instance.interceptors.request.use(
|
||||||
function customInterceptor(requestConfig) {
|
async function customInterceptor(requestConfig) {
|
||||||
const result = requestInterceptor.reduce((newConfig, requestInterceptor) => {
|
let newRequestConfig = requestConfig;
|
||||||
return requestInterceptor(newConfig);
|
|
||||||
}, requestConfig);
|
|
||||||
|
|
||||||
return result;
|
for (const interceptor of requestInterceptor) {
|
||||||
|
newRequestConfig = await interceptor(newRequestConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
return newRequestConfig;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -84,7 +86,6 @@ export function createInstance(customConfig = {}, { requestInterceptor, response
|
|||||||
(error) => Promise.reject(error)
|
(error) => Promise.reject(error)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
// not always we have custom response error interceptor
|
// not always we have custom response error interceptor
|
||||||
if (responseErrorInterceptor) {
|
if (responseErrorInterceptor) {
|
||||||
instance.interceptors.response.use(
|
instance.interceptors.response.use(
|
||||||
|
@@ -141,7 +141,7 @@ describe('Custom default axios with proxy', () => {
|
|||||||
const customInterceptor = requestInterceptors[1].fulfilled;
|
const customInterceptor = requestInterceptors[1].fulfilled;
|
||||||
|
|
||||||
expect(requestInterceptors.length).toBe(3);
|
expect(requestInterceptors.length).toBe(3);
|
||||||
expect(customInterceptor({})).toStrictEqual({ test: true });
|
await expect(customInterceptor({})).resolves.toStrictEqual({ test: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work with a custom interceptor setting a baseURL and a request to path', async () => {
|
it('should work with a custom interceptor setting a baseURL and a request to path', async () => {
|
||||||
|
@@ -33,7 +33,7 @@ export default function createHttpClient({ $, baseURL, beforeRequest = [] }) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
requestInterceptor: beforeRequest.map((originalBeforeRequest) => {
|
requestInterceptor: beforeRequest.map((originalBeforeRequest) => {
|
||||||
return (requestConfig) => originalBeforeRequest($, requestConfig);
|
return async (requestConfig) => await originalBeforeRequest($, requestConfig);
|
||||||
}),
|
}),
|
||||||
responseErrorInterceptor: interceptResponseError,
|
responseErrorInterceptor: interceptResponseError,
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user