Skip to content

Commit be2dbb7

Browse files
author
Lenz Weber
committed
fix fetchBaseQuery for usage with node-fetch
1 parent eb06822 commit be2dbb7

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

packages/toolkit/src/query/fetchBaseQuery.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,30 @@ export function fetchBaseQuery({
229229

230230
meta.response = responseClone
231231

232-
let resultData
232+
let resultData: any
233+
let responseText: string = ''
233234
try {
234-
resultData = await handleResponse(response, responseHandler)
235+
let handleResponseError
236+
await Promise.all([
237+
handleResponse(response, responseHandler).then(
238+
(r) => (resultData = r),
239+
(e) => (handleResponseError = e)
240+
),
241+
// see https://github.com/node-fetch/node-fetch/issues/665#issuecomment-538995182
242+
// we *have* to "use up" both streams at the same time or they will stop running in node-fetch scenarios
243+
responseClone.text().then(
244+
(r) => (responseText = r),
245+
() => {}
246+
),
247+
])
248+
if (handleResponseError) throw handleResponseError
235249
} catch (e) {
236250
return {
237251
error: {
238252
status: 'PARSING_ERROR',
239253
originalStatus: response.status,
240254
statusText: response.statusText,
241-
data: await responseClone.clone().text(),
255+
data: responseText,
242256
error: String(e),
243257
},
244258
meta,

0 commit comments

Comments
 (0)