Skip to content

Commit

Permalink
Various review feedback for ESLint configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jonkoops committed Aug 27, 2023
1 parent b1c8df4 commit e3534fc
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 33 deletions.
29 changes: 16 additions & 13 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
{
"env": {
"node": true,
"commonjs": true,
"es2017": true,
"mocha": true
},
"ignorePatterns": ["**/*.mjs", "**/*.js"],
"ignorePatterns": ["build", "coverage"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended-type-checked",
"plugin:@typescript-eslint/stylistic-type-checked",
"plugin:import/recommended",
"plugin:import/typescript",
"plugin:prettier/recommended"
],
"plugins": ["@typescript-eslint", "import"],
"settings": {
"import/resolver": {
"typescript": true
}
},
"parserOptions": {
"ecmaVersion": 9,
"project": true
},
"rules": {
"import/no-default-export": "error",
"import/extensions": ["error", "always"]
}
"overrides": [
{
"files": ["lib", "test"],
"rules": {
"import/no-default-export": "error"
}
}
]
}
4 changes: 0 additions & 4 deletions .prettierrc

This file was deleted.

17 changes: 13 additions & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ export function jwtDecode<T = JwtHeader>(
token: string,
options: JwtDecodeOptions & { header: true },
): T;
export function jwtDecode<T = JwtPayload>(token: string, options?: JwtDecodeOptions): T;
export function jwtDecode<T = JwtPayload>(
token: string,
options?: JwtDecodeOptions,
): T;
export function jwtDecode<T = JwtHeader | JwtPayload>(
token: string,
options?: JwtDecodeOptions,
Expand All @@ -75,23 +78,29 @@ export function jwtDecode<T = JwtHeader | JwtPayload>(
const part = token.split(".")[pos];

if (typeof part !== "string") {
throw new InvalidTokenError(`Invalid token specified: missing part #${pos + 1}`);
throw new InvalidTokenError(
`Invalid token specified: missing part #${pos + 1}`,
);
}

let decoded: string;
try {
decoded = base64UrlDecode(part);
} catch (e) {
throw new InvalidTokenError(
`Invalid token specified: invalid base64 for part #${pos + 1} (${(e as Error).message})`,
`Invalid token specified: invalid base64 for part #${pos + 1} (${
(e as Error).message
})`,
);
}

try {
return JSON.parse(decoded) as T;
} catch (e) {
throw new InvalidTokenError(
`Invalid token specified: invalid json for part #${pos + 1} (${(e as Error).message})`,
`Invalid token specified: invalid json for part #${pos + 1} (${
(e as Error).message
})`,
);
}
}
108 changes: 100 additions & 8 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"prebuild": "rimraf build",
"build": "tsc -b ./tsconfig.cjs.json ./tsconfig.esm.json && echo '{\"type\": \"commonjs\"}'> build/cjs/package.json",
"build:watch": "tsc -b --watch ./tsconfig.cjs.json ./tsconfig.esm.json && echo '{\"type\": \"commonjs\"}'> build/cjs/package.json",
"lint": "eslint ./lib ./test --ext ts",
"lint": "eslint .",
"lint:package": "publint",
"test": "npm run test:node && npm run test:browser",
"test:node": "NODE_OPTIONS='--experimental-vm-modules --no-warnings' jest --coverage",
Expand All @@ -51,8 +51,9 @@
"@typescript-eslint/parser": "^6.4.1",
"browser-sync": "^2.29.3",
"concurrently": "^8.2.0",
"eslint": "^8.47.0",
"eslint": "^8.48.0",
"eslint-config-prettier": "^9.0.0",
"eslint-import-resolver-typescript": "^3.6.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-prettier": "^5.0.0",
"husky": "^8.0.3",
Expand Down
8 changes: 6 additions & 2 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ describe("jwt-decode", () => {
const badToken = null;
expect(() => {
jwtDecode(badToken as unknown as string, { header: true });
}).toThrow(new InvalidTokenError("Invalid token specified: must be a string"));
}).toThrow(
new InvalidTokenError("Invalid token specified: must be a string"),
);
});

it("should throw InvalidTokenErrors when missing part #1", () => {
Expand Down Expand Up @@ -92,7 +94,9 @@ describe("jwt-decode", () => {
const badToken = "FAKE_TOKEN";
expect(() => {
jwtDecode(badToken);
}).toThrow(new InvalidTokenError("Invalid token specified: missing part #2"));
}).toThrow(
new InvalidTokenError("Invalid token specified: missing part #2"),
);
});

it("should throw InvalidTokenErrors when part #2 is not valid base64", () => {
Expand Down

0 comments on commit e3534fc

Please sign in to comment.