middlewarify
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -62,8 +62,11 @@ /** | ||
* @param {Function} done Callback. | ||
* @param {...*} optMiddArgs Arguments passed from the last middleware. | ||
* @private | ||
*/ | ||
middlewarify._fetchAndInvoke = function(midds, args, done) { | ||
middlewarify._fetchAndInvoke = function(midds, args, done, optMiddArgs) { | ||
var lastMiddArgs = optMiddArgs || []; | ||
if (0 === midds.length) { | ||
return done(); | ||
lastMiddArgs.unshift(null); | ||
return done.apply(null, lastMiddArgs); | ||
} | ||
@@ -76,3 +79,4 @@ try { | ||
} else { | ||
middlewarify._fetchAndInvoke(midds, args, done); | ||
var middArgs = Array.prototype.slice.call(arguments, 1); | ||
middlewarify._fetchAndInvoke(midds, args, done, middArgs); | ||
} | ||
@@ -79,0 +83,0 @@ })); |
{ | ||
"name": "middlewarify", | ||
"description": "Apply the middleware pattern to any function.", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"homepage": "https://github.com/thanpolas/middlewarify", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -107,11 +107,20 @@ # Middlewarify | ||
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. | ||
The optional callback should always be defined last, it gets invoked when all middleware have finished. It provides at least one argument, `err` which if has a truthy value (typically an instance of `Error`) meas that something did not go well. | ||
**The last middleware** to be invoked can pass arguments to the *Create Callback* like so: | ||
```js | ||
var crud = {}; | ||
middlewarify.make(crud, 'create'); | ||
var lastMiddlware = function(next) { | ||
/* ... */ | ||
next(null, 'one', 'two'); | ||
}); | ||
middlewarify.make(crud, 'create', lastMiddlware); | ||
// run all middleware | ||
crud.create(userDataObject, function(err) { | ||
crud.create(userDataObject, function(err, arg1, arg2) { | ||
if (err) { /* tough love */ } | ||
arg1 === 'one'; // true | ||
arg2 === 'two'; // true | ||
}); | ||
@@ -136,3 +145,3 @@ ``` | ||
## Release History | ||
- **v0.0.1**, *TBD JuL 2013* | ||
- **v0.0.2**, *15 JuL 2013* | ||
- Big Bang | ||
@@ -139,0 +148,0 @@ |
67
test.js
@@ -97,12 +97,2 @@ /** | ||
teardown(function(){ | ||
assert.ok(firstMidd.calledOnce, 'firstMidd should be called only once'); | ||
assert.ok(secondMidd.calledOnce, 'secondMidd should be called only once'); | ||
assert.ok(thirdMidd.calledOnce, 'thirdMidd should be called only once'); | ||
assert.ok(lastMidd.calledOnce, 'lastMidd should be called only once'); | ||
assert.ok(firstMidd.calledBefore(secondMidd), 'firstMidd should be called before secondMidd'); | ||
assert.ok(secondMidd.calledAfter(firstMidd), 'secondMidd should be called after firstMidd'); | ||
assert.ok(thirdMidd.calledAfter(secondMidd), 'thirdMidd should be called after secondMidd'); | ||
assert.ok(lastMidd.calledAfter(thirdMidd), 'lastMidd should be called after thirdMidd'); | ||
}); | ||
@@ -113,3 +103,2 @@ | ||
var bar = {b: 2}; | ||
obj.create(1, foo, bar, function(err){ | ||
@@ -124,5 +113,61 @@ assert.notOk(err, 'error should not be truthy'); | ||
callAll(3); | ||
}); | ||
}); | ||
suite('4. Final middleware arguments', function(){ | ||
test('4.1 Last middleware passes arguments to create callback', function(done) { | ||
var obj = Object.create(null); | ||
midd.make(obj, 'create', function(cb){ | ||
cb(null, 1, 2); | ||
}); | ||
obj.create(function(err, arg1, arg2){ | ||
assert.equal(1, arg1, 'Arg1 should be 1'); | ||
assert.equal(2, arg2, 'Arg2 should be 2'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
suite('5. Failing cases', function(){ | ||
var obj; | ||
setup(function(){ | ||
obj = Object.create(null); | ||
midd.make(obj, 'create'); | ||
}); | ||
test('5.1 a middleware throws an error', function(){ | ||
obj.create.use(function(){ | ||
throw new Error('an error'); | ||
}); | ||
obj.create(function(err){ | ||
assert.instanceOf(err, Error, '"err" should be instanceOf Error'); | ||
assert.equal(err.message, 'an error', 'Error message should match'); | ||
}); | ||
}); | ||
test('5.2 a middleware calls next with an error', function(){ | ||
obj.create.use(function(next){ | ||
next(new Error('an error')); | ||
}); | ||
obj.create(function(err){ | ||
assert.instanceOf(err, Error, '"err" should be instanceOf Error'); | ||
assert.equal(err.message, 'an error', 'Error message should match'); | ||
}); | ||
}); | ||
test('5.3 a failing middleware prevents rest of middleware from executing', function(){ | ||
obj.create.use(function(next){ | ||
next(new Error('an error')); | ||
}); | ||
var middSpy = sinon.spy(); | ||
obj.create.use(middSpy); | ||
obj.create(function(){ | ||
assert.notOk(middSpy.called, 'second middleware should not be called'); | ||
}); | ||
}); | ||
}); |
17647
276
158