-
-
Notifications
You must be signed in to change notification settings - Fork 100
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
Type augmentation for namespace methods? #348
Comments
You are requesting to expose the below types. Lines 20 to 38 in db719f0
|
@climba03003 - yes I think you're right. I've also updated the above to include our augmented Would the exported function types look like this? (courtesy ChatGPT). If so I'd be glad to submit a PR EDIT: Okay we're actually working on this, and the types are more likely to be something like: export interface JwtSignFunction {
(
payload: SignPayloadType,
options?: FastifyJwtSignOptions | Partial<SignOptions>
): Promise<string>
(payload: SignPayloadType, callback: SignerCallback): void
(
payload: SignPayloadType,
options: FastifyJwtSignOptions | Partial<SignOptions>,
callback: SignerCallback
): void
}
export interface JwtVerifyFunction {
<Decoded extends VerifyPayloadType>(options?: FastifyJwtVerifyOptions): Promise<Decoded>
<Decoded extends VerifyPayloadType>(callback: VerifierCallback): void
<Decoded extends VerifyPayloadType>(
options: FastifyJwtVerifyOptions,
callback: VerifierCallback
): void
<Decoded extends VerifyPayloadType>(options?: Partial<VerifyOptions>): Promise<Decoded>
<Decoded extends VerifyPayloadType>(
options: Partial<VerifyOptions>,
callback: VerifierCallback
): void
}
export interface JwtDecodeFunction {
<Decoded extends DecodePayloadType>(options?: FastifyJwtDecodeOptions): Promise<Decoded>
<Decoded extends DecodePayloadType>(callback: DecodeCallback<Decoded>): void
<Decoded extends DecodePayloadType>(
options: FastifyJwtDecodeOptions,
callback: DecodeCallback<Decoded>
): void
} And then implemented in our augmented types as : declare module 'fastify' {
interface FastifyInstance {
jwt: {
accessToken: JWT
refreshToken: JWT
}
}
interface FastifyReply {
accessTokenSign: JwtSignFunction
refreshTokenSign: JwtSignFunction
}
interface FastifyRequest {
accessTokenVerify: JwtVerifyFunction
accessTokenDecode: JwtDecodeFunction
refreshTokenVerify: JwtVerifyFunction
refreshTokenDecode: JwtDecodeFunction
}
} Again - if this is close, and I can help by submitting a PR then I'd be glad to. |
Prerequisites
Issue
EDIT: Simplifying this question a little.
We've created the following namespaced jwt plugin based on the docs here... https://github.com/fastify/fastify-jwt?tab=readme-ov-file#namespace
With the above, when calling either of the namespaced sign methods, our augmented type definition above, and the signature on the actual method don't exactly match - which means we suspect, that
accessTokenSign: JWT['sign']
is not the correct way to assign a type to theaccessTokenSign
and other methods. - since the reply and request augmented methods are asynchronous - i.e. require either a callback, or return a promise.accessTokenVerify: JWT['verify']
is also not the correct type annotation, as therequest.accessTokenVerify
method does not need a token as a parameter (it looks up the token from the request header).And so our question really is - what's the correct way to augment the
FastifyReply
andFastifyRequest
types with correct sign, verify and decode methods when using namespaces?The text was updated successfully, but these errors were encountered: