Skip to content

fix: preserve repeated header values when merging with defaults - #667

Open
abdul-karim-mia wants to merge 1 commit into
unjs:mainfrom
abdul-karim-mia:fix/merge-headers-tuple-values
Open

abdul-karim-mia wants to merge 1 commit into
unjs:mainfrom
abdul-karim-mia:fix/merge-headers-tuple-values

Conversation

@abdul-karim-mia

@abdul-karim-mia abdul-karim-mia commented Nov 27, 2026 •

Copy link
Copy Markdown

🔗 Linked issue

Closes #655

❓ Type of change

  • 📖 Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

Previously, when defaults.headers was configured, mergeHeaders iterated directly over iterable/tuple input headers and called headers.set(key, value) on each entry. For tuple arrays or other iterables containing repeated header names (such as [["Accept", "application/json"], ["Accept", "text/plain"]]), each set overwrote earlier values for the same header instead of combining them via Headers.

This updates mergeHeaders in src/utils.ts to normalize input with new Headers(input) first and clear-then-append each unique header name on the merged Headers object, preserving combined repeated values and multi-value Set-Cookie headers while still allowing request headers to override matching defaults.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly. (N/A)
  • I have added tests to cover my changes (test/index.test.ts).

Summary by CodeRabbit

  • Bug Fixes
    • Preserved repeated request header values when combining them with default headers, including multiple Accept and Set-Cookie values.

Signed-off-by: Abdul karim mia <akmia51@gmail.com>
@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

When default headers are configured, mergeHeaders now removes a matching default once and appends every request header value. Tests cover repeated Accept values, repeated default and request tuples, and Set-Cookie retrieval.

Changes: Repeated header values

Layer / File(s) Summary
Merge repeated headers
src/utils.ts, test/index.test.ts
mergeHeaders removes a matching default header once, then appends each request header value. Tests cover repeated Accept values, repeated default and request tuples, and separate Set-Cookie values.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • unjs/ofetch#666: Changes the same mergeHeaders behavior to address lost repeated request header values.
  • unjs/ofetch#656: Also addresses issue #655 through changes to mergeHeaders.

Suggested reviewers: pi0

Merge Risk: Minimal · up to 29064

This change preserves repeated request header values when default headers are configured. No actionable merge risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix: preserve repeated header values when merging with defaults" matches the main change. The raw summary states that mergeHeaders now appends every value from the input headers instead of …
Linked Issues check Passed [#655] requires that repeated header values from HeadersInit input survive when defaults are configured, and that request headers override defaults with the same name. At the reviewed head, src/utils.…
Out of Scope Changes check Passed The PR changes two files: src/utils.ts (mergeHeaders, +7/-4 per the summary) and test/index.test.ts (+64 lines, one new test). Both changes address the header-merge defect in [#655]. The head source…
Full details: Title check

Explanation

The title "fix: preserve repeated header values when merging with defaults" matches the main change. The raw summary states that mergeHeaders now appends every value from the input headers instead of replacing prior values with the last one. The added test covers repeated Accept tuples and repeated Set-Cookie values with default headers. The linked objectives describe the same bug (issue #655), so the title describes the primary change.

Full details: Linked Issues check

Explanation

[#655] requires that repeated header values from HeadersInit input survive when defaults are configured, and that request headers override defaults with the same name. At the reviewed head, src/utils.ts mergeHeaders builds new Headers(defaults), then iterates new Headers(input). Iterating a Headers object yields one combined value per name (for example application/json, text/plain for Accept). For each unique key, the code deletes the default once, then appends the combined value. This keeps the repeated Accept values and replaces a default with the same name. The x-client default is unrelated and remains in the result. The existing Headers behavior for Set-Cookie is kept, because iteration yields each Set-Cookie value separately and each is appended. The PR summary reports a new test in test/index.test.ts covering repeated request Accept tuples with defaults and Set-Cookie retrieval. The test diff could not be read in this review, so the test content is not independently verified. The code evidence alone supports the required behavior.

Full details: Out of Scope Changes check

Explanation

The PR changes two files: src/utils.ts (mergeHeaders, +7/-4 per the summary) and test/index.test.ts (+64 lines, one new test). Both changes address the header-merge defect in [#655]. The head source of mergeHeaders contains only the header-merge logic described in the PR. No unrelated behavior change was found in the reviewed content. The test diff could not be read directly, so its exact content was not checked for unrelated changes. The summary describes it as header-merge coverage only.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ed066958-d6fe-4b49-b11a-f84231283038
Commits

Reviewing files that changed from the base of the PR and between 1dbc37f and 2906433.

Files selected for processing: 2

  • src/utils.ts
  • test/index.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Default headers cause repeated values in HeadersInit tuples to be dropped

1 participant