Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

connect-rest

Package Overview
Dependencies
Maintainers
1
Versions
190
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

connect-rest - npm Package Compare versions

Comparing version 0.0.5 to 0.0.6

60

lib/connect-rest.js

@@ -6,3 +6,3 @@ /*

*/
var VERSION = '0.0.5';
var VERSION = '0.0.6';

@@ -13,2 +13,3 @@ var util = require('util');

var semver = require('semver');
var bunyan = require('bunyan');
var _ = require('underscore');

@@ -35,16 +36,9 @@ _.str = require('underscore.string');

var logger;
var LOGGER_ENTITY = {
'logger':'Rest',
'context': CONTEXT
}
function debug( message, object ){
if(logger)
logger.debug( LOGGER_ENTITY, message, object );
}
function trace( message, object ){
if(logger)
logger.trace( LOGGER_ENTITY, message, object );
}
function DummyLogger(){ };
DummyLogger.prototype.info = function() { };
DummyLogger.prototype.debug = function() { };
function addPath(key, path, action){

@@ -92,3 +86,3 @@ mapping[ key ].push( new Route( path,

exports.context = function (context){
LOGGER_ENTITY.context = CONTEXT = context;
CONTEXT = context;
}

@@ -101,3 +95,8 @@

API_KEYS = options.apiKeys;
logger = options.logger;
logger = new DummyLogger();
if( options.logger )
logger = _.isString( options.logger ) ? bunyan.createLogger({name: options.logger, src: true}) : options.logger;
logger.info('connect-rest has been configured. ', options);
}

@@ -108,3 +107,7 @@

logger.info('Incoming request.', req.headers, req.query );
if( API_KEYS && API_KEYS.indexOf(req.query.api_key)==-1 ){
logger.info('Request without api key.', req.query );
res.statusCode = 401;

@@ -116,13 +119,20 @@ res.end( 'API_KEY is required.' );

var pathname = url.parse( req.url ).pathname;
var matching = _.map( _.filter(
logger.debug('Request received from: ', pathname );
var routes = _.filter(
mapping[ req.method ], function(route){ return route.matches(
req, pathname, CONTEXT, req.headers['accept-version'] || req.headers['x-api-version'] || '*', _, semver
); }
),
function(route){ return route.action; }
);
);
var matching = _.map( routes, function(route){ return route.action; } );
if( matching.length == 0 )
if( matching.length == 0 ){
logger.info('Request won\'t be handled by connect-rest.', req.url );
return next();
}
logger.debug('Routes matching: ', routes );
var body = '';

@@ -137,8 +147,16 @@ req.on('data', function(chunk) {

req.on('end', function() {
logger.debug('Body payload: ', body );
var bodyObj = body.length > 0 ? JSON.parse( body ) : '';
logger.debug('Parsed JSON object: ', bodyObj );
var callChain = _.map( matching, function(func){ return async.apply( func, {headers: req.headers, parameters: req.query}, bodyObj ); } );
logger.debug('Calling service functions.' );
async.series( callChain,
function(err, results){
logger.info('Service(s) calling finished.', err, results );
if( err ){

@@ -163,3 +181,1 @@ res.statusCode = 500;

exports.ERROR_MESSAGE = ERROR_MESSAGE;
exports.LOGGER_ENTITY = LOGGER_ENTITY;
{
"name": "connect-rest",
"version": "0.0.5",
"version": "0.0.6",
"description": "RESTful web services middleware for Connect.",

@@ -33,3 +33,4 @@ "keywords": [

"underscore.string":"~2",
"semver": "~1"
"semver": "~1",
"bunyan": ">=0.18.0"
},

@@ -36,0 +37,0 @@ "devDependencies":{

@@ -136,3 +136,26 @@ [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.

## Logging
In the option object passed to the constructor, there is an optional parameter 'logger', which enables the logging functionality:
var options = {
'apiKeys': [ '849b7648-14b8-4154-9ef2-8d1dc4c2b7e9' ],
'discoverPath': 'discover',
'logger': 'connect-rest'
};
or
var options = {
'apiKeys': [ '849b7648-14b8-4154-9ef2-8d1dc4c2b7e9' ],
'discoverPath': 'discover',
'logger': loggerInstance
};
You can set:
- a string, which will be interpret as the name of the logger seen in the logs, or
- passing a bunyan instance to be used.
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.
## Server - extracted from the tests

@@ -194,6 +217,7 @@

- logging services should be added properly
- more detailed examples
## Changelog
- 0.0.6 : logging added
- 0.0.5 : optional parameter added

@@ -200,0 +224,0 @@ - 0.0.4 : API_KEY management added

@@ -19,3 +19,4 @@ var rest = require('../lib/connect-rest');

'apiKeys': [ '849b7648-14b8-4154-9ef2-8d1dc4c2b7e9' ],
'discoverPath': 'discover'
'discoverPath': 'discover',
'logger': 'connect-rest'
};

@@ -22,0 +23,0 @@ connectApp.use( rest.rester( options ) );

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc