Skip to content

Commit

Permalink
Add before request interceptor (#1984)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoncool authored Dec 27, 2024
1 parent 5b08a4f commit c994c7b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 34 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"@node-rs/crc32": "^1.7.2",
"ajv": "^8.12.0",
"axios": "^1.7.7",
"axios-retry": "^3.2.0",
"axios-retry": "^3.9.1",
"clipboard-copy": "^3.2.0",
"cookie-session": "^2.1.0",
"d3": "^7.8.5",
Expand Down
14 changes: 0 additions & 14 deletions src/@types/axios.d.ts

This file was deleted.

24 changes: 9 additions & 15 deletions src/ui/libs/DatalensChartkit/modules/axios/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type {ConcurrencyManagerInstance} from './axiosConcurrency';
import {concurrencyManager} from './axiosConcurrency';

let concurrencyManagerInstance: ConcurrencyManagerInstance;
let retryRequestInterceptorId: number;
let retryResponseInterceptorId: number;

const client = axios.create({
withCredentials: true,
Expand All @@ -28,30 +30,22 @@ export function initConcurrencyManager(maxConcurrentRequests: number) {
// should be added after the interseptors added by axios-concurrency, therefor below we remove earlier
// added axios-retry interseptors and pass axiosInstance first to concurrencyManager then
// in axiosRetry
// @ts-ignore
if (isNumber(client.retryRequestInterceptor)) {
// @ts-ignore
client.interceptors.request.eject(client.retryRequestInterceptor);
if (isNumber(retryRequestInterceptorId)) {
client.interceptors.request.eject(retryRequestInterceptorId);
}
// @ts-ignore
if (isNumber(client.retryResponseInterceptor)) {
// @ts-ignore
client.interceptors.response.eject(client.retryResponseInterceptor);
if (isNumber(retryResponseInterceptorId)) {
client.interceptors.response.eject(retryResponseInterceptorId);
}

concurrencyManagerInstance = concurrencyManager(client, maxConcurrentRequests);

axiosRetry(client, {
const {requestInterceptorId, responseInterceptorId} = axiosRetry(client, {
retries: 0,
retryCondition: isRetryableError,
retryDelay: () => 3000,
});

// it is necessary to store the indexes of the positions of the interceptors added by axios-retry, to bring ability to delete them
// @ts-ignore
client.retryRequestInterceptor = client.interceptors.request.handlers.length - 1;
// @ts-ignore
client.retryResponseInterceptor = client.interceptors.response.handlers.length - 1;
retryRequestInterceptorId = requestInterceptorId;
retryResponseInterceptorId = responseInterceptorId;
}

client.interceptors.response.use(
Expand Down
16 changes: 16 additions & 0 deletions src/ui/libs/axios/interceptors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type {AxiosInstance} from 'axios';

export function initBeforeRequestInterceptor(
axiosInstance: AxiosInstance,
beforeRequest: () => Promise<unknown>,
) {
const requestInterceptorId = axiosInstance.interceptors.request.use(
async (config) => {
await beforeRequest();
return config;
},
(error) => Promise.reject(error),
);

return {requestInterceptorId};
}

0 comments on commit c994c7b

Please sign in to comment.