aboutsummaryrefslogtreecommitdiffstats
path: root/packages/utils/src/fetchAsync.ts
blob: 7cb2c1759b3579623665e1683a3a43f8bcc5f31b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { FetchRequest } from '@0xproject/types';
import 'isomorphic-fetch';

export const fetchAsync = async (
    endpoint: string,
    options: FetchRequest,
    timeoutMs: number = 20000,
): Promise<Response> => {
    let finalOptions;
    if ((process as any).browser === true) {
        const controller = new AbortController();
        const signal = controller.signal;
        setTimeout(() => {
            controller.abort();
        }, timeoutMs);
        finalOptions = {
            signal,
            ...options,
        };
    } else {
        finalOptions = {
            timeout: timeoutMs,
            ...options,
        };
    }

    const response = await fetch(endpoint, finalOptions);
    return response;
};