web-std-io
web-std-io copied to clipboard
fix: Allow subclass methods of Headers to be called correctly
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
Proxyfrom the constructor. - Re-implemented the proxied methods in the
Headersclass 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
🦋 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