diff --git a/src/index.ts b/src/index.ts index cb3beec..14130d8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,14 +3,14 @@ if (typeof crypto === 'undefined' || !crypto.subtle) /** * @typedef JwtAlgorithm - * @type {'ES256'|'ES384'|'ES512'|'HS256'|'HS384'|'HS512'|'RS256'|'RS384'|'RS512'} + * @type {'ES256' | 'ES384' | 'ES512' | 'HS256' | 'HS384' | 'HS512' | 'RS256' | 'RS384' | 'RS512'} */ -export type JwtAlgorithm = 'ES256'|'ES384'|'ES512'|'HS256'|'HS384'|'HS512'|'RS256'|'RS384'|'RS512' +export type JwtAlgorithm = 'ES256' | 'ES384' | 'ES512' | 'HS256' | 'HS384' | 'HS512' | 'RS256' | 'RS384' | 'RS512' /** * @typedef JwtAlgorithms */ -export interface JwtAlgorithms { +export type JwtAlgorithms = { [key: string]: SubtleCryptoImportKeyAlgorithm } @@ -18,16 +18,14 @@ export interface JwtAlgorithms { * @typedef JwtHeader * @prop {string} [typ] Type */ -export interface JwtHeader { +export type JwtHeader = { /** * Type (default: `"JWT"`) * * @default "JWT" */ typ?: string - - [key: string]: any -} +} & T /** * @typedef JwtPayload @@ -39,7 +37,7 @@ export interface JwtHeader { * @prop {string} [iat] Issued At * @prop {string} [jti] JWT ID */ -export interface JwtPayload { +export type JwtPayload = { /** Issuer */ iss?: string @@ -62,13 +60,13 @@ export interface JwtPayload { jti?: string [key: string]: any -} +} & T /** * @typedef JwtOptions * @prop {JwtAlgorithm | string} algorithm */ -export interface JwtOptions { +export type JwtOptions = { algorithm?: JwtAlgorithm | string } @@ -77,32 +75,32 @@ export interface JwtOptions { * @extends JwtOptions * @prop {JwtHeader} [header] */ -export interface JwtSignOptions extends JwtOptions { +export type JwtSignOptions = { header?: JwtHeader -} +} & JwtOptions /** * @typedef JwtVerifyOptions * @extends JwtOptions * @prop {boolean} [throwError=false] If `true` throw error if checks fail. (default: `false`) */ -export interface JwtVerifyOptions extends JwtOptions { +export type JwtVerifyOptions = { /** * If `true` throw error if checks fail. (default: `false`) * * @default false */ throwError?: boolean -} +} & JwtOptions /** * @typedef JwtData * @prop {JwtHeader} header * @prop {JwtPayload} payload */ -export interface JwtData { - header?: JwtHeader - payload?: JwtPayload +export type JwtData = { + header?: JwtHeader
+ payload?: JwtPayload } const algorithms: JwtAlgorithms = { @@ -309,10 +307,10 @@ export async function verify(token: string, secret: string | JsonWebKey, options * @param {string} token The token string generated by `jwt.sign()`. * @returns {JwtData} Returns an `object` containing `header` and `payload`. */ -export function decode(token: string): JwtData { +export function decode(token: string): JwtData { return { - header: decodePayload(token.split('.')[0].replace(/-/g, '+').replace(/_/g, '/')), - payload: decodePayload(token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/')) + header: decodePayload>(token.split('.')[0].replace(/-/g, '+').replace(/_/g, '/')), + payload: decodePayload>(token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/')) } }