Skip to content

Commit 7a025a0

Browse files
committed
Change res.body to string
1 parent 1c961ea commit 7a025a0

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@adyen/api-library",
3-
"version": "3.2.3",
3+
"version": "3.2.4",
44
"description": "The Adyen API Library for NodeJS enables you to work with Adyen APIs.",
55
"main": "dist/lib/src/index.js",
66
"types": "dist/lib/src/index.d.ts",

src/httpClient/httpURLConnectionClient.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ class HttpURLConnectionClient implements ClientInterface {
118118
connectionRequest.flushHeaders();
119119

120120
connectionRequest.on("response", (res: IncomingMessage): void => {
121-
const response: { headers: IncomingHttpHeaders; body: (string | Buffer)[] | string; statusCode: number | undefined } = {
121+
const response: { headers: IncomingHttpHeaders; body: string; statusCode: number | undefined } = {
122122
statusCode: res.statusCode,
123123
headers: res.headers,
124-
body: []
124+
body: ""
125125
};
126126

127127
const getException = (responseBody: string): HttpClientException => new HttpClientException({
@@ -134,23 +134,18 @@ class HttpURLConnectionClient implements ClientInterface {
134134

135135
let exception: HttpClientException | Error = getException(response.body.toString());
136136

137-
res.on("data", (data: string | Buffer): void => {
138-
(response.body as (string | Buffer)[]).push(data);
137+
res.on("data", (chunk: string): void => {
138+
response.body += chunk;
139139
});
140140

141141
res.on("end", (): void => {
142142
if (!res.complete) {
143143
reject(new Error("The connection was terminated while the message was still being sent"));
144144
}
145145

146-
if (response.body.length) {
147-
response.body = (response.body as []).join();
148-
}
149-
150146
if (res.statusCode && (res.statusCode < 200 || res.statusCode >= 300)) {
151147
try {
152-
const dataString = response.body.toString();
153-
const formattedData: ApiError | {[key: string]: never} = JSON.parse(dataString);
148+
const formattedData: ApiError | {[key: string]: never} = JSON.parse(response.body);
154149
const isApiError = "status" in formattedData;
155150
const isRequestError = "errors" in formattedData;
156151

@@ -160,12 +155,12 @@ class HttpURLConnectionClient implements ClientInterface {
160155
statusCode: formattedData.status,
161156
errorCode: formattedData.errorCode,
162157
responseHeaders: res.headers,
163-
responseBody: dataString,
158+
responseBody: response.body,
164159
});
165160
} else if (isRequestError) {
166-
exception = new Error(dataString);
161+
exception = new Error(response.body);
167162
} else {
168-
exception = getException(dataString);
163+
exception = getException(response.body);
169164
}
170165
} catch (e) {
171166
reject(exception);

0 commit comments

Comments
 (0)