aboutsummaryrefslogblamecommitdiffstats
path: root/packages/utils/src/fetchAsync.ts
blob: 7cb2c1759b3579623665e1683a3a43f8bcc5f31b (plain) (tree)




























                                                         
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;
};