Skip to content

Commit e43cde6

Browse files
author
dustin deus
committed
only parse json when content-type is correct, other content-types are passed as it is.
1 parent 7d04446 commit e43cde6

File tree

2 files changed

+133
-36
lines changed

2 files changed

+133
-36
lines changed

src/http-data-source.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,22 +283,23 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
283283
signal: request.signal,
284284
})
285285

286-
responseData.body.setEncoding('utf8')
287286
let data = ''
288287
for await (const chunk of responseData.body) {
289288
data += chunk
290289
}
291290

292-
let json
293-
if (data) {
294-
json = sjson.parse(data)
291+
let json = null
292+
if (responseData.headers['content-type']?.includes('application/json')) {
293+
if (data !== '') {
294+
json = sjson.parse(data)
295+
}
295296
}
296297

297298
const response: Response<TResult> = {
298299
isFromCache: false,
299300
memoized: false,
300301
...responseData,
301-
body: json,
302+
body: json ?? data,
302303
}
303304

304305
this.onResponse<TResult>(request, response)

0 commit comments

Comments
 (0)