Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const Promise = require('any-promise')
const callMiddleware = require('koa-call-middleware')

/**
* Expose compositor.
Expand Down Expand Up @@ -41,9 +42,17 @@ function compose (middleware) {
if (i === middleware.length) fn = next
if (!fn) return Promise.resolve()
try {
return Promise.resolve(fn(context, function next () {
return dispatch(i + 1)
}))
if (fn) {
return Promise.resolve(callMiddleware(fn, context, function next () {
return dispatch(i + 1)
}))
} else {
if (next) {
return Promise.resolve(next())
} else {
return Promise.resolve()
}
}
} catch (err) {
return Promise.reject(err)
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"index.js"
],
"dependencies": {
"any-promise": "^1.1.0"
"any-promise": "^1.1.0",
"koa-call-middleware": "^1.0.0"
},
"devDependencies": {
"co": "^4.6.0",
Expand Down
80 changes: 80 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,66 @@ describe('Koa Compose', function () {
})
})

it('should wrap functions with wrappers', function () {
var arr = []
var stack = []

stack.push(function * (context, next) {
arr.push(1)
yield wait(1)
yield next()
yield wait(1)
arr.push(6)
})

stack.push(function * (context, next) {
arr.push(2)
yield wait(1)
yield next()
yield wait(1)
arr.push(5)
})

stack.push(function * (context, next) {
arr.push(3)
yield wait(1)
yield next()
yield wait(1)
arr.push(4)
})

return compose(stack)({
wrappers: [co.wrap]
}).then(function () {
arr.should.eql([1, 2, 3, 4, 5, 6])
})
})

it('should use wrappers in the correct order', function () {
let completed = []
let stack = []
let arr = []

stack.push(function * (context, next) {
arr.push(1)
yield next()
})

stack.push(function * (context, next) {
arr.push(2)
yield next()
})

return compose(stack)({
wrappers: [co.wrap, function (fn) {
return (ctx, next) => fn(ctx, next).then(completed.push(fn))
}]
}).then(function () {
arr.should.eql([1, 2])
completed.length.should.eql(stack.length)
})
})

it('should only accept an array', function () {
var err
try {
Expand All @@ -95,6 +155,26 @@ describe('Koa Compose', function () {
return (err).should.be.instanceof(TypeError)
})

it('should throw an error if calling generators or non-functions', function () {
var stack = [
function * () {}
]
return compose(stack)({}).then(function () {
throw new Error('No error thrown')
}).catch(function (err) {
(err).should.be.instanceof(TypeError)
}).then(function () {
stack = [
{ key: 'value' }
]
return compose(stack)({})
}).then(function () {
throw new Error('No error thrown')
}).catch(function (err) {
(err).should.be.instanceof(TypeError)
})
})

it('should work with 0 middleware', function () {
return compose([])({})
})
Expand Down