diff --git a/package-lock.json b/package-lock.json index 6b417bc3f7..cb759b5363 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,7 +26,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", @@ -11742,9 +11742,9 @@ } }, "node_modules/axios-retry": { - "version": "3.6.0", - "resolved": "https://registry.npmjs.org/axios-retry/-/axios-retry-3.6.0.tgz", - "integrity": "sha512-jtH4qWTKZ2a17dH6tjq52Y1ssNV0lKge6/Z9Lw67s9Wt01nGTg4hg7/LJBGYfDci44NTANJQlCPHPOT/TSFm9w==", + "version": "3.9.1", + "resolved": "https://registry.npmjs.org/axios-retry/-/axios-retry-3.9.1.tgz", + "integrity": "sha512-8PJDLJv7qTTMMwdnbMvrLYuvB47M81wRtxQmEdV5w4rgbTXTt+vtPkXwajOfOdSyv/wZICJOC+/UhXH4aQ/R+w==", "license": "Apache-2.0", "dependencies": { "@babel/runtime": "^7.15.4", diff --git a/package.json b/package.json index 4843d0a6f7..8b1fd4c0f6 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/@types/axios.d.ts b/src/@types/axios.d.ts deleted file mode 100644 index 7623a5aacc..0000000000 --- a/src/@types/axios.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -import type {IAxiosRetryConfig} from 'axios-retry'; - -declare module 'axios' { - export interface AxiosRequestConfig { - 'axios-retry'?: IAxiosRetryConfig; - } - export interface AxiosInterceptorManager { - handlers: Array<() => void>; - } - export interface AxiosInstance { - retryRequestInterceptor?: number; - retryResponseInterceptor?: number; - } -} diff --git a/src/ui/libs/DatalensChartkit/modules/axios/axios.ts b/src/ui/libs/DatalensChartkit/modules/axios/axios.ts index d14b12b201..fcc6cba2b2 100644 --- a/src/ui/libs/DatalensChartkit/modules/axios/axios.ts +++ b/src/ui/libs/DatalensChartkit/modules/axios/axios.ts @@ -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, @@ -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( diff --git a/src/ui/libs/axios/interceptors.ts b/src/ui/libs/axios/interceptors.ts new file mode 100644 index 0000000000..ff55464dcc --- /dev/null +++ b/src/ui/libs/axios/interceptors.ts @@ -0,0 +1,16 @@ +import type {AxiosInstance} from 'axios'; + +export function initBeforeRequestInterceptor( + axiosInstance: AxiosInstance, + beforeRequest: () => Promise, +) { + const requestInterceptorId = axiosInstance.interceptors.request.use( + async (config) => { + await beforeRequest(); + return config; + }, + (error) => Promise.reject(error), + ); + + return {requestInterceptorId}; +}