-
Notifications
You must be signed in to change notification settings - Fork 407
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
populate ctx.params
before running any matched middleware
#490
base: master
Are you sure you want to change the base?
Conversation
We need this as well |
This is huge. @ZijianHe can you please merge this? |
@smkoyan thanks for your work on this. this resolves |
if (mostSpecificLayer.name) { | ||
ctx._matchedRouteName = mostSpecificLayer.name; | ||
} | ||
|
||
layerChain = matchedLayers.reduce(function(memo, layer) { | ||
memo.push(function(ctx, next) { | ||
ctx.captures = layer.captures(path, ctx.captures); | ||
ctx.params = layer.params(path, ctx.captures, ctx.params); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is needed in order to pass in params
to the Router.prototype.param
function in nested routers.
ctx.params = layer.params(layer.captures(path))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in #508
expect(res.body.param).to.equal(undefined); | ||
done(); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@smkoyan this test is important and removing it will not allow us to check use cases where users want a specific route to be hit followed by a catch-all route.
router.get('/specific-route') // Hit when specific-route is provided
router.get('/:catchall') // Hit in any other case with a capture
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in #508
The problem is that when I use
router.use(someMiddleware)
, in that middleware I have access to payload from query string viactx.query
and to payload from request body viactx.request.body
but I haven't access to route params and this is not so convenient especially in my case.To be more clear I will tell my story. I have a validation middleware which works like this.
It takes request method from
ctx.request.method
and matched route fromctx._matchedRoute
and using that it takes necessary validation rules from validation schemas that look like this:and for each type of payload, if it exists in the scheme, it checks for validity and problem comes here when it tries to validate params because params are not available in the middleware defined using
router.use
method.This pull request fixes this type of problems and I think this will will bring much good in the future.
Thanks!