Skip to content

Commit 51b998a

Browse files
authored
Merge pull request #477 from Adyen/hotfix/res-body
2 parents b637feb + b5b947a commit 51b998a

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@adyen/api-library",
3-
"version": "4.0.1",
3+
"version": "4.0.2",
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",
@@ -52,17 +52,17 @@
5252
"kind-of": "^6.0.3",
5353
"minimist": ">=1.2.3",
5454
"nock": "12.0.3",
55-
"release-it": "13.6.1",
55+
"release-it": "13.6.2",
5656
"ts-auto-mock": "^1.6.0",
5757
"ts-jest": "25.5.1",
5858
"ts-loader": "7.0.5",
5959
"ttypescript": "^1.5.10",
60-
"typescript": "3.9.3",
60+
"typescript": "3.9.5",
6161
"webpack": "4.43.0",
6262
"webpack-cli": "3.3.11"
6363
},
6464
"dependencies": {
65-
"@types/node": "14.0.5",
65+
"@types/node": "14.0.9",
6666
"https-proxy-agent": "5.0.0"
6767
}
6868
}

src/httpClient/httpURLConnectionClient.ts

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

118118
connectionRequest.on("response", (res: IncomingMessage): void => {
119-
const response: { headers: IncomingHttpHeaders; body: (string | Buffer)[] | string; statusCode: number | undefined } = {
119+
const response: { headers: IncomingHttpHeaders; body: string; statusCode: number | undefined } = {
120120
statusCode: res.statusCode,
121121
headers: res.headers,
122-
body: []
122+
body: ""
123123
};
124124

125125
const getException = (responseBody: string): HttpClientException => new HttpClientException({
@@ -132,23 +132,18 @@ class HttpURLConnectionClient implements ClientInterface {
132132

133133
let exception: HttpClientException | Error = getException(response.body.toString());
134134

135-
res.on("data", (data: string | Buffer): void => {
136-
(response.body as (string | Buffer)[]).push(data);
135+
res.on("data", (chunk: string): void => {
136+
response.body += chunk;
137137
});
138138

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

144-
if (response.body.length) {
145-
response.body = (response.body as []).join();
146-
}
147-
148144
if (res.statusCode && (res.statusCode < 200 || res.statusCode >= 300)) {
149145
try {
150-
const dataString = response.body.toString();
151-
const formattedData: ApiError | {[key: string]: never} = JSON.parse(dataString);
146+
const formattedData: ApiError | {[key: string]: never} = JSON.parse(response.body);
152147
const isApiError = "status" in formattedData;
153148
const isRequestError = "errors" in formattedData;
154149

@@ -158,12 +153,12 @@ class HttpURLConnectionClient implements ClientInterface {
158153
statusCode: formattedData.status,
159154
errorCode: formattedData.errorCode,
160155
responseHeaders: res.headers,
161-
responseBody: dataString,
156+
responseBody: response.body,
162157
});
163158
} else if (isRequestError) {
164-
exception = new Error(dataString);
159+
exception = new Error(response.body);
165160
} else {
166-
exception = getException(dataString);
161+
exception = getException(response.body);
167162
}
168163
} catch (e) {
169164
reject(exception);

0 commit comments

Comments
 (0)