├── .gitignore ├── README.md ├── hapiDay2014.pdf ├── index.js ├── lib ├── index.js └── modules │ └── info │ ├── index.js │ ├── package.json │ └── version │ ├── index.js │ ├── v1 │ └── index.js │ └── v2 │ └── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Using hapi plugins to version your API 2 | ================= 3 | 4 | A talk at hapiDays, JS Fest Oakland Dec 11th, 2014 5 | 6 | View slides: http://www.slideshare.net/shakefon/hapidays-2014 7 | 8 | Run the example by cloning this repo, then `npm install` followed by `node index.js` 9 | 10 | The cURL commands in the slides should then work for you. Or if you prefer, look at it in the browser - http://localhost:9000 11 | 12 | Twitter: @dstevensio 13 | 14 | Email: ds@dstevens.io 15 | -------------------------------------------------------------------------------- /hapiDay2014.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dstevensio/hapiday14/5e0ae069473a76aaa1bd04a5bd52cc37ea171a9c/hapiDay2014.pdf -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | require('./lib'); -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | var Glue = require('glue'); 2 | 3 | var manifest = { 4 | connections: [ 5 | { 6 | port: 9000 7 | } 8 | ], 9 | plugins: { 10 | "./info": null 11 | } 12 | }; 13 | 14 | var options = { 15 | relativeTo: process.cwd() + '/lib/modules' 16 | }; 17 | 18 | Glue.compose(manifest, options, function (err, server) { 19 | if (err) throw err; 20 | 21 | server.start(function (err) { 22 | 23 | console.log('Server running on port 9000'); 24 | 25 | }); 26 | }); -------------------------------------------------------------------------------- /lib/modules/info/index.js: -------------------------------------------------------------------------------- 1 | exports.register = function (server, options, next) { 2 | 3 | server.route({ 4 | path: '/version', 5 | method: 'GET', 6 | handler: require('./version') 7 | }); 8 | 9 | server.route({ 10 | path: '/{apiVersion}/version', 11 | method: 'GET', 12 | handler: require('./version') 13 | }); 14 | 15 | next(); 16 | 17 | }; 18 | 19 | exports.register.attributes = { 20 | pkg: require('./package.json') 21 | }; -------------------------------------------------------------------------------- /lib/modules/info/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "exampleInfo", 3 | "version": "1.0.0" 4 | } -------------------------------------------------------------------------------- /lib/modules/info/version/index.js: -------------------------------------------------------------------------------- 1 | module.exports = function (request, reply) { 2 | 3 | // Versioned by URI route 4 | if (request.params.apiVersion && (/^v[0-9]+$/).test(request.params.apiVersion) ) { 5 | return require('./' + request.params.apiVersion)(request, reply); 6 | } 7 | 8 | // Versioned by customer request header 9 | if (request.headers['api-version'] && (/^[0-9]+$/).test(request.headers['api-version']) ) { 10 | return require('./v' + request.headers['api-version'])(request, reply); 11 | } 12 | 13 | // Versioned by accept header 14 | if (request.headers['accept'] && (/^application\/vnd\.exampleapi\.v[0-9]+$/).test(request.headers['accept']) ) { 15 | 16 | var version = request.headers['accept'].replace('application/vnd.exampleapi.', ''); 17 | 18 | return require('./' + version)(request, reply); 19 | } 20 | 21 | // Default gets original API, to be backwards compatible 22 | return require('./v1')(request, reply); 23 | 24 | }; 25 | 26 | -------------------------------------------------------------------------------- /lib/modules/info/version/v1/index.js: -------------------------------------------------------------------------------- 1 | module.exports = function (request, reply) { 2 | 3 | reply({ 4 | name: "exampleAPI", 5 | version: "1" 6 | }); 7 | 8 | }; -------------------------------------------------------------------------------- /lib/modules/info/version/v2/index.js: -------------------------------------------------------------------------------- 1 | module.exports = function (request, reply) { 2 | 3 | reply({ 4 | statusCode: 200, 5 | applicationDetail: { 6 | name: "exampleAPI", 7 | version: "2" 8 | } 9 | }); 10 | 11 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hapiday2014", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "dependencies": { 7 | "glue": "^2.0.0-rc4" 8 | }, 9 | "devDependencies": {}, 10 | "scripts": { 11 | "test": "echo \"Error: no test specified\" && exit 1" 12 | }, 13 | "author": "Dave Stevens ", 14 | "license": "ISC" 15 | } 16 | --------------------------------------------------------------------------------