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
Support constraints when registering plugins #3615
Support constraints when registering plugins #3615
Comments
This would be awesome to add. Would you like to send a PR? |
I'm slammed this month, but I can definitely take a stab hopefully sometime early February. Do you have any pointers or guidance on where or how it should be implemented? Would it work similar to how the prefixes work, or do you think it would be different? Or would we want to implement it in a more generic way so you could actually pass any route options during register versus specific ones (I don't know if that would actually make sense or even work :P)? |
This is already doable within a plugin that listens for the
I have always been afraid to add new https://www.fastify.io/docs/latest/Reference/Plugins/#plugin-options |
@Eomm Very interesting! I've actually never used the EDIT: To clarify, does the register for the route also need to happen within the hook, or can it happen outside but within the same scope? e.g. fastify.register(async function plugin (instance) {
instance.addHook('onRoute', function hook (routeOptions) {
routeOptions.constraints = { host: 'berry.fastify.io' }
})
instance.register(require('./routes/berry'))
}) |
Good spot! All of this could a also be automated with a combo of |
Not at all: it is the infinite possibilities fastify offers
Yes, it must be the same scope (using the |
Are we sure that we need a new plugin for this? So this means new repo right? |
Yes
Yes, most likely this would be a community plugin.
Create the plugin, submit PR for adding it as community plugin. |
Hi! I'm currently working on creating the mentioned plugin and will submit a PR to add it to community plugins as soon as it's working |
@Eomm @climba03003 you may want to take a look at the implementation we're putting together here nearform/fastify-constraints#1 |
Hi! We have released v1.0.0 of nearform/fastify-constraints. Maybe you can check it to decide if it resolves this issue. |
@Ceres6 |
@climba03003 |
Prerequisites
🚀 Feature Proposal
When registering a plugin, you can currently pass a prefix that apply to all routes in the plugin. It'd be awesome to have that same functionality, but with a version or host constraint instead of a path prefix. I don't believe there is a simple way to do this at the moment without manually passing through options to every route, but I'd love to be wrong!
Motivation
We have a few subdomain specific APIs within our app, so rather than manually passing through options to the plugin and then passing them into each route within the plugin, it'd be really nice to simply set a constraint when registering the plugin itself and have all the routes within the plugin inherit it.
Example
The text was updated successfully, but these errors were encountered: