diff --git a/packages/data-explorer-ui/src/entity/api/client.ts b/packages/data-explorer-ui/src/entity/api/client.ts index aacededb..f94c8d47 100644 --- a/packages/data-explorer-ui/src/entity/api/client.ts +++ b/packages/data-explorer-ui/src/entity/api/client.ts @@ -8,6 +8,29 @@ import { getURL } from "../../shared/utils"; let axiosInstance: AxiosInstance | null = null; +/** + * Adding response interceptors to axios instances + * @param api - instance of axios that will receive response interceptors + */ +const configureInterceptors = (api: AxiosInstance): void => { + api.interceptors.response.use( + (response: AxiosResponse) => response, + (error: AxiosError) => { + const { config, response } = error; + + if (response?.status === HttpStatusCode.ServiceUnavailable && config) { + const retryAfterValue = response.headers["Retry-After"]; + const waitingTime = retryAfterValue ? +retryAfterValue : 0; + return new Promise((resolve) => { + setTimeout(() => resolve(api(config)), waitingTime); + }); + } + + Promise.reject(error); + } + ); +}; + /** * Returns an AxiosInstance to be used to make API calls with a timeout of 10 seconds * @returns {AxiosInstance} with the current configs URL as baseURL @@ -18,26 +41,7 @@ export const api = (): AxiosInstance => { baseURL: getURL(), timeout: 10 * 1000, }); + configureInterceptors(axiosInstance); } return axiosInstance; }; - -/** - * Adding interceptors to axios responses - */ -api().interceptors.response.use( - (response: AxiosResponse) => response, - (error: AxiosError) => { - const { config, response } = error; - - if (response?.status === HttpStatusCode.ServiceUnavailable && config) { - const retryAfterValue = response.headers["Retry-After"]; - const waitingTime = retryAfterValue ? +retryAfterValue : 0; - return new Promise((resolve) => { - setTimeout(() => resolve(api()(config)), waitingTime); - }); - } - - Promise.reject(error); - } -);