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
21 changes: 21 additions & 0 deletions common.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export class InvalidTokenError extends Error {}

export interface JwtDecodeOptions {
header?: boolean;
}

export interface JwtHeader {
typ?: string;
alg?: string;
kid?: 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.js";
export * from "./common.js";

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

export default jwtDecode;
28 changes: 7 additions & 21 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,26 +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;
kid?: 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: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@
"version": "3.1.2",
"description": "Decode JWT tokens, mostly useful for browser applications.",
"main": "build/jwt-decode.cjs.js",
"module": "build/jwt-decode.esm.js",
"module": "build/jwt-decode.mjs",
"types": "index.d.ts",
"exports": {
".": {
"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
6 changes: 3 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export default [{
input: "lib/index.js",
output: [{
name: EXPORT_NAME,
file: "build/jwt-decode.esm.js",
file: "build/jwt-decode.mjs",
format: "esm",
}, ],
}],
plugins: [!isProduction &&
serve({
contentBase: ["build", "static"],
Expand All @@ -55,4 +55,4 @@ export default [{
clearScreen: false,
},
},
];
];
2 changes: 1 addition & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ <h2>decoded:</h2>
<pre><code class="js-error3"></code></pre>

<script type="module">
import jwtDecode from "/jwt-decode.esm.js";
import jwtDecode from "/jwt-decode.mjs";
var token =
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJleHAiOjEzOTMyODY4OTMsImlhdCI6MTM5MzI2ODg5M30.4-iaDojEVl0pJQMjrbM1EzUIfAZgsbK_kgnVyVxFSVo";
var decoded = jwtDecode(token);
Expand Down