From 92d70e38e1385da975d48c2992dccf1cbac73c26 Mon Sep 17 00:00:00 2001 From: Lee Bousfield Date: Tue, 12 Apr 2016 15:57:28 -0600 Subject: [PATCH] Added on used support --- index.js | 8 +++++++- test/test.js | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 16537e4..e7f4640 100644 --- a/index.js +++ b/index.js @@ -30,7 +30,7 @@ function compose (middleware) { * @api public */ - return function (context, next) { + let ret = function (context, next) { // last called middleware # let index = -1 return dispatch(0) @@ -48,4 +48,10 @@ function compose (middleware) { } } } + ret.onUsed = function (useCtx) { + if (useCtx.prepareMiddleware) { + middleware = middleware.map((mw) => useCtx.prepareMiddleware(mw)) + } + } + return ret } diff --git a/test/test.js b/test/test.js index 2590b52..2a27f8c 100644 --- a/test/test.js +++ b/test/test.js @@ -256,4 +256,25 @@ describe('Koa Compose', function () { val.should.equal(1) }) }) + + it('should accept any onUsed object', function () { + var composed = compose([(ctx, next) => next()]) + composed.onUsed({}) + return composed({}) + }) + + it('should prepare the middleware', function () { + var mw = (ctx) => { ctx.body = 'body' } + var composed = compose([mw]) + var prepared + var useContext = { + app: {}, + useChain: [], + prepareMiddleware: function (toPrepare) { + assert.equal(toPrepare, mw) + } + } + composed.onUsed(useContext) + return composed({}, () => {}) + }) })