
fastify-constraints
Fastify plugin to add constraints to multiple routes
Install
npm i fastify-constraints
Usage
Register fastify-constraints as a Fastify plugin.
The plugin will create an onRoute hook that will add the constraints to the routes.
Contstraints should be specified in the constraints property of the plugin options.
To see available constraints refer to the Fastify documentation.
import Fastify from 'fastify'
import fastifyConstraints from 'fastify-constraints'
const fastify = Fastify()
await fastify.register(fastifyConstraints)
await fastify.register(await (instance, opts) =>
instance.get('/', () => 'Hello from version 1.0.0'),
{ constraints: { version: '1.0.0' } }
)
await fastify.listen({ port: 3000 })
You can also specify constraints for specific routes which will be merged:
await fastify.register(fastifyConstraints)
await fastify.register(async (instance, opts) => {
fastify.get('/', () => 'Hello from version 1.0.0')
fastify.get('/host-restricted', { constraints: { host: 'example.com' } }, () => 'Hello from example.com')
}, {
constraints: { version: '1.0.0' }
})
Note: In case of collision the constraints specified in the route will take precedence over the constraints specified in the plugin options.
Examples
See the examples folder for a complete examples.