-
Notifications
You must be signed in to change notification settings - Fork 170
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
Router.use do not receive ctx.param(s) when route has prefix with params #69
Conversation
I tried to simplify my test route as it seemed more complex than necessary however I discovered another bug. With test modified to this: it.only('populates ctx.params correctly for router prefix', function(done) {
var app = new Koa();
var router = new Router({ prefix: '/:category' });
app.use(router.routes());
router
.use((ctx, next) => {
ctx.should.have.property('params');
ctx.params.should.be.type('object');
ctx.params.should.have.property('category', 'cats');
return next();
})
.get('/suffixHere', function(ctx) {
ctx.should.have.property('params');
ctx.params.should.be.type('object');
ctx.params.should.have.property('category', 'cats');
ctx.status = 204;
});
request(http.createServer(app.callback()))
.get('/cats/suffixHere')
.expect(204)
.end(function(err, res) {
if (err) return done(err);
done();
});
}); I get:
So while I think I fixed my specific bug it looks like a more general fix would be good. It looks like the |
Alright, I think I have it now -- by updating the default prefix matcher to be I added more tests -- we still get the correct matches for say |
@niftylettuce this looks like a great addition. What do you think? |
@niftylettuce Have you had a chance to look at this? |
bb817a5
to
51eb6ab
Compare
Hey @cymen by chance are all your tests passing locally with the current up to date master? |
@JacobMGEvans Yes -- are you referring to the CI failures? If you see the run for 1ee4d04 it passed on CI. There was a change in how |
No not CI, I gotcha though. I was referring to running the automated tests locally, I pulled master and all my local tests except like two fails now lol |
Oh, I was confused if you meant my master for this PR or master for this repo. You meant the later right? But yeah, it's passing for me (will double check tomorrow too). |
@niftylettuce Any chance you can take a look at this? |
v9.2.0 released to npm @cymen |
@niftylettuce Yay! Thank you! |
I've been stuck on koa-router v7.0.1 due to this bug. I finally spent the time to dig into it deeper and figure out the cause.
There was this bug: ZijianHe/koa-router#247
And it was fixed with this PR: ZijianHe/koa-router#287
However, for those of use using koa-router with the prior behavior, that broke our
use
on routers that had a prefix with a param (and theuse
expected the param to be preset). Bit of an edge case so now it makes sense why it broke and why most probably just ignored the bug.This may resolve this issue: #38
I went with the simplest fix that got my added test to green and didn't break the existing tests. The fix is adding
&& !router.opts.prefix
here:This is somewhat niave in that the router prefix could be just a string -- we could get more complicated and check if the prefix actually has params (update: I made it do this check with commit e06ac96).
Lastly, I discovered
use
can optionally take a path as the first argument and that would likely have resolved my issue too but I would argue the pre-existing behavior was present and it is a bug that if a prefix is present (with param(s)), thenctx.params
should be populated foruse
even without a given path (logically, they should assume the path of the prefix if one is present).