BlueWallet/util/fetch.ts
Marcos Rodriguez Velez b42290ceee REF: Rename util
2025-02-18 13:27:35 -04:00

14 lines
616 B
TypeScript

const DEFAULT_TIMEOUT = 10000; // default timeout in ms
// protection against calling itself recursively
const nativeFetch = globalThis.fetch.bind(globalThis);
export function fetch(input: RequestInfo | URL, init: RequestInit & { timeout?: number } = {}): Promise<Response> {
if (__DEV__) {
console.log('fetch wrapper: ', input, init);
}
const { timeout = DEFAULT_TIMEOUT, ...rest } = init;
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeout);
return nativeFetch(input, { ...rest, signal: controller.signal }).finally(() => clearTimeout(timer));
}