fix(axios): update order of interceptors
This commit is contained in:
@@ -1,65 +1,43 @@
|
||||
import HttpError from '../../errors/http.js';
|
||||
import axios from '../axios-with-proxy.js';
|
||||
|
||||
// Mutates the `toInstance` by copying the request interceptors from `fromInstance`
|
||||
const copyRequestInterceptors = (fromInstance, toInstance) => {
|
||||
// Copy request interceptors
|
||||
fromInstance.interceptors.request.forEach(interceptor => {
|
||||
toInstance.interceptors.request.use(
|
||||
interceptor.fulfilled,
|
||||
interceptor.rejected,
|
||||
{
|
||||
synchronous: interceptor.synchronous,
|
||||
runWhen: interceptor.runWhen
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
import { createInstance } from '../axios-with-proxy.js';
|
||||
|
||||
export default function createHttpClient({ $, baseURL, beforeRequest = [] }) {
|
||||
const instance = axios.create({
|
||||
baseURL,
|
||||
});
|
||||
async function interceptResponseError(error) {
|
||||
const { config, response } = error;
|
||||
// Do not destructure `status` from `error.response` because it might not exist
|
||||
const status = response?.status;
|
||||
|
||||
// 1. apply the beforeRequest functions from the app
|
||||
instance.interceptors.request.use((requestConfig) => {
|
||||
const result = beforeRequest.reduce((newConfig, beforeRequestFunc) => {
|
||||
return beforeRequestFunc($, newConfig);
|
||||
}, requestConfig);
|
||||
if (
|
||||
// TODO: provide a `shouldRefreshToken` function in the app
|
||||
(status === 401 || status === 403) &&
|
||||
$.app.auth &&
|
||||
$.app.auth.refreshToken &&
|
||||
!$.app.auth.isRefreshTokenRequested
|
||||
) {
|
||||
$.app.auth.isRefreshTokenRequested = true;
|
||||
await $.app.auth.refreshToken($);
|
||||
|
||||
return result;
|
||||
});
|
||||
// retry the previous request before the expired token error
|
||||
const newResponse = await instance.request(config);
|
||||
$.app.auth.isRefreshTokenRequested = false;
|
||||
|
||||
// 2. inherit the request inceptors from the parent instance
|
||||
copyRequestInterceptors(axios, instance);
|
||||
|
||||
instance.interceptors.response.use(
|
||||
(response) => response,
|
||||
async (error) => {
|
||||
const { config, response } = error;
|
||||
// Do not destructure `status` from `error.response` because it might not exist
|
||||
const status = response?.status;
|
||||
|
||||
if (
|
||||
// TODO: provide a `shouldRefreshToken` function in the app
|
||||
(status === 401 || status === 403) &&
|
||||
$.app.auth &&
|
||||
$.app.auth.refreshToken &&
|
||||
!$.app.auth.isRefreshTokenRequested
|
||||
) {
|
||||
$.app.auth.isRefreshTokenRequested = true;
|
||||
await $.app.auth.refreshToken($);
|
||||
|
||||
// retry the previous request before the expired token error
|
||||
const newResponse = await instance.request(config);
|
||||
$.app.auth.isRefreshTokenRequested = false;
|
||||
|
||||
return newResponse;
|
||||
}
|
||||
|
||||
throw new HttpError(error);
|
||||
return newResponse;
|
||||
}
|
||||
);
|
||||
|
||||
throw new HttpError(error);
|
||||
};
|
||||
|
||||
const instance = createInstance(
|
||||
{
|
||||
baseURL,
|
||||
},
|
||||
{
|
||||
requestInterceptor: beforeRequest.map((originalBeforeRequest) => {
|
||||
return (requestConfig) => originalBeforeRequest($, requestConfig);
|
||||
}),
|
||||
responseErrorInterceptor: interceptResponseError,
|
||||
}
|
||||
)
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user