diff --git a/README.md b/README.md index bf12bca..5b680de 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ![CI](https://github.com/fastify/fastify-auth/workflows/CI/badge.svg) [![NPM version](https://img.shields.io/npm/v/@fastify/auth.svg?style=flat)](https://www.npmjs.com/package/@fastify/auth) -[![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) This module does not provide an authentication strategy, but it provides a very fast utility to handle authentication (and multiple strategies) in your routes, without adding overhead. Check out a complete example [here](test/example.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 +}) diff --git a/package.json b/package.json index 066cbc5..1777c11 100644 --- a/package.json +++ b/package.json @@ -15,8 +15,8 @@ "types": "types/index.d.ts", "scripts": { "clean": "rimraf authdb", - "lint": "standard", - "lint:fix": "standard --fix", + "lint": "eslint", + "lint:fix": "eslint --fix", "test": "npm run test:unit && npm run test:typescript", "test:typescript": "tsd", "test:unit": "c8 --100 node --test" @@ -43,8 +43,8 @@ "@types/node": "^22.0.0", "c8": "^10.1.2", "fastify": "^5.0.0", + "neostandard": "^0.11.9", "rimraf": "^6.0.1", - "standard": "^17.1.0", "tsd": "^0.31.1" }, "dependencies": { diff --git a/types/index.d.ts b/types/index.d.ts index eaf5c93..2de3e06 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -7,7 +7,7 @@ import { FastifySchema, RouteGenericInterface, preHandlerHookHandler -} from 'fastify'; +} from 'fastify' declare module 'fastify' { interface FastifyInstance { @@ -37,7 +37,7 @@ declare namespace fastifyAuth { request: Request, reply: Reply, done: (error?: Error) => void - ) => void; + ) => void /** * @link [`fastify-auth` options documentation](https://github.com/fastify/fastify-auth#options) @@ -55,5 +55,5 @@ declare namespace fastifyAuth { export { fastifyAuth as default } } -declare function fastifyAuth(...params: Parameters): ReturnType +declare function fastifyAuth (...params: Parameters): ReturnType export = fastifyAuth diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 3ab1adb..60a4054 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -1,45 +1,45 @@ -import fastify, { FastifyInstance, FastifyReply, FastifyRequest, preHandlerHookHandler } from 'fastify'; +import fastify, { FastifyInstance, FastifyReply, FastifyRequest, preHandlerHookHandler } from 'fastify' import fastifyAuth from '..' -import { expectType } from 'tsd'; +import { expectType } from 'tsd' import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox' import { JsonSchemaToTsProvider } from '@fastify/type-provider-json-schema-to-ts' -const app = fastify(); +const app = fastify() type Done = (error?: Error) => void app.register(fastifyAuth).after((err) => { app.auth([ (request, reply, done) => { - expectType(request) - expectType(reply) - expectType(done) + expectType(request) + expectType(reply) + expectType(done) }, - ], {relation: 'or'}); + ], { relation: 'or' }) app.auth([ (request, reply, done) => { - expectType(request) - expectType(reply) - expectType(done) + expectType(request) + expectType(reply) + expectType(done) }, - ], {run: 'all'}); + ], { run: 'all' }) app.auth([ (request, reply, done) => { - expectType(request) - expectType(reply) - expectType(done) + expectType(request) + expectType(reply) + expectType(done) }, - ]); + ]) app.auth([ function (request, reply, done) { expectType(this) }, - ]); - const auth = app.auth([(request, reply, done) => {}]); - expectType(auth); - app.get('/secret', {preHandler: auth}, (request, reply) => {}); - app.get('/private', {preHandler: [auth]}, (request, reply) => {}); -}); + ]) + const auth = app.auth([(request, reply, done) => {}]) + expectType(auth) + app.get('/secret', { preHandler: auth }, (request, reply) => {}) + app.get('/private', { preHandler: [auth] }, (request, reply) => {}) +}) const typebox = fastify().withTypeProvider() typebox.register(fastifyAuth) @@ -59,9 +59,9 @@ jsonSchemaToTS.route({ handler: () => {} }) -declare module "fastify" { +declare module 'fastify' { interface FastifyRequest { - identity: {actorId: string}; + identity: { actorId: string }; } interface FastifyInstance { @@ -74,44 +74,46 @@ export const usersMutationAccessPolicy = async ( request: FastifyRequest<{ Params: { userId: string } - }>, + }> ): Promise => { - const { actorId } = request.identity; - const isOwner = actorId === request.params.userId; + const { actorId } = request.identity + const isOwner = actorId === request.params.userId if (isOwner) { - return; + return } - fastify.log.warn("Actor should not be able to see this route"); + fastify.log.warn('Actor should not be able to see this route') - throw new Error(request.params.userId); - }; + throw new Error(request.params.userId) + } -async function usersController(fastify: FastifyInstance): Promise { +// @ts-expect-error +async function usersController (fastify: FastifyInstance): Promise { fastify.patch<{ Params: { userId: string }; Body: { name: string }; }>( - "/:userId", + '/:userId', { onRequest: fastify.auth([ usersMutationAccessPolicy(fastify), ]), }, - async (req, res) => ({ success: true }), - ); + async (req, res) => ({ success: true }) + ) } -async function usersControllerV2(fastify: FastifyInstance): Promise { +// @ts-expect-error +async function usersControllerV2 (fastify: FastifyInstance): Promise { fastify.patch<{ Params: { userId: string }; Body: { name: string }; }>( - "/:userId", + '/:userId', { onRequest: usersMutationAccessPolicy(fastify), }, - async (req, res) => ({ success: true }), - ); -} \ No newline at end of file + async (req, res) => ({ success: true }) + ) +}