Skip to content

Commit a29bb3b

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

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-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))
54+
}
55+
}
56+
return ret
5157
}

test/test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,24 @@ 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 useContext = {
270+
app: {},
271+
useChain: [],
272+
prepareMiddleware: function (toPrepare) {
273+
assert.equal(toPrepare, mw)
274+
}
275+
}
276+
composed.onUsed(useContext)
277+
return composed({}, () => {})
278+
})
259279
})

0 commit comments

Comments
 (0)