Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
Signed-off-by: Anatolii Bazko <[email protected]>
  • Loading branch information
tolusha committed Oct 6, 2023
1 parent 8045a2d commit 7cb56e9
Showing 1 changed file with 15 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import { delay } from '../helpers/delay';

const retryCount = 3;
const retryDelay = 500;
Expand Down Expand Up @@ -78,29 +79,23 @@ export class AxiosWrapper {
return this.doRetryFunc(() => axiosFunc(url, data, config), url, retryCount);
}

private doRetryFunc<T = any, R = AxiosResponse<T>>(
async doRetryFunc<T = any, R = AxiosResponse<T>>(
fun: () => Promise<R>,
url: string,
retry: number,
): Promise<R> {
return new Promise<R>((resolve, reject) => {
fun()
.then(response => resolve(response))
.catch(err => {
const { message } = err;
if (!retry || !message.includes('Bearer Token Authorization is required')) {
reject(err);
}

const delayRetryRequest = new Promise<void>(resolve => {
setTimeout(() => {
console.warn(`Retrying request ${url}... ${retry} left`);
resolve();
}, retryDelay);
});

return delayRetryRequest.then(() => resolve(this.doRetryFunc(fun, url, --retry)));
});
});
try {
return await fun();
} catch (err) {
if (!retry || !(err as Error)?.message?.includes('Bearer Token Authorization is required')) {
throw err;
}

// Retry the request after a delay.
console.warn(`Retrying request ${url}... ${retry} left`);
await delay(retryDelay);

return await this.doRetryFunc(fun, url, --retry);
}
}
}

0 comments on commit 7cb56e9

Please sign in to comment.