1
- import axios , { AxiosRequestConfig , AxiosResponse } from 'axios' ;
2
- import { logger } from '../wrapper/loggerConfig' ;
3
- import { Client } from "./Client" ;
1
+ import axios , { AxiosRequestConfig , AxiosResponse } from 'axios' ;
2
+ import { logger } from '../wrapper/loggerConfig' ;
3
+ import { Client } from "./Client" ;
4
+ import { HttpsProxyAgent } from 'https-proxy-agent' ; // Import as a function
4
5
5
6
export class HttpClient implements Client {
6
7
private readonly axiosConfig : AxiosRequestConfig ;
@@ -9,47 +10,35 @@ export class HttpClient implements Client {
9
10
constructor ( ) {
10
11
this . axiosConfig = {
11
12
responseType : 'stream' ,
12
- proxy : this . getProxyConfig ( ) ,
13
+ httpsAgent : this . getProxyConfig ( ) , // Use httpsAgent instead of proxy
13
14
} ;
14
15
}
15
16
16
17
public getProxyConfig ( ) {
17
18
const proxyUrl = process . env . HTTP_PROXY ;
18
19
if ( proxyUrl ) {
19
20
logger . info ( `Detected proxy configuration in HTTP_PROXY` ) ;
20
- const parsedProxy = new URL ( proxyUrl ) ;
21
-
22
- return {
23
- host : parsedProxy . hostname ,
24
- port : parseInt ( parsedProxy . port , 10 ) ,
25
- protocol : parsedProxy . protocol . replace ( ':' , '' ) , // remove the colon
26
- auth : parsedProxy . username && parsedProxy . password
27
- ? { username : parsedProxy . username , password : parsedProxy . password }
28
- : undefined ,
29
- } ;
21
+ return new HttpsProxyAgent ( proxyUrl ) ;
30
22
}
31
23
logger . info ( 'No proxy configuration detected.' ) ;
32
24
return undefined ;
33
25
}
34
-
26
+
35
27
public async request ( url : string , method : string , data : any ) : Promise < AxiosResponse < any , any > > {
36
28
logger . info ( `Sending ${ method } request to URL: ${ url } ` ) ;
37
- if ( this . axiosConfig . proxy ) {
38
- logger . info (
39
- `Using proxy - Host: ${ this . axiosConfig . proxy . host } , Port: ${ this . axiosConfig . proxy . port } ,` +
40
- `Protocol: ${ this . axiosConfig . proxy . protocol } , Auth: ${ this . axiosConfig . proxy . auth ? 'Yes' : 'No' } `
41
- ) ;
29
+ if ( this . axiosConfig . httpsAgent ) {
30
+ logger . info ( `Using proxy - URL: ${ process . env . HTTP_PROXY } ` ) ;
42
31
}
43
32
try {
44
- const response = await axios ( { ...this . axiosConfig , url, method, data} ) ;
33
+ const response = await axios ( { ...this . axiosConfig , url, method, data } ) ;
45
34
logger . info ( `Request completed successfully.` ) ;
46
35
return response ;
47
36
} catch ( error ) {
48
37
logger . error ( `Error sending ${ method } request to ${ url } : ${ error . message || error } ` ) ;
49
- if ( this . axiosConfig . proxy !== undefined ) {
38
+ if ( this . axiosConfig . httpsAgent !== undefined ) {
50
39
throw new Error ( `${ this . domainErrMsg } \nError: ${ error . message || error } ` ) ;
51
40
}
52
41
throw error ;
53
42
}
54
43
}
55
- }
44
+ }
0 commit comments