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

Using a different decorator name with TypeScript #344

Open
2 tasks done
kerolloz opened this issue Aug 20, 2024 · 0 comments
Open
2 tasks done

Using a different decorator name with TypeScript #344

kerolloz opened this issue Aug 20, 2024 · 0 comments

Comments

@kerolloz
Copy link
Contributor

kerolloz commented Aug 20, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.28.1

Plugin version

8.0.1

Node.js version

using Bun

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

24.04

Description

How can I make TypeSciprt understand the new decorator for my custom decorator name?

For example if I have the following:

export default fp(async (fastify) => {
  fastify.register(fastifyJwt, {
    secret: env('JWT_SECRET'),
    decoratorName: "jwtUser"
  });

  fastify.decorate(
    'authenticate',
    async (request: FastifyRequest, reply: FastifyReply) => {
      try {
        await request.jwtVerify();
      } catch (err) {
        reply.send(err);
      }
    },
  );
});

How can I make TypeScript understand that I have a new property jwtUser?

I have tried the following but it doesn't seem to work:

// fastify-jwt.d.ts
import '@fastify/jwt';
import type { ObjectId } from 'mongodb';

type UserPayload = { id: ObjectId };
type DecodedUser = UserPayload;

declare module '@fastify/jwt' {
  interface FastifyJWT {
    // payload type is used for signing and verifying
    payload: UserPayload;
    // user type is return type of `request.user` object
    jwtUser: DecodedUser;
  }
  interface FastifyRequest {
    jwtUser: DecodedUser;
  }
}

declare module 'fastify' {
  interface FastifyInstance {
    authenticate: FastifyMiddleware;
  }
}

Link to code that reproduces the bug

No response

Expected Behavior

No response

Tasks

Preview Give feedback
No tasks being tracked yet.
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

1 participant