2023-03-21 19:29:22 +02:00
|
|
|
/* eslint-disable import/prefer-default-export */
|
|
|
|
|
2023-08-21 17:01:20 +02:00
|
|
|
import { msleep } from './time';
|
2023-04-04 12:04:42 +02:00
|
|
|
import fetch from 'node-fetch';
|
2023-03-21 19:29:22 +02:00
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-03-21 19:29:22 +02:00
|
|
|
export const fetchWithRetry = async (url: string, opts: any = null) => {
|
|
|
|
if (!opts) opts = {};
|
|
|
|
let retry = opts && opts.retry || 3;
|
|
|
|
|
|
|
|
while (retry > 0) {
|
|
|
|
try {
|
|
|
|
return fetch(url, opts);
|
|
|
|
} catch (e) {
|
|
|
|
if (opts && opts.callback) {
|
|
|
|
opts.callback(retry);
|
|
|
|
}
|
|
|
|
retry = retry - 1;
|
|
|
|
if (retry === 0) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts && opts.pause) {
|
2023-08-21 17:01:20 +02:00
|
|
|
await msleep(opts.pause);
|
2023-03-21 19:29:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|