Skip to content
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

Add generic types #258

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
30 changes: 16 additions & 14 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type Algorithm =

declare class TokenError extends Error {
static wrap(originalError: Error, code: string, message: string): TokenError

static codes: {
invalidType: 'FAST_JWT_INVALID_TYPE'
invalidOption: 'FAST_JWT_INVALID_OPTION'
Expand All @@ -36,7 +37,8 @@ declare class TokenError extends Error {
verifyError: 'FAST_JWT_VERIFY_ERROR'
}

code: string;
code: string

[key: string]: any
}

Expand All @@ -47,13 +49,13 @@ type KeyFetcher =
| ((header: { [key: string]: any }) => Promise<string | Buffer>)
| ((header: { [key: string]: any }, cb: (err: Error | TokenError | null, key: string | Buffer) => void) => void)

declare function SignerSync(payload: string | Buffer | { [key: string]: any }): string
declare function SignerAsync(payload: string | Buffer | { [key: string]: any }): Promise<string>
declare function SignerAsync(payload: string | Buffer | { [key: string]: any }, cb: SignerCallback): void
type Payload = string | Buffer | { [key: string]: any }

type SignerSync<TPayload extends Payload = Payload> = <T = TPayload>(payload: T) => string
type SignerAsync<TPayload extends Payload = Payload> = <T = TPayload, U extends SignerCallback | undefined = undefined>(payload: T, cb?: U) => U extends undefined ? Promise<string> : void

declare function VerifierSync(token: string | Buffer): any
declare function VerifierAsync(token: string | Buffer): Promise<any>
declare function VerifierAsync(token: string | Buffer, cb: object): void
type VerifierSync<TPayload = any> = <T = TPayload>(token: string | Buffer) => T
type VerifierAsync<TPayload = any> = <T = TPayload, U extends VerifierCallback | undefined = undefined>(token: string | Buffer, cb?: U) => U extends undefined ? Promise<T> : void

export interface JwtHeader {
alg: string | Algorithm
Expand Down Expand Up @@ -111,10 +113,10 @@ export interface PrivateKey {
passphrase: string | undefined
}

export function createSigner(
options?: Partial<SignerOptions & { key: string | Buffer | PrivateKey }>
): typeof SignerSync
export function createSigner(options?: Partial<SignerOptions & { key: KeyFetcher }>): typeof SignerAsync
export function createDecoder(options?: Partial<DecoderOptions>): (token: string | Buffer) => any
export function createVerifier(options?: Partial<VerifierOptions & { key: string | Buffer }>): typeof VerifierSync
export function createVerifier(options?: Partial<VerifierOptions & { key: KeyFetcher }>): typeof VerifierAsync
export function createSigner<T extends Payload = Payload>(options?: Partial<SignerOptions & { key: string | Buffer | PrivateKey }>): SignerSync<T>
export function createSigner<T extends Payload = Payload>(options?: Partial<SignerOptions & { key: KeyFetcher }>): SignerAsync<T>

export function createDecoder<T = any>(options?: Partial<DecoderOptions>): <TData = T>(token: string | Buffer) => TData

export function createVerifier<T>(options?: Partial<VerifierOptions & { key: string | Buffer }>): VerifierSync<T>
export function createVerifier<T>(options?: Partial<VerifierOptions & { key: KeyFetcher }>): VerifierAsync<T>
35 changes: 30 additions & 5 deletions test/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ import { expectAssignable, expectNotAssignable } from 'tsd'
const signerSync = createSigner({ key: Buffer.from('KEY'), algorithm: 'RS256' })
signerSync({ key: '1' })

const signerAsync = createSigner({ key: () => Buffer.from('KEY'), algorithm: 'RS256' })
signerAsync({ key: '1' }).then(console.log, console.log)
signerAsync({ key: '1' }, (_e: Error | null, _token?: string) => {})
// Inline type
signerSync<{ key: '1' }>({ key: '1' })

// With specific payload type
const signerAsyncTyped = createSigner<{ key: '1' }>({ key: () => Buffer.from('KEY'), algorithm: 'RS256' })
signerAsyncTyped({ key: '1' }).then(console.log, console.log)
signerAsyncTyped({ key: '1' }, (_e: Error | null, _token?: string) => {})

// Without specific payload type
const signerAsyncAny = createSigner({ key: () => Buffer.from('KEY'), algorithm: 'RS256' })
signerAsyncAny({ key: '1', yek: '2' }).then(console.log, console.log)
signerAsyncAny({ key: '1', yek: '2', eyk: '3' }).then(console.log, console.log)

// Dynamic key in callback style
createSigner({
Expand All @@ -31,7 +40,13 @@ createSigner({
// Decoding
const decoder = createDecoder({ checkTyp: 'true' })
decoder('FOO')
decoder(Buffer.from('FOO'))

// Specify type inline
const { key: _key } = decoder<Record<'key', any>>(Buffer.from('FOO'))

// Specify type in createDecoder
const decoderTyped = createDecoder<{ sub: string }>({ checkTyp: 'true' })
const { sub: _sub } = decoderTyped('FOO')

// Verifying
// String key, both async/callback styles
Expand All @@ -40,7 +55,17 @@ verifierSync('2134')

const verifierAsync = createVerifier({ key: () => 'KEY', algorithms: ['RS256'] })
verifierAsync('123').then(console.log, console.log)
verifierAsync(Buffer.from('456'), (_e: Error | null, _token?: string) => {})
verifierAsync(Buffer.from('456'), (_e: Error | null, _payload: any) => {})

// Specify type inline
verifierAsync<{ key: 'a' }>('123').then(({ key }) => console.log(key))

// Specify type in createVerifier
type TestType = { foo: 'bar' }
const verifierAsyncTyped = createVerifier<TestType>({ key: () => 'KEY' })
verifierAsyncTyped('123').then(({ foo }) => console.log(foo))
verifierAsyncTyped('123', (_err: Error | TokenError | null, { foo }: TestType) => console.log(foo))


// Dynamic key in callback style
createVerifier({
Expand Down