https://github.com/marcbachmann/http-condition
Support conditions for http requests based on a simple dsl.
https://github.com/marcbachmann/http-condition
code-generation condition http nodejs request server
Last synced: about 1 year ago
JSON representation
Support conditions for http requests based on a simple dsl.
- Host: GitHub
- URL: https://github.com/marcbachmann/http-condition
- Owner: marcbachmann
- License: mit
- Created: 2017-09-29T02:44:47.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T02:58:48.000Z (over 2 years ago)
- Last Synced: 2025-01-07T20:08:38.501Z (about 1 year ago)
- Topics: code-generation, condition, http, nodejs, request, server
- Language: JavaScript
- Homepage:
- Size: 5.86 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# http-condition
Support conditions for http requests based on a simple dsl.
This is an attempt on simplifying some configurations/middlewares in a node server.
```js
const httpCondition = require('http-condition')
const languages = ['de', 'fr', 'en']
const patterns = languages.map((l) => `url^:/${l}`)
const getLanguage = httpCondition(patterns, languages)
function (req, res, next) {
const language = getLanguage(req)
// language is one of 'de', 'fr', 'en' or undefined if it doesn't match
}
```
Currently supported directives:
- 'accept' (only does string matching, no parsing & priorization)
- 'url'
- 'user-agent'
- 'accept-language' (only does string matching, no parsing & priorization)
Selectors:
pattern: {{directive}}{{operator}}:{{value}}
- url*:url-that-contains-this
- url^:/url-that-starts-with-this
- url$:url-that-ends-with-this
- url:/url-that-exactly-matches
```js
const _ = require('lodash')
const httpCondition = require('http-condition')
// Allows more complex expressions. Check https://npm.im/boolean-expression
const languages = [
['url^:/de OR accept-language:de', {content: 'German content'}],
['url^:/en OR accept-language:en', {content: 'English content'}]
]
const getLanguage = httpCondition(_.map(languages, 0), _.map(languages, 1))
function (req, res, next) {
const language = getLanguage(req)
// language is one of
// {content: 'German content'}
// or
// {content: 'English content'}
}
```