Skip to content

Commit a8466f2

Browse files
committed
Added on used support
1 parent 8c7e940 commit a8466f2

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function compose (middleware) {
3030
* @api public
3131
*/
3232

33-
return function (context, next) {
33+
let ret = function (context, next) {
3434
// last called middleware #
3535
let index = -1
3636
return dispatch(0)
@@ -48,4 +48,10 @@ function compose (middleware) {
4848
}
4949
}
5050
}
51+
ret.onUsed = function (useCtx) {
52+
if (useCtx.prepareMiddleware) {
53+
middleware = middleware.map((mw) => useCtx.prepareMiddleware(mw, 'compose'))
54+
}
55+
}
56+
return ret
5157
}

test/test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,28 @@ describe('Koa Compose', function () {
256256
val.should.equal(1)
257257
})
258258
})
259+
260+
it('should accept any onUsed object', function () {
261+
var composed = compose([(ctx, next) => next()])
262+
composed.onUsed({})
263+
return composed({})
264+
})
265+
266+
it('should prepare the middleware', function () {
267+
var mw = (ctx) => { ctx.body = 'body' }
268+
var composed = compose([mw])
269+
var prepared
270+
var useContext = {
271+
app: {},
272+
useChain: [],
273+
prepareMiddleware: function (mw, name) {
274+
prepared = mw
275+
assert.equal(name, 'compose')
276+
}
277+
}
278+
composed.onUsed(useContext)
279+
return composed({}, () => {}).then(function () {
280+
assert.equal(prepared, mw)
281+
})
282+
})
259283
})

0 commit comments

Comments
 (0)