import { DecoderOptions, JwtHeader, KeyFetcher, SignerCallback, SignerOptions, VerifierCallback, VerifierOptions } from 'fast-jwt' import { FastifyPluginCallback, FastifyRequest } from 'fastify' declare module 'fastify' { interface FastifyInstance { jwt: fastifyJwt.JWT } interface FastifyReply { jwtSign(payload: fastifyJwt.SignPayloadType, options?: fastifyJwt.FastifyJwtSignOptions): Promise jwtSign(payload: fastifyJwt.SignPayloadType, callback: SignerCallback): void jwtSign(payload: fastifyJwt.SignPayloadType, options: fastifyJwt.FastifyJwtSignOptions, callback: SignerCallback): void jwtSign(payload: fastifyJwt.SignPayloadType, options?: Partial): Promise jwtSign(payload: fastifyJwt.SignPayloadType, options: Partial, callback: SignerCallback): void } interface FastifyRequest { jwtVerify(options?: fastifyJwt.FastifyJwtVerifyOptions): Promise jwtVerify(callback: VerifierCallback): void jwtVerify(options: fastifyJwt.FastifyJwtVerifyOptions, callback: VerifierCallback): void jwtVerify(options?: Partial): Promise jwtVerify(options: Partial, callback: VerifierCallback): void jwtDecode(options?: fastifyJwt.FastifyJwtDecodeOptions): Promise jwtDecode(callback: fastifyJwt.DecodeCallback): void jwtDecode(options: fastifyJwt.FastifyJwtDecodeOptions, callback: fastifyJwt.DecodeCallback): void user: fastifyJwt.UserType } } type FastifyJwt = FastifyPluginCallback declare namespace fastifyJwt { /** * for declaration merging * @example * ``` * declare module '@fastify/jwt' { * interface FastifyJWT { * payload: { name: string; email: string } * } * } * ``` * @example * ``` * // With `formatUser`. * declare module '@fastify/jwt' { * interface FastifyJWT { * payload: { Name: string; e_mail: string } * user: { name: string; email: string } * } * } * ``` */ export interface FastifyJWT { // payload: ... // user: ... } export type SignPayloadType = FastifyJWT extends { payload: infer T } ? T extends string | object | Buffer ? T : string | object | Buffer : string | object | Buffer export type UserType = FastifyJWT extends { user: infer T } ? T : SignPayloadType export type TokenOrHeader = JwtHeader | { header: JwtHeader; payload: any } export type Secret = string | Buffer | KeyFetcher | { key: Secret; passphrase: string } | ((request: FastifyRequest, tokenOrHeader: TokenOrHeader, cb: (e: Error | null, secret: string | Buffer | undefined) => void) => void) | ((request: FastifyRequest, tokenOrHeader: TokenOrHeader) => Promise) export type VerifyPayloadType = object | string export type DecodePayloadType = object | string export interface DecodeCallback { (err: Error, decoded: Decoded): void } export interface SignOptions extends Omit { expiresIn: number | string; notBefore: number | string; } export interface VerifyOptions extends Omit { maxAge: number | string; onlyCookie: boolean; } export interface FastifyJWTOptions { secret: Secret | { public: Secret; private: Secret } decode?: Partial sign?: Partial verify?: Partial & { extractToken?: (request: FastifyRequest) => string | void } cookie?: { cookieName: string, signed: boolean } messages?: { badRequestErrorMessage?: string badCookieRequestErrorMessage?: string noAuthorizationInHeaderMessage?: string noAuthorizationInCookieMessage?: string authorizationTokenExpiredMessage?: string authorizationTokenInvalid?: ((err: Error) => string) | string authorizationTokenUntrusted?: string } trusted?: (request: FastifyRequest, decodedToken: { [k: string]: any }) => boolean | Promise | SignPayloadType | Promise formatUser?: (payload: SignPayloadType) => UserType, jwtDecode?: boolean | string namespace?: string jwtVerify?: string jwtSign?: string decoratorName?: string } export interface JWT { options: { decode: Partial sign: Partial verify: Partial } cookie?: { cookieName: string, signed: boolean } sign(payload: SignPayloadType, options?: Partial): string sign(payload: SignPayloadType, callback: SignerCallback): void sign(payload: SignPayloadType, options: Partial, callback: SignerCallback): void verify(token: string, options?: Partial): Decoded verify(token: string, callback: VerifierCallback): void verify(token: string, options: Partial, callback: VerifierCallback): void decode(token: string, options?: Partial): null | Decoded lookupToken(request: FastifyRequest, options?: FastifyJWTOptions['verify']): string } export type { JwtHeader } export interface FastifyJwtSignOptions { sign?: Partial } export interface FastifyJwtVerifyOptions { decode: Partial verify: Partial } export interface FastifyJwtDecodeOptions { decode: Partial verify: Partial } export const fastifyJwt: FastifyJwt export { fastifyJwt as default } } declare function fastifyJwt(...params: Parameters): ReturnType export = fastifyJwt