From 5fc13707652b20ebb3533f64deac2a1ca9d8406b Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Sun, 8 Dec 2024 10:44:41 +0000 Subject: [PATCH 1/3] build(deps-dev): replace standard with neostandard --- README.md | 2 +- package.json | 6 ++-- types/jwt.d.ts | 22 ++++++------ types/jwt.test-d.ts | 85 ++++++++++++++++++++++----------------------- 4 files changed, 57 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index 21bb566..686da65 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![CI](https://github.com/fastify/fastify-jwt/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fastify/fastify-jwt/actions/workflows/ci.yml) [![NPM version](https://img.shields.io/npm/v/@fastify/jwt.svg?style=flat)](https://www.npmjs.com/package/@fastify/jwt) -[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/) +[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard) JWT utils for Fastify, internally it uses [fast-jwt](https://github.com/nearform/fast-jwt). diff --git a/package.json b/package.json index eee0d64..eb58b65 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,8 @@ "type": "commonjs", "types": "types/jwt.d.ts", "scripts": { - "lint": "standard", - "lint:fix": "standard --fix", + "lint": "eslint", + "lint:fix": "eslint --fix", "test": "npm run lint && npm run test:unit && npm run test:typescript", "test:typescript": "tsd", "test:unit": "tap", @@ -43,7 +43,7 @@ "@fastify/pre-commit": "^2.1.0", "@types/node": "^22.0.0", "fastify": "^5.0.0", - "standard": "^17.1.0", + "neostandard": "^0.11.9", "tap": "^18.7.1", "tsd": "^0.31.0" }, diff --git a/types/jwt.d.ts b/types/jwt.d.ts index 3590e9c..1f8581d 100644 --- a/types/jwt.d.ts +++ b/types/jwt.d.ts @@ -48,23 +48,23 @@ declare namespace fastifyJwt { jwtVerify?: string; jwtSign?: string; }> = - Record & - Record & - Record @@ -97,8 +97,8 @@ declare namespace fastifyJwt { export type SignPayloadType = FastifyJWT extends { payload: infer T } ? T extends string | object | Buffer - ? T - : string | object | Buffer + ? T + : string | object | Buffer : string | object | Buffer export type UserType = FastifyJWT extends { user: infer T } @@ -118,13 +118,13 @@ declare namespace fastifyJwt { (err: Error, decoded: Decoded): void } - export interface SignOptions extends Omit { + export interface SignOptions extends Omit { expiresIn: number | string; notBefore: number | string; key?: string | Buffer } - export interface VerifyOptions extends Omit { + export interface VerifyOptions extends Omit { maxAge: number | string; onlyCookie: boolean; key?: string | Buffer @@ -207,5 +207,5 @@ declare namespace fastifyJwt { export { fastifyJwt as default } } -declare function fastifyJwt(...params: Parameters): ReturnType +declare function fastifyJwt (...params: Parameters): ReturnType export = fastifyJwt diff --git a/types/jwt.test-d.ts b/types/jwt.test-d.ts index ee5bafb..3e05dd8 100644 --- a/types/jwt.test-d.ts +++ b/types/jwt.test-d.ts @@ -1,8 +1,8 @@ -import fastify from 'fastify'; +import fastify from 'fastify' import fastifyJwt, { FastifyJWTOptions, FastifyJwtNamespace, JWT, SignOptions, VerifyOptions } from '..' import { expectAssignable, expectType } from 'tsd' -const app = fastify(); +const app = fastify() const secretOptions = { secret: 'supersecret', @@ -58,7 +58,7 @@ const jwtOptions: FastifyJWTOptions = { ? JSON.parse(payload) : Buffer.isBuffer(payload) ? JSON.parse(payload.toString()) - : payload; + : payload return { name: objectPayload.userName } }, namespace: 'security', @@ -66,13 +66,13 @@ const jwtOptions: FastifyJWTOptions = { jwtSign: 'securitySign' } -app.register(fastifyJwt, jwtOptions); +app.register(fastifyJwt, jwtOptions) Object.values(secretOptions).forEach((value) => { - app.register(fastifyJwt, {...jwtOptions, secret: value }); + app.register(fastifyJwt, { ...jwtOptions, secret: value }) }) -app.register(fastifyJwt, {...jwtOptions, trusted: () => Promise.resolve(false || '' || Buffer.from('foo')) }) +app.register(fastifyJwt, { ...jwtOptions, trusted: () => Promise.resolve(false || '' || Buffer.from('foo')) }) app.register(fastifyJwt, { secret: { @@ -85,7 +85,7 @@ app.register(fastifyJwt, { sign: { algorithm: 'ES256' }, }) -app.register(fastifyJwt, {...jwtOptions, decoratorName: 'token' }) +app.register(fastifyJwt, { ...jwtOptions, decoratorName: 'token' }) // expect jwt and its subsequent methods have merged with the fastify instance expectAssignable(app.jwt) @@ -95,7 +95,7 @@ expectAssignable(app.jwt.decode) expectAssignable(app.jwt.lookupToken) expectAssignable(app.jwt.cookie) -app.addHook("preHandler", async (request, reply) => { +app.addHook('preHandler', async (request, reply) => { // assert request and reply specific interface merges expectAssignable(request.jwtVerify) expectAssignable(request.jwtDecode) @@ -103,19 +103,18 @@ app.addHook("preHandler", async (request, reply) => { expectAssignable(reply.jwtSign) try { - await request.jwtVerify(); + await request.jwtVerify() + } catch (err) { + reply.send(err) } - catch (err) { - reply.send(err); - } -}); +}) app.post('/signup', async (req, reply) => { - const token = app.jwt.sign({ user: "userName" }); - let data = await app.jwt.verify(token); - const user = req.user; - reply.send({ token }); -}); + const token = app.jwt.sign({ user: 'userName' }) + const data = await app.jwt.verify(token) + const user = req.user + reply.send({ token }) +}) // define custom payload // declare module './jwt' { @@ -138,12 +137,12 @@ app.post('/signup', async (req, reply) => { // } // } -expectType(({} as FastifyJwtNamespace<{namespace: 'security'}>).securityJwtDecode) -expectType(({} as FastifyJwtNamespace<{namespace: 'security'}>).securityJwtSign) -expectType(({} as FastifyJwtNamespace<{namespace: 'security'}>).securityJwtVerify) +expectType(({} as FastifyJwtNamespace<{ namespace: 'security' }>).securityJwtDecode) +expectType(({} as FastifyJwtNamespace<{ namespace: 'security' }>).securityJwtSign) +expectType(({} as FastifyJwtNamespace<{ namespace: 'security' }>).securityJwtVerify) declare module 'fastify' { - interface FastifyInstance extends FastifyJwtNamespace<{namespace: 'tsdTest'}> { + interface FastifyInstance extends FastifyJwtNamespace<{ namespace: 'tsdTest' }> { } } @@ -151,51 +150,51 @@ expectType(app.tsdTestJwtDecode) expectType(app.tsdTestJwtSign) expectType(app.tsdTestJwtVerify) -expectType(({} as FastifyJwtNamespace<{ namespace: 'security', jwtDecode: 'decode'}>).decode) -expectType(({} as FastifyJwtNamespace<{ namespace: 'security', jwtDecode: 'decode'}>).securityJwtSign) -expectType(({} as FastifyJwtNamespace<{ namespace: 'security', jwtDecode: 'decode'}>).securityJwtVerify) +expectType(({} as FastifyJwtNamespace<{ namespace: 'security', jwtDecode: 'decode' }>).decode) +expectType(({} as FastifyJwtNamespace<{ namespace: 'security', jwtDecode: 'decode' }>).securityJwtSign) +expectType(({} as FastifyJwtNamespace<{ namespace: 'security', jwtDecode: 'decode' }>).securityJwtVerify) -expectType(({} as FastifyJwtNamespace<{ namespace: 'security', jwtSign: 'decode'}>).securityJwtDecode) -expectType(({} as FastifyJwtNamespace<{ namespace: 'security', jwtSign: 'sign'}>).sign) -expectType(({} as FastifyJwtNamespace<{ namespace: 'security', jwtSign: 'decode'}>).securityJwtVerify) +expectType(({} as FastifyJwtNamespace<{ namespace: 'security', jwtSign: 'decode' }>).securityJwtDecode) +expectType(({} as FastifyJwtNamespace<{ namespace: 'security', jwtSign: 'sign' }>).sign) +expectType(({} as FastifyJwtNamespace<{ namespace: 'security', jwtSign: 'decode' }>).securityJwtVerify) -expectType(({} as FastifyJwtNamespace<{ namespace: 'security', jwtVerify: 'verify'}>).securityJwtDecode) -expectType(({} as FastifyJwtNamespace<{ namespace: 'security', jwtVerify: 'verify'}>).securityJwtSign) -expectType(({} as FastifyJwtNamespace<{ namespace: 'security', jwtVerify: 'verify'}>).verify) +expectType(({} as FastifyJwtNamespace<{ namespace: 'security', jwtVerify: 'verify' }>).securityJwtDecode) +expectType(({} as FastifyJwtNamespace<{ namespace: 'security', jwtVerify: 'verify' }>).securityJwtSign) +expectType(({} as FastifyJwtNamespace<{ namespace: 'security', jwtVerify: 'verify' }>).verify) -expectType(({} as FastifyJwtNamespace<{ jwtDecode: 'decode'}>).decode) -expectType(({} as FastifyJwtNamespace<{ jwtSign: 'sign'}>).sign) -expectType(({} as FastifyJwtNamespace<{ jwtVerify: 'verify'}>).verify) +expectType(({} as FastifyJwtNamespace<{ jwtDecode: 'decode' }>).decode) +expectType(({} as FastifyJwtNamespace<{ jwtSign: 'sign' }>).sign) +expectType(({} as FastifyJwtNamespace<{ jwtVerify: 'verify' }>).verify) let signOptions: SignOptions = { - key: "supersecret", - algorithm: "HS256", + key: 'supersecret', + algorithm: 'HS256', mutatePayload: true, expiresIn: 3600, notBefore: 0, } signOptions = { - key: Buffer.from("supersecret", "utf-8"), - algorithm: "HS256", + key: Buffer.from('supersecret', 'utf-8'), + algorithm: 'HS256', mutatePayload: true, expiresIn: 3600, notBefore: 0, } let verifyOptions: VerifyOptions = { - key: "supersecret", - algorithms: ["HS256"], + key: 'supersecret', + algorithms: ['HS256'], complete: true, cache: true, cacheTTL: 3600, - maxAge: "1 hour", + maxAge: '1 hour', onlyCookie: false, } verifyOptions = { - key: Buffer.from("supersecret", "utf-8"), - algorithms: ["HS256"], + key: Buffer.from('supersecret', 'utf-8'), + algorithms: ['HS256'], complete: true, cache: 3600, cacheTTL: 3600, From e4561824c83ba1d3fe535b20afbad0b19ec58139 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Sun, 8 Dec 2024 10:53:38 +0000 Subject: [PATCH 2/3] chore: add eslint.config.js --- eslint.config.js | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 eslint.config.js diff --git a/eslint.config.js b/eslint.config.js new file mode 100644 index 0000000..89fd678 --- /dev/null +++ b/eslint.config.js @@ -0,0 +1,6 @@ +'use strict' + +module.exports = require('neostandard')({ + ignores: require('neostandard').resolveIgnoresFromGitignore(), + ts: true +}) From 30606bfad6680fec3bed339b4f90f9539b4d60d7 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Mon, 16 Dec 2024 20:20:43 +0000 Subject: [PATCH 3/3] Update package.json Signed-off-by: Frazer Smith --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index eb58b65..15aa001 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "@fastify/cookie": "^11.0.1", "@fastify/pre-commit": "^2.1.0", "@types/node": "^22.0.0", + "eslint": "^9.17.0", "fastify": "^5.0.0", "neostandard": "^0.11.9", "tap": "^18.7.1",