Skip to content

opentable/hapi-version-prereq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#Hapi-version-prereq Build Status NPM version Dependencies

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' }

About

detects version info in the url/headers and populates the prereq object

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •