diff --git a/types/index.d.ts b/types/index.d.ts index 2de3e06..99af7f2 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -29,9 +29,9 @@ type FastifyAuth = FastifyPluginCallback declare namespace fastifyAuth { export type FastifyAuthRelation = 'and' | 'or' - export type FastifyAuthFunction< - Request extends FastifyRequest = FastifyRequest, - Reply extends FastifyReply = FastifyReply + export type FastifyAuthSyncFunction< + Request extends FastifyRequest = FastifyRequest, + Reply extends FastifyReply = FastifyReply > = ( this: FastifyInstance, request: Request, @@ -39,6 +39,26 @@ declare namespace fastifyAuth { 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, + 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 44c44be..158f9f7 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -9,6 +9,7 @@ const app = fastify() type Done = (error?: Error) => void app.register(fastifyAuth).after((_err) => { + // Callback tests app.auth([ (request, reply, done) => { expectType(request) @@ -16,6 +17,13 @@ app.register(fastifyAuth).after((_err) => { expectType(done) }, ], { relation: 'or' }) + app.auth([ + (request, reply, done) => { + expectType(request) + expectType(reply) + expectType(done) + }, + ], { relation: 'and' }) app.auth([ (request, reply, done) => { expectType(request) @@ -29,12 +37,123 @@ 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([ + async (request: FastifyRequest, reply: FastifyReply) => { + expectType(request) + expectType(reply) + await Promise.resolve() + }, + ], { relation: 'or' }) + app.auth([ + 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: 'or' }) + 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([ + async function () { + expectType(this) + await Promise.resolve() + }, ]) app.auth([ function () { expectType(this) + return Promise.resolve() }, ]) + app.auth([ + async function () { + expectType(this) + await Promise.resolve() + }, + ]) + const auth = app.auth([() => {}]) expectType(auth) app.get('/secret', { preHandler: auth }, () => {})