npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

header-constraint-strategy

v2.0.0

Published

a find-my-way header constraint

Downloads

282

Readme

header-constraint-strategy

JavaScript Style Guide Build Status

A general purpose find-my-way custom constraint strategy.

Tested for Fastify ✅!

This module let you to drive the incoming HTTP request into a route based on the header's strict content. Doing so, if a request has a specific string header, it can reach a route hide behind a constraint. Go to the Usage section to get a complete overview of this feature!

Install

npm install header-constraint-strategy

Usage with Fastify

Here all the constraint types you can define with this module!
This setup shows you all the settings header-constraint-strategy provides to you.

const headerConstraintStrategy = require('header-constraint-strategy')
const Fastify = require('fastify')

// STEP 1: setup the constraints into your fastify instance
const app = Fastify({
  constraints: {
    // basic usage
    foo: headerConstraintStrategy('foo'),
    // strict usage
    mustBeIn: headerConstraintStrategy({ header: 'mustBeIn', mustMatchWhenDerived: true }),
    // custom header usage
    appOption: headerConstraintStrategy({ name: 'appOption', header: 'x-my-app' })
  }
})

// STEP 2: use the constraint where you need them
app.get('/', {
  handler: reply('no constraint')
})

app.get('/', {
  handler: reply('foo'),
  constraints: {
    foo: 'bar'
  }
})

app.get('/', {
  handler: reply('mustBeIn'),
  constraints: {
    mustBeIn: '123'
  }
})

app.get('/', {
  handler: reply('appOption'),
  constraints: {
    appOption: 'ABC'
  }
})

app.get('/', {
  handler: reply('mustBeIn and appOption'),
  constraints: {
    mustBeIn: '123',
    appOption: 'ABC'
  }
})

app.listen(80)

The routes can be reached via an HTTP request with these headers.

| # | foo header | mustBeIn header | x-my-app header | response | |---|--------------|-------------------|-------------------|----------| |1| - | - | - | 200 - no constraint |2| bar | - | - | 200 - foo |3| hello | - | - | 200 - no constraint |4| - | 123 | - | 200 - mustBeIn |5| - | 456 | - | 404 |6| - | - | ABC | 200 - appOption |7| - | 123 | ABC | 200 - mustBeIn and appOption |8| - | ops | ABC | 404 |9| bar | 123 | ABC | 200 - mustBeIn and appOption |10| bar | ops | ABC | 404

Cases explanation

  1. When the is not headers that meets the constraint, the route without constraint will be used if set. Otherwise 404
  2. The foo constraint is matched
  3. The foo constraint is not matched so the route without constraint is used
  4. The mustBeIn constraint is matched
  5. Like the 4), but this time the route without constraint is no used because of the flag mustMatchWhenDerived: true
  6. The appOption constraint is matched
  7. Multiple constraint matches
  8. Regardless the appOption constraint is matched, the mustBeIn constraint with mustMatchWhenDerived=true forces the handler to be used
  9. When there are multiple matches (the route with foo and route with mustBeIn and appOption), the route with more fulfilled constraint wins!
  10. As the 8), the mustBeIn constraint is not fulfilled

Options

You can pass the following options during the registration:

| Option | Default | Description | |--------|---------|-------------| |name| as the header if not set | The name of the JSON property that you will set in the route's constraints option |header| as the name if not set | The HTTP header where read the input to match the constraint |mustMatchWhenDerived| false | Define if the same route without constraint must be evaluated for the routing. You can translate it as: is this constraint mandatory?

License

Copyright Manuel Spigolon, Licensed under MIT.