Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware does not accept a single function #1214

Closed
JamesMessinger opened this issue Sep 8, 2018 · 9 comments
Closed

Middleware does not accept a single function #1214

JamesMessinger opened this issue Sep 8, 2018 · 9 comments
Assignees

Comments

@JamesMessinger
Copy link
Contributor

According to the docs for the .middleware() method:

The callbacks parameter can be a function or a list of functions

However, it currently (as of v12.0.2) only works with an array of functions. Passing a single function does nothing.

yargs
  .command('myCommand', 'some command', {}, function(argv){
    console.log('Running myCommand!');
  })
  .middleware(() => console.log('Running middleware 1'))   // <--- this never runs
  .middleware(() => console.log('Running middleware 2'))   // <--- neither does this
  .parse(['myCommand']);

Here is a reproduction of the issue on RunKit.

The problem is caused by this block of code:

if (Array.isArray(callback)) {
Array.prototype.push.apply(globalMiddleware, callback)
} else if (typeof callback === 'object') {
globalMiddleware.push(callback)
}

I believe that if (typeof callback === 'object') should be if (typeof callback === 'function').

I will submit a PR for this momentarily

JamesMessinger added a commit to JamesMessinger/yargs that referenced this issue Sep 8, 2018
`if (typeof callback === 'object')` should be `if (typeof callback === 'function')`

See Issue yargs#1214 for details
JamesMessinger added a commit to JamesMessinger/yargs that referenced this issue Sep 8, 2018
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 yargs#1214 for more details
@JimiC
Copy link

JimiC commented Sep 8, 2018

Confirmed. Currently, if you need to pass a single function, it's mandatory to add it to an Array and pass that.

What I don't get either, is why and when a callback is an object (See also https://github.com/yargs/yargs/blob/v12.0.2/test/middleware.js#L24).
From what I know a callback is always a function.

/cc @aorinevo

@JamesMessinger
Copy link
Contributor Author

@JimiC - Agreed. I think the 'object' in the type check was just a typo or misunderstanding. It probably should have always been 'function' instead

@aorinevo
Copy link
Contributor

aorinevo commented Sep 8, 2018

Hi @BigstickCarpet thank you for raising this issue; looking forward to the PR.

@JimiC thank you for validating.

It was a misunderstanding. Although functions are objects, typeof function returns 'function'. For reference, I'm including a link to Todd Motto's post on use of typeof https://toddmotto.com/understanding-javascript-types-and-reliable-type-checking/#typeof-operator

@BigstickCarpet: As part of your PR, can you add a unit test for this to /test/middleware.js?

@JamesMessinger
Copy link
Contributor Author

@aorinevo - Here's the PR: #1215

@JimiC
Copy link

JimiC commented Sep 8, 2018

And the related test is also adjusted.

aorinevo pushed a commit that referenced this issue Sep 16, 2018
* 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.
@bcoe
Copy link
Member

bcoe commented Oct 6, 2018

@JamesMessinger @JimiC please try npm i yargs@next, which should have a patch that fixes this; if the release is looking good, I'll promote it next week.

@JimiC
Copy link

JimiC commented Oct 8, 2018

LGTM 👍

@arkon
Copy link

arkon commented Oct 5, 2019

Seems like this should be closed?

@bcoe
Copy link
Member

bcoe commented Oct 7, 2019

@arkon thanks for helping us groom some of our old issues 👍

@bcoe bcoe closed this as completed Oct 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants