fix: preserve Headers object during 402 retry in x402-fetch
Summary
- Fixed Headers object preservation during 402 retry in
x402-fetch - Replaced spread operator with Headers constructor to properly handle all HeadersInit types
- Added test coverage for Headers object preservation
Problem
The retry logic in wrapFetchWithPayment used the spread operator on init.headers, which doesn't work correctly with Headers objects. The spread operator does not iterate through the actual header entries, causing headers to be lost during retry.
Example of the issue:
// Original headers
const headers = new Headers({
'Accept': 'application/json, text/event-stream',
'Content-Type': 'application/json'
});
// Spread operator fails - results in empty objects
{ ...headers }
Solution
Use the Headers constructor which properly handles all HeadersInit types:
- Headers objects:
new Headers(headersObj)clones all entries - Plain objects:
new Headers({ 'Accept': 'application/json' })works as expected - Arrays:
new Headers([['Accept', 'application/json']])converts correctly
Impact
This fix ensures headers are preserved during retry.
✅ Heimdall Review Status
| Requirement | Status | More Info | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Reviews |
✅
1/1
|
Denominator calculation
|
@dankelleher is attempting to deploy a commit to the Coinbase Team on Vercel.
A member of the Team first needs to authorize it.
@dankelleher you need to sign your commits, otherwise lgtm
@dankelleher ping-- need you to sign your commits but otherwise g2g