fix(types): expose namespace decorator types#411
Open
abdulmunimjemal wants to merge 1 commit into
Open
Conversation
The `FastifyJwtNamespace` helper previously mapped namespaced JWT decorators (`<ns>JwtSign`, `<ns>JwtVerify`, `<ns>JwtDecode`) to `JWT['sign']`, `JWT['verify']` and `JWT['decode']`. Those refer to the synchronous `fastify.jwt` instance methods, which take an explicit token argument — not the asynchronous request/reply decorators that extract the token from the request. Extract the request/reply decorator signatures into reusable `JwtSignFunction`, `JwtVerifyFunction` and `JwtDecodeFunction` interfaces, reuse them for the default `FastifyReply.jwtSign`, `FastifyRequest.jwtVerify` and `FastifyRequest.jwtDecode` declarations, and map `FastifyJwtNamespace` entries to them so consumers using the `namespace` option get correctly typed decorators. Refs fastify#348
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #348.
Problem
The
FastifyJwtNamespace<T>helper currently maps the namespaceddecorators (
<ns>JwtSign,<ns>JwtVerify,<ns>JwtDecode) toJWT['sign'],JWT['verify']andJWT['decode']. Those types describethe synchronous
fastify.jwt.*instance methods, which take an explicittoken argument and return a value directly.
The decorators that actually end up on
FastifyRequest/FastifyReplyhave different signatures:
reply.jwtSign(...)is asynchronous and returnsPromise<string>.request.jwtVerify(...)andrequest.jwtDecode(...)extract thetoken from the request, so they do not take a
tokenargument.As a result, users following the namespace docs
either get incorrect IntelliSense or have to hand-roll the augmentation
themselves to call
request.accessTokenVerify()orreply.accessTokenSign(payload).Fix
JwtSignFunction,JwtVerifyFunctionandJwtDecodeFunctioninterfaces.
FastifyReply.jwtSign,FastifyRequest.jwtVerifyandFastifyRequest.jwtDecodedeclarations, so there is a single source of truth.
FastifyJwtNamespaceentries to those interfaces, so consumersusing the
namespace/jwtDecode/jwtSign/jwtVerifyregistrationoptions get correctly typed decorators on
FastifyRequestandFastifyReply.This is a types-only change. No runtime code is touched.
Tests
types/index.tst.ts:FastifyJwtNamespaceassertions updated to expect the newfunction types instead of the
JWTinstance method types.intentionally distinct from the
JWTinstance method types.FastifyInstance,FastifyRequestandFastifyReplywithaccessToken*decorators(the exact scenario from Type augmentation for namespace methods? #348) and verifies the decorators are
callable without a token argument and return the expected payload
type.
npm testpasses: 175 unit tests, 100% coverage, 34 type assertions.Checklist
npm run test