1
1
import axios , { AxiosRequestConfig , AxiosResponse } from "axios" ;
2
+ import httpsProxyAgentPkg from "https-proxy-agent" ;
3
+ const { HttpsProxyAgent } = httpsProxyAgentPkg ;
4
+ import * as https from "https" ;
5
+ import * as fs from "fs" ;
6
+ import config from "../config.js" ;
2
7
3
8
type RequestOptions = {
4
9
url : string ;
@@ -54,15 +59,52 @@ class ApiResponse<T = any> {
54
59
}
55
60
}
56
61
62
+ // Utility to create HTTPS agent if needed (proxy/CA)
63
+ function getAxiosAgent ( ) : AxiosRequestConfig [ "httpsAgent" ] | undefined {
64
+ const proxyHost = config . browserstackLocalOptions . proxyHost ;
65
+ const proxyPort = config . browserstackLocalOptions . proxyPort ;
66
+ const caCertPath = config . browserstackLocalOptions . useCaCertificate ;
67
+
68
+ // If both proxy host and port are defined
69
+ if ( proxyHost && proxyPort ) {
70
+ const proxyUrl = `http://${ proxyHost } :${ proxyPort } ` ;
71
+ if ( caCertPath && fs . existsSync ( caCertPath ) ) {
72
+ // Proxy + CA cert
73
+ const ca = fs . readFileSync ( caCertPath ) ;
74
+ return new HttpsProxyAgent ( {
75
+ host : proxyHost ,
76
+ port : Number ( proxyPort ) ,
77
+ ca,
78
+ rejectUnauthorized : false , // Set to true if you want strict SSL
79
+ } ) ;
80
+ } else {
81
+ // Proxy only
82
+ return new HttpsProxyAgent ( proxyUrl ) ;
83
+ }
84
+ } else if ( caCertPath && fs . existsSync ( caCertPath ) ) {
85
+ // CA only
86
+ return new https . Agent ( {
87
+ ca : fs . readFileSync ( caCertPath ) ,
88
+ rejectUnauthorized : false , // Set to true for strict SSL
89
+ } ) ;
90
+ }
91
+ // Default agent (no proxy, no CA)
92
+ return undefined ;
93
+ }
94
+
57
95
class ApiClient {
58
96
private instance = axios . create ( ) ;
59
97
98
+ private get axiosAgent ( ) {
99
+ return getAxiosAgent ( ) ;
100
+ }
101
+
60
102
private async requestWrapper < T > (
61
- fn : ( ) => Promise < AxiosResponse < T > > ,
103
+ fn : ( agent : AxiosRequestConfig [ "httpsAgent" ] ) => Promise < AxiosResponse < T > > ,
62
104
raise_error : boolean = true ,
63
105
) : Promise < ApiResponse < T > > {
64
106
try {
65
- const res = await fn ( ) ;
107
+ const res = await fn ( this . axiosAgent ) ;
66
108
return new ApiResponse < T > ( res ) ;
67
109
} catch ( error : any ) {
68
110
if ( error . response && ! raise_error ) {
@@ -79,7 +121,8 @@ class ApiClient {
79
121
raise_error = true ,
80
122
} : RequestOptions ) : Promise < ApiResponse < T > > {
81
123
return this . requestWrapper < T > (
82
- ( ) => this . instance . get < T > ( url , { headers, params } ) ,
124
+ ( agent ) =>
125
+ this . instance . get < T > ( url , { headers, params, httpsAgent : agent } ) ,
83
126
raise_error ,
84
127
) ;
85
128
}
@@ -91,7 +134,8 @@ class ApiClient {
91
134
raise_error = true ,
92
135
} : RequestOptions ) : Promise < ApiResponse < T > > {
93
136
return this . requestWrapper < T > (
94
- ( ) => this . instance . post < T > ( url , body , { headers } ) ,
137
+ ( agent ) =>
138
+ this . instance . post < T > ( url , body , { headers, httpsAgent : agent } ) ,
95
139
raise_error ,
96
140
) ;
97
141
}
@@ -103,7 +147,8 @@ class ApiClient {
103
147
raise_error = true ,
104
148
} : RequestOptions ) : Promise < ApiResponse < T > > {
105
149
return this . requestWrapper < T > (
106
- ( ) => this . instance . put < T > ( url , body , { headers } ) ,
150
+ ( agent ) =>
151
+ this . instance . put < T > ( url , body , { headers, httpsAgent : agent } ) ,
107
152
raise_error ,
108
153
) ;
109
154
}
@@ -115,7 +160,8 @@ class ApiClient {
115
160
raise_error = true ,
116
161
} : RequestOptions ) : Promise < ApiResponse < T > > {
117
162
return this . requestWrapper < T > (
118
- ( ) => this . instance . patch < T > ( url , body , { headers } ) ,
163
+ ( agent ) =>
164
+ this . instance . patch < T > ( url , body , { headers, httpsAgent : agent } ) ,
119
165
raise_error ,
120
166
) ;
121
167
}
@@ -127,7 +173,8 @@ class ApiClient {
127
173
raise_error = true ,
128
174
} : RequestOptions ) : Promise < ApiResponse < T > > {
129
175
return this . requestWrapper < T > (
130
- ( ) => this . instance . delete < T > ( url , { headers, params } ) ,
176
+ ( agent ) =>
177
+ this . instance . delete < T > ( url , { headers, params, httpsAgent : agent } ) ,
131
178
raise_error ,
132
179
) ;
133
180
}
0 commit comments