Middlewarify
Apply the middleware pattern to any function.
Install
npm install middlewarify --save
Documentation
Apply the middleware pattern:
var midd = require('middlewarify');
var tasks = module.exports = {};
tasks._create = function() {
console.log('tasks._create');
};
midd.make(tasks, 'create', tasks._create);
...Add middleware
var tasks = require('./tasks');
tasks.create.use(function(done){
console.log('middleware 1');
done();
});
tasks.create.use(function(done){
console.log('middleware 2');
done();
});
... Invoke all the middleware
tasks.create(function(err){
});
Methods
make(object, property, optFinalCallback)
The middlewarify.make()
method will apply the middleware pattern to an Object's property.
var crud = {};
middlewarify.make(crud, 'create');
middlewarify.make(crud, 'read');
middlewarify.make(crud, 'update');
middlewarify.make(crud, 'delete');
Each time make()
is used two new functions are added to the crud
Object:
crud.create([, ...], optCallback)
This method will invoke all added middleware in the sequence they were defined. If the final argument is a Function, it will be treated as a callback after all middleware have finished. crud.create( function( err ){ /* check err */ } )
crud.create.use(middleware [, ...])
This method will add middleware Functions to the crud.create
container.
The use(fn) Method
The middleware container exposes a use
method to add middleware. use()
accepts any number of parameters as long they are type Function or Arrays of Functions.
var crud = {};
middlewarify.make(crud, 'create');
crud.create.use([fn1, fn2], fn3);
Invoking the callbacks
The middleware container is a function that accepts any number of arguments and an optional callback.
Any argument passed to the middleware container will also be passed to all middleware.
var crud = {};
middlewarify.make(crud, 'create');
crud.create(userDataObject);
The optional callback should always be defined last, it gets invoked when all middleware have finished. It provides one argument, the err
which if has a truthy value (typically an instance of Error
) meas that something did not go well.
var crud = {};
middlewarify.make(crud, 'create');
crud.create(userDataObject, function(err) {
if (err) { }
});
The Middleware Callback
When adding a middleware
Function (i.e. crud.create.use(middleware)
) by default it will be invoked with only one argument, the next
callback. If you add arguments when calling the middleware container crud.create(arg1, arg2)
, all middleware callbacks will be invoked with these arguments first. The next
callback will always be last.
Invoking next() with a truthy argument (i.e. next(err)
) will stop invoking more middleware. The flow is passed to the optFinalCallback
if defined in the make()
method or silently dismissed.
crud.create.use(function(arg1, arg2, next) {
if (arg1 !== arg2) { return next(new Error('not a match'))}
next();
});
crud.create(foo, bar);
Release History
License
Copyright 2013 Thanasis Polychronakis
Licensed under the MIT License