connect-rest
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -6,12 +6,4 @@ /* | ||
*/ | ||
var VERSION = '0.0.2'; | ||
var VERSION = '0.0.3'; | ||
/* | ||
server.put('/hello', send); | ||
server.put('/hello', [send, send, send]); | ||
server.get('/hello/:name', send); | ||
server.get(/^\/([a-zA-Z0-9_\.~-]+)\/(.*)/, function(req, res, next) { | ||
server.get({path: PATH, version: '1.1.3'}, sendV1); | ||
*/ | ||
var util = require('util'); | ||
@@ -47,2 +39,19 @@ var async = require('async'); | ||
function discover( request, content ){ | ||
var version = request.parameters.version; | ||
var matchingMaps = {}; | ||
_.each(mapping, function(value, key, list){ | ||
matchingMaps[ key ] = []; | ||
_.each(value, function(element, index, list){ | ||
_.each( element.matchings( version, _, semver ), function(match, matchIndex, matchList){ | ||
matchingMaps[ key ].push( match ); | ||
}); | ||
}); | ||
}); | ||
return matchingMaps; | ||
} | ||
exports.head = function (path, functionRef){ | ||
@@ -68,4 +77,11 @@ addPath("HEAD", path, functionRef); | ||
exports.rester = function() { | ||
exports.rester = function( options ) { | ||
if( options ){ | ||
if( options.discoverPath ) | ||
addPath('GET', options.discoverPath + '/:version', discover ); | ||
} | ||
return function(req, res, next) { | ||
this.options = options || { }; | ||
var matching = _.map( _.filter( | ||
@@ -72,0 +88,0 @@ mapping[ req.method ], function(route){ return route.matches( |
@@ -29,4 +29,2 @@ /* | ||
Path.prototype.matches = function( req, uri, version, _, semver ){ | ||
console.log( uri + ' ' + version + ' ' ); | ||
if( this.isRegex ){ | ||
@@ -67,2 +65,12 @@ return this.path.test( uri ); | ||
Path.prototype.matchings = function( version, _, semver ){ | ||
if( this.isRegex || this.isString ) | ||
return true; | ||
if( this.isObject ) | ||
return matchesVersion( semver, version, this.path.version ); | ||
return false; | ||
} | ||
module.exports = Path; |
@@ -27,2 +27,13 @@ var Path = require('./path'); | ||
Route.prototype.matchings = function( version, _, semver ){ | ||
var found = _.map( | ||
_.filter( this.paths, function( path ){ return path.matchings( version, _, semver); } ) | ||
, function(path){ | ||
return path.path; | ||
} | ||
); | ||
return found; | ||
} | ||
module.exports = Route; |
{ | ||
"name": "connect-rest", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "RESTful web services middleware for Connect.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -64,15 +64,42 @@ [connect-rest](https://github.com/imrefazekas/connect-rest) is a small middleware for [connect](http://www.senchalabs.org/connect/) for building REST APIs. | ||
or | ||
rest.get('/books/:title/:chapter', functionN0 ); | ||
You can define parametrised paths for services to accept REST variables from the caller. | ||
In this case, whatever string is after the 'books', will be interpret as a variable and passed to the service function via the request object. | ||
In this case, whatever string is after the 'books', will be interpret as variable(s) and passed to the service function via the request object. | ||
So sending a get request to the uri '/api/books/AliceInWonderland', will result the following request object: | ||
So sending a get request to the uri '/api/books/AliceInWonderland/1', will result the following request object: | ||
{"headers": ...,"parameters":{"title":"AliceInWonderland"}} | ||
{"headers": ...,"parameters":{"title":"AliceInWonderland", "chapter": "1"}} | ||
## Context: | ||
## Context | ||
connect-rest also supports uri prefix if you want to put every REST function behind the same context: | ||
rest.context( '/api' ); // means that ever rest calls need to be sent to '/api/X' path. | ||
rest.context( '/api' ); // means that every rest calls need to be sent to '/api/X' path. | ||
## Discover services | ||
connect-rest provides a built-in service: discover. Via a simple get request, it allows you - by specifying a version - to discover the plublished REST apis matching the given version. | ||
var options = { | ||
'discoverPath': 'discover' | ||
}; | ||
connectApp.use( rest.rester( options ) ); | ||
This will enable this service - considering the context descrived above - on the path '/api/discover/:version'. Sending a get request to - lets say - this path | ||
http://localhost:8080/api/discover/3.0.0 | ||
would retrieve all services which can be called using version 3.0.0 (non-versioned and matching versioned services). The returned JSON is the following: | ||
{ | ||
"HEAD":["/peek"], | ||
"GET":["discover/:version","/books/:title/:chapter"], | ||
"POST":["/store",{"path":"/make","version":">=1.0.0"},"/act","/do",{"path":"/shake","version":">=2.0.0"},{"path":"/twist","version":">=2.1.1"}], | ||
"PUT":[], | ||
"DELETE":[] | ||
} | ||
## Server - extracted from the tests | ||
@@ -86,4 +113,8 @@ | ||
connectApp.use( connect.query() ); | ||
connectApp.use( rest.rester() ); | ||
var options = { | ||
'discoverPath': 'discover' | ||
}; | ||
connectApp.use( rest.rester( options ) ); | ||
rest.get('/books/:title', functionN0 ); | ||
@@ -130,1 +161,10 @@ | ||
## ToDo | ||
- logging services should be added properly | ||
- api_key management | ||
## Changelog | ||
- 0.0.3 : discovery managemenet added | ||
- 0.0.2 : named parameters added | ||
- 0.0.1 : initial release |
@@ -40,3 +40,3 @@ var options = { | ||
var voptions = _.clone( options ); | ||
voptions.path = '/api/books/AliceInWonderland'; | ||
voptions.path = '/api/books/AliceInWonderland/1'; | ||
voptions.method = 'GET'; | ||
@@ -43,0 +43,0 @@ |
@@ -8,3 +8,3 @@ function buildUpRestAPI(rest){ | ||
}); | ||
rest.get('/books/:title', function( request ){ | ||
rest.get('/books/:title/:chapter', function( request ){ | ||
console.log( 'Received:' + JSON.stringify( request ) ); | ||
@@ -11,0 +11,0 @@ return 'ok'; |
@@ -17,3 +17,7 @@ var rest = require('../lib/connect-rest'); | ||
connectApp.use( rest.rester() ); | ||
var options = { | ||
'apiKeys': [], | ||
'discoverPath': 'discover' | ||
}; | ||
connectApp.use( rest.rester( options ) ); | ||
@@ -20,0 +24,0 @@ var server = http.createServer( connectApp ); |
18265
328
167