mirror of
https://github.com/prymitive/karma
synced 2026-05-13 03:56:59 +00:00
chore(ui): migrate more code to typescript
This commit is contained in:
committed by
Łukasz Mierzwa
parent
55170f8812
commit
4d4dd111c1
46
ui/src/Common/Fetch.ts
Normal file
46
ui/src/Common/Fetch.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import merge from "lodash.merge";
|
||||
|
||||
import promiseRetry from "promise-retry";
|
||||
|
||||
const CommonOptions = {
|
||||
mode: "cors",
|
||||
credentials: "include",
|
||||
redirect: "follow",
|
||||
};
|
||||
|
||||
const FetchRetryConfig = {
|
||||
retries: 9,
|
||||
minTimeout: 2000,
|
||||
maxTimeout: 5000,
|
||||
};
|
||||
|
||||
type PreRetryCallback = (number: number) => void;
|
||||
|
||||
const FetchGet = async (
|
||||
uri: string,
|
||||
options: RequestInit,
|
||||
beforeRetry: PreRetryCallback
|
||||
) =>
|
||||
await promiseRetry(
|
||||
(retry, number) =>
|
||||
fetch(
|
||||
uri,
|
||||
merge(
|
||||
{},
|
||||
{
|
||||
method: "GET",
|
||||
},
|
||||
CommonOptions,
|
||||
{
|
||||
mode: number <= FetchRetryConfig.retries ? "cors" : "no-cors",
|
||||
},
|
||||
options
|
||||
)
|
||||
).catch((err) => {
|
||||
beforeRetry && beforeRetry(number);
|
||||
return retry(err);
|
||||
}),
|
||||
FetchRetryConfig
|
||||
);
|
||||
|
||||
export { CommonOptions, FetchGet, FetchRetryConfig };
|
||||
Reference in New Issue
Block a user