/
axiosProxyOverridesFor.ts
39 lines (34 loc) · 1.22 KB
/
axiosProxyOverridesFor.ts
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
30
31
32
33
34
35
36
37
38
39
import { type CreateAxiosDefaults } from 'axios';
import type * as http from 'http';
import { ensure, isDefined } from 'tiny-types';
import { createUrl } from './createUrl';
import { ProxyAgent } from './ProxyAgent';
/**
* @param options
*/
export function axiosProxyOverridesFor<Data = any>(options: CreateAxiosDefaults<Data>): {
proxy: false, httpAgent: http.Agent, httpsAgent: http.Agent
} {
const envProxyOverride: string | false = options.proxy
&& createUrl({
username: options.proxy?.auth?.username,
password: options.proxy?.auth?.password,
protocol: options.proxy?.protocol,
hostname: ensure('proxy.host', options.proxy?.host, isDefined()),
port: options.proxy?.port
}).toString();
const agent = new ProxyAgent({
httpAgent: options.httpAgent,
httpsAgent: options.httpsAgent,
// if there's a specific proxy override configured, use it
// if not - detect proxy automatically based on env variables
getProxyForUrl: envProxyOverride
? (url_: string) => envProxyOverride
: undefined,
});
return {
proxy: false,
httpAgent: agent,
httpsAgent: agent,
};
}