connect-rest
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -6,3 +6,3 @@ /* | ||
*/ | ||
var VERSION = '0.0.6'; | ||
var VERSION = '0.0.7'; | ||
@@ -25,2 +25,4 @@ var util = require('util'); | ||
var SERVICE_METHOD_PATTERN = /^[a-zA-Z]([a-zA-Z]|\d|_)*$/g; | ||
var mapping = { | ||
@@ -68,18 +70,29 @@ "HEAD": [], | ||
exports.head = function (path, functionRef){ | ||
exports.head = function headRest(path, functionRef){ | ||
addPath("HEAD", path, functionRef); | ||
} | ||
exports.get = function (path, functionRef){ | ||
exports.get = function getRest(path, functionRef){ | ||
addPath("GET", path, functionRef); | ||
} | ||
exports.post = function (path, functionRef){ | ||
exports.post = function postRest(path, functionRef){ | ||
addPath("POST", path, functionRef); | ||
} | ||
exports.put = function (path, functionRef){ | ||
exports.put = function putRest(path, functionRef){ | ||
addPath("PUT", path, functionRef); | ||
} | ||
exports.delete = function (path, functionRef){ | ||
exports.delete = function deleteRest(path, functionRef){ | ||
addPath("DELETE", path, functionRef); | ||
} | ||
exports.publish = function (services){ | ||
_.each( _.values( services ), function(element, index, list){ | ||
if( _.isFunction( element ) && SERVICE_METHOD_PATTERN.test( element.name ) ){ | ||
if( element.length == 1 ) | ||
get( '/' + element.name, element ); | ||
if( element.length == 2 ) | ||
post( '/' + element.name, element ); | ||
} | ||
} ); | ||
} | ||
exports.context = function (context){ | ||
@@ -176,1 +189,3 @@ CONTEXT = context; | ||
exports.ERROR_MESSAGE = ERROR_MESSAGE; | ||
exports.SERVICE_METHOD_PATTERN = SERVICE_METHOD_PATTERN; | ||
{ | ||
"name": "connect-rest", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "RESTful web services middleware for Connect.", | ||
@@ -44,4 +44,4 @@ "keywords": [ | ||
"readme": "README.md", | ||
"_id": "connect-rest@0.0.5", | ||
"_from": "connect-rest@>=0.0.5" | ||
"_id": "connect-rest@0.0.7", | ||
"_from": "connect-rest@>=0.0.7" | ||
} |
@@ -1,2 +0,2 @@ | ||
[connect-rest](https://github.com/imrefazekas/connect-rest) is a middleware for [connect](http://www.senchalabs.org/connect/) for building REST APIs providing service discovery and path-based parameter mapping as well. | ||
[connect-rest](https://github.com/imrefazekas/connect-rest) is a middleware for [connect](http://www.senchalabs.org/connect/) for building REST APIs providing service discovery and path-based parameter mapping and "reflective" publishing as well. | ||
@@ -160,2 +160,37 @@ # Usage | ||
## Reflective publishing | ||
connect-rest allows you to have an extremely easy and fast way to publish your services. | ||
You can define your own services like this in a file (services.js in this example): | ||
function health( request ){ | ||
return 'ok'; | ||
}; | ||
function record( request, content ){ | ||
return 'saved'; | ||
} | ||
exports.health = health; | ||
exports.record = record; | ||
and publish them this way: | ||
var services = require('./services'); | ||
... | ||
rest.publish( services ); | ||
This will discover all functions assigned to the exports having a name which conforms the following regular expression: | ||
/^[a-zA-Z]([a-zA-Z]|\d|_)*$/g | ||
The logic is simple. If the function has | ||
- 1 parameter: it will be a 'get' method | ||
- 2 parameters: it will be a 'post' method | ||
and the path will be its name. So, by executing one single statement you will automatically have the following services: | ||
/health on Get | ||
/record on Post | ||
If you have 100 services defined, then 100 rest api you will have automatically. Nice. | ||
## Server - extracted from the tests | ||
@@ -162,0 +197,0 @@ |
@@ -1,2 +0,2 @@ | ||
function buildUpRestAPI(rest){ | ||
function buildUpRestAPI( rest, _ ){ | ||
rest.context( '/api' ); | ||
@@ -3,0 +3,0 @@ |
@@ -17,2 +17,4 @@ var rest = require('../lib/connect-rest'); | ||
var SERVICE_METHOD_PATTERN = /^[a-zA-Z]([a-zA-Z]|\d|_)*$/g; | ||
var options = { | ||
@@ -29,3 +31,3 @@ 'apiKeys': [ '849b7648-14b8-4154-9ef2-8d1dc4c2b7e9' ], | ||
restBuilder.buildUpRestAPI( rest ); | ||
restBuilder.buildUpRestAPI( rest, _ ); | ||
@@ -32,0 +34,0 @@ async.parallel([ |
24153
376
260