-
-
Notifications
You must be signed in to change notification settings - Fork 21
Refactor/leverage getdecorator api #102
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
base: main
Are you sure you want to change the base?
Refactor/leverage getdecorator api #102
Conversation
|
Started from an existing branch before pulling, which caused this commit history mess. |
I need several feedback on the style, this is why I ask for 3 reviews. |
function verifyAccess (this: FastifyRequest, reply: FastifyReply, role: string) { | ||
if (!this.session.user.roles.includes(role)) { | ||
reply.status(403).send('You are not authorized to access this resource.') | ||
function createChecker () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function createChecker () { | |
function createManager () { |
// you should decorate the request. | ||
// This reduces coupling with the authentication strategy. | ||
// If you change or add new authentication strategies in the future, | ||
// `request.auth` remains the single source of truth. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// `request.auth` remains the single source of truth. | |
// `request[kAuth]` remains the single source of truth. |
@Eomm @mcollina @gurgunday I understand that the official demo needs to represent the current state of Fastify's practices. Maybe I can add some comments to explain the alternatives to |
Implement #99
This change uses the new
getDecorator
API to reduce module augmentation and improve both type safety and runtime safety.Reference: https://fastify.dev/docs/latest/Reference/Decorators/#getdecoratort-api
There are other ways to ensure runtime safety with decorators, but using TypeScript, this approach it easier as perform static and runtime checks in one place.
I used Symbol to name decorators, don't know what you think about that.