controller
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -14,4 +14,6 @@ var isRegExp = require('util').isRegExp; | ||
Controller.prototype.middleware = function middleware(scope) { | ||
Controller.prototype.middleware = function middleware(scope, fn) { | ||
if (scope == null) scope = 'all'; | ||
else if (typeof scope === 'function') fn = scope, scope = 'all'; | ||
if (fn != null) this.middleware(scope).push(fn); | ||
return this.middlewares[scope] || (this.middlewares[scope] = []); | ||
@@ -21,3 +23,3 @@ } | ||
Controller.prototype.route = function route(method, path, action) { | ||
this.routes[action] = { method: method, path: path }; | ||
this.routes[action] = { method: method.toLowerCase(), path: path }; | ||
} | ||
@@ -30,10 +32,26 @@ | ||
Controller.prototype.direct = (function() { | ||
var anonCount = 0; | ||
var anonId = function() { return 'anonymous-' + ++anonCount; } | ||
return function direct(method, path /* [mw/g], fn */) { | ||
var args = [].slice.call(arguments), | ||
groups = [], mw = [], item, id = anonId(); | ||
args.shift(), args.shift(); | ||
var handler = args.pop(); | ||
while (args.length > 0) | ||
(typeof (item = args.shift()) === 'string' ? groups : mw).push(item); | ||
this.define(id, groups, handler); | ||
while (mw.length > 0) this.middleware(id).push(mw.pop()) | ||
this.route(method, path, id); | ||
} | ||
})() | ||
Controller.prototype.attach = function attach(app) { | ||
var self = this; | ||
Object.keys(this.routes).forEach(function(actionName) { | ||
var middleware = [].concat(self.middleware()); | ||
var action = self.actions[actionName]; | ||
var route = self.routes[actionName]; | ||
var middleware = [].concat(self.middleware()), | ||
action = self.actions[actionName], | ||
route = self.routes[actionName]; | ||
action.groups.concat([actionName]).forEach(function(group) { | ||
middleware.concat(self.middleware(group)); | ||
middleware.push.apply(middleware, self.middleware(group)); | ||
}); | ||
@@ -40,0 +58,0 @@ var path = route.path; |
{ | ||
"name": "controller", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "an action controller for express", | ||
@@ -14,4 +14,4 @@ "main": "lib/controller.js", | ||
"devDependencies": { | ||
"mocha":"*" | ||
"mocha": "*" | ||
} | ||
} |
@@ -6,2 +6,5 @@ # controller | ||
This code sets up an app with 3 handlers, 4 routes, and some middleware which | ||
applies to different handler groups. | ||
```javascript | ||
@@ -56,3 +59,3 @@ var express = require('express'); | ||
The `Controller` function can also take an `options` paramete. Available | ||
The `Controller` function can also take an `options` parameter. Available | ||
options are: | ||
@@ -64,2 +67,12 @@ | ||
Example with options: | ||
```javascript | ||
var users = controller({ prefix: '/user/' }); | ||
users.direct('get', '/:id', function(req,res) { | ||
res.send(Users.read(req.params.id)); | ||
}) | ||
``` | ||
--- | ||
@@ -99,4 +112,15 @@ | ||
Define some middleware for a group. If `middleware` is not defined, an array of | ||
middleware for the group is returned instead. | ||
middleware for the group is returned instead. If `group` is not defined, | ||
the `'all'` group is returned - this is a group of middleware which applies to | ||
all handlers. | ||
The array that is returned can also be used to add more middleware. | ||
The order that middleware is added is as follows: | ||
1. Controller-wide middleware under the 'all' group. | ||
2. Group middleware, in the order the middleware was added, in the order the | ||
groups were specified when the handler was defined. | ||
3. Handler-specific middleware that was defined only for this handler. | ||
__Paramaters__ | ||
@@ -114,2 +138,3 @@ * `group` *optional* - defaults to `'all'` | ||
users.middleware('require-login'); // -> [ [Function checkLoggedIn] ] | ||
users.middleware('require-login').push(function(req,res,next) {}); | ||
@@ -176,3 +201,3 @@ // Define some middleware for all routes | ||
Attach the routes to an express app. Note that after calling this function, | ||
making changes to the routes the controller will do nothing. | ||
making changes to the routes on the controller will do nothing. | ||
@@ -22,3 +22,3 @@ var assert = require('assert'); | ||
}); | ||
it('should apply an action and middleware at a route', function() { | ||
it('should call the right routing functions', function() { | ||
var c = ctrl(); | ||
@@ -28,4 +28,3 @@ var action = function someAction(){}; | ||
c.define('action', ['thing'], action); | ||
c.middleware('thing').push(doThing); | ||
c.route('get', '/action', 'action'); | ||
c.route('GET', '/action', 'action'); | ||
@@ -35,5 +34,2 @@ var anapp = app(); | ||
anapp.on('get', function(route, mw, theAction) { | ||
assert(route === '/action'); | ||
assert.deepEqual(mw, [doThing]); | ||
assert(action === theAction); | ||
didCall = true; | ||
@@ -45,2 +41,168 @@ }); | ||
}); | ||
describe('direct()', function() { | ||
it('should allow direct attachment', function() { | ||
var c = ctrl(); | ||
var fn = function() {} | ||
c.direct('get', '/action', fn); | ||
var anapp = app(); | ||
var didCall = false; | ||
anapp.on('get', function(route, mw, theAction) { | ||
assert(fn === theAction); | ||
assert(route === '/action'); | ||
didCall = true; | ||
}); | ||
c.attach(anapp); | ||
assert(didCall === true); | ||
}) | ||
it('should allow more than one direct attachment', function() { | ||
var c = ctrl(); | ||
var fn = function() {} | ||
var fn2 = function() {} | ||
var fn3 = function() {} | ||
var fn4 = function() {} | ||
c.direct('get', '/action', fn3, fn); | ||
c.direct('post', '/action2', fn4, fn2); | ||
var anapp = app(); | ||
var didCall = 0; | ||
anapp.on('get', function(route, mw, theAction) { | ||
assert(fn === theAction); | ||
assert(route === '/action'); | ||
assert.deepEqual(mw, [fn3]); | ||
didCall++; | ||
}); | ||
anapp.on('post', function(route, mw, theAction) { | ||
assert(fn2 === theAction); | ||
assert(route === '/action2'); | ||
assert.deepEqual(mw, [fn4]); | ||
didCall++; | ||
}); | ||
c.attach(anapp); | ||
assert(didCall === 2); | ||
}) | ||
it('should allow direct attachment mixing groups and fns', function() { | ||
var c = ctrl(); | ||
var fn = function() {}; | ||
var fn1 = function() {}; | ||
var fn2 = function() {}; | ||
var fn3 = function() {}; | ||
c.middleware('gr1', fn1); | ||
c.middleware('gr2', fn2); | ||
c.direct('get', '/action', 'gr1', fn3, 'gr2', fn); | ||
var anapp = app(); | ||
var didCall = false; | ||
anapp.on('get', function(route, mw, theAction) { | ||
assert.deepEqual(mw, [fn1, fn2, fn3]) | ||
assert(theAction === fn) | ||
didCall = true; | ||
}); | ||
c.attach(anapp); | ||
assert(didCall === true); | ||
}) | ||
}); | ||
describe('middleware()', function() { | ||
it('should return an array of middlewares when not adding', function() { | ||
assert(Array.isArray(ctrl().middleware())); | ||
var c = ctrl(); | ||
var fn = function() {}; | ||
c.middleware().push(fn); | ||
assert.deepEqual(c.middleware(), [fn]); | ||
}); | ||
it('should allow for groups', function() { | ||
var c = ctrl(); | ||
var fn = function() {} | ||
c.middleware('group').push(fn); | ||
assert.deepEqual(c.middleware('group'), [fn]); | ||
}) | ||
it('should allow middleware to be added directly', function() { | ||
var c = ctrl(); | ||
var fn = function() {} | ||
c.middleware('group', fn); | ||
assert.deepEqual(c.middleware('group'), [fn]); | ||
c = ctrl(); | ||
c.middleware(fn); | ||
assert.deepEqual(c.middleware(), [fn]); | ||
}) | ||
it('should apply grouped middleware at attach', function() { | ||
var c = ctrl(); | ||
var action = function someAction(){}; | ||
var doThing = function thing(){}; | ||
c.define('action', ['thing'], action); | ||
c.middleware('thing').push(doThing); | ||
c.route('get', '/action', 'action'); | ||
var anapp = app(); | ||
var didCall = false; | ||
anapp.on('get', function(route, mw, theAction) { | ||
assert(route === '/action'); | ||
assert.deepEqual(mw, [doThing]); | ||
assert(action === theAction); | ||
didCall = true; | ||
}); | ||
c.attach(anapp); | ||
assert(didCall === true); | ||
}); | ||
it('should apply global middleware at attach', function() { | ||
var c = ctrl(); | ||
var action = function someAction(){}; | ||
var doThing = function thing(){}; | ||
c.define('action', ['thing'], action); | ||
c.middleware(doThing); | ||
c.route('get', '/action', 'action'); | ||
var anapp = app(); | ||
var didCall = false; | ||
anapp.on('get', function(route, mw, theAction) { | ||
assert.deepEqual(mw, [doThing]); | ||
didCall = true; | ||
}); | ||
c.attach(anapp); | ||
assert(didCall === true); | ||
}); | ||
it('should apply handler specific middleware at attach', function() { | ||
var c = ctrl(); | ||
var action = function someAction(){}; | ||
var doThing = function thing(){}; | ||
c.define('action', ['thing'], action); | ||
c.middleware('action', doThing); | ||
c.route('get', '/action', 'action'); | ||
var anapp = app(); | ||
var didCall = false; | ||
anapp.on('get', function(route, mw, theAction) { | ||
assert.deepEqual(mw, [doThing]); | ||
didCall = true; | ||
}); | ||
c.attach(anapp); | ||
assert(didCall === true); | ||
}); | ||
it('should apply middleware in the correct order', function() { | ||
var c = ctrl(); | ||
var action = function someAction(){}; | ||
var doThing1 = function thing1(){}; | ||
var doThing2 = function thing2(){}; | ||
var doThing3 = function thing3(){}; | ||
c.define('action', ['thing'], action); | ||
c.middleware('action', doThing3); | ||
c.middleware(doThing1); | ||
c.middleware('thing', doThing2); | ||
c.route('get', '/action', 'action'); | ||
var anapp = app(); | ||
var didCall = false; | ||
anapp.on('get', function(route, mw, theAction) { | ||
assert.deepEqual(mw, [doThing1, doThing2, doThing3]); | ||
didCall = true; | ||
}); | ||
c.attach(anapp); | ||
assert(didCall === true); | ||
}); | ||
}) | ||
}) |
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
15011
252
198