Skip to content

Conversation

omarmciver
Copy link

Summary

Fixes #1429 - Response object mutation fails in ESM environments

Problem

The Fetch client template directly mutates Response objects by adding data and error properties. This works in CommonJS but fails in ESM with node-fetch v3 where Response objects are truly read-only.

Solution

Instead of mutating the Response object, this PR creates a wrapper object that:

  • Contains the custom data and error properties
  • Delegates all Response properties and methods to the original response
  • Maintains full backward compatibility with existing code
  • Works in both CommonJS and ESM environments

Changes

Modified templates/base/http-clients/fetch-http-client.ejs to create a wrapper object instead of directly mutating the Response:

// Before (fails in ESM):
const r = response as HttpResponse<T, E>;
r.data = null;
r.error = null;

// After (works everywhere):
const r = {
  data: null,
  error: null,
  // Delegate Response properties
  ok: response.ok,
  status: response.status,
  // ... etc
  // Delegate Response methods
  json: () => response.json(),
  text: () => response.text(),
  // ... etc
} as HttpResponse<T, E>;

Testing

  • ✅ Created test script to verify the template generates correct code
  • ✅ Tested that the fix doesn't directly mutate Response objects
  • ✅ Verified wrapper object is created properly
  • ✅ Confirmed backward compatibility is maintained

Breaking Changes

None - this change is fully backward compatible. The generated API clients will have the same interface and behavior, but will now work in ESM environments.

Additional Notes

This fix enables users to migrate to:

  • ESM modules ("type": "module" in package.json)
  • node-fetch v3 (ESM-only version)
  • Native fetch in Node.js 18+

Without this fix, users need workarounds like wrapping fetch with a Proxy to make Response properties writable, which is not ideal.

Copy link

changeset-bot bot commented Sep 23, 2025

⚠️ No Changeset found

Latest commit: 839fb08

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

When using fetch in ESM environments (e.g., node-fetch v3+), the Response
object is read-only and attempting to set properties like 'data' and 'error'
directly throws an error.

This fix creates a wrapper object that delegates to the original Response
while allowing the assignment of custom properties, ensuring compatibility
with both CommonJS and ESM environments.

Fixes compatibility with node-fetch v3+ and other ESM-only fetch implementations.
@omarmciver omarmciver force-pushed the fix/esm-response-mutation branch from ef1631a to 839fb08 Compare September 23, 2025 14:03
@smorimoto
Copy link
Collaborator

bugbot run

@smorimoto
Copy link
Collaborator

@codex review

@smorimoto
Copy link
Collaborator

@omarmciver Could you add a changeset?

@smorimoto smorimoto added the bug Something isn't working label Oct 4, 2025
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting

Comment on lines +199 to +214
// 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep HttpResponse.bodyUsed in sync with underlying Response

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 when responseFormat is defined. After a call resolves, data.bodyUsed still reports the initial value (false) even though the underlying Response has been read and json()/text() can no longer be called without throwing. Previously, because the original Response instance was returned, bodyUsed reflected the consumed state. Any client code that checks bodyUsed to determine whether the body can be re-read will now be misled and may attempt a second read that fails.

Useful? React with 👍 / 👎.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is being reviewed by Cursor Bugbot

Details

You are on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

formData: () => response.formData(),
json: () => response.json(),
text: () => response.text(),
} as HttpResponse<T, E>;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: HttpResponse Wrapper Causes Compatibility Problems

The new HttpResponse wrapper object introduces several compatibility issues. It's a plain object, which breaks instanceof Response checks. Properties like body and bodyUsed are statically copied, leading to stale values, and the stream() method is missing from the delegated Response methods.

Fix in Cursor Fix in Web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix: Response object mutation fails in ESM environments with node-fetch v3
2 participants