diff options
Diffstat (limited to 'packages/utils/src/fetchAsync.ts')
-rw-r--r-- | packages/utils/src/fetchAsync.ts | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/packages/utils/src/fetchAsync.ts b/packages/utils/src/fetchAsync.ts index 7cb2c1759..a009f0c86 100644 --- a/packages/utils/src/fetchAsync.ts +++ b/packages/utils/src/fetchAsync.ts @@ -1,29 +1,31 @@ -import { FetchRequest } from '@0xproject/types'; import 'isomorphic-fetch'; export const fetchAsync = async ( endpoint: string, - options: FetchRequest, + options: RequestInit = {}, timeoutMs: number = 20000, ): Promise<Response> => { - let finalOptions; + let optionsWithAbortParam; if ((process as any).browser === true) { const controller = new AbortController(); const signal = controller.signal; setTimeout(() => { controller.abort(); }, timeoutMs); - finalOptions = { + optionsWithAbortParam = { signal, ...options, }; } else { - finalOptions = { + // HACK: the `timeout` param only exists in `node-fetch`, and not on the `isomorphic-fetch` + // `RequestInit` type. Since `isomorphic-fetch` conditionally wraps `node-fetch` when the + // execution environment is `Node.js`, we need to cast it to `any` in that scenario. + optionsWithAbortParam = { timeout: timeoutMs, ...options, - }; + } as any; } - const response = await fetch(endpoint, finalOptions); + const response = await fetch(endpoint, optionsWithAbortParam); return response; }; |