Skip to content

Commit 32cdb8d

Browse files
committed
Code format and renaming
1 parent 690fe91 commit 32cdb8d

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

src/httpClient/httpURLConnectionClient.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,26 @@ class HttpURLConnectionClient implements ClientInterface {
3939
public proxy?: AgentOptions;
4040
private agentOptions!: AgentOptions;
4141

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+
*/
4257
public request(
43-
endpoint: string, json: string, config: Config, isApiRequired: boolean,
58+
endpoint: string,
59+
json: string,
60+
config: Config,
61+
isApiRequired: boolean,
4462
requestOptions: IRequest.Options,
4563
): Promise<string> {
4664
requestOptions.headers ??= {};
@@ -68,13 +86,13 @@ class HttpURLConnectionClient implements ClientInterface {
6886
requestOptions.headers[ApiConstants.CONTENT_TYPE] = ApiConstants.APPLICATION_JSON_TYPE;
6987

7088
const httpConnection: ClientRequest = this.createRequest(endpoint, requestOptions, config.applicationName);
71-
return this.doPostRequest(httpConnection, json);
89+
return this.doRequest(httpConnection, json);
7290
}
7391

7492
public post(endpoint: string, postParameters: [string, string][], config: Config): Promise<string> {
7593
const postQuery: string = this.getQuery(postParameters);
7694
const connectionRequest: ClientRequest = this.createRequest(endpoint, {}, config.applicationName);
77-
return this.doPostRequest(connectionRequest, postQuery);
95+
return this.doRequest(connectionRequest, postQuery);
7896
}
7997

8098
private createRequest(endpoint: string, requestOptions: IRequest.Options, applicationName?: string): ClientRequest {
@@ -113,7 +131,7 @@ class HttpURLConnectionClient implements ClientInterface {
113131
requestOptions.headers[ApiConstants.ACCEPT_CHARSET] = HttpURLConnectionClient.CHARSET;
114132
// user-agent header
115133
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;
117135
// custom headers
118136
requestOptions.headers[ApiConstants.ADYEN_LIBRARY_NAME] = LibraryConstants.LIB_NAME;
119137
requestOptions.headers[ApiConstants.ADYEN_LIBRARY_VERSION] = LibraryConstants.LIB_VERSION;
@@ -133,7 +151,7 @@ class HttpURLConnectionClient implements ClientInterface {
133151
return params.map(([key, value]): string => `${key}=${value}`).join("&");
134152
}
135153

136-
private doPostRequest(connectionRequest: ClientRequest, json: string): Promise<string> {
154+
private doRequest(connectionRequest: ClientRequest, json: string): Promise<string> {
137155
return new Promise((resolve, reject): void => {
138156
connectionRequest.flushHeaders();
139157

@@ -144,15 +162,15 @@ class HttpURLConnectionClient implements ClientInterface {
144162
body: ""
145163
};
146164

165+
// define default exception (in case of error during the handling of the response)
147166
const getException = (responseBody: string): HttpClientException => new HttpClientException({
148167
message: `HTTP Exception: ${response.statusCode}. ${res.statusMessage}`,
149168
statusCode: response.statusCode,
150169
errorCode: undefined,
151170
responseHeaders: response.headers,
152171
responseBody,
153172
});
154-
155-
let exception: HttpClientException | Error = getException(response.body.toString());
173+
let exception: HttpClientException | Error = getException(response.body);
156174

157175
res.on("data", (chunk: string): void => {
158176
response.body += chunk;
@@ -164,12 +182,14 @@ class HttpURLConnectionClient implements ClientInterface {
164182
}
165183

166184
if (res.statusCode && (res.statusCode < 200 || res.statusCode >= 300)) {
185+
// API error handling
167186
try {
168-
const formattedData: ApiError | {[key: string]: never} = JSON.parse(response.body);
187+
const formattedData: ApiError | { [key: string]: never } = JSON.parse(response.body);
169188
const isApiError = "status" in formattedData;
170189
const isRequestError = "errors" in formattedData;
171190

172191
if (isApiError) {
192+
// Adyen API has returned an error
173193
exception = new HttpClientException({
174194
message: `HTTP Exception: ${formattedData.status}. ${res.statusMessage}: ${formattedData.message}`,
175195
statusCode: formattedData.status,
@@ -213,7 +233,7 @@ class HttpURLConnectionClient implements ClientInterface {
213233

214234
private installCertificateVerifier(terminalCertificatePath: string): void | Promise<HttpClientException> {
215235
try {
216-
if (terminalCertificatePath == "unencrypted"){
236+
if (terminalCertificatePath == "unencrypted") {
217237
this.agentOptions = {
218238
rejectUnauthorized: false
219239
};
@@ -226,7 +246,7 @@ class HttpURLConnectionClient implements ClientInterface {
226246
}
227247

228248
} catch (e) {
229-
const message = e instanceof Error ? e.message: "undefined";
249+
const message = e instanceof Error ? e.message : "undefined";
230250
return Promise.reject(new HttpClientException({ message: `Error loading certificate from path: ${message}` }));
231251
}
232252

0 commit comments

Comments
 (0)