Skip to content

Commit 5e37230

Browse files
shfshanyuemiwnwski
authored andcommitted
chore: allow the compose function to be configurable
1 parent 30242ee commit 5e37230

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

__tests__/application/compose.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
'use strict'
3+
4+
const request = require('supertest')
5+
const assert = require('assert')
6+
const Koa = require('../..')
7+
8+
describe('app.compose', () => {
9+
it('should work with default compose ', async () => {
10+
const app = new Koa()
11+
const calls = []
12+
13+
app.use((ctx, next) => {
14+
calls.push(1)
15+
return next().then(() => {
16+
calls.push(4)
17+
})
18+
})
19+
20+
app.use((ctx, next) => {
21+
calls.push(2)
22+
return next().then(() => {
23+
calls.push(3)
24+
})
25+
})
26+
27+
const server = app.listen()
28+
29+
await request(server)
30+
.get('/')
31+
.expect(404)
32+
33+
assert.deepStrictEqual(calls, [1, 2, 3, 4])
34+
})
35+
36+
it('should work with configurable compose', async () => {
37+
const calls = []
38+
let count = 0
39+
const app = new Koa({
40+
compose (fns){
41+
return async (ctx) => {
42+
const dispatch = async () => {
43+
count++
44+
const fn = fns.shift()
45+
fn && fn(ctx, dispatch)
46+
}
47+
dispatch()
48+
}
49+
}
50+
})
51+
52+
app.use((ctx, next) => {
53+
calls.push(1)
54+
next()
55+
calls.push(4)
56+
})
57+
app.use((ctx, next) => {
58+
calls.push(2)
59+
next()
60+
calls.push(3)
61+
})
62+
63+
const server = app.listen()
64+
65+
await request(server)
66+
.get('/')
67+
68+
assert.deepStrictEqual(calls, [1, 2, 3, 4])
69+
assert.equal(count, 3)
70+
})
71+
})

__tests__/application/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ describe('app', () => {
7878
assert.strictEqual(app.subdomainOffset, subdomainOffset)
7979
})
8080

81+
it('should set compose from the constructor', () => {
82+
const compose = () => (ctx) => {}
83+
const app = new Koa({ compose })
84+
assert.strictEqual(app.compose, compose)
85+
})
86+
8187
it('should have a static property exporting `HttpError` from http-errors library', () => {
8288
const CreateError = require('http-errors')
8389

lib/application.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ module.exports = class Application extends Emitter {
5151
this.proxyIpHeader = options.proxyIpHeader || 'X-Forwarded-For'
5252
this.maxIpsCount = options.maxIpsCount || 0
5353
this.env = options.env || process.env.NODE_ENV || 'development'
54+
this.compose = options.compose || compose
5455
if (options.keys) this.keys = options.keys
5556
this.middleware = []
5657
this.context = Object.create(context)
@@ -132,7 +133,7 @@ module.exports = class Application extends Emitter {
132133
*/
133134

134135
callback () {
135-
const fn = compose(this.middleware)
136+
const fn = this.compose(this.middleware)
136137

137138
if (!this.listenerCount('error')) this.on('error', this.onerror)
138139

0 commit comments

Comments
 (0)