diff --git a/README.md b/README.md index 5f8e12c..6ea672a 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,10 @@ Run `npm install jwt-decode` or `yarn add jwt-decode` to install the library. ### Usage ```js -import jwt_decode from "jwt-decode"; +import { jwtDecode } from "jwt-decode"; -var token = "eyJ0eXAiO.../// jwt token"; -var decoded = jwt_decode(token); +const token = "eyJ0eXAiO.../// jwt token"; +const decoded = jwtDecode(token); console.log(decoded); @@ -40,7 +40,7 @@ console.log(decoded); */ // decode header by passing in options (useful for when you need `kid` to verify a JWT): -var decodedHeader = jwt_decode(token, { header: true }); +const decodedHeader = jwtDecode(token, { header: true }); console.log(decodedHeader); /* prints: @@ -69,35 +69,50 @@ Not adhering to the format will result in a `InvalidTokenError` with one of the - `Invalid token specified: invalid base64 for part #` => the part could not be base64 decoded (the message should contain the error the base64 decoder gave) - `Invalid token specified: invalid json for part #` => the part was correctly base64 decoded, however, the decoded value was not valid JSON (the message should contain the error the JSON parser gave) -#### Use with typescript +#### Use with TypeScript -The return type of the `jwt_decode` function is determined by the `header` property of the object passed as the second argument. If omitted (or set to false), it'll use `JwtPayload`, when true it will use `JwtHeader`. -If needed, you can specify what the expected return type should be by passing a type argument to the `jwt_decode` function. +The return type of the `jwtDecode` function is determined by the `header` property of the object passed as the second argument. If omitted (or set to false), it'll use `JwtPayload`, when true it will use `JwtHeader`. +If needed, you can specify what the expected return type should be by passing a type argument to the `jwtDecode` function. You can extend both `JwtHeader` and `JwtPayload` to include non-standard claims or properties. ```typescript -import jwtDecode from "jwt-decode"; +import { jwtDecode } from "jwt-decode"; -const token: string = "eyJhsw5c"; +const token = "eyJhsw5c"; const decoded = jwtDecode(token); // Returns with the JwtPayload type ``` #### Use as a CommonJS package ```javascript -const jwt_decode = require('jwt-decode'); +const { jwtDecode } = require('jwt-decode'); ... ``` #### Include with a script tag -Copy the file `jwt-decode.js` from the `build/` folder to your project somewhere, then include it like so: +Copy the file `jwt-decode.js` from the root of the `build/` folder to your project somewhere, then include it like so: ```html ``` +Once this script has loaded, the `jwt_decode` function will be exposed as a global: + +```javascript +const token = "eyJhsw5c"; +const decoded = jwt_decode(token); +``` + +Alternatively, if you are using the [Asynchronous Module Definition (AMD) API](https://github.com/amdjs/amdjs-api/blob/master/AMD.md), you can load the same function as follows: + +```javascript +define(["jwt_decode"], (jwtDecode) => { + const token = "eyJhsw5c"; + const decoded = jwtDecode(token); +}); +``` ## Feedback diff --git a/lib/base64_url_decode.ts b/lib/base64_url_decode.ts index 3328ec6..aaf3ec2 100644 --- a/lib/base64_url_decode.ts +++ b/lib/base64_url_decode.ts @@ -10,7 +10,7 @@ function b64DecodeUnicode(str: string) { ); } -export default function(str: string) { +export function base64UrlDecode(str: string) { let output = str.replace(/-/g, "+").replace(/_/g, "/"); switch (output.length % 4) { case 0: diff --git a/lib/index.cjs.ts b/lib/index.cjs.ts deleted file mode 100644 index 4d61224..0000000 --- a/lib/index.cjs.ts +++ /dev/null @@ -1,6 +0,0 @@ -import jwtDecode, { InvalidTokenError } from "./index"; - -const wrapper = jwtDecode as any; -wrapper.default = jwtDecode; -wrapper.InvalidTokenError = InvalidTokenError; -export default wrapper; diff --git a/lib/index.standalone.ts b/lib/index.standalone.ts deleted file mode 100644 index 37258be..0000000 --- a/lib/index.standalone.ts +++ /dev/null @@ -1,3 +0,0 @@ -import jwtDecode from "./index"; - -export default jwtDecode; diff --git a/lib/index.ts b/lib/index.ts index 9e59477..35270e7 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,4 +1,4 @@ -import base64_url_decode from "./base64_url_decode"; +import { base64UrlDecode } from "./base64_url_decode"; export interface JwtDecodeOptions { header?: boolean; @@ -28,12 +28,12 @@ export class InvalidTokenError extends Error { InvalidTokenError.prototype.name = "InvalidTokenError"; -function jwtDecode( +export function jwtDecode( token: string, options: JwtDecodeOptions & { header: true } ): T; -function jwtDecode(token: string, options?: JwtDecodeOptions): T; -function jwtDecode(token: string, options?: JwtDecodeOptions) { +export function jwtDecode(token: string, options?: JwtDecodeOptions): T; +export function jwtDecode(token: string, options?: JwtDecodeOptions) { if (typeof token !== "string") { throw new InvalidTokenError("Invalid token specified: must be a string"); } @@ -50,7 +50,7 @@ function jwtDecode(token: string, options?: JwtDecodeOptions) { let decoded: string; try { - decoded = base64_url_decode(part); + decoded = base64UrlDecode(part); } catch (e: any) { throw new InvalidTokenError( "Invalid token specified: invalid base64 for part #" + @@ -74,4 +74,3 @@ function jwtDecode(token: string, options?: JwtDecodeOptions) { } } -export default jwtDecode; diff --git a/lib/index.umd.ts b/lib/index.umd.ts new file mode 100644 index 0000000..4726b67 --- /dev/null +++ b/lib/index.umd.ts @@ -0,0 +1 @@ +export { jwtDecode as default } from './index'; diff --git a/rollup.config.ts b/rollup.config.ts index cf17662..68d21fe 100644 --- a/rollup.config.ts +++ b/rollup.config.ts @@ -4,7 +4,6 @@ import { defineConfig } from "rollup"; import livereload from "rollup-plugin-livereload"; import serve from "rollup-plugin-serve"; -const EXPORT_NAME = "jwt-decode"; const isProduction = process.env.NODE_ENV === "production"; const tsPlugin = typescript({ rootDir: "lib", @@ -16,8 +15,10 @@ const plugins = [ isProduction && terser(), ]; +const input = "lib/index.ts"; + export default defineConfig([{ - input: "lib/index.standalone.ts", + input: "lib/index.umd.ts", output: { name: "jwt_decode", file: "build/jwt-decode.js", @@ -29,24 +30,21 @@ export default defineConfig([{ ] }, { - input: "lib/index.cjs.ts", - output: [{ - name: EXPORT_NAME, + input, + output: { file: "build/cjs/jwt-decode.js", format: "cjs", - exports: "auto", sourcemap: true, - }, ], + }, plugins, }, { - input: "lib/index.ts", - output: [{ - name: EXPORT_NAME, + input, + output: { file: "build/esm/jwt-decode.js", format: "esm", sourcemap: true, - }, ], + }, plugins: [!isProduction && serve({ contentBase: ["build", "static"], diff --git a/static/index.html b/static/index.html index bb03dda..19ca9ae 100644 --- a/static/index.html +++ b/static/index.html @@ -13,7 +13,7 @@

decoded: