File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -30,7 +30,7 @@ function compose (middleware) {
30
30
* @api public
31
31
*/
32
32
33
- return function ( context , next ) {
33
+ let ret = function ( context , next ) {
34
34
// last called middleware #
35
35
let index = - 1
36
36
return dispatch ( 0 )
@@ -48,4 +48,10 @@ function compose (middleware) {
48
48
}
49
49
}
50
50
}
51
+ ret . onUsed = function ( useCtx ) {
52
+ if ( useCtx . prepareMiddleware ) {
53
+ middleware = middleware . map ( ( mw ) => useCtx . prepareMiddleware ( mw ) )
54
+ }
55
+ }
56
+ return ret
51
57
}
Original file line number Diff line number Diff line change @@ -256,4 +256,24 @@ describe('Koa Compose', function () {
256
256
val . should . equal ( 1 )
257
257
} )
258
258
} )
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
+ } )
259
279
} )
You can’t perform that action at this time.
0 commit comments