Skip to content

Commit

Permalink
build(deps-dev): replace standard with neostandard (#239)
Browse files Browse the repository at this point in the history
* build(deps-dev): replace standard with neostandard

* Update types/index.test-d.ts

Co-authored-by: Manuel Spigolon <manuel.spigolon@nearform.com>
Signed-off-by: Frazer Smith <frazer.dev@icloud.com>

* Update types/index.test-d.ts

Co-authored-by: Manuel Spigolon <manuel.spigolon@nearform.com>
Signed-off-by: Frazer Smith <frazer.dev@icloud.com>

* chore: rebase

---------

Signed-off-by: Frazer Smith <frazer.dev@icloud.com>
Co-authored-by: Manuel Spigolon <manuel.spigolon@nearform.com>
  • Loading branch information
Fdawgs and Eomm authored Dec 15, 2024
1 parent c821daa commit c8b5df3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 47 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-auth/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fastify/fastify-auth/actions/workflows/ci.yml)
[![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).
Expand Down
6 changes: 6 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict'

module.exports = require('neostandard')({
ignores: require('neostandard').resolveIgnoresFromGitignore(),
ts: true
})
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,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"
Expand Down Expand Up @@ -66,8 +66,8 @@
"@types/node": "^22.0.0",
"c8": "^10.1.2",
"fastify": "^5.0.0",
"neostandard": "^0.12.0",
"rimraf": "^6.0.1",
"standard": "^17.1.0",
"tsd": "^0.31.1"
},
"dependencies": {
Expand Down
6 changes: 3 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
FastifySchema,
RouteGenericInterface,
preHandlerHookHandler
} from 'fastify';
} from 'fastify'

declare module 'fastify' {
interface FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider> {
Expand Down Expand Up @@ -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)
Expand All @@ -55,5 +55,5 @@ declare namespace fastifyAuth {
export { fastifyAuth as default }
}

declare function fastifyAuth(...params: Parameters<FastifyAuth>): ReturnType<FastifyAuth>
declare function fastifyAuth (...params: Parameters<FastifyAuth>): ReturnType<FastifyAuth>
export = fastifyAuth
82 changes: 42 additions & 40 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -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.register(fastifyAuth).after((_err) => {
app.auth([
(request, reply, done) => {
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
expectType<Done>(done)
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
expectType<Done>(done)
},
], {relation: 'or'});
], { relation: 'or' })
app.auth([
(request, reply, done) => {
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
expectType<Done>(done)
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
expectType<Done>(done)
},
], {run: 'all'});
], { run: 'all' })
app.auth([
(request, reply, done) => {
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
expectType<Done>(done)
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
expectType<Done>(done)
},
]);
])
app.auth([
function (request, reply, done) {
expectType<FastifyInstance>(this)
},
]);
const auth = app.auth([(request, reply, done) => {}]);
expectType<preHandlerHookHandler>(auth);
app.get('/secret', {preHandler: auth}, (request, reply) => {});
app.get('/private', {preHandler: [auth]}, (request, reply) => {});
});
])
const auth = app.auth([(request, reply, done) => {}])
expectType<preHandlerHookHandler>(auth)
app.get('/secret', { preHandler: auth }, (request, reply) => {})
app.get('/private', { preHandler: [auth] }, (request, reply) => {})
})

const typebox = fastify().withTypeProvider<TypeBoxTypeProvider>()
typebox.register(fastifyAuth)
Expand All @@ -59,9 +59,9 @@ jsonSchemaToTS.route({
handler: () => {}
})

declare module "fastify" {
declare module 'fastify' {
interface FastifyRequest {
identity: {actorId: string};
identity: { actorId: string };
}

interface FastifyInstance {
Expand All @@ -74,44 +74,46 @@ export const usersMutationAccessPolicy =
async (
request: FastifyRequest<{
Params: { userId: string }
}>,
}>
): Promise<void> => {
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<void> {
async function usersController (fastify: FastifyInstance): Promise<void> {
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 })
)
}
await usersController(app)

async function usersControllerV2(fastify: FastifyInstance): Promise<void> {
async function usersControllerV2 (fastify: FastifyInstance): Promise<void> {
fastify.patch<{
Params: { userId: string };
Body: { name: string };
}>(
"/:userId",
'/:userId',
{
onRequest: usersMutationAccessPolicy(fastify),
},
async (req, res) => ({ success: true }),
);
}
async (req, res) => ({ success: true })
)
}
await usersControllerV2(app)

0 comments on commit c8b5df3

Please sign in to comment.