Skip to content

Commit

Permalink
Use recommended configurations for typescript-eslint (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonkoops authored Aug 23, 2023
1 parent 41e8d8d commit b1c8df4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
10 changes: 4 additions & 6 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@
"ignorePatterns": ["**/*.mjs", "**/*.js"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-type-checked",
"plugin:@typescript-eslint/stylistic-type-checked",
"plugin:prettier/recommended"
],
"plugins": ["@typescript-eslint", "import"],
"parserOptions": {
"ecmaVersion": 9
"ecmaVersion": 9,
"project": true
},
"rules": {
"@typescript-eslint/no-empty-interface": "warn",
"@typescript-eslint/no-explicit-any": "off",
"import/no-default-export": "error",
"@typescript-eslint/no-unused-vars": "error",
"import/extensions": ["error", "always"]
}
}
17 changes: 10 additions & 7 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ InvalidTokenError.prototype.name = "InvalidTokenError";
function b64DecodeUnicode(str: string) {
return decodeURIComponent(
atob(str).replace(/(.)/g, (m, p) => {
let code = p.charCodeAt(0).toString(16).toUpperCase();
let code = (p as string).charCodeAt(0).toString(16).toUpperCase();
if (code.length < 2) {
code = "0" + code;
}
Expand Down Expand Up @@ -61,7 +61,10 @@ export function jwtDecode<T = JwtHeader>(
options: JwtDecodeOptions & { header: true },
): T;
export function jwtDecode<T = JwtPayload>(token: string, options?: JwtDecodeOptions): T;
export function jwtDecode(token: string, options?: JwtDecodeOptions) {
export function jwtDecode<T = JwtHeader | JwtPayload>(
token: string,
options?: JwtDecodeOptions,
): T {
if (typeof token !== "string") {
throw new InvalidTokenError("Invalid token specified: must be a string");
}
Expand All @@ -78,17 +81,17 @@ export function jwtDecode(token: string, options?: JwtDecodeOptions) {
let decoded: string;
try {
decoded = base64UrlDecode(part);
} catch (e: any) {
} catch (e) {
throw new InvalidTokenError(
`Invalid token specified: invalid base64 for part #${pos + 1} (${e.message})`,
`Invalid token specified: invalid base64 for part #${pos + 1} (${(e as Error).message})`,
);
}

try {
return JSON.parse(decoded);
} catch (e: any) {
return JSON.parse(decoded) as T;
} catch (e) {
throw new InvalidTokenError(
`Invalid token specified: invalid json for part #${pos + 1} (${e.message})`,
`Invalid token specified: invalid json for part #${pos + 1} (${(e as Error).message})`,
);
}
}
4 changes: 2 additions & 2 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe("jwt-decode", () => {
it("should throw InvalidTokenError on nonstring", () => {
const badToken = null;
expect(() => {
jwtDecode(badToken as any);
jwtDecode(badToken as unknown as string);
}).toThrow(InvalidTokenError);
});

Expand All @@ -63,7 +63,7 @@ describe("jwt-decode", () => {
it("should throw InvalidTokenErrors when token is null", () => {
const badToken = null;
expect(() => {
jwtDecode(badToken as any, { header: true });
jwtDecode(badToken as unknown as string, { header: true });
}).toThrow(new InvalidTokenError("Invalid token specified: must be a string"));
});

Expand Down

0 comments on commit b1c8df4

Please sign in to comment.