Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
},
"devDependencies": {
"@node-rs/jsonwebtoken": "^0.5.9",
"@sinonjs/fake-timers": "^13.0.5",
"@sinonjs/fake-timers": "^14.0.0",
"@types/node": "^22.10.2",
"cronometro": "^4.0.0",
"eslint": "^9.17.0",
Expand Down
22 changes: 11 additions & 11 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ type KeyFetcher =

type SignerPayload = Bufferable | Record<string, any>

declare function SignerSync(payload: SignerPayload): string
declare function SignerAsync(payload: SignerPayload): Promise<string>
declare function SignerAsync(payload: SignerPayload, cb: SignerCallback): void
declare function SignerSync<T = SignerPayload>(payload: T): string
declare function SignerAsync<T = SignerPayload>(payload: T): Promise<string>
declare function SignerAsync<T = SignerPayload>(payload: T, cb: SignerCallback): void

declare function VerifierSync(token: Bufferable): any
declare function VerifierAsync(token: Bufferable): Promise<any>
declare function VerifierAsync(token: Bufferable, cb: VerifierCallback): void
declare function VerifierSync<T = Bufferable>(token: T): any
declare function VerifierAsync<T = Bufferable>(token: T): Promise<any>
declare function VerifierAsync<T = Bufferable>(token: T, cb: VerifierCallback): void

export interface JwtHeader extends Record<string, any> {
alg: string | Algorithm
Expand Down Expand Up @@ -147,10 +147,10 @@ export interface PrivateKey {
passphrase: string | undefined
}

export function createSigner(
export function createSigner<T = SignerPayload>(
options?: Partial<SignerOptions & { key: Bufferable | PrivateKey }>
): typeof SignerSync
export function createSigner(options?: Partial<SignerOptions & { key: KeyFetcher }>): typeof SignerAsync
): typeof SignerSync<T>
export function createSigner<T = SignerPayload>(options?: Partial<SignerOptions & { key: KeyFetcher }>): typeof SignerAsync<T>
export function createDecoder(options?: Partial<DecoderOptions>): (token: Bufferable) => any
export function createVerifier(options?: Partial<VerifierOptions & { key: Bufferable }>): typeof VerifierSync
export function createVerifier(options?: Partial<VerifierOptions & { key: KeyFetcher }>): typeof VerifierAsync
export function createVerifier<T = Bufferable>(options?: Partial<VerifierOptions & { key: Bufferable }>): typeof VerifierSync<T>
export function createVerifier<T = Bufferable>(options?: Partial<VerifierOptions & { key: KeyFetcher }>): typeof VerifierAsync<T>
18 changes: 17 additions & 1 deletion test/types.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-unused-expressions */
/* eslint-disable @typescript-eslint/no-unused-vars */

import { expectAssignable, expectNotAssignable, expectType } from 'tsd'
import {
createDecoder,
createSigner,
Expand All @@ -10,7 +11,6 @@ import {
TokenError,
TokenValidationErrorCode
} from '..'
import { expectAssignable, expectNotAssignable, expectType } from 'tsd'

// Signing
// Buffer key, both async/callback styles
Expand Down Expand Up @@ -44,6 +44,14 @@ createSigner({
algorithm: 'RS256'
})

// Generic signer
const signer = createSigner<Record<string, number>>({
expiresIn: '10min',
key: Buffer.from('KEY'),
algorithm: 'RS256'
})
signer({ key: 1 })

// Decoding
const decoder = createDecoder({ checkTyp: 'true' })
decoder('FOO')
Expand Down Expand Up @@ -77,6 +85,14 @@ createVerifier({
}
})('456').then(console.log, console.log)

// Generic verifier
createVerifier<Record<string, number>>({
key: 'KEY',
algorithms: ['RS256'],
requiredClaims: ['aud'],
checkTyp: 'JWT'
})({ key: 1 }).then(console.log, console.log)

// Errors
const wrapped = TokenError.wrap(new Error('ORIGINAL'), 'FAST_JWT_INVALID_TYPE', 'MESSAGE')
wrapped.code === 'FAST_JWT_INVALID_TYPE'
Expand Down
Loading