Allow param middleware to access param name #133
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Change
this
binding of param handler call to the middleware itself, allowing access to param name. This allows for generic param middleware that can be re-used for multiple params.Background
Express (used to) support a
param
call signature that included the param name, and this allowed for param middleware that could be applied to multiple params, and act accordingly. That has been changed, but this feature is useful, and it is annoying that there is no easy access to the param name metadata from inside the param middleware.Alternatives do exist, such as a factory that takes the param name to construct the middleware:
However, that duplicates the param name parameter. This can be worked around like this:
But this is a bit too fancy for my preferences. It also makes the factory function have to know about the arguments
param
takes.If the param name was somehow exposed to the middleware callback, this would be much easier. One option (as implemented) is passing the "middleware" object as the "this" binding:
An alternative would be to pass the param name (as express once did):
This is a bit more explicit, but the drawback here is maintaining the argument order. If later more metadata about the middleware was desired, that would also need to be added to the arguments list. However, the "explicitness" might be worth it...
Feedback welcome, I hope to start productive discussion about this. :)