diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b483640 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.DS_Store +node_modules +npm-debug.log +*.chproj +.settings.xml +API.json +API.html diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..89dfe1e --- /dev/null +++ b/lib/index.js @@ -0,0 +1,19 @@ +// Copyright 2012 Mark Lussier All rights reserved. + +var fs = require('fs'); +var path = require('path'); + +// Exports + +module.exports = {}; + +// Just load up all the JS in this directory and export it. +fs.readdirSync(__dirname + "/plugins").forEach(function (file) { + if (!/.+\.js$/.test(file)) + return; + + var plugin = require('./' + path.basename(file, '.js')); + + if (plugin && plugin.name) + module.exports[plugin.name] = plugin; +}); diff --git a/lib/plugins/version_parser.js b/lib/plugins/version_parser.js new file mode 100644 index 0000000..d35c243 --- /dev/null +++ b/lib/plugins/version_parser.js @@ -0,0 +1,23 @@ +// Copyright 2012 Mark Lussier, All rights reserved. + +/** + * Returns a plugin that will extract the api version from the Accept header in an very + * opinionated form. ie: Accept: application/vnd.intabulas+json; version >= 1.0 + * + * @param {String} optional regular expression to override the default + * @return {Function} restify handler. + */ +function versionParser(expression) { + if (!expression) + expression = /;[ ]*version[ :=]+([^$;]+)/i + + return function parseAcceptVersion(req, res, next) { + + api_version = req.headers['accept'].match(expression); + if ( api_version) + req.headers['X-Api-Version'] = api_version[1]; + return next(); + }; +} + +module.exports = versionParser; diff --git a/package.json b/package.json new file mode 100644 index 0000000..b2f505d --- /dev/null +++ b/package.json @@ -0,0 +1,7 @@ +{ "name" : "restify-moreversion" +, "version" : "0.0.1" +, "description" : "Restify middleware that will extract the API version from the Accept header" + "main": "lib/index.js", + "directories": { "lib": "./lib"} +, "repository" : "git://github.com/intabulas/restify-plugins.git" +} \ No newline at end of file