web-std-io icon indicating copy to clipboard operation
web-std-io copied to clipboard

fix: Allow subclass methods of Headers to be called correctly

Open ArnoSaine opened this issue 1 year ago • 1 comments
trafficstars

This PR fixes an issue where subclass methods of Headers were not being called correctly.

Problem

When a class extends Headers, the subclass's methods (like append) were not accessible or called properly. This was caused by the Proxy instance in the Headers constructor, which intercepted method calls and directed them to the URLSearchParams prototype, bypassing any overrides defined in subclasses.

Solution

  • Removed the Proxy from the constructor.
  • Re-implemented the proxied methods in the Headers class to maintain the expected behavior and allowing overrides by subclasses.

Example

import SuperHeaders from "@mjackson/headers"; // Extends `Headers` in Remix

// Correctly initialized. The constructor calls a setter directly from `SuperHeaders.prototype`.
console.log(new SuperHeaders({ cookie: "foo=123" }).cookie.size); // 1

// Incorrectly initialized. The constructor calls `this.append`, which is unexpectedly not handled
// by the subclass due to the Proxy.
console.log(new SuperHeaders([["cookie", "foo=123"]]).cookie.size); // 0

cc: @mjackson

ArnoSaine avatar Aug 30 '24 20:08 ArnoSaine

🦋 Changeset detected

Latest commit: ff5d0ff5b7450b115e8ac23b54edb4e6f989f445

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@remix-run/web-fetch Patch

Not sure what this means? Click here to learn what changesets are.

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

changeset-bot[bot] avatar Aug 30 '24 20:08 changeset-bot[bot]