18c8eefe0SEvan Baconimport createHttpsProxyAgent from 'https-proxy-agent';
28c8eefe0SEvan Bacon
38c8eefe0SEvan Baconimport { FetchLike } from './client.types';
4*8a424bebSJames Ideimport { env } from '../../utils/env';
58c8eefe0SEvan Bacon
68c8eefe0SEvan Baconconst debug = require('debug')('expo:api:fetch:proxy') as typeof console.log;
78c8eefe0SEvan Bacon
88c8eefe0SEvan Bacon/** Wrap fetch with support for proxies. */
98c8eefe0SEvan Baconexport function wrapFetchWithProxy(fetchFunction: FetchLike): FetchLike {
1082f3de79SEvan Bacon  // NOTE(EvanBacon): DO NOT RETURN AN ASYNC WRAPPER. THIS BREAKS LOADING INDICATORS.
1182f3de79SEvan Bacon  return function fetchWithProxy(url, options = {}) {
128c8eefe0SEvan Bacon    const proxy = env.HTTP_PROXY;
138c8eefe0SEvan Bacon    if (!options.agent && proxy) {
148c8eefe0SEvan Bacon      debug('Using proxy:', proxy);
158c8eefe0SEvan Bacon      options.agent = createHttpsProxyAgent(proxy);
168c8eefe0SEvan Bacon    }
178c8eefe0SEvan Bacon    return fetchFunction(url, options);
188c8eefe0SEvan Bacon  };
198c8eefe0SEvan Bacon}
20