Skip to content

fix(types): export request and reply namespace type helpers - #434

Open
helmipradita wants to merge 1 commit into
fastify:mainfrom
helmipradita:fix/types-namespace-request-reply
Open

helmipradita wants to merge 1 commit into
fastify:mainfrom
helmipradita:fix/types-namespace-request-reply

Conversation

@helmipradita

Copy link
Copy Markdown

Fixes #321.

Problem

When registering the plugin with the namespace option (e.g. namespace: 'auth'), runtime decorators are registered on FastifyRequest (request.authJwtVerify, request.authJwtDecode) and FastifyReply (reply.authJwtSign).

However, the documentation and type definitions provided only FastifyJwtNamespace, a union type merging both request and reply functions, and instructed users to extend FastifyInstance:

declare module 'fastify' {
  interface FastifyInstance extends FastifyJwtNamespace<{namespace: 'auth'}> {}
}

This caused:

  1. FastifyInstance incorrectly receiving request/reply methods.
  2. FastifyRequest and FastifyReply not receiving their respective decorator typings, making calls like request.authJwtVerify() or reply.authJwtSign() fail TypeScript compilation.

Fix

  • Export FastifyJwtRequestNamespace and FastifyJwtReplyNamespace helper types scoped specifically to request and reply interfaces.
  • Keep FastifyJwtNamespace as an intersection of both helpers for backwards compatibility.
  • Update README.md documenting the correct augmentation pattern for FastifyRequest and FastifyReply.
  • Add type tests in types/namespace.tst.ts asserting that request.authJwtVerify, request.authJwtDecode, and reply.authJwtSign are properly typed.

Checklist

  • run npm test — lint clean, 199/199 unit tests (100% coverage), 49/49 type assertions
  • tests are included
  • documentation is changed or added
  • commit message and code follows the Developer's Certificate of Origin and the Code of Conduct

@SameRainbows SameRainbows 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.

Nice work separating the namespace decorator typings.

A few key points verified:

  • Preserving FastifyJwtNamespace as the intersection FastifyJwtRequestNamespace<C> & FastifyJwtReplyNamespace<C> maintains complete backwards compatibility for existing codebases.
  • Cleanly addresses #321 by allowing users to augment FastifyRequest and FastifyReply independently without polluting FastifyInstance.
  • Fallback logic for custom method keys (jwtDecode, jwtVerify, jwtSign) behaves as expected when only a subset of method names are overridden.
  • All 15 CI matrix jobs across Node 20 through Node 26 (Ubuntu, macOS, Windows) and tstyche assertions pass cleanly.

This branch has not been deployed

No deployments
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.

FastifyInterface TypeScript augmentation when using namespaces

2 participants