Middleware pattern implementation, robust, easy, fast. You can add two types of middleware, a single queue type using the keyword use() or a Before/After type using before() and after() hooks. All middleware accept promises or vanilla callbacks and final resolution is done using the Promises/A+ spec.

The middlewarify.make() method will apply the middleware pattern to an Object's property, this property will be called the Middleware Container.

// create a Middleware Container

var crud ={};

middlewarify.make(crud,'create');

This example has created the Middleware Container create in the object crud. crud.create() is a function that will invoke all the middleware.

You can pass a third argument, the optMainCallback, a Function. This will be the Main callback of your middleware, the result returned, or resolved if a promise is used, will get passed to the final promise:

crud.create().then(function(val){

// this is the final promise.

// val is passed from the Main callback.

});

optOptions defines behavior. Both optOptions and optMainCallback are optional and can be interswitched, i.e. you can pass options as a third argument, read on for examples and what are the available options.

The Middleware Container by default exposes a use hook so you can add any number of middleware. use() accepts any number of parameters as long they are of type Function or Array of Functions. When the Before/After flag is enabled use is no longer there and instead you get before and after hooks. All three hook types accept the same argument types and patterns as described bellow.

// create the Middleware Container

var crud ={};

middlewarify.make(crud,'create', fnFinal);

// add 3 middleware functions

crud.create.use([fn1, fn2], fn3);

// then add another one

crud.create.use(fn4);

In the above example we added 4 middleware before the final method fnFinal will be invoked. A FIFO queue is implemented so the order of execution will be: