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 support for node hybrid cjs and esm modules #130

Closed
wants to merge 11 commits into from
4 changes: 2 additions & 2 deletions build/jwt-decode.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions common.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export class InvalidTokenError extends Error {}

export interface JwtDecodeOptions {
header?: boolean;
}

export interface JwtHeader {
typ?: string;
alg?: string;
}

export interface JwtPayload {
iss?: string;
sub?: string;
aud?: string[] | string;
exp?: number;
nbf?: number;
iat?: number;
jti?: string;
}
9 changes: 9 additions & 0 deletions index.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { JwtDecodeOptions } from "./common";
export * from "./common";

declare function jwtDecode<T = unknown>(
token: string,
options?: JwtDecodeOptions
): T

export default jwtDecode;
27 changes: 7 additions & 20 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
export class InvalidTokenError extends Error {}
import { InvalidTokenError, JwtDecodeOptions, JwtHeader, JwtPayload } from "./common";

export interface JwtDecodeOptions {
header?: boolean;
declare namespace jwtDecode {
export { InvalidTokenError, JwtDecodeOptions, JwtHeader, JwtPayload }
}

export interface JwtHeader {
typ?: string;
alg?: string;
}

export interface JwtPayload {
iss?: string;
sub?: string;
aud?: string[] | string;
exp?: number;
nbf?: number;
iat?: number;
jti?: string;
}

export default function jwtDecode<T = unknown>(
declare function jwtDecode<T = unknown>(
token: string,
options?: JwtDecodeOptions
): T;
): T

export = jwtDecode;
perrin4869 marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
"main": "build/jwt-decode.cjs.js",
"module": "build/jwt-decode.esm.js",
"types": "index.d.ts",
"exports": {
".": {
"types": {
"require": "./index.d.ts",
"import": "./index.d.mts",
"default": "./index.d.mts"
},
"require": "./build/jwt-decode.cjs.js",
"import": "./build/jwt-decode.mjs",
"default": "./build/jwt-decode.mjs"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default export should match the type field in package.json. The absence of type, means it’s commonjs.

Also the value that matches the default can be omitted.

Suggested change
"types": {
"require": "./index.d.ts",
"import": "./index.d.mts",
"default": "./index.d.mts"
},
"require": "./build/jwt-decode.cjs.js",
"import": "./build/jwt-decode.mjs",
"default": "./build/jwt-decode.mjs"
"types": {
"import": "./index.d.mts",
"default": "./index.d.ts"
},
"import": "./build/jwt-decode.mjs",
"default": "./build/jwt-decode.cjs.js"

},
"./package.json": "./package.json"
},
"keywords": [
"jwt",
"browser"
Expand Down
11 changes: 10 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ export default [{
}, ],
plugins,
},
{
input: "lib/index.js",
output: [{
name: EXPORT_NAME,
file: "build/jwt-decode.mjs",
format: "esm",
}],
plugins,
perrin4869 marked this conversation as resolved.
Show resolved Hide resolved
},
{
input: "lib/index.js",
output: [{
Expand All @@ -55,4 +64,4 @@ export default [{
clearScreen: false,
},
},
];
];