1import createHttpsProxyAgent from 'https-proxy-agent';
2
3import { FetchLike } from './client.types';
4import { env } from '../../utils/env';
5
6const debug = require('debug')('expo:api:fetch:proxy') as typeof console.log;
7
8/** Wrap fetch with support for proxies. */
9export function wrapFetchWithProxy(fetchFunction: FetchLike): FetchLike {
10  // NOTE(EvanBacon): DO NOT RETURN AN ASYNC WRAPPER. THIS BREAKS LOADING INDICATORS.
11  return function fetchWithProxy(url, options = {}) {
12    const proxy = env.HTTP_PROXY;
13    if (!options.agent && proxy) {
14      debug('Using proxy:', proxy);
15      options.agent = createHttpsProxyAgent(proxy);
16    }
17    return fetchFunction(url, options);
18  };
19}
20