Skip to content

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

Open
wants to merge 21 commits into
base: main
Choose a base branch
from

Conversation

jean-michelet
Copy link
Member

@jean-michelet jean-michelet commented Apr 15, 2025

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.

@jean-michelet
Copy link
Member Author

Started from an existing branch before pulling, which caused this commit history mess.
Will squash commits properly before merging.

@jean-michelet
Copy link
Member Author

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 () {
Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested change
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.
Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested change
// `request.auth` remains the single source of truth.
// `request[kAuth]` remains the single source of truth.

@jean-michelet
Copy link
Member Author

@Eomm @mcollina @gurgunday
Not interested in this feature?

I understand that the official demo needs to represent the current state of Fastify's practices.
But it can also be a way for people to discover new features and ways to enjoy Fastify.

Maybe I can add some comments to explain the alternatives to getDecorator api? Like module augmentation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant