Comparing version 0.0.2 to 0.0.3
@@ -9,4 +9,10 @@ var fs = require('fs'); | ||
// Current Request passed from app | ||
this.req = null; | ||
// Current Responde that passed to clent | ||
this.res = null; | ||
// Cache all settings | ||
this.settingsCache = {}; | ||
@@ -24,20 +30,14 @@ return function(req, res, next) { | ||
Rapidly.prototype = { | ||
getRouteFilePath : function( url ) { | ||
var url = url.toLowerCase() | ||
, parts = url.split('/') | ||
, routeRootDirPath = __dirname + '/../../../' + 'routes/'; | ||
isHTTPMethodValid : function( schemaName, actionName, method ) { | ||
/* | ||
* Support /api/xxx/ or /api/xxx | ||
* | ||
* Example : /api/note/add, /api/note/add/ | ||
* then parts would be [ 'api', 'note', 'add' ] | ||
*/ | ||
if ( parts[ parts.length - 1 ] === "" ) { | ||
parts.pop(); | ||
var schemaPath = getSchemaPath( schemaName ); | ||
if ( typeof this.settingsCache[ schemaName ] === 'undefined' ) { | ||
// I/O Once, cache forever | ||
this.settingsCache[ schemaName ] = JSON.parse( fs.readFileSync( schemaPath ) ); | ||
} | ||
return routeRootDirPath + parts.join('/') + '.js'; | ||
return (this.settingsCache[ schemaName ][ actionName ]['method'] === method); | ||
}, | ||
@@ -47,7 +47,51 @@ exec : function() { | ||
var url = this.req.params[0], | ||
filePath = this.getRouteFilePath( url ), | ||
relatedAction = require( filePath ); | ||
routePath, | ||
schemaName, | ||
actionName, | ||
callback; | ||
var o = urlToSchemaNameAndActionName( url ); | ||
relatedAction(this.req, this.res); | ||
schemaName = o[0]; | ||
actionName = o[1]; | ||
routePath = getRoutePath( schemaName, actionName ); | ||
if ( !this.isHTTPMethodValid( schemaName, actionName, this.req.method ) ) { | ||
this.res.send('Wrong HTTP method'); | ||
} | ||
cb = require( routePath ); | ||
cb(this.req, this.res); | ||
} | ||
}; | ||
function urlToSchemaNameAndActionName( url ) { | ||
var url = url.toLowerCase() | ||
, parts = url.split('/'); | ||
/* | ||
* Support /api/xxx/ or /api/xxx | ||
* | ||
* Example : /api/(note/add), /api/(note/add/) | ||
* then parts would be [ 'note', 'add' ] | ||
*/ | ||
if ( parts[ parts.length - 1 ] === "" ) { | ||
parts.pop(); | ||
} | ||
return [ parts[0], parts[1] ]; | ||
} | ||
function getSchemaPath( schemaName ) { | ||
return getAppRootPath() + 'schema/' + schemaName + '.js'; | ||
} | ||
function getRoutePath( schemaName, actionName ) { | ||
return getAppRootPath() + 'routes/' + schemaName + '/' + actionName + '.js'; | ||
} | ||
function getAppRootPath() { | ||
return __dirname + '/../../../'; | ||
} |
{ | ||
"name" : "rapidly", | ||
"version" : "0.0.2", | ||
"version" : "0.0.3", | ||
"description" : "This helper can help you set your API routes/actions rapidly based on Express.js", | ||
@@ -5,0 +5,0 @@ "author" : "EragonJ <eragonj@eragonj.me>", |
@@ -9,3 +9,3 @@ Rapidly | ||
npm install -g rapidly | ||
npm install -g rapidly | ||
@@ -15,4 +15,6 @@ Command | ||
Usage: rapidly [options] | ||
Usage: | ||
rapidly [options] | ||
Options: | ||
@@ -33,8 +35,5 @@ | ||
EragonJ | 陳佳隆 ( Chia-Lung, Chen ) | ||
EragonJ ( Chia-Lung, Chen ) | ||
Email - eragonj [AT] eragonj.me | ||
Blog - [A humble navigator](http://eragonj.me) | ||
LICENSE | ||
@@ -41,0 +40,0 @@ ------- |
@@ -17,3 +17,3 @@ { | ||
"scope" : "private" | ||
}, | ||
} | ||
} |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
7791
93
40