1import createHttpsProxyAgent from 'https-proxy-agent';
2
3import { env } from '../../utils/env';
4import { FetchLike } from './client.types';
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  return async function fetchWithProxy(url, options = {}) {
11    const proxy = env.HTTP_PROXY;
12    if (!options.agent && proxy) {
13      debug('Using proxy:', proxy);
14      options.agent = createHttpsProxyAgent(proxy);
15    }
16    return fetchFunction(url, options);
17  };
18}
19