Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FastifyInterface TypeScript augmentation when using namespaces #321

Open
chiptoma opened this issue Dec 22, 2023 · 3 comments
Open

FastifyInterface TypeScript augmentation when using namespaces #321

chiptoma opened this issue Dec 22, 2023 · 3 comments

Comments

@chiptoma
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch @fastify/[email protected] for the project I'm working on.

When using the namespace option, the fastify.jwt object no longer contains the JWT interface, but the namespace objects.

In my case, I created two namespaces auth and refresh and the fastify.jwt now contains auth: JWT, refresh: JWT.

Unfortunately, I wasn't able to override the augmentation, so I commented it out for the moment.

Maybe this should be left for the implementation to be typed.

Here is the diff that solved my problem:

diff --git a/node_modules/@fastify/jwt/types/jwt.d.ts b/node_modules/@fastify/jwt/types/jwt.d.ts
index d9bc2b4..7d1605b 100644
--- a/node_modules/@fastify/jwt/types/jwt.d.ts
+++ b/node_modules/@fastify/jwt/types/jwt.d.ts
@@ -13,9 +13,11 @@ import {
 } from 'fastify'
 
 declare module 'fastify' {
-  interface FastifyInstance {
-    jwt: fastifyJwt.JWT
-  }
+  // PATCH: If namespaces are used, the `jwt` object will contain the namespaces as a property.
+  // This augmentation prevents later augmentations from overwriting the namespace property.
+  // interface FastifyInstance {
+  //   jwt: fastifyJwt.JWT
+  // }
 
   interface FastifyReply {
     jwtSign(payload: fastifyJwt.SignPayloadType, options?: fastifyJwt.FastifyJwtSignOptions): Promise<string>

This issue body was partially generated by patch-package.

@mcollina
Copy link
Member

This is definitely something we would need to fix for Fastify v5 (fastify/fastify#5061)

@piotr-cz
Copy link

piotr-cz commented Jan 15, 2024

Here is my workaround when using the 'player' namespace.

I found that FastifyJwtNamespace doesn't augment FastifyRequest and FastifyReply interfaces, but incorrectly FastifyInstance.

import type { FastifyPluginCallback } from 'fastify'
import type { FastifyJWTOptions, FastifyJwtNamespace } from '@fastify/jwt'
import jwt from '@fastify/jwt'

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

  interface FastifyRequest {
    // Custom namespace
    playerJwtVerify: FastifyRequest['jwtVerify'],
    playerJwtDecode: FastifyRequest['jwtDecode'],
  }

  interface FastifyReply {
    // Custon namespace
    playerJwtSign: FastifyReply['jwtSign'],
  }
}

declare module '@fastify/jwt' {
  // Custom Namespace (undocumented)
  interface JWT { player: jwt.JWT }
}

const jwtPlayerPlugin: FastifyPluginCallback = async (fastify) => {
  fastify.register(jwt, {
    namespace: 'player',
  })
})

@hobi9
Copy link

hobi9 commented Feb 8, 2024

I'm having the same issue, it looks like the types are wrong

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

No branches or pull requests

4 participants