Skip to content

feat: add reply.getCookie and reply.getCookies methods - #372

Open
lx3133584 wants to merge 1 commit into
fastify:mainfrom
lx3133584:feat/reply-get-cookie
Open

lx3133584 wants to merge 1 commit into
fastify:mainfrom
lx3133584:feat/reply-get-cookie

Conversation

@lx3133584

Copy link
Copy Markdown

Problem

When cookies are set on the reply via reply.setCookie, there was no method to inspect or read the pending response cookies inside the route handler or early lifecycle hooks.

Root Cause

Pending cookies are accumulated in an internal reply[kReplySetCookies] map until the onSend hook writes them to the Set-Cookie header. FastifyReply lacked getter methods to query this map.

Fix

  • Added fastifyCookieGetCookie and fastifyCookieGetCookies helpers.
  • Decorated FastifyReply with reply.getCookie(name) and reply.getCookies().
  • Added TypeScript definitions for the new reply methods in types/index.d.ts and types/index.tst.ts.

Testing

  • Added unit tests in test/cookie.test.js asserting reply.getCookie and reply.getCookies return correct values before onSend.
  • Verified 100% test coverage and TypeScript type tests pass.

Fixes fastify#269

Signed-off-by: Liang Xu <lx3133584@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds reply APIs for inspecting pending response cookies before onSend.

Changes:

  • Adds reply.getCookie() and reply.getCookies().
  • Adds TypeScript declarations and type assertions.
  • Tests basic retrieval of pending cookies.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.

File Description
index.js Implements and decorates cookie getters.
test/cookie.test.js Tests basic getter behavior.
types/index.d.ts Declares the new APIs.
types/index.tst.ts Verifies inferred return types.
Suppressed comments (1)

index.js:87

  • A valid cookie name such as __proto__ invokes the inherited prototype setter here instead of creating an own property. The cookie is then absent from Object.keys()/normal lookup semantics, and the returned object's prototype is unexpectedly replaced. Define the property explicitly (or otherwise use a prototype-safe container).
    result[c.name] = Object.assign({ value: c.value }, c.opts)

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread index.js
Comment on lines +85 to +87
const result = {}
for (const c of reply[kReplySetCookies].values()) {
result[c.name] = Object.assign({ value: c.value }, c.opts)
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.

2 participants