fastify / fastify/fastify-auth
Extend FastifyAuthFunction type to accept async functions
- Dominant language
- JavaScript
- Stars
- 380
- Forks
- 58
- PR merge metrics
- No merged PRs in 30d
Description
### Prerequisites
- [x] I have written a descriptive issue title
- [x] I have searched existing issues to ensure the feature has not already been requested
### 🚀 Feature Proposal
## Context
The `FastifyAuthFunction` type provided only covers the callback-style function.
This library supports both callback-style and promise-based functions.
> [!NOTE] Not sure if this should be filed as bug. The feature is implemented for promise-based functions, it's just missing the types I need. Feel free to move this to appropriate place. 🙂
## Proposal
Extend the `FastifyAuthFunction` type to be a union of the callback-style functions and promise-based functions.
Something like:
```ts
import type { FastifyInstance, FastifyRequest, FastifyReply, HookHandlerDoneFunction } from "fastify";
...
export FastifyAuthFunction =
| ((this: FastifyInstance, request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction) => void)
| ((this: FastifyInstance, request: FastifyRequest, reply: FastifyReply) => Promise);
```
### Motivation
When creating several authentication strategies, I want their functions to have consistent signature. I started using the promise-based APIs in fastify and when I used this library, I found that typescript complains about a 4th argument when I have some tests calling the strategy function like this:
```ts
await strategyFn.call(mockFastifyInstance, mockRequest, mockReply); // Expects a 4th argument
```
This matches the current type definition wherein we should provide a `done` handler.
However, that's not required for async functions.
### Example
```ts
await strategyFn.call(mockFastifyInstance, mockRequest, mockReply); // valid in TS
```
Contributor guide
Assessment
This issue has not been assessed yet.