@@ -39,8 +39,26 @@ class HttpURLConnectionClient implements ClientInterface {
39
39
public proxy ?: AgentOptions ;
40
40
private agentOptions ! : AgentOptions ;
41
41
42
+ /**
43
+ * Sends an HTTP request to the specified endpoint with the provided JSON payload and configuration.
44
+ *
45
+ * This method sets up request headers, including authentication (API key or basic auth), content type,
46
+ * and timeout. If a certificate path is provided in the config, it installs a certificate verifier.
47
+ * Throws an ApiException when an error occurs (invalid API key, API error response, etc.).
48
+ *
49
+ * @param endpoint - The URL to which the request will be sent.
50
+ * @param json - The JSON string to be sent as the request body.
51
+ * @param config - The configuration object containing authentication, timeout, and certificate details.
52
+ * @param isApiRequired - Indicates whether an API key is required for this request.
53
+ * @param requestOptions - Additional options for the HTTP request, such as headers and timeout.
54
+ * @returns A promise that resolves with the response body as a string.
55
+ * @throws {ApiException } when an error occurs
56
+ */
42
57
public request (
43
- endpoint : string , json : string , config : Config , isApiRequired : boolean ,
58
+ endpoint : string ,
59
+ json : string ,
60
+ config : Config ,
61
+ isApiRequired : boolean ,
44
62
requestOptions : IRequest . Options ,
45
63
) : Promise < string > {
46
64
requestOptions . headers ??= { } ;
@@ -68,13 +86,13 @@ class HttpURLConnectionClient implements ClientInterface {
68
86
requestOptions . headers [ ApiConstants . CONTENT_TYPE ] = ApiConstants . APPLICATION_JSON_TYPE ;
69
87
70
88
const httpConnection : ClientRequest = this . createRequest ( endpoint , requestOptions , config . applicationName ) ;
71
- return this . doPostRequest ( httpConnection , json ) ;
89
+ return this . doRequest ( httpConnection , json ) ;
72
90
}
73
91
74
92
public post ( endpoint : string , postParameters : [ string , string ] [ ] , config : Config ) : Promise < string > {
75
93
const postQuery : string = this . getQuery ( postParameters ) ;
76
94
const connectionRequest : ClientRequest = this . createRequest ( endpoint , { } , config . applicationName ) ;
77
- return this . doPostRequest ( connectionRequest , postQuery ) ;
95
+ return this . doRequest ( connectionRequest , postQuery ) ;
78
96
}
79
97
80
98
private createRequest ( endpoint : string , requestOptions : IRequest . Options , applicationName ?: string ) : ClientRequest {
@@ -113,7 +131,7 @@ class HttpURLConnectionClient implements ClientInterface {
113
131
requestOptions . headers [ ApiConstants . ACCEPT_CHARSET ] = HttpURLConnectionClient . CHARSET ;
114
132
// user-agent header
115
133
const libInfo = `${ LibraryConstants . LIB_NAME } /${ LibraryConstants . LIB_VERSION } ` ;
116
- requestOptions . headers [ ApiConstants . USER_AGENT ] = applicationName ? `${ applicationName } ${ libInfo } ` : libInfo ;
134
+ requestOptions . headers [ ApiConstants . USER_AGENT ] = applicationName ? `${ applicationName } ${ libInfo } ` : libInfo ;
117
135
// custom headers
118
136
requestOptions . headers [ ApiConstants . ADYEN_LIBRARY_NAME ] = LibraryConstants . LIB_NAME ;
119
137
requestOptions . headers [ ApiConstants . ADYEN_LIBRARY_VERSION ] = LibraryConstants . LIB_VERSION ;
@@ -133,7 +151,7 @@ class HttpURLConnectionClient implements ClientInterface {
133
151
return params . map ( ( [ key , value ] ) : string => `${ key } =${ value } ` ) . join ( "&" ) ;
134
152
}
135
153
136
- private doPostRequest ( connectionRequest : ClientRequest , json : string ) : Promise < string > {
154
+ private doRequest ( connectionRequest : ClientRequest , json : string ) : Promise < string > {
137
155
return new Promise ( ( resolve , reject ) : void => {
138
156
connectionRequest . flushHeaders ( ) ;
139
157
@@ -144,15 +162,15 @@ class HttpURLConnectionClient implements ClientInterface {
144
162
body : ""
145
163
} ;
146
164
165
+ // define default exception (in case of error during the handling of the response)
147
166
const getException = ( responseBody : string ) : HttpClientException => new HttpClientException ( {
148
167
message : `HTTP Exception: ${ response . statusCode } . ${ res . statusMessage } ` ,
149
168
statusCode : response . statusCode ,
150
169
errorCode : undefined ,
151
170
responseHeaders : response . headers ,
152
171
responseBody,
153
172
} ) ;
154
-
155
- let exception : HttpClientException | Error = getException ( response . body . toString ( ) ) ;
173
+ let exception : HttpClientException | Error = getException ( response . body ) ;
156
174
157
175
res . on ( "data" , ( chunk : string ) : void => {
158
176
response . body += chunk ;
@@ -164,12 +182,14 @@ class HttpURLConnectionClient implements ClientInterface {
164
182
}
165
183
166
184
if ( res . statusCode && ( res . statusCode < 200 || res . statusCode >= 300 ) ) {
185
+ // API error handling
167
186
try {
168
- const formattedData : ApiError | { [ key : string ] : never } = JSON . parse ( response . body ) ;
187
+ const formattedData : ApiError | { [ key : string ] : never } = JSON . parse ( response . body ) ;
169
188
const isApiError = "status" in formattedData ;
170
189
const isRequestError = "errors" in formattedData ;
171
190
172
191
if ( isApiError ) {
192
+ // Adyen API has returned an error
173
193
exception = new HttpClientException ( {
174
194
message : `HTTP Exception: ${ formattedData . status } . ${ res . statusMessage } : ${ formattedData . message } ` ,
175
195
statusCode : formattedData . status ,
@@ -213,7 +233,7 @@ class HttpURLConnectionClient implements ClientInterface {
213
233
214
234
private installCertificateVerifier ( terminalCertificatePath : string ) : void | Promise < HttpClientException > {
215
235
try {
216
- if ( terminalCertificatePath == "unencrypted" ) {
236
+ if ( terminalCertificatePath == "unencrypted" ) {
217
237
this . agentOptions = {
218
238
rejectUnauthorized : false
219
239
} ;
@@ -226,7 +246,7 @@ class HttpURLConnectionClient implements ClientInterface {
226
246
}
227
247
228
248
} catch ( e ) {
229
- const message = e instanceof Error ? e . message : "undefined" ;
249
+ const message = e instanceof Error ? e . message : "undefined" ;
230
250
return Promise . reject ( new HttpClientException ( { message : `Error loading certificate from path: ${ message } ` } ) ) ;
231
251
}
232
252
0 commit comments