From f15806edfcdc1aef3899bdab4835e84053db139a Mon Sep 17 00:00:00 2001 From: Ozer Date: Thu, 15 May 2025 01:15:15 +0300 Subject: [PATCH 1/3] #258 extend FastifyAuthFunction type to accept async functions --- types/index.d.ts | 9 ++-- types/index.test-d.ts | 101 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 102 insertions(+), 8 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 2de3e06..dddf1fa 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -32,12 +32,9 @@ declare namespace fastifyAuth { export type FastifyAuthFunction< Request extends FastifyRequest = FastifyRequest, Reply extends FastifyReply = FastifyReply - > = ( - this: FastifyInstance, - request: Request, - reply: Reply, - done: (error?: Error) => void - ) => void + > = + | ((this: FastifyInstance, request: Request, reply: Reply, done: (error?: Error) => void) => void) + | ((this: FastifyInstance, request: Request, reply: Reply) => Promise) /** * @link [`fastify-auth` options documentation](https://github.com/fastify/fastify-auth#options) diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 44c44be..8211e34 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -9,13 +9,14 @@ const app = fastify() type Done = (error?: Error) => void app.register(fastifyAuth).after((_err) => { + // Callback tests app.auth([ (request, reply, done) => { expectType(request) expectType(reply) expectType(done) }, - ], { relation: 'or' }) + ], { relation: 'and' }) app.auth([ (request, reply, done) => { expectType(request) @@ -29,12 +30,108 @@ app.register(fastifyAuth).after((_err) => { expectType(reply) expectType(done) }, + ], { relation: 'or', run: 'all' }) + app.auth([ + (request, reply, done) => { + expectType(request) + expectType(reply) + expectType(done) + }, + ], { relation: 'and', run: 'all' }) + + // Async function tests + app.auth([ + async (request: FastifyRequest, reply: FastifyReply) => { + expectType(request) + expectType(reply) + await Promise.resolve() + }, ]) app.auth([ - function () { + async (request: FastifyRequest, reply: FastifyReply) => { + expectType(request) + expectType(reply) + await Promise.resolve() + }, + ], { relation: 'and' }) + app.auth([ + async (request: FastifyRequest, reply: FastifyReply) => { + expectType(request) + expectType(reply) + await Promise.resolve() + }, + ], { run: 'all' }) + app.auth([ + async (request: FastifyRequest, reply: FastifyReply) => { + expectType(request) + expectType(reply) + await Promise.resolve() + }, + ], { relation: 'or', run: 'all' }) + app.auth([ + async (request: FastifyRequest, reply: FastifyReply) => { + expectType(request) + expectType(reply) + await Promise.resolve() + }, + ], { relation: 'and', run: 'all' }) + + // Promise-based function tests + app.auth([ + (request: FastifyRequest, reply: FastifyReply) => { + expectType(request) + expectType(reply) + return Promise.resolve() + }, + ]) + app.auth([ + (request: FastifyRequest, reply: FastifyReply) => { + expectType(request) + expectType(reply) + return Promise.resolve() + }, + ], { relation: 'and' }) + app.auth([ + (request: FastifyRequest, reply: FastifyReply) => { + expectType(request) + expectType(reply) + return Promise.resolve() + }, + ], { run: 'all' }) + app.auth([ + (request: FastifyRequest, reply: FastifyReply) => { + expectType(request) + expectType(reply) + return Promise.resolve() + }, + ], { relation: 'or', run: 'all' }) + app.auth([ + (request: FastifyRequest, reply: FastifyReply) => { + expectType(request) + expectType(reply) + return Promise.resolve() + }, + ], { relation: 'and', run: 'all' }) + + // this context tests + app.auth([ + function (this: FastifyInstance) { expectType(this) }, ]) + app.auth([ + function (this: FastifyInstance) { + expectType(this) + return Promise.resolve() + }, + ]) + app.auth([ + async function (this: FastifyInstance) { + expectType(this) + await Promise.resolve() + }, + ]) + const auth = app.auth([() => {}]) expectType(auth) app.get('/secret', { preHandler: auth }, () => {}) From 34424f296f37108149d175837904498587a5dcaa Mon Sep 17 00:00:00 2001 From: Ozer Date: Thu, 15 May 2025 01:35:07 +0300 Subject: [PATCH 2/3] tests with { relation: 'or' } added, defaultRelation is already 'or' but defaultRelation definition can be changed. --- types/index.test-d.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 8211e34..00535c1 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -10,6 +10,13 @@ type Done = (error?: Error) => void app.register(fastifyAuth).after((_err) => { // Callback tests + app.auth([ + (request, reply, done) => { + expectType(request) + expectType(reply) + expectType(done) + }, + ], { relation: 'or' }) app.auth([ (request, reply, done) => { expectType(request) @@ -47,6 +54,13 @@ app.register(fastifyAuth).after((_err) => { await Promise.resolve() }, ]) + app.auth([ + async (request: FastifyRequest, reply: FastifyReply) => { + expectType(request) + expectType(reply) + await Promise.resolve() + }, + ], { relation: 'or' }) app.auth([ async (request: FastifyRequest, reply: FastifyReply) => { expectType(request) @@ -84,6 +98,13 @@ app.register(fastifyAuth).after((_err) => { return Promise.resolve() }, ]) + app.auth([ + (request: FastifyRequest, reply: FastifyReply) => { + expectType(request) + expectType(reply) + return Promise.resolve() + }, + ], { relation: 'or' }) app.auth([ (request: FastifyRequest, reply: FastifyReply) => { expectType(request) From 2e00f04a8705c0e10941f28a1e87ac62deca0378 Mon Sep 17 00:00:00 2001 From: Ozer Date: Thu, 15 May 2025 21:49:56 +0300 Subject: [PATCH 3/3] type seperated to FastifyAuthSyncFunction and FastifyAuthAsyncFunction, then FastifyAuthFunction updated according to it. --- types/index.d.ts | 33 ++++++++++++++++++++++++++++----- types/index.test-d.ts | 7 ++++--- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index dddf1fa..99af7f2 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -29,12 +29,35 @@ type FastifyAuth = FastifyPluginCallback declare namespace fastifyAuth { export type FastifyAuthRelation = 'and' | 'or' + export type FastifyAuthSyncFunction< + Request extends FastifyRequest = FastifyRequest, + Reply extends FastifyReply = FastifyReply + > = ( + this: FastifyInstance, + request: Request, + reply: Reply, + done: (error?: Error) => void + ) => void + + export type FastifyAuthAsyncFunction< + Request extends FastifyRequest = FastifyRequest, + Reply extends FastifyReply = FastifyReply + > = ( + this: FastifyInstance, + request: Request, + reply: Reply + ) => Promise + export type FastifyAuthFunction< - Request extends FastifyRequest = FastifyRequest, - Reply extends FastifyReply = FastifyReply - > = - | ((this: FastifyInstance, request: Request, reply: Reply, done: (error?: Error) => void) => void) - | ((this: FastifyInstance, request: Request, reply: Reply) => Promise) + Request extends FastifyRequest = FastifyRequest, + Reply extends FastifyReply = FastifyReply, + Return extends + | ReturnType> + | ReturnType> + = ReturnType> + > = Return extends ReturnType> + ? FastifyAuthSyncFunction + : FastifyAuthAsyncFunction /** * @link [`fastify-auth` options documentation](https://github.com/fastify/fastify-auth#options) diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 00535c1..158f9f7 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -136,18 +136,19 @@ app.register(fastifyAuth).after((_err) => { // this context tests app.auth([ - function (this: FastifyInstance) { + async function () { expectType(this) + await Promise.resolve() }, ]) app.auth([ - function (this: FastifyInstance) { + function () { expectType(this) return Promise.resolve() }, ]) app.auth([ - async function (this: FastifyInstance) { + async function () { expectType(this) await Promise.resolve() },