-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Add a second route for optional parameters (with a '?' suffix) - only… #2607
Conversation
… if the parameter is at the end of the path Added test for optional route parameter
I like this quite a lot... however having this feature in find-my-way could be better? Wdyt @delvedor? |
const optionalParam = path.match(/:([^/]*)\?\/?$/) | ||
if (optionalParam) { | ||
opts.url = path.replace(/(:[^/]*)\?(\/?)$/, '$1$2') | ||
const optionalPath = path.replace(/\/:([^/]*)\?(\/?)$/, '$2') |
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.
These regular expressions need more explanation in the code. There's way too many brackets and optionals for these to be easily legible.
method: 'GET', | ||
path: '/foo/:opt?', | ||
handler: (req, res) => { | ||
res.send({ opt: req.params?.opt || false }) |
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.
The optional chain operator is only supported since Node v14.5.0.
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.
however having this feature in find-my-way could be better? Wdyt @delvedor?
Big yes!!
Alright, I will make another pull request in https://github.com/delvedor/find-my-way when I find time for that! :) |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Add a second route for optional parameters (with a '?' suffix) - only if the parameter is at the end of the path
Added test for optional route parameter
Hapi uses the same route format, their documentation:
Issue reference: #1206
Checklist
npm run test
andnpm run benchmark
and the Code of conduct