fix(http): merge default options when partial opts are provided - #2
Conversation
defineCachedHandler used a default parameter instead of spreading
defaults into the function body. Passing partial options like
{ maxAge: 60 } left swr as undefined, silently producing max-age
instead of s-maxage + stale-while-revalidate headers.
Matches the existing pattern in defineCachedFunction (cache.ts:28).
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughRefactored defineCachedHandler to apply default cache options by merging defaults with provided opts inside the function body; added tests verifying partial opts are merged; updated README snippet of the function signature. Changes
Sequence Diagram(s)(omitted) Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
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 |
defineCachedHandlerused a default parameter value instead of merging defaults into the function body. This meant passing partial options silently dropped all unspecified defaults.defineCachedFunctionalready does this correctly (cache.ts line 28):But
defineCachedHandlerrelied on the parameter default, which only applies when no argument is passed at all:The practical effect:
defineCachedHandler(handler, { maxAge: 60 })leftswrasundefined, so the cache-control header logic fell into the non-SWR branch and producedmax-age=60instead ofs-maxage=60, stale-while-revalidate. The cache layer itself worked fine (becausecachedFunctionmerges defaults internally), but the HTTP-specific header logic read from the un-merged opts.I noticed this while reading through the codebase - the inconsistency between how the two functions handle defaults stood out. PLAN.md already had it listed as issue #4.
The fix applies the same spread pattern from
defineCachedFunction, plus a regression test that confirms SWR headers are produced when onlymaxAgeis passed.Summary by CodeRabbit
Bug Fixes
Tests
Documentation