fix(setCookie): handle encode and stringify serialize options#1377
Conversation
📝 WalkthroughWalkthroughThe changes refactor Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 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 |
commit: |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
test/cookies.test.ts (1)
145-152: Good targeted regression test; consider extending coverage tostringify.The test correctly exercises the fix: with the previous behavior,
encodewould have been ignored and the default encoder would have re-escaped%to%25, so this would have failed. Nice.Two optional nits:
stringifyis the other option extracted insetCookiebut is not covered by any test. A small companion test (e.g.setCookie(event, "x", { foo: 1 } as any, { stringify: (v) => JSON.stringify(v) })) would lock in both halves of the fix.- For consistency with the surrounding tests, you may want to also assert
expect(await result.text()).toBe("200");so the handler's return path is verified.it("respects custom encode option", async () => { t.app.get("/", (event) => { setCookie(event, "x", "%22true%22", { encode: (v) => v }); return "200"; }); const result = await t.fetch("/"); expect(result.headers.getSetCookie()).toEqual(["x=%22true%22; Path=/"]); + expect(await result.text()).toBe("200"); });🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@test/cookies.test.ts` around lines 145 - 152, Add a companion test to cover the `stringify` option in addition to `encode`: create a test (e.g., "respects custom stringify option") that calls setCookie(event, "x", { foo: 1 } as any, { stringify: (v) => JSON.stringify(v) }) in the request handler, fetch the route, assert the Set-Cookie header contains the JSON stringified value, and also assert the response body via expect(await result.text()).toBe("200");; update the existing "respects custom encode option" test to include expect(await result.text()).toBe("200"); so both handler return paths are verified.src/utils/cookie.ts (1)
88-89: LGTM — clean fix for the encode/stringify pass-through.Splitting
encode/stringifyout of the cookie attributes object and forwarding them as the dedicatedSerializeOptionsargument is the correct fix. The previous code path was effectively dropping these callbacks because cookie-es ignores them when present on theSetCookieattribute object.Two small notes (non-blocking):
- Line 99 just below still uses
options || {}while the new line 88 usesoptions ?? {}. Consider aligning to??for consistency (treats onlynull/undefinedas missing, which is the intent here sinceoptionsis typed asCookieSerializeOptions | undefined).- As you already called out in the PR description,
{ name, value, path: "/", ...attrs }lets a caller-suppliedname/value/pathinoptionssilently override the explicit args. Worth following up upstream in cookie-es as you suggested — not a regression in this PR.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/utils/cookie.ts` around lines 88 - 89, The code correctly extracts encode/stringify into serialize options, but two small fixes remain: (1) normalize nullish handling by using the nullish coalescing operator for options everywhere (replace uses of options || {} with options ?? {}) so only null/undefined fall back; (2) prevent caller-supplied name/value/path in options from silently overriding explicit args by excluding name, value, and path from the spread attrs before building the cookie object passed to serializeCookie (ensure { name, value, path: "/", ...attrsWithoutNameValuePath } is used), keeping encode/stringify passed as the second argument to serializeCookie.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/utils/cookie.ts`:
- Around line 88-89: The code correctly extracts encode/stringify into serialize
options, but two small fixes remain: (1) normalize nullish handling by using the
nullish coalescing operator for options everywhere (replace uses of options ||
{} with options ?? {}) so only null/undefined fall back; (2) prevent
caller-supplied name/value/path in options from silently overriding explicit
args by excluding name, value, and path from the spread attrs before building
the cookie object passed to serializeCookie (ensure { name, value, path: "/",
...attrsWithoutNameValuePath } is used), keeping encode/stringify passed as the
second argument to serializeCookie.
In `@test/cookies.test.ts`:
- Around line 145-152: Add a companion test to cover the `stringify` option in
addition to `encode`: create a test (e.g., "respects custom stringify option")
that calls setCookie(event, "x", { foo: 1 } as any, { stringify: (v) =>
JSON.stringify(v) }) in the request handler, fetch the route, assert the
Set-Cookie header contains the JSON stringified value, and also assert the
response body via expect(await result.text()).toBe("200");; update the existing
"respects custom encode option" test to include expect(await
result.text()).toBe("200"); so both handler return paths are verified.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: c469e1ec-3d99-43e0-a3e8-7258a296103e
📒 Files selected for processing (2)
src/utils/cookie.tstest/cookies.test.ts
encode and stringify serialize options
related to a0761e9
this respects encode/stringify options - these do not seem to be used in the first argument (
SetCookie).(spreading defeats type safety (a ts limitation), but we could update the function signature in
cookie-esto disallow accidental spreading if you're interested)Summary by CodeRabbit