Skip to content
This repository has been archived by the owner on Oct 21, 2023. It is now read-only.

Commit

Permalink
middleware initialization methods now take App as first parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddentao committed Sep 12, 2016
1 parent 126e07a commit 8d06831
Show file tree
Hide file tree
Showing 16 changed files with 35 additions and 35 deletions.
4 changes: 2 additions & 2 deletions docs/Middleware/Configuring.md
Expand Up @@ -7,7 +7,7 @@ When initializing middleware it's possible to pass in configuration options to t

const parser = require('co-body');

module.exports = function(options) {
module.exports = function(App, options) {
return function*(next) {
this.request.body = yield parser(this, options);

Expand All @@ -20,7 +20,7 @@ In order to construct and use this middleware we would need to pass in options t

```javascript
// let's go for a 16MB request body size limit
const middlewareFn = waigo.load('support/middleware/bodyParser')({
const middlewareFn = waigo.load('support/middleware/bodyParser')(App, {
limit: '16mb'
});
```
Expand Down
12 changes: 6 additions & 6 deletions docs/Middleware/Defining.md
Expand Up @@ -9,7 +9,7 @@ For example:
```javascript
// file: <project folder>/src/support/middleware/example.js

module.exports = function(options) {
module.exports = function(App, options) {
return function*(next) {
// do nothing and pass through
yield next;
Expand All @@ -22,7 +22,7 @@ The above middleware doesn't do anything and simply passes control to the next h
If we wanted to catch and handle any errors thrown by subsequent handlers in the chain we could do:

```javascript
module.exports = function(options) {
module.exports = function(App, options) {
return function*(next) {
try {
yield next;
Expand All @@ -36,7 +36,7 @@ module.exports = function(options) {
Middleware functions in Waigo work in the same way as ones in Koa, in that they have a defined context and associated `request` and `response` objects:

```javascript
module.exports = function(options) {
module.exports = function(App, options) {
return function*(next) {
console.log(`URL: ${this.request.url}`);

Expand All @@ -45,15 +45,15 @@ module.exports = function(options) {
};
```

Thys, existing Koa middleware functions can be re-used very easily within Waigo.
Thus, existing Koa middleware functions can be re-used very easily within Waigo.

## App object

Middleware functions in Waigo also have access to your application's `App` object, which is set on the context:
Middleware functions in Waigo also have access to your application's `App` object, which is set on the context as well as being passed in during the construction phase:


```javascript
module.exports = function(options) {
module.exports = function(App, options) {
return function*(next) {
console.log(`Running in mode: ${this.App.config.mode}`);

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "waigo",
"description": "Node.js ES6 framework for reactive, data-driven apps and APIs (Koa, RethinkDB)",
"version": "2.1.10",
"version": "2.2.0",
"license": "MIT",
"author": {
"name": "Ramesh Nair",
Expand Down
2 changes: 1 addition & 1 deletion src/support/middleware/assertUser.js
Expand Up @@ -21,7 +21,7 @@ const waigo = global.waigo,
*
* @return {Function}
*/
module.exports = function(options) {
module.exports = function(App, options) {
return function*(next) {
this.App.logger.debug('assertUser is logged in');

Expand Down
2 changes: 1 addition & 1 deletion src/support/middleware/bodyParser.js
Expand Up @@ -23,7 +23,7 @@ const waigo = global.waigo,
*
* @return {Function} middleware
*/
var fn = module.exports = function(options) {
var fn = module.exports = function(App, options) {
return function*(next) {
this.request.body = yield fn._bodyParser(this, options);

Expand Down
2 changes: 1 addition & 1 deletion src/support/middleware/outputFormats.js
Expand Up @@ -28,7 +28,7 @@ const OutputFormatError = errors.define('OutputFormatError');
*
* @return {Function} Express middleware.
*/
module.exports = function(options) {
module.exports = function(App, options) {
let enabledFormats = {};

let formatNames = Object.keys(options.formats);
Expand Down
5 changes: 2 additions & 3 deletions src/support/middleware/sessions.js
Expand Up @@ -25,10 +25,9 @@ const waigo = global.waigo;
* @param {Object} options.cookie Session cookie options.
* @param {Integer} options.cookie.validForDays No. of days cookie remains valid for.
* @param {String} options.cookie.path Cookie path.
* @param {Application} The active application instance.
*/
module.exports = function(options) {
let App = waigo.App;

module.exports = function(App, options) {
if (!options.keys) {
throw new Error('Please specify cookie signing keys in the config file.');
}
Expand Down
2 changes: 1 addition & 1 deletion src/support/middleware/staticResources.js
Expand Up @@ -22,7 +22,7 @@ const waigo = global.waigo,
*
* @return {Function} middleware
*/
module.exports = function(options) {
module.exports = function(App, options) {
let moreOptions = _.extend({
maxage: 0,
hidden: false,
Expand Down
2 changes: 1 addition & 1 deletion src/support/routeMapper.js
Expand Up @@ -117,7 +117,7 @@ class RouteMapper {
middlewareOptions = middlewareOptions || {};
}

return waigo.load(`support/middleware/${middlewareName}`)(middlewareOptions);
return waigo.load(`support/middleware/${middlewareName}`)(this.App, middlewareOptions);
}


Expand Down
1 change: 1 addition & 0 deletions src/support/startup/middleware.js
Expand Up @@ -17,6 +17,7 @@ module.exports = function*(App) {
App.logger.debug(`Loading middleware: ${m}`);

App.koa.use(waigo.load(`support/middleware/${m}`)(
App,
_.get(App.config.middleware.ALL, m, {})
));
}
Expand Down
10 changes: 5 additions & 5 deletions test/unit/support/middleware/assertUser.test.js
Expand Up @@ -36,7 +36,7 @@ test['assert user'] = {

'user must be logged in': function*() {
yield this.shouldThrow(
assertUser({}).call(this.ctx, Q.resolve()),
assertUser(this.App, {}).call(this.ctx, Q.resolve()),
'You must be logged in'
);
},
Expand All @@ -48,7 +48,7 @@ test['assert user'] = {
assertAccess: spy,
};

yield assertUser({canAccess: 'admin'}).call(this.ctx, Q.resolve());
yield assertUser(this.App, {canAccess: 'admin'}).call(this.ctx, Q.resolve());

spy.should.have.been.calledWithExactly('admin');
},
Expand All @@ -58,7 +58,7 @@ test['assert user'] = {

this.ctx.currentUser = {};

yield assertUser({}).call(this.ctx, function*() {
yield assertUser(this.App, {}).call(this.ctx, function*() {
count++;
});

Expand All @@ -80,7 +80,7 @@ test['assert user'] = {

let routeSpy = this.mocker.spy(this.App.routes, 'url');

yield assertUser({
yield assertUser(this.App, {
redirectToLogin: true,
canAccess: 'admin'
}).call(this.ctx);
Expand All @@ -105,7 +105,7 @@ test['assert user'] = {
},
});

yield this.shouldThrow(assertUser({
yield this.shouldThrow(assertUser(this.App, {
canAccess: 'admin'
}).call(this.ctx), 'blah');
},
Expand Down
14 changes: 7 additions & 7 deletions test/unit/support/middleware/outputFormats.test.js
Expand Up @@ -41,7 +41,7 @@ test['output formats'] = {

'invalid format in config': function() {
this.expect(function() {
outputFormats({
outputFormats({}, {
formats: {
html3: true
}
Expand All @@ -50,7 +50,7 @@ test['output formats'] = {
},

'uses default format when not specified': function*() {
var fn = outputFormats({
var fn = outputFormats(this.App, {
paramName: 'format',
default: 'json',
formats: {
Expand All @@ -72,7 +72,7 @@ test['output formats'] = {
},

'invalid format in request': function*() {
var fn = outputFormats({
var fn = outputFormats(this.App, {
paramName: 'format',
default: 'json',
formats: {
Expand All @@ -93,7 +93,7 @@ test['output formats'] = {


'custom format': function*() {
var fn = outputFormats({
var fn = outputFormats(this.App, {
paramName: 'format',
default: 'json',
formats: {
Expand All @@ -116,7 +116,7 @@ test['output formats'] = {


'override format after middleware is setup': function*() {
var fn = outputFormats({
var fn = outputFormats(this.App, {
paramName: 'format',
default: 'json',
formats: {
Expand Down Expand Up @@ -144,7 +144,7 @@ test['output formats'] = {
'converts locals to view objects if possible': function*() {
const toViewObjectMethodName = waigo.load('support/viewObjects').METHOD_NAME;

var fn = outputFormats({
var fn = outputFormats(this.App, {
paramName: 'format',
default: 'json',
formats: {
Expand Down Expand Up @@ -199,7 +199,7 @@ test['output formats'] = {


'redirect to url': function*() {
var fn = outputFormats({
var fn = outputFormats(this.App, {
paramName: 'format',
default: 'json',
formats: {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/support/middleware/sessions.test.js
Expand Up @@ -41,7 +41,7 @@ test['sessions'] = {

'verifies that cookie signing keys are set': function*() {
this.expect(function() {
middleware({});
middleware({}, {});
}).to.throw('Please specify cookie signing keys in the config file.');
},
'default': function*() {
Expand All @@ -66,7 +66,7 @@ test['sessions'] = {
}
};

var fn = middleware(options);
var fn = middleware(this.App, options);

this.App.koa.keys.should.eql(['my', 'key']);
createStoreSpy.should.have.been.calledOnce;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/support/middleware/staticResources.test.js
Expand Up @@ -22,7 +22,7 @@ test['static resources middleware'] = {

var pathJoinSpy = this.mocker.spy(path, 'join');

var fn = m({
var fn = m(this.App, {
folder: 'static'
});

Expand Down
2 changes: 1 addition & 1 deletion test/unit/support/routeMapper.test.js
Expand Up @@ -23,7 +23,7 @@ test['route mapper'] = {
'controllers/hello': 'exports.world = function*() { this.mega.push("hello world"); };',
'controllers/good': 'exports.bye = function*() { this.mega.push("goodbye"); };',
'support/middleware/before_next': 'module.exports = function() { return function*(next) { this.mega.push("test"); yield next; }; };',
'support/middleware/after_next': 'module.exports = function(o) { return function*(next) { yield next; this.mega.push("test_" + o.num); }; };'
'support/middleware/after_next': 'module.exports = function(App, o) { return function*(next) { yield next; this.mega.push("test_" + o.num); }; };'
});

yield this.initApp();
Expand Down
4 changes: 2 additions & 2 deletions test/unit/support/startup/middleware.test.js
Expand Up @@ -15,8 +15,8 @@ const waigo = global.waigo;
test['middleware'] = {
beforeEach: function*() {
this.createAppModules({
'support/middleware/test1': 'module.exports = function(options) { return function*() { return ["test1", options, arguments[0]]; }; };',
'support/middleware/test2': 'module.exports = function(options) { return function*() { return ["test2", options, arguments[0]]; }; };',
'support/middleware/test1': 'module.exports = function(App, options) { return function*() { return ["test1", options, arguments[0]]; }; };',
'support/middleware/test2': 'module.exports = function(App, options) { return function*() { return ["test2", options, arguments[0]]; }; };',
});

yield this.initApp();
Expand Down

0 comments on commit 8d06831

Please sign in to comment.