Skip to content

Commit 15f0f8d

Browse files
authored
Feat/generic signer verifier (nearform#529)
* chore: fake timer * feat: generic signer verifier
1 parent 4691667 commit 15f0f8d

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

src/index.d.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ type KeyFetcher =
7777

7878
type SignerPayload = Bufferable | Record<string, any>
7979

80-
declare function SignerSync(payload: SignerPayload): string
81-
declare function SignerAsync(payload: SignerPayload): Promise<string>
82-
declare function SignerAsync(payload: SignerPayload, cb: SignerCallback): void
80+
declare function SignerSync<T = SignerPayload>(payload: T): string
81+
declare function SignerAsync<T = SignerPayload>(payload: T): Promise<string>
82+
declare function SignerAsync<T = SignerPayload>(payload: T, cb: SignerCallback): void
8383

84-
declare function VerifierSync(token: Bufferable): any
85-
declare function VerifierAsync(token: Bufferable): Promise<any>
86-
declare function VerifierAsync(token: Bufferable, cb: VerifierCallback): void
84+
declare function VerifierSync<T = Bufferable>(token: T): any
85+
declare function VerifierAsync<T = Bufferable>(token: T): Promise<any>
86+
declare function VerifierAsync<T = Bufferable>(token: T, cb: VerifierCallback): void
8787

8888
export interface JwtHeader extends Record<string, any> {
8989
alg: string | Algorithm
@@ -147,10 +147,10 @@ export interface PrivateKey {
147147
passphrase: string | undefined
148148
}
149149

150-
export function createSigner(
150+
export function createSigner<T = SignerPayload>(
151151
options?: Partial<SignerOptions & { key: Bufferable | PrivateKey }>
152-
): typeof SignerSync
153-
export function createSigner(options?: Partial<SignerOptions & { key: KeyFetcher }>): typeof SignerAsync
152+
): typeof SignerSync<T>
153+
export function createSigner<T = SignerPayload>(options?: Partial<SignerOptions & { key: KeyFetcher }>): typeof SignerAsync<T>
154154
export function createDecoder(options?: Partial<DecoderOptions>): (token: Bufferable) => any
155-
export function createVerifier(options?: Partial<VerifierOptions & { key: Bufferable }>): typeof VerifierSync
156-
export function createVerifier(options?: Partial<VerifierOptions & { key: KeyFetcher }>): typeof VerifierAsync
155+
export function createVerifier<T = Bufferable>(options?: Partial<VerifierOptions & { key: Bufferable }>): typeof VerifierSync<T>
156+
export function createVerifier<T = Bufferable>(options?: Partial<VerifierOptions & { key: KeyFetcher }>): typeof VerifierAsync<T>

test/types.spec.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-unused-expressions */
22
/* eslint-disable @typescript-eslint/no-unused-vars */
33

4+
import { expectAssignable, expectNotAssignable, expectType } from 'tsd'
45
import {
56
createDecoder,
67
createSigner,
@@ -10,7 +11,6 @@ import {
1011
TokenError,
1112
TokenValidationErrorCode
1213
} from '..'
13-
import { expectAssignable, expectNotAssignable, expectType } from 'tsd'
1414

1515
// Signing
1616
// Buffer key, both async/callback styles
@@ -44,6 +44,14 @@ createSigner({
4444
algorithm: 'RS256'
4545
})
4646

47+
// Generic signer
48+
const signer = createSigner<Record<string, number>>({
49+
expiresIn: '10min',
50+
key: Buffer.from('KEY'),
51+
algorithm: 'RS256'
52+
})
53+
signer({ key: 1 })
54+
4755
// Decoding
4856
const decoder = createDecoder({ checkTyp: 'true' })
4957
decoder('FOO')
@@ -77,6 +85,14 @@ createVerifier({
7785
}
7886
})('456').then(console.log, console.log)
7987

88+
// Generic verifier
89+
createVerifier<Record<string, number>>({
90+
key: 'KEY',
91+
algorithms: ['RS256'],
92+
requiredClaims: ['aud'],
93+
checkTyp: 'JWT'
94+
})({ key: 1 }).then(console.log, console.log)
95+
8096
// Errors
8197
const wrapped = TokenError.wrap(new Error('ORIGINAL'), 'FAST_JWT_INVALID_TYPE', 'MESSAGE')
8298
wrapped.code === 'FAST_JWT_INVALID_TYPE'

0 commit comments

Comments
 (0)