https://github.com/flesch/stages
A small utility to parse the `Accept` request header into an array of "stages".
https://github.com/flesch/stages
accept feature header javascript node npm preview request stage toggle
Last synced: 2 months ago
JSON representation
A small utility to parse the `Accept` request header into an array of "stages".
- Host: GitHub
- URL: https://github.com/flesch/stages
- Owner: flesch
- License: mit
- Created: 2018-10-30T15:26:59.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-10-30T15:35:27.000Z (over 7 years ago)
- Last Synced: 2025-08-09T16:46:34.202Z (11 months ago)
- Topics: accept, feature, header, javascript, node, npm, preview, request, stage, toggle
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/stages
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# stages
This is a small utility to parse the `Accept` header into an array of "stages", allowing a client to opt-in to functionality, like [GitHub uses in the GraphQL API](https://developer.github.com/v4/previews/).
### Install
```shell
$ npm install --save stages
```
### Usage
#### `stages(acceptHeaderString, regexPattern)`
**stages** accepts two arguments: the `Accept` header string, and a pattern to match against.
```js
const stages = require('stages');
const accept = '*/*,application/vnd.github.antiope-preview+json,application/vnd.github.echo-preview+json';
const pattern = /application\/vnd\..*\.(.*)\+json/;
console.log(stages(accept, pattern)); // ==> ['antiope-preview', 'echo-preview']
```
Use **stages** in an Express app:
```diff
const express = require('express');
+ const stages = require('stages');
const app = express();
+ app.use((req, res, next) => {
+ req.stages = stages(req.headers.accept, /application\/vnd\..*\.(.*)\+json/);
+ next();
+ });
app.get('/', (req, res, next) => {
- return res.send('Hello');
+ if (req.stages.includes('echo-preview')) {
+ return res.send(`The echo-preview stage is enabled!`);
+ }
+ res.send(req.stages.length ? `The stages enabled are: ${req.stages.join(', ')}` : `No stages are enabled!`);
});
app.listen(3000);
+ // curl --header "Accept: application/vnd.github.echo-preview+json" http://localhost:3000/
```