From 182465832fcdd8da8e872151f6c7ed7635ef4739 Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Sat, 14 Dec 2024 23:18:47 +0100 Subject: [PATCH] Chore/types definition (#527) * chore: upgrade deps * chore: types definition --- src/index.d.ts | 59 ++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index 323b16a..573ad58 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -69,29 +69,33 @@ type DecodedJwt = { input: string } +type Bufferable = string | Buffer + type KeyFetcher = - | ((DecodedJwt: DecodedJwt) => Promise) - | ((DecodedJwt: DecodedJwt, cb: (err: Error | TokenError | null, key: string | Buffer) => void) => void) + | ((DecodedJwt: DecodedJwt) => Promise) + | ((DecodedJwt: DecodedJwt, cb: (err: Error | TokenError | null, key: Bufferable) => void) => void) + +type SignerPayload = Bufferable | Record -declare function SignerSync(payload: string | Buffer | { [key: string]: any }): string -declare function SignerAsync(payload: string | Buffer | { [key: string]: any }): Promise -declare function SignerAsync(payload: string | Buffer | { [key: string]: any }, cb: SignerCallback): void +declare function SignerSync(payload: SignerPayload): string +declare function SignerAsync(payload: SignerPayload): Promise +declare function SignerAsync(payload: SignerPayload, cb: SignerCallback): void -declare function VerifierSync(token: string | Buffer): any -declare function VerifierAsync(token: string | Buffer): Promise -declare function VerifierAsync(token: string | Buffer, cb: object): void +declare function VerifierSync(token: Bufferable): any +declare function VerifierAsync(token: Bufferable): Promise +declare function VerifierAsync(token: Bufferable, cb: VerifierCallback): void export interface JwtHeader extends Record { alg: string | Algorithm - typ?: string | undefined - cty?: string | undefined - crit?: Array> | undefined - kid?: string | undefined - jku?: string | undefined - x5u?: string | string[] | undefined - 'x5t#S256'?: string | undefined - x5t?: string | undefined - x5c?: string | string[] | undefined + typ?: string + cty?: string + crit?: Array> + kid?: string + jku?: string + x5u?: string | string[] + 'x5t#S256'?: string + x5t?: string + x5c?: string | string[] } export interface SignerOptions { @@ -115,17 +119,20 @@ export interface DecoderOptions { checkTyp?: string } +type VerifierAllowedBase = string | RegExp +type VerifierAllowed = VerifierAllowedBase | Array + export interface VerifierOptions { algorithms?: Algorithm[] complete?: boolean cache?: boolean | number cacheTTL?: number errorCacheTTL?: number | ((tokenError: TokenError) => number) - allowedJti?: string | RegExp | Array - allowedAud?: string | RegExp | Array - allowedIss?: string | RegExp | Array - allowedSub?: string | RegExp | Array - allowedNonce?: string | RegExp | Array + allowedJti?: VerifierAllowed + allowedAud?: VerifierAllowed + allowedIss?: VerifierAllowed + allowedSub?: VerifierAllowed + allowedNonce?: VerifierAllowed ignoreExpiration?: boolean ignoreNotBefore?: boolean maxAge?: number @@ -136,14 +143,14 @@ export interface VerifierOptions { } export interface PrivateKey { - key: string | Buffer + key: Bufferable passphrase: string | undefined } export function createSigner( - options?: Partial + options?: Partial ): typeof SignerSync export function createSigner(options?: Partial): typeof SignerAsync -export function createDecoder(options?: Partial): (token: string | Buffer) => any -export function createVerifier(options?: Partial): typeof VerifierSync +export function createDecoder(options?: Partial): (token: Bufferable) => any +export function createVerifier(options?: Partial): typeof VerifierSync export function createVerifier(options?: Partial): typeof VerifierAsync