{"id":22485125,"url":"https://github.com/Eomm/header-constraint-strategy","last_synced_at":"2025-08-02T18:31:31.752Z","repository":{"id":36988495,"uuid":"383202375","full_name":"Eomm/header-constraint-strategy","owner":"Eomm","description":"A general purpose find-my-way custom constraint strategy","archived":false,"fork":false,"pushed_at":"2025-04-30T00:19:00.000Z","size":34,"stargazers_count":6,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-16T11:19:37.090Z","etag":null,"topics":["constraints","fastify","fastify-plugin","find-my-way","routing"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Eomm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"Eomm"}},"created_at":"2021-07-05T16:28:04.000Z","updated_at":"2025-04-30T00:18:58.000Z","dependencies_parsed_at":"2023-12-27T01:33:50.611Z","dependency_job_id":"fc5c8fb5-4dc9-4767-bc9a-da925424c5f3","html_url":"https://github.com/Eomm/header-constraint-strategy","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":"Eomm/fastify-plugin-template","purl":"pkg:github/Eomm/header-constraint-strategy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Fheader-constraint-strategy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Fheader-constraint-strategy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Fheader-constraint-strategy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Fheader-constraint-strategy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Eomm","download_url":"https://codeload.github.com/Eomm/header-constraint-strategy/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Fheader-constraint-strategy/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268432792,"owners_count":24249619,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-02T02:00:12.353Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["constraints","fastify","fastify-plugin","find-my-way","routing"],"created_at":"2024-12-06T17:12:02.817Z","updated_at":"2025-08-02T18:31:31.474Z","avatar_url":"https://github.com/Eomm.png","language":"JavaScript","funding_links":["https://github.com/sponsors/Eomm"],"categories":["JavaScript"],"sub_categories":[],"readme":"# header-constraint-strategy\n\n[![ci](https://github.com/Eomm/header-constraint-strategy/actions/workflows/ci.yml/badge.svg)](https://github.com/Eomm/header-constraint-strategy/actions/workflows/ci.yml)\n[![npm](https://img.shields.io/npm/v/header-constraint-strategy)](https://www.npmjs.com/package/header-constraint-strategy)\n[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n\nA general purpose [`find-my-way`](https://github.com/delvedor/find-my-way) custom [constraint strategy](https://www.fastify.io/docs/latest/Routes/#constraints).\n\nTested for [Fastify](https://github.com/fastify/fastify) ✅!\n\nThis module let you to drive the incoming HTTP request into a route based on the header's strict content.\nDoing so, if a request has a specific string header, it can reach a route hide behind a constraint.\nGo to the _Usage_ section to get a complete overview of this feature!\n\n\n## Install\n\n```\nnpm install header-constraint-strategy\n```\n\n\n## Usage with Fastify\n\nHere all the constraint types you can define with this module!  \nThis setup shows you all the settings `header-constraint-strategy` provides to you.\n\n```js\nconst headerConstraintStrategy = require('header-constraint-strategy')\nconst Fastify = require('fastify')\n\n// STEP 1: setup the constraints into your fastify instance\nconst app = Fastify({\n  constraints: {\n    // basic usage\n    foo: headerConstraintStrategy('foo'),\n    // strict usage\n    mustBeIn: headerConstraintStrategy({ header: 'mustBeIn', mustMatchWhenDerived: true }),\n    // custom header usage\n    appOption: headerConstraintStrategy({ name: 'appOption', header: 'x-my-app' })\n  }\n})\n\n// STEP 2: use the constraint where you need them\napp.get('/', {\n  handler: reply('no constraint')\n})\n\napp.get('/', {\n  handler: reply('foo'),\n  constraints: {\n    foo: 'bar'\n  }\n})\n\napp.get('/', {\n  handler: reply('mustBeIn'),\n  constraints: {\n    mustBeIn: '123'\n  }\n})\n\napp.get('/', {\n  handler: reply('appOption'),\n  constraints: {\n    appOption: 'ABC'\n  }\n})\n\napp.get('/', {\n  handler: reply('mustBeIn and appOption'),\n  constraints: {\n    mustBeIn: '123',\n    appOption: 'ABC'\n  }\n})\n\napp.listen(80)\n```\n\nThe routes can be reached via an HTTP request with these headers.\n\n| # | `foo` header | `mustBeIn` header | `x-my-app` header | response |\n|---|--------------|-------------------|-------------------|----------|\n|1| - | - | - | 200 - no constraint\n|2| `bar` | - | - | 200 - foo\n|3| `hello` | - | - | 200 - no constraint\n|4| - | `123` | - | 200 - mustBeIn\n|5| - | `456` | - | 404\n|6| - | - | `ABC` | 200 - appOption\n|7| - | `123` | `ABC` | 200 - mustBeIn and appOption\n|8| - | `ops` | `ABC` | 404\n|9| `bar` | `123` | `ABC` | 200 - mustBeIn and appOption\n|10| `bar` | `ops` | `ABC` | 404\n\n### Cases explanation\n\n1) When the is not headers that meets the constraint, the route without constraint will be used if set. Otherwise 404\n2) The `foo` constraint is matched\n3) The `foo` constraint is not matched so the route without constraint is used\n4) The `mustBeIn` constraint is matched\n5) Like the 4), but this time the route without constraint is no used because of the flag `mustMatchWhenDerived: true`\n6) The `appOption` constraint is matched\n7) Multiple constraint matches\n8) Regardless the `appOption` constraint is matched, the `mustBeIn` constraint with `mustMatchWhenDerived=true` forces the handler to be used\n9) When there are multiple matches _(the route with `foo` and route with `mustBeIn` and `appOption`)_, the route with more fulfilled constraint wins!\n10) As the 8), the `mustBeIn` constraint is not fulfilled\n\n\n## Options\n\nYou can pass the following options during the registration:\n\n| Option | Default | Description |\n|--------|---------|-------------|\n|`name`| as the header if not set | The name of the JSON property that you will set in the route's `constraints` option\n|`header`| as the name if not set | The HTTP header where read the input to match the constraint\n|`mustMatchWhenDerived`| `false` | Define if the same route without constraint must be evaluated for the routing. You can translate it as: is this constraint mandatory?\n\n\n## License\n\nCopyright [Manuel Spigolon](https://github.com/Eomm), Licensed under [MIT](./LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEomm%2Fheader-constraint-strategy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FEomm%2Fheader-constraint-strategy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEomm%2Fheader-constraint-strategy/lists"}