Comparing version 0.5.4 to 1.0.0
66
index.js
@@ -1,2 +0,2 @@ | ||
var approot = process.env.PWD; | ||
var APP_ROOT = process.env.PWD; | ||
@@ -7,2 +7,3 @@ var path = require('path'); | ||
var methods = require('methods'); | ||
var express = require('express'); | ||
@@ -20,43 +21,34 @@ function joinParam (url, param) { | ||
function route (url, method, instance) { | ||
var router = instance[method.toLowerCase()] || instance[method.toUpperCase()]; | ||
if (router) { | ||
var filters = (instance.filters || []).concat(router.filters || []); | ||
var chain = filters.concat(router instanceof Array ? router : [router]); | ||
var m = method.toLowerCase(); | ||
if (typeof this[m] != 'function') { | ||
console.error('[rainbow]: http method "' + method + '" is not supported.'); | ||
return; | ||
} | ||
this[m].apply(this, | ||
[joinParam(url, router.params)].concat(chain) | ||
); | ||
} | ||
} | ||
/** | ||
* Main function to initialize routers of a Express app. | ||
* | ||
* @param {Express} app Express app instance | ||
* @param {Object} paths (optional) For configure relative paths of | ||
* controllers and filters rather than defaults. | ||
*/ | ||
exports.route = function (app, paths) { | ||
function route (url, method, instance) { | ||
var router = instance[method.toLowerCase()] || instance[method.toUpperCase()]; | ||
if (router) { | ||
var filters = (instance.filters || []).concat(router.filters || []).map(function (item) { | ||
switch (typeof item) { | ||
case 'function': | ||
return item; | ||
case 'string': | ||
return require(path.join(fltrDir, item)); | ||
default: | ||
console.error('[rainbow]: Filter only support function or string of path.'); | ||
return null; | ||
} | ||
}).filter(function (item) { | ||
return !!item; | ||
}); | ||
app[method.toLowerCase()].apply(app, [joinParam(url, router.params)] | ||
.concat(filters) | ||
.concat([router]) | ||
); | ||
} | ||
} | ||
module.exports = function (options = {}) { | ||
var router = express.Router(); | ||
var ctrlDir = options.controllers || path.join(APP_ROOT, 'controllers'); | ||
paths = paths || {}; | ||
var ctrlDir = path.join(approot, (paths.controllers || 'controllers')); | ||
var fltrDir = path.join(approot, (paths.filters || 'filters')); | ||
glob.sync(ctrlDir + "/**/*.+(coffee|js)").forEach(function (file) { | ||
glob.sync(ctrlDir + "/**/*.js").forEach(function (file) { | ||
file = file.replace(/\.[^.]*$/, ''); | ||
var instance = require(file); | ||
@@ -66,3 +58,3 @@ var single = typeof instance == 'function'; | ||
single ? route(url, 'ALL', {ALL: instance}) : | ||
single ? route.call(router, url, 'ALL', {ALL: instance}) : | ||
methods.forEach(function (method) { | ||
@@ -72,5 +64,7 @@ if (instance[method.toLowerCase()]) { | ||
} | ||
route(url, method, instance); | ||
route.call(router, url, method, instance); | ||
}); | ||
}); | ||
return router; | ||
}; |
{ | ||
"name": "rainbow", | ||
"description": "Express router middleware for RESTful API base on certain folder path", | ||
"version": "0.5.4", | ||
"version": "1.0.0", | ||
"author": "mytharcher <mytharcher@gmail.com>", | ||
"main" : "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"dependencies": { | ||
"glob": "3.x", | ||
"methods": "1.x" | ||
"glob": "7.1.2", | ||
"methods": "1.1.2" | ||
}, | ||
"peerDependencies": { | ||
"express": "^4.x" | ||
}, | ||
"devDependencies": { | ||
"axios": "0.16.2", | ||
"express": "^4.x", | ||
"mocha": "3.5.2" | ||
}, | ||
"keywords": [ | ||
@@ -27,3 +38,4 @@ "express", | ||
"node": "*" | ||
} | ||
}, | ||
"license": "MIT" | ||
} |
@@ -27,3 +27,3 @@ Rainbow | ||
// Here using Rainbow to initialize all routers | ||
rainbow.route(app); | ||
app.use('api/', rainbow()); | ||
@@ -47,5 +47,7 @@ app.listen(6060); | ||
If you need some filters, just add a `filters` array property which contains your filters in `filters/` folder to the handle function like this: | ||
If you need some filters, just add a `filters` array property which contains your filters each as a function to the handle function like this: | ||
```javascript | ||
var authorization = require('authorization'); | ||
exports.GET = function (req, res) { | ||
@@ -55,3 +57,3 @@ res.send(200, 'Simple getting.'); | ||
// add filters | ||
exports.GET.filters = ['authorization']; | ||
exports.GET.filters = [authorization]; | ||
``` | ||
@@ -86,16 +88,4 @@ | ||
You can write controllers with coffeescript using `.coffee` in example `controllers/user.coffee`: | ||
CoffeeScript file with `.coffee` suffix will not be supported from v1.0. | ||
```coffeescript | ||
exports.GET = (req, res) -> | ||
User.find(where: req.query.name) | ||
.success (user) -> | ||
res.send 200, user | ||
exports.PUT = (req, res) -> | ||
User.create(req.body) | ||
.success (user) -> | ||
res.send 201, user.id | ||
``` | ||
### Params ### | ||
@@ -128,3 +118,3 @@ | ||
Make sure the filters you need had been defined in `filters/` folder (could be changed) as same module name, because them will be required when initilizing. Here `authorization.js` is a example for intecepting by non-authenticated user before `GET` `http://yourapp:6060/something`: | ||
Filter is as same as a origin middleware in Express. Define an action with filters by using `.filters` property as an array. Here `authorization.js` is a example for intecepting by non-authenticated user before `GET` `http://yourapp:6060/something`: | ||
@@ -152,6 +142,4 @@ ```javascript | ||
You could see filters is as same as a origin router in Express, just be put together in `filters/` folder to be interceptors like in Java SSH. | ||
Filters only support function from v1.0. | ||
Filters also support variable name of filter function in same file than string filter file name in filters directory (from v0.4): | ||
```javascript | ||
@@ -177,4 +165,4 @@ // controller file test.js route to [GET]/test | ||
exports.POST = function (req, res) {}; | ||
exports.POST.filters = ['validation']; | ||
exports.filters = ['session']; | ||
exports.POST.filters = [validation]; | ||
exports.filters = [session]; | ||
``` | ||
@@ -186,12 +174,11 @@ | ||
Controllers and filters default path could be changed by passing a path config object to `route` function when initializing: | ||
Controllers default path could be changed by passing a path config object to `route` function when initializing: | ||
```javascript | ||
rainbow.route(app, { | ||
controllers: '/your/controllers/path', | ||
filters: '/your/filters/path' | ||
}); | ||
app.use(rainbow({ | ||
controllers: '/your/controllers/path' | ||
})); | ||
``` | ||
These paths are all **RELATIVE** to your app path! | ||
These paths are all **ABSOLUTE** file path! | ||
@@ -198,0 +185,0 @@ ## Troubleshooting ## |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
3
7232
3
3
3
54
188
+ Addedaccepts@1.3.8(transitive)
+ Addedarray-flatten@1.1.1(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbody-parser@1.20.3(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedbytes@3.1.2(transitive)
+ Addedcall-bind-apply-helpers@1.0.1(transitive)
+ Addedcall-bound@1.0.3(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedcontent-disposition@0.5.4(transitive)
+ Addedcontent-type@1.0.5(transitive)
+ Addedcookie@0.7.1(transitive)
+ Addedcookie-signature@1.0.6(transitive)
+ Addeddebug@2.6.9(transitive)
+ Addeddepd@2.0.0(transitive)
+ Addeddestroy@1.2.0(transitive)
+ Addeddunder-proto@1.0.1(transitive)
+ Addedee-first@1.1.1(transitive)
+ Addedencodeurl@1.0.22.0.0(transitive)
+ Addedes-define-property@1.0.1(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.0.0(transitive)
+ Addedescape-html@1.0.3(transitive)
+ Addedetag@1.8.1(transitive)
+ Addedexpress@4.21.2(transitive)
+ Addedfinalhandler@1.3.1(transitive)
+ Addedforwarded@0.2.0(transitive)
+ Addedfresh@0.5.2(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.6(transitive)
+ Addedglob@7.1.2(transitive)
+ Addedgopd@1.2.0(transitive)
+ Addedhas-symbols@1.1.0(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhttp-errors@2.0.0(transitive)
+ Addediconv-lite@0.4.24(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedipaddr.js@1.9.1(transitive)
+ Addedmath-intrinsics@1.1.0(transitive)
+ Addedmedia-typer@0.3.0(transitive)
+ Addedmerge-descriptors@1.0.3(transitive)
+ Addedmime@1.6.0(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedms@2.0.02.1.3(transitive)
+ Addednegotiator@0.6.3(transitive)
+ Addedobject-inspect@1.13.3(transitive)
+ Addedon-finished@2.4.1(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedparseurl@1.3.3(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedpath-to-regexp@0.1.12(transitive)
+ Addedproxy-addr@2.0.7(transitive)
+ Addedqs@6.13.0(transitive)
+ Addedrange-parser@1.2.1(transitive)
+ Addedraw-body@2.5.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsend@0.19.0(transitive)
+ Addedserve-static@1.16.2(transitive)
+ Addedsetprototypeof@1.2.0(transitive)
+ Addedside-channel@1.1.0(transitive)
+ Addedside-channel-list@1.0.0(transitive)
+ Addedside-channel-map@1.0.1(transitive)
+ Addedside-channel-weakmap@1.0.2(transitive)
+ Addedstatuses@2.0.1(transitive)
+ Addedtoidentifier@1.0.1(transitive)
+ Addedtype-is@1.6.18(transitive)
+ Addedunpipe@1.0.0(transitive)
+ Addedutils-merge@1.0.1(transitive)
+ Addedvary@1.1.2(transitive)
+ Addedwrappy@1.0.2(transitive)
- Removedglob@3.2.11(transitive)
- Removedlru-cache@2.7.3(transitive)
- Removedminimatch@0.3.0(transitive)
- Removedsigmund@1.0.1(transitive)
Updatedglob@7.1.2
Updatedmethods@1.1.2