Skip to content
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 () {
Copy link

Copilot AI Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The async function is missing the explicit 'this: FastifyInstance' type annotation that was mentioned as necessary in the PR description. This should be 'async function (this: FastifyInstance) {' to match the requirement for TypeScript's inability to infer 'this' context in union types.

Suggested change
async function () {
async function (this: FastifyInstance) {

Copilot uses AI. Check for mistakes.

expectType<FastifyInstance>(this)
await Promise.resolve()
},
])
app.auth([
function () {
Copy link

Copilot AI Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function is missing the explicit 'this: FastifyInstance' type annotation that was mentioned as necessary in the PR description. This should be 'function (this: FastifyInstance) {' to match the requirement for TypeScript's inability to infer 'this' context in union types.

Suggested change
function () {
function (this: FastifyInstance) {

Copilot uses AI. Check for mistakes.

expectType<FastifyInstance>(this)
return Promise.resolve()
},
])
app.auth([
async function () {
Copy link

Copilot AI Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The async function is missing the explicit 'this: FastifyInstance' type annotation that was mentioned as necessary in the PR description. This should be 'async function (this: FastifyInstance) {' to match the requirement for TypeScript's inability to infer 'this' context in union types.

Suggested change
async function () {
async function (this: FastifyInstance) {

Copilot uses AI. Check for mistakes.

expectType<FastifyInstance>(this)
await Promise.resolve()
},
])

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