
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
fastify-constraints
Advanced tools
fastify-constraintsFastify plugin to add constraints to multiple routes
npm i fastify-constraints
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()
// Register the plugin
await fastify.register(fastifyConstraints)
// Register plugins containing routes with the constraints
await fastify.register(await (instance, opts) => 
  instance.get('/', () => 'Hello from version 1.0.0'), // Add a route
  { constraints: { version: '1.0.0' } }
)
// Start the server
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') // This route will have the version constraint
  fastify.get('/host-restricted', { constraints: { host: 'example.com' } }, () => 'Hello from example.com') // This route will have the version and host constraint
}, {
  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.
See the examples folder for a complete examples.
FAQs
Fastify plugin to add constraints to multiple routes
The npm package fastify-constraints receives a total of 287 weekly downloads. As such, fastify-constraints popularity was classified as not popular.
We found that fastify-constraints demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.