Skip to content

Commit 1824658

Browse files
authored
Chore/types definition (#527)
* chore: upgrade deps * chore: types definition
1 parent c9fd3b6 commit 1824658

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

src/index.d.ts

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,29 +69,33 @@ type DecodedJwt = {
6969
input: string
7070
}
7171

72+
type Bufferable = string | Buffer
73+
7274
type KeyFetcher =
73-
| ((DecodedJwt: DecodedJwt) => Promise<string | Buffer>)
74-
| ((DecodedJwt: DecodedJwt, cb: (err: Error | TokenError | null, key: string | Buffer) => void) => void)
75+
| ((DecodedJwt: DecodedJwt) => Promise<Bufferable>)
76+
| ((DecodedJwt: DecodedJwt, cb: (err: Error | TokenError | null, key: Bufferable) => void) => void)
77+
78+
type SignerPayload = Bufferable | Record<string, any>
7579

76-
declare function SignerSync(payload: string | Buffer | { [key: string]: any }): string
77-
declare function SignerAsync(payload: string | Buffer | { [key: string]: any }): Promise<string>
78-
declare function SignerAsync(payload: string | Buffer | { [key: string]: any }, cb: SignerCallback): void
80+
declare function SignerSync(payload: SignerPayload): string
81+
declare function SignerAsync(payload: SignerPayload): Promise<string>
82+
declare function SignerAsync(payload: SignerPayload, cb: SignerCallback): void
7983

80-
declare function VerifierSync(token: string | Buffer): any
81-
declare function VerifierAsync(token: string | Buffer): Promise<any>
82-
declare function VerifierAsync(token: string | Buffer, cb: object): void
84+
declare function VerifierSync(token: Bufferable): any
85+
declare function VerifierAsync(token: Bufferable): Promise<any>
86+
declare function VerifierAsync(token: Bufferable, cb: VerifierCallback): void
8387

8488
export interface JwtHeader extends Record<string, any> {
8589
alg: string | Algorithm
86-
typ?: string | undefined
87-
cty?: string | undefined
88-
crit?: Array<string | Exclude<keyof JwtHeader, 'crit'>> | undefined
89-
kid?: string | undefined
90-
jku?: string | undefined
91-
x5u?: string | string[] | undefined
92-
'x5t#S256'?: string | undefined
93-
x5t?: string | undefined
94-
x5c?: string | string[] | undefined
90+
typ?: string
91+
cty?: string
92+
crit?: Array<string | Exclude<keyof JwtHeader, 'crit'>>
93+
kid?: string
94+
jku?: string
95+
x5u?: string | string[]
96+
'x5t#S256'?: string
97+
x5t?: string
98+
x5c?: string | string[]
9599
}
96100

97101
export interface SignerOptions {
@@ -115,17 +119,20 @@ export interface DecoderOptions {
115119
checkTyp?: string
116120
}
117121

122+
type VerifierAllowedBase = string | RegExp
123+
type VerifierAllowed = VerifierAllowedBase | Array<VerifierAllowedBase>
124+
118125
export interface VerifierOptions {
119126
algorithms?: Algorithm[]
120127
complete?: boolean
121128
cache?: boolean | number
122129
cacheTTL?: number
123130
errorCacheTTL?: number | ((tokenError: TokenError) => number)
124-
allowedJti?: string | RegExp | Array<string | RegExp>
125-
allowedAud?: string | RegExp | Array<string | RegExp>
126-
allowedIss?: string | RegExp | Array<string | RegExp>
127-
allowedSub?: string | RegExp | Array<string | RegExp>
128-
allowedNonce?: string | RegExp | Array<string | RegExp>
131+
allowedJti?: VerifierAllowed
132+
allowedAud?: VerifierAllowed
133+
allowedIss?: VerifierAllowed
134+
allowedSub?: VerifierAllowed
135+
allowedNonce?: VerifierAllowed
129136
ignoreExpiration?: boolean
130137
ignoreNotBefore?: boolean
131138
maxAge?: number
@@ -136,14 +143,14 @@ export interface VerifierOptions {
136143
}
137144

138145
export interface PrivateKey {
139-
key: string | Buffer
146+
key: Bufferable
140147
passphrase: string | undefined
141148
}
142149

143150
export function createSigner(
144-
options?: Partial<SignerOptions & { key: string | Buffer | PrivateKey }>
151+
options?: Partial<SignerOptions & { key: Bufferable | PrivateKey }>
145152
): typeof SignerSync
146153
export function createSigner(options?: Partial<SignerOptions & { key: KeyFetcher }>): typeof SignerAsync
147-
export function createDecoder(options?: Partial<DecoderOptions>): (token: string | Buffer) => any
148-
export function createVerifier(options?: Partial<VerifierOptions & { key: string | Buffer }>): typeof VerifierSync
154+
export function createDecoder(options?: Partial<DecoderOptions>): (token: Bufferable) => any
155+
export function createVerifier(options?: Partial<VerifierOptions & { key: Bufferable }>): typeof VerifierSync
149156
export function createVerifier(options?: Partial<VerifierOptions & { key: KeyFetcher }>): typeof VerifierAsync

0 commit comments

Comments
 (0)