fix: normalize headers reassigned in an onRequest hook#604
Conversation
An `onRequest` hook can reassign `options.headers` to a plain object, but the request flow calls `.get()` on it right after the hook runs, throwing `headers.get is not a function` for payload requests. Re-normalize it to a `Headers` instance after the hook, mirroring the v1 fix in unjs#524.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughIn ChangesHeader normalization after onRequest hooks
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
resolveFetchOptionsreturnsoptions.headersas aHeadersinstance, but anonRequesthook can reassign it to a plain object (a validHeadersInit). In v2 the request flow readscontext.options.headers.get("content-type")immediately after the hook runs, so reassigning a plain object throwsTypeError: context.options.headers.get is not a functionon any payload request with a JSON-serializable body.This was already handled on v1 in #524 (and #436 before it), but the v2 rewrite reintroduced it. The normalization now lives inside the JSON-body branch and runs after that first
.get()call, so it never gets a chance to run.Repro:
The fix mirrors #524: re-normalize
options.headersback to aHeadersinstance right after theonRequesthook, and drop the now-redundant re-creation in the body branch (resolveFetchOptionsalready returns a freshHeaders, so the later.set()calls can mutate it directly).Added a regression test covering both a
Headersinstance and a plain object reassigned in the hook, matching the v1 test. The new test fails without the change (headers.get is not a function) and passes with it. Full suite (29) is green andeslint/prettier/tsc --noEmitare clean.Summary by CodeRabbit