Deqorator
A very lightweight queued decorator for Node.js to easily decorate objects using middleware.
To make you feel immediately familiar with the API, the syntax is based on the Express.js middleware syntax.
What is deqorator used for?
Suppose you have an object and you want to decorate it with data (e.g. fill properties with data from several database queries).
Instead of nesting all kinds of callbacks, deqorator allows you to easily create a reusable decorator using one or more layers of middleware.
Middleware layers are processed in the order you define them, so you can easily depend on data you added in a previous layer.
All without worrying about asynchronous callbacks.
Quick demo
First create a decorator:
var deqorator = ; decorator = deqorator;
Then add as many middleware handlers as you like:
decorator;
Asynchronous callbacks are supported, just call next()
when finished:
decorator;
If you want to raise an error, just call next()
with an error and all subsequent middleware handlers are skipped:
decorator;
Each middleware handler is passed the item and is processed in the order you add it to the decorator
:
decorator;
Once you have defined all the middleware for the decorator, you can use it to decorate as many objects as you like.
The callback of the decorate()
function is called when the object has been completely decorated or when an error was raised:
var item = {}; decorator;
Method of operation
There is no need to worry about asynchronous behavior:
- all middleware handlers are processed in the order you define them.
- all objects are decorated in the order you decorate them.
So a scenario like this:
var item1 = {} item2 = {} item3 = {} decorator = { ... } { ... } { ... }; decorator;decorator;decorator; decorator;decorator;decorator;
will be processed like this:
item1
is decorated:middleware1
function called withitem1
middleware2
function called withitem1
middleware3
function called withitem1
- if a
decorate
callback is defined, it is called with(null, item1)
- if an error occurs in one of the middleware handlers and a
decorate
callback is defined, it is immediately called with(err, item1)
and subsequent middleware handlers are skipped
item2
is decorated:middleware1
function called withitem2
middleware2
function called withitem2
middleware3
function called withitem2
- if a
decorate
callback is defined, it is called with(null, item2)
- if an error occurs in one of the middleware handlers and a
decorate
callback is defined, it is immediately called with(err, item2)
and subsequent middleware handlers are skipped
item3
is decorated:middleware1
function called withitem3
middleware2
function called withitem3
middleware3
function called withitem3
- if a
decorate
callback is defined, it is called with(null, item3)
- if an error occurs in one of the middleware handlers and a
decorate
callback is defined, it is immediately called with(err, item3)
and subsequent middleware handlers are skipped
Change log
v1.0.0
- Added factory function to create new decorators
- Updated API for user convenience
v0.3.0
- Removed error handling from middleware
v0.2.0
- Added error handling
- Updated documentation
- Added additional unit tests
v0.1.0
- Initial version