Skip to content

Commit 66fd6f7

Browse files
JamesMessingeraorinevo
authored andcommittedSep 16, 2018
fix: accept single function for middleware
* Fixed type check in .middleware() method `if (typeof callback === 'object')` should be `if (typeof callback === 'function')` See Issue #1214 for details * middewareFactory accepts a function, not an object According to [the docs for the `.middleware()` method](http://yargs.js.org/docs/#api-middlewarecallbacks): > The `callbacks` parameter can be a function or a list of functions See Issue #1214 for more details * Added/Updated middleware tests I added a new test to verify that the `.middleware()` method works when a single function is passed, rather than an array of functions. Also enhanced an existing test to ensure that _all_ middleware is run before commands, regardless of whether it was added as an array of functions or a single function.
1 parent 6c6c27d commit 66fd6f7

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed
 

‎lib/middleware.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = function (globalMiddleware, context) {
22
return function (callback) {
33
if (Array.isArray(callback)) {
44
Array.prototype.push.apply(globalMiddleware, callback)
5-
} else if (typeof callback === 'object') {
5+
} else if (typeof callback === 'function') {
66
globalMiddleware.push(callback)
77
}
88
return context

‎test/middleware.js

+32-7
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,30 @@ describe('middleware', () => {
2424
it('should add a single callback to global middleware', () => {
2525
const globalMiddleware = []
2626

27-
middlewareFactory(globalMiddleware)({})
27+
middlewareFactory(globalMiddleware)(function () {})
2828

2929
globalMiddleware.should.have.lengthOf(1)
3030
})
3131

32+
it('runs the middleware before reaching the handler', function (done) {
33+
yargs(['mw'])
34+
.middleware(function (argv) {
35+
argv.mw = 'mw'
36+
})
37+
.command(
38+
'mw',
39+
'adds func to middleware',
40+
function () {},
41+
function (argv) {
42+
// we should get the argv filled with data from the middleware
43+
argv.mw.should.equal('mw')
44+
return done()
45+
}
46+
)
47+
.exitProcess(false) // defaults to true.
48+
.parse()
49+
})
50+
3251
it('runs all middleware before reaching the handler', function (done) {
3352
yargs(['mw'])
3453
.middleware([
@@ -56,11 +75,9 @@ describe('middleware', () => {
5675

5776
it('should be able to register middleware regardless of when middleware is called', function (done) {
5877
yargs(['mw'])
59-
.middleware([
60-
function (argv) {
61-
argv.mw1 = 'mw1'
62-
}
63-
])
78+
.middleware(function (argv) {
79+
argv.mw1 = 'mw1'
80+
})
6481
.command(
6582
'mw',
6683
'adds func list to middleware',
@@ -69,12 +86,20 @@ describe('middleware', () => {
6986
// we should get the argv filled with data from the middleware
7087
argv.mw1.should.equal('mw1')
7188
argv.mw2.should.equal('mw2')
89+
argv.mw3.should.equal('mw3')
90+
argv.mw4.should.equal('mw4')
7291
return done()
7392
}
7493
)
94+
.middleware(function (argv) {
95+
argv.mw2 = 'mw2'
96+
})
7597
.middleware([
7698
function (argv) {
77-
argv.mw2 = 'mw2'
99+
argv.mw3 = 'mw3'
100+
},
101+
function (argv) {
102+
argv.mw4 = 'mw4'
78103
}
79104
])
80105
.exitProcess(false) // defaults to true.

0 commit comments

Comments
 (0)
Please sign in to comment.