types: extended FastifyAuthFunction type to accept async functions#260
types: extended FastifyAuthFunction type to accept async functions#260ozers wants to merge 5 commits intofastify:mainfrom
Conversation
…but defaultRelation definition can be changed.
|
Afaik this is not working properly. Basically you get a "merged" function. Basically ((this: FastifyInstance, request: Request, reply: Reply, done: (error?: Error) => void) => void)
| ((this: FastifyInstance, request: Request, reply: Reply) => Promise<void>)results in ((this: FastifyInstance, request: Request, reply: Reply, done?: (error?: Error) => void) => (void | Promise<void)compare with |
…n, then FastifyAuthFunction updated according to it.
|
Thanks for your comment @Uzlopak , I updated the PR after reviewing the hook link you mentioned and now it looks better and meets the issues mentioned above. |
There was a problem hiding this comment.
Pull Request Overview
This PR extends the FastifyAuthFunction type to accept async functions in addition to the existing callback-based functions. The change allows authentication functions to use modern async/await patterns or return Promises directly, providing better TypeScript support for asynchronous authentication workflows.
- Extended
FastifyAuthFunctiontype to support async functions and Promise-returning functions - Added comprehensive TypeScript tests covering callback, async, and Promise-based authentication patterns
- Added tests for various configuration options (
relation: 'or'/'and',run: 'all')
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
Checklist
npm run testandnpm run benchmarkand the Code of conduct
All tests are passing:
I would like to open a PR for this issue due to contribute if you allow me.
I've made the the update and updated the tests in this context. Due to TypeScript's inability to automatically infer 'this' context in union types, needed to explicitly specify 'this: FastifyInstance' type annotation in functions that use 'this'. I've updated the tests to reflect this requirement, also added async and promise-based tests to cover the update.
In the function below, which is used without specifying a pre-usable type, the need to use 'this: FastifyInstance' arises due to the situation I mentioned above.
Changes made:
Please review the changes.