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,