named-routes
named-routes copied to clipboard
Allow to pass name/options as third argument in standalone mode
This PR changes how router.add
works:
// Name after route path + no need for an array when using more than one middleware
router.add('get', '/user/:id', 'user', isAuthenticated, someOtherMiddleware, (req, res) => {
res.send('Hello!');
});
// ... but, you still can use array
router.add('get', '/post/:id', 'post', [someOtherMiddleware, (req, res) => {
res.send('Hello!');
}]);
// Have more than one option? Just pass an object before your middlewares
router.add('get', '/page/:id', {name: 'page', caseSensitive: true}, (req, res) => {
res.send('Hello!');
});
// Don't want to use a name? Sure, just ditch it
router.add('get', '/category/:id', someOtherMiddleware, (req, res) => {
res.send('Hello!');
});
Closes #25
Thanks for putting together this PR!
I believe this PR also changes the behaviour of router.add
so that it in addition to an array of callbacks/middlewares, it can now take any length of arguments. I think I prefer that behaviour, so now there are two options:
- Update the tests to include cases with multiple callbacks without using arrays and update the documentation
- Extract this change for a second PR What do you think?
Thanks for quick reply! I'll add a few new test cases and update the docs accordingly.
Sorry for the delay, but the missing tests are now included. @alubbe, could you verify if everything is now good? Would it be okay if I would rewrite the tests (add more cases and split existing into smaller groups)?
Could you please update the docs, as well? The tests are a left-over from the original fork and are in serious need of a makeover. Aside from new cases and smaller groups, the whole thing should really use describe and it instead of that object notation. So that would be two PRs - one converting the current tests into something more modern and another one adding / rewriting them.