Skip to content

Commit

Permalink
nodenext compatibility (#270)
Browse files Browse the repository at this point in the history
* add exports

* make typings nodenext compatible
  • Loading branch information
Uzlopak authored Nov 29, 2022
1 parent 052a431 commit ac7785c
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 130 deletions.
270 changes: 140 additions & 130 deletions jwt.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,160 +7,170 @@ import {
VerifierCallback,
VerifierOptions
} from 'fast-jwt'
import * as fastify from 'fastify'

/**
* 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: ...
}
import {
FastifyPluginCallback,
FastifyRequest
} from 'fastify'

export type SignPayloadType = FastifyJWT extends { payload: infer T }
? T extends string | object | Buffer
? T
: string | object | Buffer
: string | object | Buffer
declare module 'fastify' {
interface FastifyInstance {
jwt: fastifyJwt.JWT
}

export type UserType = FastifyJWT extends { user: infer T }
? T
: SignPayloadType
interface FastifyReply {
jwtSign(payload: fastifyJwt.SignPayloadType, options?: fastifyJwt.FastifyJwtSignOptions): Promise<string>
jwtSign(payload: fastifyJwt.SignPayloadType, callback: SignerCallback): void
jwtSign(payload: fastifyJwt.SignPayloadType, options: fastifyJwt.FastifyJwtSignOptions, callback: SignerCallback): void
jwtSign(payload: fastifyJwt.SignPayloadType, options?: Partial<fastifyJwt.SignOptions>): Promise<string>
jwtSign(payload: fastifyJwt.SignPayloadType, options: Partial<fastifyJwt.SignOptions>, callback: SignerCallback): void
}

export type TokenOrHeader = JwtHeader | { header: JwtHeader; payload: any }
interface FastifyRequest {
jwtVerify<Decoded extends fastifyJwt.VerifyPayloadType>(options?: fastifyJwt.FastifyJwtVerifyOptions): Promise<Decoded>
jwtVerify<Decoded extends fastifyJwt.VerifyPayloadType>(callback: VerifierCallback): void
jwtVerify<Decoded extends fastifyJwt.VerifyPayloadType>(options: fastifyJwt.FastifyJwtVerifyOptions, callback: VerifierCallback): void
jwtVerify<Decoded extends fastifyJwt.VerifyPayloadType>(options?: Partial<fastifyJwt.VerifyOptions>): Promise<Decoded>
jwtVerify<Decoded extends fastifyJwt.VerifyPayloadType>(options: Partial<fastifyJwt.VerifyOptions>, callback: VerifierCallback): void
jwtDecode<Decoded extends fastifyJwt.DecodePayloadType>(options?: fastifyJwt.FastifyJwtDecodeOptions): Promise<Decoded>
jwtDecode<Decoded extends fastifyJwt.DecodePayloadType>(callback: fastifyJwt.DecodeCallback<Decoded>): void
jwtDecode<Decoded extends fastifyJwt.DecodePayloadType>(options: fastifyJwt.FastifyJwtDecodeOptions, callback: fastifyJwt.DecodeCallback<Decoded>): void
user: fastifyJwt.UserType
}
}

export type Secret = string | Buffer | KeyFetcher | { key: Secret; passphrase: string }
| ((request: fastify.FastifyRequest, tokenOrHeader: TokenOrHeader, cb: (e: Error | null, secret: string | Buffer | undefined) => void) => void)
| ((request: fastify.FastifyRequest, tokenOrHeader: TokenOrHeader) => Promise<string | Buffer>)
type FastifyJwt = FastifyPluginCallback<fastifyJwt.FastifyJWTOptions>

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 VerifyPayloadType = object | string
export type DecodePayloadType = object | string
export type SignPayloadType = FastifyJWT extends { payload: infer T }
? T extends string | object | Buffer
? T
: string | object | Buffer
: string | object | Buffer

export interface DecodeCallback<Decoded extends DecodePayloadType> {
(err: Error, decoded: Decoded): void
}
export type UserType = FastifyJWT extends { user: infer T }
? T
: SignPayloadType

export interface SignOptions extends Omit<SignerOptions, "expiresIn" | "notBefore"> {
expiresIn: number | string;
notBefore: number | string;
}
export type TokenOrHeader = JwtHeader | { header: JwtHeader; payload: any }

export interface VerifyOptions extends Omit<VerifierOptions, "maxAge"> {
maxAge: number | string;
onlyCookie: boolean;
}
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<string | Buffer>)

export interface FastifyJWTOptions {
secret: Secret | { public: Secret; private: Secret }
decode?: Partial<DecoderOptions>
sign?: Partial<SignOptions>
verify?: Partial<VerifyOptions> & { extractToken?: (request: fastify.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: fastify.FastifyRequest, decodedToken: { [k: string]: any }) => boolean | Promise<boolean> | SignPayloadType | Promise<SignPayloadType>
formatUser?: (payload: SignPayloadType) => UserType,
jwtDecode?: boolean | string
namespace?: string
jwtVerify?: string
jwtSign?: string
decoratorName?: string
}
export type VerifyPayloadType = object | string
export type DecodePayloadType = object | string

export interface JWT {
options: {
decode: Partial<DecoderOptions>
sign: Partial<SignOptions>
verify: Partial<VerifyOptions>
}
cookie?: {
cookieName: string,
signed: boolean
export interface DecodeCallback<Decoded extends DecodePayloadType> {
(err: Error, decoded: Decoded): void
}

sign(payload: SignPayloadType, options?: Partial<SignOptions>): string
sign(payload: SignPayloadType, callback: SignerCallback): void
sign(payload: SignPayloadType, options: Partial<SignOptions>, callback: SignerCallback): void

verify<Decoded extends VerifyPayloadType>(token: string, options?: Partial<VerifyOptions>): Decoded
verify<Decoded extends VerifyPayloadType>(token: string, callback: VerifierCallback): void
verify<Decoded extends VerifyPayloadType>(token: string, options: Partial<VerifyOptions>, callback: VerifierCallback): void
export interface SignOptions extends Omit<SignerOptions, "expiresIn" | "notBefore"> {
expiresIn: number | string;
notBefore: number | string;
}

decode<Decoded extends DecodePayloadType>(token: string, options?: Partial<DecoderOptions>): null | Decoded
export interface VerifyOptions extends Omit<VerifierOptions, "maxAge"> {
maxAge: number | string;
onlyCookie: boolean;
}

lookupToken(request: fastify.FastifyRequest, options?: FastifyJWTOptions['verify']): string
}
export interface FastifyJWTOptions {
secret: Secret | { public: Secret; private: Secret }
decode?: Partial<DecoderOptions>
sign?: Partial<SignOptions>
verify?: Partial<VerifyOptions> & { 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<boolean> | SignPayloadType | Promise<SignPayloadType>
formatUser?: (payload: SignPayloadType) => UserType,
jwtDecode?: boolean | string
namespace?: string
jwtVerify?: string
jwtSign?: string
decoratorName?: string
}

export type { JwtHeader } from 'fast-jwt'
export interface JWT {
options: {
decode: Partial<DecoderOptions>
sign: Partial<SignOptions>
verify: Partial<VerifyOptions>
}
cookie?: {
cookieName: string,
signed: boolean
}

export const fastifyJwt: fastify.FastifyPluginCallback<FastifyJWTOptions>
sign(payload: SignPayloadType, options?: Partial<SignOptions>): string
sign(payload: SignPayloadType, callback: SignerCallback): void
sign(payload: SignPayloadType, options: Partial<SignOptions>, callback: SignerCallback): void

export default fastifyJwt
verify<Decoded extends VerifyPayloadType>(token: string, options?: Partial<VerifyOptions>): Decoded
verify<Decoded extends VerifyPayloadType>(token: string, callback: VerifierCallback): void
verify<Decoded extends VerifyPayloadType>(token: string, options: Partial<VerifyOptions>, callback: VerifierCallback): void

export interface FastifyJwtSignOptions {
sign?: Partial<SignOptions>
}
decode<Decoded extends DecodePayloadType>(token: string, options?: Partial<DecoderOptions>): null | Decoded

export interface FastifyJwtVerifyOptions {
decode: Partial<DecoderOptions>
verify: Partial<VerifyOptions>
}
lookupToken(request: FastifyRequest, options?: FastifyJWTOptions['verify']): string
}

export interface FastifyJwtDecodeOptions {
decode: Partial<DecoderOptions>
verify: Partial<VerifyOptions>
}
export type { JwtHeader }

declare module 'fastify' {
interface FastifyInstance {
jwt: JWT
export interface FastifyJwtSignOptions {
sign?: Partial<SignOptions>
}

interface FastifyReply {
jwtSign(payload: SignPayloadType, options?: FastifyJwtSignOptions): Promise<string>
jwtSign(payload: SignPayloadType, callback: SignerCallback): void
jwtSign(payload: SignPayloadType, options: FastifyJwtSignOptions, callback: SignerCallback): void
jwtSign(payload: SignPayloadType, options?: Partial<SignOptions>): Promise<string>
jwtSign(payload: SignPayloadType, options: Partial<SignOptions>, callback: SignerCallback): void
export interface FastifyJwtVerifyOptions {
decode: Partial<DecoderOptions>
verify: Partial<VerifyOptions>
}

interface FastifyRequest {
jwtVerify<Decoded extends VerifyPayloadType>(options?: FastifyJwtVerifyOptions): Promise<Decoded>
jwtVerify<Decoded extends VerifyPayloadType>(callback: VerifierCallback): void
jwtVerify<Decoded extends VerifyPayloadType>(options: FastifyJwtVerifyOptions, callback: VerifierCallback): void
jwtVerify<Decoded extends VerifyPayloadType>(options?: Partial<VerifyOptions>): Promise<Decoded>
jwtVerify<Decoded extends VerifyPayloadType>(options: Partial<VerifyOptions>, callback: VerifierCallback): void
jwtDecode<Decoded extends DecodePayloadType>(options?: FastifyJwtDecodeOptions): Promise<Decoded>
jwtDecode<Decoded extends DecodePayloadType>(callback: DecodeCallback<Decoded>): void
jwtDecode<Decoded extends DecodePayloadType>(options: FastifyJwtDecodeOptions, callback: DecodeCallback<Decoded>): void
user: UserType
export interface FastifyJwtDecodeOptions {
decode: Partial<DecoderOptions>
verify: Partial<VerifyOptions>
}

export const fastifyJwt: FastifyJwt
export { fastifyJwt as default }
}

declare function fastifyJwt(...params: Parameters<FastifyJwt>): ReturnType<FastifyJwt>
export = fastifyJwt
2 changes: 2 additions & 0 deletions jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,5 @@ module.exports = fp(fastifyJwt, {
fastify: '4.x',
name: '@fastify/jwt'
})
module.exports.default = fastifyJwt
module.exports.fastifyJwt = fastifyJwt

0 comments on commit ac7785c

Please sign in to comment.