Skip to content

types: extended FastifyAuthFunction type to accept async functions #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,36 @@ type FastifyAuth = FastifyPluginCallback<fastifyAuth.FastifyAuthPluginOptions>
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,
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<void>

export type FastifyAuthFunction<
Request extends FastifyRequest = FastifyRequest,
Reply extends FastifyReply = FastifyReply,
Return extends
| ReturnType<FastifyAuthSyncFunction<Request, Reply>>
| ReturnType<FastifyAuthAsyncFunction<Request, Reply>>
= ReturnType<FastifyAuthSyncFunction<Request, Reply>>
> = Return extends ReturnType<FastifyAuthSyncFunction<Request, Reply>>
? FastifyAuthSyncFunction<Request, Reply>
: FastifyAuthAsyncFunction<Request, Reply>

/**
* @link [`fastify-auth` options documentation](https://github.com/fastify/fastify-auth#options)
*/
Expand Down
119 changes: 119 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ const app = fastify()
type Done = (error?: Error) => void

app.register(fastifyAuth).after((_err) => {
// Callback tests
app.auth([
(request, reply, done) => {
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
expectType<Done>(done)
},
], { relation: 'or' })
app.auth([
(request, reply, done) => {
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
expectType<Done>(done)
},
], { relation: 'and' })
app.auth([
(request, reply, done) => {
expectType<FastifyRequest>(request)
Expand All @@ -29,12 +37,123 @@ app.register(fastifyAuth).after((_err) => {
expectType<FastifyReply>(reply)
expectType<Done>(done)
},
], { relation: 'or', run: 'all' })
app.auth([
(request, reply, done) => {
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
expectType<Done>(done)
},
], { relation: 'and', run: 'all' })

// Async function tests
app.auth([
async (request: FastifyRequest, reply: FastifyReply) => {
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
await Promise.resolve()
},
])
app.auth([
async (request: FastifyRequest, reply: FastifyReply) => {
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
await Promise.resolve()
},
], { relation: 'or' })
app.auth([
async (request: FastifyRequest, reply: FastifyReply) => {
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
await Promise.resolve()
},
], { relation: 'and' })
app.auth([
async (request: FastifyRequest, reply: FastifyReply) => {
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
await Promise.resolve()
},
], { run: 'all' })
app.auth([
async (request: FastifyRequest, reply: FastifyReply) => {
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
await Promise.resolve()
},
], { relation: 'or', run: 'all' })
app.auth([
async (request: FastifyRequest, reply: FastifyReply) => {
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
await Promise.resolve()
},
], { relation: 'and', run: 'all' })

// Promise-based function tests
app.auth([
(request: FastifyRequest, reply: FastifyReply) => {
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
return Promise.resolve()
},
])
app.auth([
(request: FastifyRequest, reply: FastifyReply) => {
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
return Promise.resolve()
},
], { relation: 'or' })
app.auth([
(request: FastifyRequest, reply: FastifyReply) => {
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
return Promise.resolve()
},
], { relation: 'and' })
app.auth([
(request: FastifyRequest, reply: FastifyReply) => {
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
return Promise.resolve()
},
], { run: 'all' })
app.auth([
(request: FastifyRequest, reply: FastifyReply) => {
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
return Promise.resolve()
},
], { relation: 'or', run: 'all' })
app.auth([
(request: FastifyRequest, reply: FastifyReply) => {
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
return Promise.resolve()
},
], { relation: 'and', run: 'all' })

// this context tests
app.auth([
async function () {
expectType<FastifyInstance>(this)
await Promise.resolve()
},
])
app.auth([
function () {
expectType<FastifyInstance>(this)
return Promise.resolve()
},
])
app.auth([
async function () {
expectType<FastifyInstance>(this)
await Promise.resolve()
},
])

const auth = app.auth([() => {}])
expectType<preHandlerHookHandler>(auth)
app.get('/secret', { preHandler: auth }, () => {})
Expand Down