0.2.0 • Published 9 years ago
vez v0.2.0
vez
Middleware composition at new level. Ultimate alternative to
ware
,plugins
,koa-compose
andcomposition
packages. Allows you to use callbacks, promises, generators and async/await functions as middlewares.
Install
npm i vez --save
npm test
Usage
For more use-cases see the tests
var vez = require('vez')
var assert = require('assert')
var Bluebird = require('bluebird')
vez()
.use(Bluebird.resolve(123))
.use(function () {
assert.deepEqual(this, {a: 'b', c: 'd', e: 'f'})
return Bluebird.resolve(456)
})
.use(function (foo, next) {
assert.deepEqual(this, {a: 'b', c: 'd', e: 'f'})
next(null, foo, 789)
})
.use(function * (first, second) {
this.g = first + second
assert.deepEqual(this, {a: 'b', c: 'd', e: 'f', g: 1245})
// because generators are handled by `co@4.6`
return yield {
gens: this.g
}
})
.run({a: 'b'}, {c: 'd'}, {e: 'f'}, function (err, res) {
if (err) {
return console.error(err)
}
assert.deepEqual(this, {a: 'b', c: 'd', e: 'f', g: 1245})
assert.deepEqual(res, [123, 456, [ 456, 789 ], { gens: 1245 }])
done()
})
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.