chore(ui): migrate more code to typescript

This commit is contained in:
Łukasz Mierzwa
2020-06-29 16:14:53 +01:00
committed by Łukasz Mierzwa
parent 55170f8812
commit 4d4dd111c1
36 changed files with 392 additions and 205 deletions

46
ui/src/Common/Fetch.ts Normal file
View 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 };