Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Decorater Hook #5274

Open
2 tasks done
matthyk opened this issue Jan 17, 2024 · 4 comments
Open
2 tasks done

feat: Decorater Hook #5274

matthyk opened this issue Jan 17, 2024 · 4 comments
Labels
feature request New feature to be added

Comments

@matthyk
Copy link
Contributor

matthyk commented Jan 17, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

馃殌 Feature Proposal

Hooks called onDecorate, onDecorateRequest and onDecorateReply, which is triggered each time decorate, decorateRequest, decorateReply is called.

Motivation

Very interesting ideas could be implemented with this feature. For example:

  • Plugin that automatically generate a types.d.ts file that provides types for all decorators, which is useful even for js projects

Example

import Fastify from 'fastify'

const app = Fastify()

app.addHook('onDecorate', function(instance, name, fn) {
   console.log(`Fastify instance got decorated with a property named ${name} of type ${typeof fn}`)
})

app.decorate('test', () => 'Test')
@mcollina mcollina added the feature request New feature to be added label Jan 17, 2024
@mcollina
Copy link
Member

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

@climba03003
Copy link
Member

Gathering information for diagnostics is the only usage I can think of.

Gathering for types generation is not realistic since you need to at least start the server before it can generate the types.

@metcoder95
Copy link
Member

I like the idea but have concerns about its implications, like when and how to run it.
Usually, the number of decorations is bigger than the number of hooks per app.

Every time a decoration happens, we will need to run the hooks at least once (although we do that already with onRoute, I doubt the cardinality will be the same with decorate and its variants; I imagine it will be bigger).

@matthyk
Copy link
Contributor Author

matthyk commented Jan 20, 2024

https://github.com/Eomm/fastify-overview is a good example of how such a hook could be used.

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

No branches or pull requests

4 participants