|
| 1 | +import fetch, { Response } from 'node-fetch'; |
| 2 | +import { ProxyAgent } from 'proxy-agent'; |
| 3 | +import { Transport } from '../Transport/Transport'; |
| 4 | +import { Utils } from '../../Utils/Utils'; |
| 5 | +import FormData from 'form-data'; |
| 6 | +export interface HttpProxyConfig { |
| 7 | + PAC_PROXY?: string; |
| 8 | + API_KEY?: string; |
| 9 | + NO_PROXY?: Array<string>; |
| 10 | + HTTP_PROXY?: string; |
| 11 | + HTTPS_PROXY?: string; |
| 12 | + IGNORE_CERT_ERRORS?: boolean; |
| 13 | + CA_CERT?: string; |
| 14 | + |
| 15 | +} |
| 16 | +export class HttpClient extends Transport<Response> { |
| 17 | + |
| 18 | + private readonly proxyAgent: ProxyAgent; |
| 19 | + private cfg: HttpProxyConfig; |
| 20 | + |
| 21 | + constructor(cfg?: HttpProxyConfig){ |
| 22 | + super(); |
| 23 | + this.cfg = cfg; |
| 24 | + this.init(); |
| 25 | + this.proxyAgent = new ProxyAgent(); |
| 26 | + } |
| 27 | + |
| 28 | + private init(){ |
| 29 | + const PAC_URL = this.cfg?.PAC_PROXY ? `pac+${this.cfg.PAC_PROXY.trim()}` : null; |
| 30 | + const proxyConfig = { |
| 31 | + HTTP_PROXY: PAC_URL || this.cfg?.HTTP_PROXY || '', |
| 32 | + HTTPS_PROXY: PAC_URL || this.cfg?.HTTPS_PROXY || '', |
| 33 | + NO_PROXY: this.cfg?.NO_PROXY ? this.cfg?.NO_PROXY.join(',') : null, |
| 34 | + CA_CERT: this.cfg?.CA_CERT || null, |
| 35 | + IGNORE_CERT_ERRORS: this.cfg?.IGNORE_CERT_ERRORS || false |
| 36 | + } |
| 37 | + |
| 38 | + process.env.NO_PROXY = process.env.NO_PROXY || proxyConfig.NO_PROXY |
| 39 | + process.env.HTTP_PROXY = process.env.HTTP_PROXY || proxyConfig.HTTP_PROXY |
| 40 | + process.env.HTTPS_PROXY = process.env.HTTPS_PROXY ||proxyConfig.HTTPS_PROXY |
| 41 | + if (proxyConfig.IGNORE_CERT_ERRORS ) { |
| 42 | + process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' |
| 43 | + } else { |
| 44 | + process.env.NODE_TLS_REJECT_UNAUTHORIZED='1'; |
| 45 | + } |
| 46 | + const caCertPath = |
| 47 | + proxyConfig.CA_CERT || process.env.NODE_EXTRA_CA_CERTS; |
| 48 | + |
| 49 | + if (caCertPath) Utils.loadCaCertFromFile(caCertPath); |
| 50 | + } |
| 51 | + |
| 52 | + async get(url: string): Promise<Response> { |
| 53 | + return await fetch(url, { |
| 54 | + agent: this.proxyAgent, |
| 55 | + method: 'get', |
| 56 | + headers: { |
| 57 | + ...(this.cfg.API_KEY && { 'X-Session': this.cfg.API_KEY }), |
| 58 | + }, |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + async post(url: string, body: FormData): Promise<Response> { |
| 63 | + return await fetch(url, { |
| 64 | + agent: this.proxyAgent, |
| 65 | + method: 'post', |
| 66 | + body: body, |
| 67 | + headers: { |
| 68 | + ...(this.cfg.API_KEY && { 'X-Session': this.cfg.API_KEY }) |
| 69 | + }, |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + async delete(url: string): Promise<Response> { |
| 74 | + return await fetch(url, { |
| 75 | + agent: this.proxyAgent, |
| 76 | + method: 'delete', |
| 77 | + headers: { |
| 78 | + ...(this.cfg.API_KEY && { 'X-Session': this.cfg.API_KEY }), |
| 79 | + }, |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + async put(url: string, body: FormData): Promise<Response> { |
| 84 | + return await fetch(url, { |
| 85 | + agent: this.proxyAgent, |
| 86 | + method: 'put', |
| 87 | + body: body, |
| 88 | + headers: { |
| 89 | + ...(this.cfg.API_KEY && { 'X-Session': this.cfg.API_KEY }) |
| 90 | + }, |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | +} |
0 commit comments