Skip to content

Commit ffc3bf0

Browse files
committed
Refactor to ESM
1 parent 4372cc5 commit ffc3bf0

38 files changed

+855
-461
lines changed

.eslintrc

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
{
2+
"parser": "@typescript-eslint/parser",
23
"parserOptions": {
3-
"ecmaVersion": 2018
4+
"ecmaVersion": 2020,
5+
"sourceType": "module",
6+
"project": "./tsconfig.json"
47
},
8+
"plugins": ["@typescript-eslint"],
9+
"extends": [
10+
"eslint:recommended",
11+
"plugin:@typescript-eslint/recommended"
12+
],
513
"rules": {
614
"array-bracket-spacing": [2, "always"],
715
"comma-dangle": [2, "never"],
@@ -11,7 +19,15 @@
1119
}],
1220
"jsx-quotes": [2, "prefer-double"],
1321
"no-multiple-empty-lines": 2,
14-
"no-unused-vars": 2,
22+
// Disable base no-unused-vars, use TS version
23+
"no-unused-vars": "off",
24+
"@typescript-eslint/no-unused-vars": [
25+
"error",
26+
{ "argsIgnorePattern": "^_", "varsIgnorePattern": "^_", "caughtErrorsIgnorePattern": "^_" }
27+
],
28+
// Allow bare expressions (needed for chai.expect)
29+
"no-unused-expressions": "off",
30+
"@typescript-eslint/no-unused-expressions": "off",
1531
"no-var": 2,
1632
"no-extra-semi": 2,
1733
"object-curly-spacing": [2, "always"],
@@ -23,7 +39,6 @@
2339
"env": {
2440
"node": true,
2541
"mocha": true,
26-
"es6": true
27-
},
28-
"extends": "eslint:recommended"
42+
"es2020": true
43+
}
2944
}

index.d.ts

Lines changed: 53 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
12
import { Agent as HttpAgent } from 'http';
23
import { Agent as HttpsAgent } from 'https';
3-
import type {Jwt, Secret} from 'jsonwebtoken'
4-
import Express = require('express')
4+
import type { Jwt, Secret } from 'jsonwebtoken';
55

6-
declare function JwksRsa(options: JwksRsa.Options): JwksRsa.JwksClient;
7-
8-
declare namespace JwksRsa {
6+
/** Namespace for types */
7+
declare namespace JWKSRsaTypes {
98
class JwksClient {
109
constructor(options: Options);
11-
1210
getKeys(): Promise<unknown>;
1311
getSigningKeys(): Promise<SigningKey[]>;
1412
getSigningKey(kid?: string | null | undefined): Promise<SigningKey>;
1513
getSigningKey(kid: string | null | undefined, cb: (err: Error | null, key?: SigningKey) => void): void;
1614
}
17-
18-
interface Headers {
19-
[key: string]: string;
20-
}
21-
15+
interface Headers { [key: string]: string; }
2216
interface Options {
2317
jwksUri: string;
2418
rateLimit?: boolean;
@@ -30,101 +24,63 @@ declare namespace JwksRsa {
3024
requestHeaders?: Headers;
3125
timeout?: number;
3226
requestAgent?: HttpAgent | HttpsAgent;
33-
fetcher?(jwksUri: string): Promise<{ keys: any }>;
27+
fetcher?(jwksUri: string): Promise<{ keys: any }>; // eslint-disable-line @typescript-eslint/no-explicit-any
3428
getKeysInterceptor?(): Promise<JSONWebKey[]>;
3529
}
36-
37-
interface JSONWebKey {
38-
kid: string,
39-
alg: string,
40-
[key: string]: any
41-
}
42-
43-
interface CertSigningKey {
44-
kid: string;
45-
alg: string;
46-
getPublicKey(): string;
47-
publicKey: string;
48-
}
49-
50-
interface RsaSigningKey {
51-
kid: string;
52-
alg: string;
53-
getPublicKey(): string;
54-
rsaPublicKey: string;
55-
}
56-
30+
interface JSONWebKey { kid: string; alg: string; [key: string]: any } // eslint-disable-line @typescript-eslint/no-explicit-any
31+
interface CertSigningKey { kid?: string; alg?: string; getPublicKey(): string; publicKey: string; }
32+
interface RsaSigningKey { kid?: string; alg?: string; getPublicKey(): string; rsaPublicKey: string; }
5733
type SigningKey = CertSigningKey | RsaSigningKey;
58-
59-
/**
60-
* Types are duplicated from express-jwt@6/7
61-
* due to numerous breaking changes in the lib's types
62-
* whilst this lib supportd both <=6 & >=7 implementations
63-
*
64-
* express-jwt's installed version (or its @types)
65-
* will be the types used at transpilation time
66-
*/
67-
68-
/** Types from express-jwt@<=6 */
69-
type secretType = string|Buffer;
70-
type SecretCallbackLong = (req: unknown, header: any, payload: any, done: (err: any, secret?: secretType) => void) => void;
71-
type SecretCallback = (req: unknown, payload: any, done: (err: any, secret?: secretType) => void) => void;
72-
73-
/** Types from express-jwt@>=7 */
34+
// express-jwt <=6
35+
type secretType = string | Buffer;
36+
type SecretCallbackLong = (req: unknown, header: any, payload: any, done: (err: any, secret?: secretType) => void) => void; // eslint-disable-line @typescript-eslint/no-explicit-any
37+
type SecretCallback = (req: unknown, payload: any, done: (err: any, secret?: secretType) => void) => void; // eslint-disable-line @typescript-eslint/no-explicit-any
38+
// express-jwt >=7
7439
type GetVerificationKey = (req: unknown, token: Jwt | undefined) => Secret | undefined | Promise<Secret | undefined>;
75-
7640
function expressJwtSecret(options: ExpressJwtOptions): SecretCallbackLong & GetVerificationKey;
77-
7841
function passportJwtSecret(options: ExpressJwtOptions): SecretCallback;
79-
80-
interface ExpressJwtOptions extends Options {
81-
handleSigningKeyError?: (err: Error | null, cb: (err: Error | null) => void) => void;
82-
}
83-
42+
interface ExpressJwtOptions extends Options { handleSigningKeyError?: (err: Error | null, cb: (err: Error | null) => void) => void; }
8443
function hapiJwt2Key(options: HapiJwtOptions): (decodedToken: DecodedToken, cb: HapiCallback) => void;
85-
86-
interface HapiJwtOptions extends Options {
87-
handleSigningKeyError?: (err: Error | null, cb: HapiCallback) => void;
88-
}
89-
90-
type HapiCallback = (err: Error | null, publicKey: string, signingKey: SigningKey) => void;
91-
92-
interface DecodedToken {
93-
header: TokenHeader;
94-
}
95-
96-
interface TokenHeader {
97-
alg: string;
98-
kid: string;
99-
}
100-
44+
interface HapiJwtOptions extends Options { handleSigningKeyError?: (err: Error | null, cb: HapiCallback) => void; }
45+
type HapiCallback = (err: Error | null, publicKey: string | undefined, signingKey: SigningKey | undefined) => void;
46+
interface DecodedToken { header: TokenHeader; }
47+
interface TokenHeader { alg: string; kid: string; }
10148
function hapiJwt2KeyAsync(options: HapiJwtOptions): (decodedToken: DecodedToken) => Promise<{ key: string }>;
102-
10349
function koaJwtSecret(options: KoaJwtOptions): (header: TokenHeader) => Promise<string>;
50+
interface KoaJwtOptions extends Options { handleSigningKeyError?(err: Error | null): Promise<void>; }
51+
class ArgumentError extends Error { name: string; constructor(message: any); }
52+
class JwksError extends Error { name: string; constructor(message: any); }
53+
class JwksRateLimitError extends Error { name: string; constructor(message: any); }
54+
class SigningKeyNotFoundError extends Error { name: string; constructor(message: any); }
55+
}
10456

105-
interface KoaJwtOptions extends Options {
106-
handleSigningKeyError?(err: Error | null): Promise<void>;
107-
}
108-
109-
class ArgumentError extends Error {
110-
name: 'ArgumentError';
111-
constructor(message: string);
112-
}
113-
114-
class JwksError extends Error {
115-
name: 'JwksError';
116-
constructor(message: string);
117-
}
118-
119-
class JwksRateLimitError extends Error {
120-
name: 'JwksRateLimitError';
121-
constructor(message: string);
122-
}
123-
124-
class SigningKeyNotFoundError extends Error {
125-
name: 'SigningKeyNotFoundError';
126-
constructor(message: string);
127-
}
57+
/** Shape of callable default export */
58+
export interface JWKSRSAModule {
59+
(options: JWKSRsaTypes.Options): JWKSRsaTypes.JwksClient;
60+
JwksClient: typeof JWKSRsaTypes.JwksClient;
61+
ArgumentError: typeof JWKSRsaTypes.ArgumentError;
62+
JwksError: typeof JWKSRsaTypes.JwksError;
63+
JwksRateLimitError: typeof JWKSRsaTypes.JwksRateLimitError;
64+
SigningKeyNotFoundError: typeof JWKSRsaTypes.SigningKeyNotFoundError;
65+
expressJwtSecret: typeof JWKSRsaTypes.expressJwtSecret;
66+
passportJwtSecret: typeof JWKSRsaTypes.passportJwtSecret;
67+
hapiJwt2Key: typeof JWKSRsaTypes.hapiJwt2Key;
68+
hapiJwt2KeyAsync: typeof JWKSRsaTypes.hapiJwt2KeyAsync;
69+
koaJwtSecret: typeof JWKSRsaTypes.koaJwtSecret;
12870
}
12971

130-
export = JwksRsa;
72+
declare const jwksRsa: JWKSRSAModule;
73+
export default jwksRsa;
74+
export { JWKSRsaTypes as JwksRsa };
75+
export type { HttpAgent, HttpsAgent };
76+
// Named re-exports
77+
export const JwksClient: typeof JWKSRsaTypes.JwksClient;
78+
export const ArgumentError: typeof JWKSRsaTypes.ArgumentError;
79+
export const JwksError: typeof JWKSRsaTypes.JwksError;
80+
export const JwksRateLimitError: typeof JWKSRsaTypes.JwksRateLimitError;
81+
export const SigningKeyNotFoundError: typeof JWKSRsaTypes.SigningKeyNotFoundError;
82+
export const expressJwtSecret: typeof JWKSRsaTypes.expressJwtSecret;
83+
export const passportJwtSecret: typeof JWKSRsaTypes.passportJwtSecret;
84+
export const hapiJwt2Key: typeof JWKSRsaTypes.hapiJwt2Key;
85+
export const hapiJwt2KeyAsync: typeof JWKSRsaTypes.hapiJwt2KeyAsync;
86+
export const koaJwtSecret: typeof JWKSRsaTypes.koaJwtSecret;

0 commit comments

Comments
 (0)