Skip to content

Support constraints when registering plugins #3615

@ghost

Description

Prerequisites

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

🚀 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

fastify.register(require('./routes/v1/users'), { prefix: '/v1' }) // currently supported
fastify.register(require('./routes/v1/users'), { constraints: { host: 'auth.fastify.io' } }) // not supported
fastify.register(require('./routes/apple'), { constraints: { host: 'apple.fastify.io' } })
fastify.register(require('./routes/berry'), { constraints: { host: 'berry.fastify.io' } })
GET https://apple.fastify.io/
  200 => I LOVE APPLES

GET https://berry.fastify.io/
  200 => I LOVE BERRIES

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions