Skip to content

Commit

Permalink
build(deps-dev): replace standard with neostandard
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs committed Dec 8, 2024
1 parent 4f9facc commit 5fc1370
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 58 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
},
Expand Down
22 changes: 11 additions & 11 deletions types/jwt.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@ declare namespace fastifyJwt {
jwtVerify?: string;
jwtSign?: string;
}> =
Record<C extends { jwtDecode: string}
Record<C extends { jwtDecode: string }
? C['jwtDecode']
: C extends {namespace: string}
: C extends { namespace: string }
? `${C['namespace']}JwtDecode`
: never,
JWT['decode']>
&
Record<C extends { jwtSign: string}
Record<C extends { jwtSign: string }
? C['jwtSign']
: C extends {namespace: string}
: C extends { namespace: string }
? `${C['namespace']}JwtSign`
: never,
JWT['sign']>
&
Record<C extends { jwtVerify: string}
Record<C extends { jwtVerify: string }
? C['jwtVerify']
: C extends {namespace: string}
: C extends { namespace: string }
? `${C['namespace']}JwtVerify`
: never,
JWT['verify']>
Expand Down Expand Up @@ -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 }
Expand All @@ -118,13 +118,13 @@ declare namespace fastifyJwt {
(err: Error, decoded: Decoded): void
}

export interface SignOptions extends Omit<SignerOptions, "expiresIn" | "notBefore"> {
export interface SignOptions extends Omit<SignerOptions, 'expiresIn' | 'notBefore'> {
expiresIn: number | string;
notBefore: number | string;
key?: string | Buffer
}

export interface VerifyOptions extends Omit<VerifierOptions, "maxAge"> {
export interface VerifyOptions extends Omit<VerifierOptions, 'maxAge'> {
maxAge: number | string;
onlyCookie: boolean;
key?: string | Buffer
Expand Down Expand Up @@ -207,5 +207,5 @@ declare namespace fastifyJwt {
export { fastifyJwt as default }
}

declare function fastifyJwt(...params: Parameters<FastifyJwt>): ReturnType<FastifyJwt>
declare function fastifyJwt (...params: Parameters<FastifyJwt>): ReturnType<FastifyJwt>
export = fastifyJwt
85 changes: 42 additions & 43 deletions types/jwt.test-d.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -58,21 +58,21 @@ const jwtOptions: FastifyJWTOptions = {
? JSON.parse(payload)
: Buffer.isBuffer(payload)
? JSON.parse(payload.toString())
: payload;
: payload
return { name: objectPayload.userName }
},
namespace: 'security',
jwtVerify: 'securityVerify',
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: {
Expand All @@ -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<object>(app.jwt)
Expand All @@ -95,27 +95,26 @@ expectAssignable<Function>(app.jwt.decode)
expectAssignable<Function>(app.jwt.lookupToken)
expectAssignable<FastifyJWTOptions['cookie']>(app.jwt.cookie)

app.addHook("preHandler", async (request, reply) => {
app.addHook('preHandler', async (request, reply) => {
// assert request and reply specific interface merges
expectAssignable<Function>(request.jwtVerify)
expectAssignable<Function>(request.jwtDecode)
expectAssignable<object | string | Buffer>(request.user)
expectAssignable<Function>(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 })
})

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.

// define custom payload
// declare module './jwt' {
Expand All @@ -138,64 +137,64 @@ app.post('/signup', async (req, reply) => {
// }
// }

expectType<JWT['decode']>(({} as FastifyJwtNamespace<{namespace: 'security'}>).securityJwtDecode)
expectType<JWT['sign']>(({} as FastifyJwtNamespace<{namespace: 'security'}>).securityJwtSign)
expectType<JWT['verify']>(({} as FastifyJwtNamespace<{namespace: 'security'}>).securityJwtVerify)
expectType<JWT['decode']>(({} as FastifyJwtNamespace<{ namespace: 'security' }>).securityJwtDecode)
expectType<JWT['sign']>(({} as FastifyJwtNamespace<{ namespace: 'security' }>).securityJwtSign)
expectType<JWT['verify']>(({} as FastifyJwtNamespace<{ namespace: 'security' }>).securityJwtVerify)

declare module 'fastify' {
interface FastifyInstance extends FastifyJwtNamespace<{namespace: 'tsdTest'}> {
interface FastifyInstance extends FastifyJwtNamespace<{ namespace: 'tsdTest' }> {
}
}

expectType<JWT['decode']>(app.tsdTestJwtDecode)
expectType<JWT['sign']>(app.tsdTestJwtSign)
expectType<JWT['verify']>(app.tsdTestJwtVerify)

expectType<JWT['decode']>(({} as FastifyJwtNamespace<{ namespace: 'security', jwtDecode: 'decode'}>).decode)
expectType<JWT['sign']>(({} as FastifyJwtNamespace<{ namespace: 'security', jwtDecode: 'decode'}>).securityJwtSign)
expectType<JWT['verify']>(({} as FastifyJwtNamespace<{ namespace: 'security', jwtDecode: 'decode'}>).securityJwtVerify)
expectType<JWT['decode']>(({} as FastifyJwtNamespace<{ namespace: 'security', jwtDecode: 'decode' }>).decode)
expectType<JWT['sign']>(({} as FastifyJwtNamespace<{ namespace: 'security', jwtDecode: 'decode' }>).securityJwtSign)
expectType<JWT['verify']>(({} as FastifyJwtNamespace<{ namespace: 'security', jwtDecode: 'decode' }>).securityJwtVerify)

expectType<JWT['decode']>(({} as FastifyJwtNamespace<{ namespace: 'security', jwtSign: 'decode'}>).securityJwtDecode)
expectType<JWT['sign']>(({} as FastifyJwtNamespace<{ namespace: 'security', jwtSign: 'sign'}>).sign)
expectType<JWT['verify']>(({} as FastifyJwtNamespace<{ namespace: 'security', jwtSign: 'decode'}>).securityJwtVerify)
expectType<JWT['decode']>(({} as FastifyJwtNamespace<{ namespace: 'security', jwtSign: 'decode' }>).securityJwtDecode)
expectType<JWT['sign']>(({} as FastifyJwtNamespace<{ namespace: 'security', jwtSign: 'sign' }>).sign)
expectType<JWT['verify']>(({} as FastifyJwtNamespace<{ namespace: 'security', jwtSign: 'decode' }>).securityJwtVerify)

expectType<JWT['decode']>(({} as FastifyJwtNamespace<{ namespace: 'security', jwtVerify: 'verify'}>).securityJwtDecode)
expectType<JWT['sign']>(({} as FastifyJwtNamespace<{ namespace: 'security', jwtVerify: 'verify'}>).securityJwtSign)
expectType<JWT['verify']>(({} as FastifyJwtNamespace<{ namespace: 'security', jwtVerify: 'verify'}>).verify)
expectType<JWT['decode']>(({} as FastifyJwtNamespace<{ namespace: 'security', jwtVerify: 'verify' }>).securityJwtDecode)
expectType<JWT['sign']>(({} as FastifyJwtNamespace<{ namespace: 'security', jwtVerify: 'verify' }>).securityJwtSign)
expectType<JWT['verify']>(({} as FastifyJwtNamespace<{ namespace: 'security', jwtVerify: 'verify' }>).verify)

expectType<JWT['decode']>(({} as FastifyJwtNamespace<{ jwtDecode: 'decode'}>).decode)
expectType<JWT['sign']>(({} as FastifyJwtNamespace<{ jwtSign: 'sign'}>).sign)
expectType<JWT['verify']>(({} as FastifyJwtNamespace<{ jwtVerify: 'verify'}>).verify)
expectType<JWT['decode']>(({} as FastifyJwtNamespace<{ jwtDecode: 'decode' }>).decode)
expectType<JWT['sign']>(({} as FastifyJwtNamespace<{ jwtSign: 'sign' }>).sign)
expectType<JWT['verify']>(({} 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,
Expand Down

0 comments on commit 5fc1370

Please sign in to comment.