connect-rest
Advanced tools
Comparing version 0.0.9 to 0.0.10
@@ -6,6 +6,5 @@ /* | ||
*/ | ||
var VERSION = '0.0.9'; | ||
var VERSION = '0.0.10'; | ||
var connect = require('connect'); | ||
var util = require('util'); | ||
var url = require('url'); | ||
@@ -40,3 +39,3 @@ var async = require('async'); | ||
function DummyLogger(){ }; | ||
function DummyLogger(){ } | ||
DummyLogger.prototype.info = function() { }; | ||
@@ -54,2 +53,17 @@ DummyLogger.prototype.debug = function() { }; | ||
function protoPather( request, content ){ | ||
var utokens = _.words( request.parameters.path, '/'); | ||
var method = _.first( utokens ); | ||
var version = utokens[1]; | ||
var pathname = '/' + _.toSentence( _.rest(utokens, 2 ), '/', '/' ); | ||
var routes = _.filter( | ||
mapping[ method ], function(route){ return route.matches( | ||
null, pathname, CONTEXT, version, _, semver, false | ||
); } | ||
); | ||
return routes.length > 0 ? _.first( _.map( routes, function(route){ return route.prototypeObject; } ) ) : 'No matching service'; | ||
} | ||
function discover( request, content ){ | ||
@@ -85,3 +99,3 @@ var version = request.parameters.version; | ||
res.statusCode = 500; | ||
res.end( 'Error occurred: ' + err ); | ||
res.end( 'Error occurred: ' + err ); | ||
} | ||
@@ -99,15 +113,15 @@ else{ | ||
addPath("HEAD", path, functionRef); | ||
} | ||
}; | ||
exports.get = function getRest(path, functionRef){ | ||
addPath("GET", path, functionRef); | ||
} | ||
}; | ||
exports.post = function postRest(path, functionRef, prototypeObject){ | ||
addPath("POST", path, functionRef, prototypeObject); | ||
} | ||
}; | ||
exports.put = function putRest(path, functionRef, prototypeObject){ | ||
addPath("PUT", path, functionRef, prototypeObject); | ||
} | ||
}; | ||
exports.delete = function deleteRest(path, functionRef, prototypeObject){ | ||
addPath("DELETE", path, functionRef, prototypeObject); | ||
} | ||
}; | ||
@@ -118,12 +132,12 @@ exports.publish = function (services){ | ||
if( element.length == 1 ) | ||
get( '/' + element.name, element ); | ||
getRest( '/' + element.name, element ); | ||
if( element.length == 2 ) | ||
post( '/' + element.name, element ); | ||
postRest( '/' + element.name, element ); | ||
} | ||
} ); | ||
} | ||
}; | ||
exports.context = function (context){ | ||
CONTEXT = context; | ||
} | ||
}; | ||
@@ -135,3 +149,3 @@ exports.rester = function( options ) { | ||
if( options.protoPath ) | ||
addPath('GET', options.protoPath + '/*path', discover ); | ||
addPath('GET', options.protoPath + '/*path', protoPather ); | ||
API_KEYS = options.apiKeys; | ||
@@ -165,3 +179,3 @@ | ||
mapping[ req.method ], function(route){ return route.matches( | ||
req, pathname, CONTEXT, req.headers['accept-version'] || req.headers['x-api-version'] || '*', _, semver | ||
req, pathname, CONTEXT, req.headers['accept-version'] || req.headers['x-api-version'] || '*', _, semver, true | ||
); } | ||
@@ -171,3 +185,3 @@ ); | ||
if( matching.length == 0 ){ | ||
if( matching.length === 0 ){ | ||
logger.info('Request won\'t be handled by connect-rest.', req.url ); | ||
@@ -183,11 +197,11 @@ | ||
} else{ | ||
var body = ''; | ||
req.on('data', function(chunk) { | ||
body += chunk; | ||
if (body.length > LOAD_SIZE_LIMIT) { | ||
request.connection.destroy(); | ||
} | ||
}); | ||
var body = ''; | ||
req.on('data', function(chunk) { | ||
body += chunk; | ||
if (body.length > LOAD_SIZE_LIMIT) { | ||
req.connection.destroy(); | ||
} | ||
}); | ||
req.on('end', function() { | ||
req.on('end', function() { | ||
logger.debug('Body payload: ', body ); | ||
@@ -198,6 +212,6 @@ | ||
process(req, res, matching, bodyObj); | ||
} ); | ||
} | ||
} | ||
} | ||
} ); | ||
} | ||
}; | ||
}; | ||
@@ -209,2 +223,1 @@ exports.VERSION = VERSION; | ||
exports.SERVICE_METHOD_PATTERN = SERVICE_METHOD_PATTERN; | ||
@@ -9,3 +9,3 @@ var PARAMETER_M_DELIMETER = ':'; | ||
this.isRegex = _.isRegExp( path ); | ||
this.isString = _.isString( path ); | ||
this.isString = _.isString( path ); | ||
this.isObject = _.isObject( path ) && path.path && path.version && _.isString( path.path ) && _.isString( path.version ); | ||
@@ -20,6 +20,5 @@ this.isSubReged = this.isObject && _.isRegExp( path.path ); | ||
return semver.satisfies( reqVersion, apiVersion ); | ||
}; | ||
} | ||
Path.prototype.matches = function( req, pathname, version, _, semver ){ | ||
Path.prototype.matches = function( req, pathname, version, _, semver, alterEnvironment ){ | ||
if( this.isRegex ){ | ||
@@ -50,5 +49,5 @@ return this.path.test( pathname ); | ||
return false; | ||
s | ||
var parameterReplacements = {}; | ||
for (i=0;i<utokens.length;i++){ | ||
for (var i=0;i<utokens.length;i++){ | ||
if( _( ptokens[i] ).startsWith( PARAMETER_M_DELIMETER ) ) | ||
@@ -67,5 +66,6 @@ parameterReplacements[ ptokens[i].substring( PARAMETER_M_DELIMETER.length ) ] = utokens[i]; | ||
} | ||
_.each(parameterReplacements, function(value, key, list){ | ||
req.query[ key ] = value; | ||
}); | ||
if( alterEnvironment ) | ||
_.each(parameterReplacements, function(value, key, list){ | ||
req.query[ key ] = value; | ||
}); | ||
@@ -80,3 +80,3 @@ return true; | ||
return false; | ||
} | ||
}; | ||
@@ -90,5 +90,5 @@ Path.prototype.matchings = function( version, _, semver ){ | ||
return false; | ||
} | ||
}; | ||
module.exports = Path; |
var Path = require('./path'); | ||
function Route(path, prototypeObject, action, _){ | ||
this.action = action; | ||
this.action = action; | ||
this.prototypeObject = prototypeObject; | ||
this.paths = []; | ||
this.paths = []; | ||
if( !path || !action || !_.isFunction( action ) ) | ||
throw new Error('You need to give proper parameters.'); | ||
if( !path || !action || !_.isFunction( action ) ) | ||
throw new Error('You need to give proper parameters.'); | ||
if( _.isArray( path ) ){ | ||
if( _.isArray( path ) ){ | ||
this.paths = _.map( path, function(element){ return new Path( element, _ ); } ); | ||
} else{ | ||
this.paths.push( new Path( path, _ ) ); | ||
} | ||
} else{ | ||
this.paths.push( new Path( path, _ ) ); | ||
} | ||
} | ||
Route.prototype.matches = function( req, pathname, context, version, _, semver){ | ||
Route.prototype.matches = function( req, pathname, context, version, _, semver, alterEnvironment){ | ||
if( !_( pathname ).startsWith( context ) ) | ||
@@ -23,6 +23,7 @@ return false; | ||
var rPathname = pathname.substring( context.length ); | ||
var found = _.find( this.paths, function(path){ return path.matches( req, rPathname, version || '*', _, semver); } ); | ||
var found = _.find( this.paths, function(path){ return path.matches( req, rPathname, version || '*', _, semver, alterEnvironment); } ); | ||
return found; | ||
} | ||
}; | ||
@@ -38,4 +39,4 @@ Route.prototype.matchings = function( version, _, semver ){ | ||
return found; | ||
} | ||
}; | ||
module.exports = Route; |
{ | ||
"name": "connect-rest", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"description": "RESTful web services middleware for Connect.", | ||
@@ -44,4 +44,4 @@ "keywords": [ | ||
"readme": "README.md", | ||
"_id": "connect-rest@0.0.9", | ||
"_from": "connect-rest@>=0.0.9" | ||
"_id": "connect-rest@0.0.10", | ||
"_from": "connect-rest@>=0.0.10" | ||
} |
@@ -23,2 +23,3 @@ [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. | ||
- [Discover services](#discover-services) | ||
- [Prototype services](#prototype-services) | ||
- [API_KEY management](#api_key-management) | ||
@@ -161,2 +162,32 @@ - [Logging](#logging) | ||
## Prototype services | ||
The assign-methods allows you to pass a third parameter, an object which can be considered as a prototype of the expected parameter of the service when a client wants to make a call. | ||
rest.post( [ { path: '/shake', version: '>=2.0.0' }, { path: '/twist', version: '>=2.1.1' } ], functionN, {'title': 'Alice in Wonderland'} ); | ||
That parameter debriefs the client what structure the functionN expects to receive. To activate this feature, first you have to add a new attribute to the options object: | ||
var options = { | ||
'apiKeys': [ '849b7648-14b8-4154-9ef2-8d1dc4c2b7e9' ], | ||
'discoverPath': 'discover', | ||
'protoPath': 'proto', | ||
'logger': 'connect-rest' | ||
}; | ||
This 'protoPath' means that sending a request to the server on path: | ||
'/api/proto/POST/2.3.0/api/twist?api_key=849b7648-14b8-4154-9ef2-8d1dc4c2b7e9' | ||
will retrieve the object | ||
{'title': 'Alice in Wonderland'} | ||
because the service | ||
on path '/api/twist' and method 'POST' with version '2.3.0' | ||
can be called and there is an assigned prototype object to it. | ||
Giving access method, version and path is mandatory for this feature. | ||
## API_KEY management | ||
@@ -296,3 +327,4 @@ The option passed to the connect-rest might contain an array enumerating accepted api_keys: | ||
- 0.0.8 : General path matcher added, optional refined | ||
- 0.0.10 : Prototyping added | ||
- 0.0.9 : General path matcher added, optional now marked with '?' | ||
- 0.0.8 : Other body parsing middlewares are respected | ||
@@ -299,0 +331,0 @@ - 0.0.6 : logging added |
@@ -96,2 +96,11 @@ var options = { | ||
function testCall8(http, _, callback){ | ||
var voptions = _.clone( options ); | ||
voptions.path = '/api/proto/POST/2.3.0/api/twist?api_key=849b7648-14b8-4154-9ef2-8d1dc4c2b7e9'; | ||
voptions.method = 'GET'; | ||
voptions.headers['accept-version'] = '2.2.0'; | ||
testCallZero( http, _, callback, voptions, {'message': 'ok'} ); | ||
} | ||
exports.testCall1 = testCall1; | ||
@@ -105,1 +114,2 @@ exports.testCall2 = testCall2; | ||
exports.testCall7 = testCall7; | ||
exports.testCall8 = testCall8; |
@@ -31,5 +31,5 @@ function buildUpRestAPI( rest, _ ){ | ||
return JSON.stringify(content); | ||
}); | ||
}, {'title': 'Alice in Wonderland'} ); | ||
} | ||
exports.buildUpRestAPI = buildUpRestAPI; |
@@ -22,2 +22,3 @@ var rest = require('../lib/connect-rest'); | ||
'discoverPath': 'discover', | ||
'protoPath': 'proto', | ||
'logger': 'connect-rest' | ||
@@ -41,3 +42,4 @@ }; | ||
async.apply( caller.testCall6, http, _ ), | ||
async.apply( caller.testCall7, http, _ ) | ||
async.apply( caller.testCall7, http, _ ), | ||
async.apply( caller.testCall8, http, _ ) | ||
], function(err, results){ | ||
@@ -44,0 +46,0 @@ console.log('Tests finished.'); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34748
11
433
333