Extract library from plugin

This commit is contained in:
Andras Schmelczer 2025-02-18 22:32:41 +00:00
parent 8374c971ee
commit ae3acb9e1e
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
37 changed files with 61 additions and 77 deletions

View file

@ -0,0 +1,36 @@
import * as fetchRetryFactory from "fetch-retry";
import type { RequestInitRetryParams } from "fetch-retry";
import { Logger } from "src/tracing/logger";
const fetchWithRetry = fetchRetryFactory.default(fetch);
function getUrlFromInput(input: RequestInfo | URL): string {
if (input instanceof URL) {
return input.href;
}
if (typeof input === "string") {
return input;
}
return input.url;
}
export async function retriedFetch(
input: RequestInfo | URL,
init: RequestInitRetryParams<typeof fetch> = {}
): Promise<Response> {
return fetchWithRetry(input, {
retryOn: function (attempt, error, response) {
if (error !== null || !response || response.status >= 500) {
Logger.getInstance().warn(
`Retrying fetch for ${getUrlFromInput(input)}, attempt ${attempt}`
);
return true;
}
return false;
},
retries: 6,
retryDelay: (attempt) => Math.pow(1.5, attempt) * 500,
...init
});
}