-
-
Notifications
You must be signed in to change notification settings - Fork 414
fix: ESM compatibility - avoid direct Response object mutation #1430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,9 +196,29 @@ export class HttpClient<SecurityDataType = unknown> { | |
body: typeof body === "undefined" || body === null ? null : payloadFormatter(body), | ||
} | ||
).then(async (response) => { | ||
const r = response as HttpResponse<T, E>; | ||
r.data = (null as unknown) as T; | ||
r.error = (null as unknown) as E; | ||
// Create a wrapper object that doesn't mutate the Response | ||
// This ensures compatibility with ESM environments where Response is read-only | ||
const r = { | ||
data: (null as unknown) as T, | ||
error: (null as unknown) as E, | ||
// Delegate Response properties | ||
ok: response.ok, | ||
status: response.status, | ||
statusText: response.statusText, | ||
headers: response.headers, | ||
url: response.url, | ||
redirected: response.redirected, | ||
type: response.type, | ||
body: response.body, | ||
bodyUsed: response.bodyUsed, | ||
// Delegate Response methods | ||
arrayBuffer: () => response.arrayBuffer(), | ||
blob: () => response.blob(), | ||
clone: () => response.clone(), | ||
formData: () => response.formData(), | ||
json: () => response.json(), | ||
text: () => response.text(), | ||
} as HttpResponse<T, E>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: HttpResponse Wrapper Causes Compatibility ProblemsThe new |
||
|
||
const responseToParse = responseFormat ? response.clone() : response; | ||
const data = !responseFormat ? r : await responseToParse[responseFormat]() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new wrapper records
response.bodyUsed
once and stores the boolean on the returned object, but the response body is consumed later in the function whenresponseFormat
is defined. After a call resolves,data.bodyUsed
still reports the initial value (false
) even though the underlyingResponse
has been read andjson()/text()
can no longer be called without throwing. Previously, because the originalResponse
instance was returned,bodyUsed
reflected the consumed state. Any client code that checksbodyUsed
to determine whether the body can be re-read will now be misled and may attempt a second read that fails.Useful? React with 👍 / 👎.