https://github.com/opentable/hapi-version-prereq
detects version info in the url/headers and populates the prereq object
https://github.com/opentable/hapi-version-prereq
Last synced: 3 months ago
JSON representation
detects version info in the url/headers and populates the prereq object
- Host: GitHub
- URL: https://github.com/opentable/hapi-version-prereq
- Owner: opentable
- License: mit
- Created: 2014-10-29T12:27:26.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2017-06-30T15:41:34.000Z (almost 8 years ago)
- Last Synced: 2024-05-14T00:26:37.567Z (about 1 year ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 1
- Watchers: 26
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
#Hapi-version-prereq
[](https://travis-ci.org/opentable/hapi-version-prereq) [](http://badge.fury.io/js/hapi-version-prereq) Hapi plugin for automagically populating the version header on routes that support it. At OT we use a mix of url-versioning and header-versioning. In some cases our APIs need to support both. This module represents some shared code for our narrow use-case.
installation:
```npm install hapi-version-prereq```
usage:
```
var hapi = require("hapi");
var joi = require("joi");var server = Hapi.createServer('127.0.0.1', 3000, {});
server.route([{
method: 'GET',
path: '/v1/my-url',
config: {
handler: function(request, reply){
reply(request.pre.version)
}
}
},
{
method: 'GET',
path: '/my-url',
config: {
handler: function(request, reply){
reply(request.pre.version)
},
validate: {
headers: {
accept: joi.string().regex(/application\/vnd.myorg.mytype.v1\+json/)
}
}
}
}]);server.pack.register([
{
plugin: require('hapi-version-prereq'),
options: {}
}], function(err){
if(err){
throw err;
}server.start(function(){
server.log('server started');
});
});```
- `/v1/my-url => { version: 'v1', mode: 'url' }`
- `/my-url => { version: 'v1', mode: 'header' }`