File size: 592 Bytes
f0953a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import axios, { AxiosInstance, AxiosRequestHeaders } from "axios";
import tunnel from "tunnel";

interface ProxyConfig {
  host: string;
  port: number;
}

export function createAxiosInstance(
  baseURL: string,
  headers?: AxiosRequestHeaders,
  useProxy: boolean = false,
  proxyConfig?: ProxyConfig
): AxiosInstance {
  let agent;
  if (useProxy && proxyConfig) {
    agent = tunnel.httpsOverHttp({
      proxy: proxyConfig,
    });
  }

  return axios.create({
    baseURL,
    timeout: 30000,
    headers,
    httpsAgent: useProxy ? agent : undefined,
    withCredentials: true,
  });
}