connect-rest
Advanced tools
Comparing version 0.0.25 to 0.0.26
@@ -6,3 +6,3 @@ /* | ||
*/ | ||
var VERSION = '0.0.25'; | ||
var VERSION = '0.0.26'; | ||
@@ -15,2 +15,3 @@ var connect = require('connect'); | ||
var http = require('http'); | ||
var https = require('https'); | ||
var _ = require('underscore'); | ||
@@ -104,2 +105,4 @@ _.str = require('underscore.string'); | ||
var asyncCall = req.query.callbackURL; | ||
var callChain = _.map( matching, function(func){ | ||
@@ -114,4 +117,2 @@ var headers = req.headers; headers.httpVersion=req.httpVersion; headers.method=req.method; headers.originalUrl=req.originalUrl; | ||
var asyncCall = req.query.callbackURL; | ||
async.series( callChain, | ||
@@ -121,8 +122,12 @@ function(err, results){ | ||
var result; | ||
if( err ) | ||
logger.error( err ); | ||
else | ||
result = _.find(results, function(returnValue){ return returnValue && returnValue.result; }) || { result: '', resOptions:{ statusCode: 204 } }; | ||
if( asyncCall ){ | ||
var voptions = global._.clone( httphelper.opt ); | ||
voptions.path = async; | ||
httphelper.generalCall( http, voptions, function(err, response){ | ||
if(err) | ||
logger.error( err ); | ||
httphelper.generalCall( http, https, url, asyncCall, err, result, logger, function(er, response){ | ||
if(er) | ||
logger.error( er ); | ||
else | ||
@@ -133,3 +138,2 @@ logger.info('Response:', response); | ||
if( err ){ | ||
logger.error( err ); | ||
res.statusCode = err.statusCode || 500; | ||
@@ -139,4 +143,2 @@ res.end( 'Error occurred: ' + err ); | ||
else{ | ||
logger.debug( 'Results:', results ); | ||
var result = _.find(results, function(returnValue){ return returnValue && returnValue.result; }) || { result: '', resOptions:{ statusCode: 204 } }; | ||
var headers = result.resOptions.headers || { }; | ||
@@ -143,0 +145,0 @@ if( !headers['Content-Type'] ) |
exports.opt = { | ||
hostname: 'localhost', | ||
port: 8080, | ||
port: 80, | ||
path: '', | ||
method: 'POST', | ||
headers: { | ||
'accept-version': '1.0.0' | ||
'accept-version': '1.0.0', | ||
'Content-Type': 'application/json' | ||
} | ||
}; | ||
exports.generalCall = function(http, voptions, callback, payload){ | ||
var req = http.request( voptions, function(res) { | ||
console.log("Got response: " + res.statusCode); | ||
exports.generalCall = function(http, https, url, serverURL, err, result, logger, callback){ | ||
var server = url.parse( serverURL ); | ||
if(logger) | ||
logger.debug('Async server data:', server); | ||
var voptions = global._.clone( exports.opt ); | ||
voptions.hostname = server.hostname; | ||
voptions.port = server.port; | ||
voptions.path = server.path; | ||
var lib =(server.protocol == 'https:' ? https : http); | ||
var data; | ||
var payload = err ? { errorMessage: err.message, errorCode: err.errorCode||err.code||err.statusCode||-1 } : result; | ||
if( payload ){ | ||
data = JSON.stringify( payload ); | ||
voptions.headers['Content-Length'] = data.length; | ||
if(logger) | ||
logger.debug('Payload to be sent:', data); | ||
} | ||
var req = lib.request( voptions, function(res) { | ||
var body = ''; | ||
@@ -21,9 +42,9 @@ res.on('data', function (chunk) { | ||
}); | ||
req.on('error', function(err) { | ||
console.log("Got error: " + e.message); | ||
callback(err, 'failed.'); | ||
req.on('error', function(er) { | ||
callback(er, 'failed.'); | ||
}); | ||
if( payload ) | ||
req.write( JSON.stringify( payload ) ); | ||
if( data ) | ||
req.write( data ); | ||
req.end(); | ||
}; |
{ | ||
"name": "connect-rest", | ||
"version": "0.0.25", | ||
"version": "0.0.26", | ||
"description": "RESTful web services middleware for Connect.", | ||
@@ -49,4 +49,4 @@ "keywords": [ | ||
"readme": "README.md", | ||
"_id": "connect-rest@0.0.25", | ||
"_from": "connect-rest@>=0.0.25" | ||
"_id": "connect-rest@0.0.26", | ||
"_from": "connect-rest@>=0.0.26" | ||
} |
@@ -5,4 +5,4 @@ [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 and node domains as well. | ||
The connect-rest is a simple, yet powerful middleware for [connect](http://www.senchalabs.org/connect/), inspired by [restify](http://mcavage.github.com/node-restify/). | ||
The aim is to focus on the business logic, so connect-rest is managing body payload and parameters as well in the background, your business logic function does not need to take care of any request or response object at all. | ||
The [connect-rest](https://github.com/imrefazekas/connect-rest) is a simple, yet powerful middleware for [connect](http://www.senchalabs.org/connect/), inspired by [restify](http://mcavage.github.com/node-restify/). | ||
The aim is to focus on the business logic, so [connect-rest](https://github.com/imrefazekas/connect-rest) is managing body payload and parameters as well in the background, your business logic function does not need to take care of any request or response object at all. | ||
@@ -13,3 +13,3 @@ The payload of the body - if exists - with proper mime-type will be interpret as JSON object and will be parsed and passed to the service function you assign to. | ||
Features: | ||
## Features: | ||
- [Assign](#assign) | ||
@@ -33,2 +33,3 @@ - [Path description](#path-description) | ||
- [Customization: Validation and Response mime-types](#customization) | ||
- [Answering async rest requests](#answering-async-rest-requests) | ||
@@ -53,3 +54,3 @@ ## Assign | ||
## Path description | ||
connect-rest supports many options to be used as path description. | ||
[connect-rest](https://github.com/imrefazekas/connect-rest) supports many options to be used as path description. | ||
@@ -72,2 +73,4 @@ Simple path: | ||
[Back to Feature list](#features) | ||
## Rest functions | ||
@@ -101,2 +104,4 @@ | ||
[Back to Feature list](#features) | ||
## Response headers | ||
@@ -172,4 +177,6 @@ | ||
[Back to Feature list](#features) | ||
## Context | ||
connect-rest also supports uri prefix if you want to put every REST function behind the same context: | ||
[connect-rest](https://github.com/imrefazekas/connect-rest) also supports uri prefix if you want to put every REST function behind the same context: | ||
@@ -179,3 +186,3 @@ rest.context( '/api' ); // means that every rest calls need to be sent to '/api/X' path. | ||
## Discovery 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. | ||
[connect-rest](https://github.com/imrefazekas/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. | ||
@@ -230,5 +237,6 @@ var options = { | ||
[Back to Feature list](#features) | ||
## API_KEY management | ||
The option passed to the connect-rest might contain an array enumerating accepted api_keys: | ||
The option passed to the [connect-rest](https://github.com/imrefazekas/connect-rest) might contain an array enumerating accepted api_keys: | ||
@@ -269,6 +277,8 @@ var options = { | ||
In the absence of 'logger' property, no logs will be made. | ||
The connect-rest will use level 'info' for entry and exit points of services and 'debug' for the milestones of all internal processes. | ||
The [connect-rest](https://github.com/imrefazekas/connect-rest) will use level 'info' for entry and exit points of services and 'debug' for the milestones of all internal processes. | ||
[Back to Feature list](#features) | ||
## Reflective publishing | ||
connect-rest allows you to have an extremely easy and fast way to publish your services. | ||
[connect-rest](https://github.com/imrefazekas/connect-rest) allows you to have an extremely easy and fast way to publish your services. | ||
@@ -308,3 +318,3 @@ You can define your own services like this in a file (services.js in this example): | ||
## Domain support | ||
connect-rest adds support for domain-based error handling. To the options object you can pass a domain too: | ||
[connect-rest](https://github.com/imrefazekas/connect-rest) adds support for domain-based error handling. To the options object you can pass a domain too: | ||
@@ -325,4 +335,6 @@ var createDomain = require('domain').create; | ||
By passing the restDomain object, connect-rest will assign req and rest object to that domain and in any occurring error, it will be sent to the caller with HTTP status code 500. | ||
By passing the restDomain object, [connect-rest](https://github.com/imrefazekas/connect-rest) will assign req and rest object to that domain and in any occurring error, it will be sent to the caller with HTTP status code 500. | ||
[Back to Feature list](#features) | ||
## Customization | ||
@@ -345,3 +357,13 @@ | ||
## Answering async rest requests | ||
[connect-rest](https://github.com/imrefazekas/connect-rest) provides a way to serve async rest requests. It might be important - especially between fragmented server-side environment - to call rest services and accept the answer on a specific callback URL specified by the requestor. | ||
The _client_ has to specify a request parameter _"callbackURL"_ possessing the callback URL where the answer has to be sent. | ||
Having sent the request, [connect-rest](https://github.com/imrefazekas/connect-rest) will answer it right away with status code _200_ and message _Ok._ and having the result created, it will sent via _HTTP POST_ to the URL given in the HTTP parameters. | ||
This process is performed behind the scenes, you do not have do anything special about it. If that parameter can be found in the HTTP request, the call will be threaten as async request. | ||
[Back to Feature list](#features) | ||
## Server - extracted from the tests | ||
@@ -407,2 +429,3 @@ | ||
- 0.0.26: async request fix | ||
- 0.0.23-25: small fix for content type management | ||
@@ -409,0 +432,0 @@ - 0.0.22: response header customization added |
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
Network access
Supply chain riskThis module accesses the network.
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
47399
634
441
3