Skip to content

Commit c020f97

Browse files
authored
fix fetchBaseQuery for usage with node-fetch (#1473)
1 parent e3bc1fe commit c020f97

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
@@ -236,15 +236,29 @@ export function fetchBaseQuery({
236236

237237
meta.response = responseClone
238238

239-
let resultData
239+
let resultData: any
240+
let responseText: string = ''
240241
try {
241-
resultData = await handleResponse(response, responseHandler)
242+
let handleResponseError
243+
await Promise.all([
244+
handleResponse(response, responseHandler).then(
245+
(r) => (resultData = r),
246+
(e) => (handleResponseError = e)
247+
),
248+
// see https://github.com/node-fetch/node-fetch/issues/665#issuecomment-538995182
249+
// we *have* to "use up" both streams at the same time or they will stop running in node-fetch scenarios
250+
responseClone.text().then(
251+
(r) => (responseText = r),
252+
() => {}
253+
),
254+
])
255+
if (handleResponseError) throw handleResponseError
242256
} catch (e) {
243257
return {
244258
error: {
245259
status: 'PARSING_ERROR',
246260
originalStatus: response.status,
247-
data: await responseClone.clone().text(),
261+
data: responseText,
248262
error: String(e),
249263
},
250264
meta,

0 commit comments

Comments
 (0)