Skip to content
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

Closed

Conversation

JulianBerger
Copy link

@JulianBerger JulianBerger commented Oct 7, 2020

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:

An optional '?' suffix following the parameter name indicates an optional parameter (only allowed if the parameter is at the ends of the path or only covers part of the segment as in '/a{param?}/b'). For example, the route '/book/{id?}' matches '/book/' with the value of request.params.id set to an empty string ''.

Issue reference: #1206

Checklist

… if the parameter is at the end of the path

Added test for optional route parameter
@mcollina
Copy link
Member

mcollina commented Oct 7, 2020

I like this quite a lot... however having this feature in find-my-way could be better? Wdyt @delvedor?

Comment on lines +147 to +150
const optionalParam = path.match(/:([^/]*)\?\/?$/)
if (optionalParam) {
opts.url = path.replace(/(:[^/]*)\?(\/?)$/, '$1$2')
const optionalPath = path.replace(/\/:([^/]*)\?(\/?)$/, '$2')
Copy link
Member

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 })
Copy link
Member

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.

Copy link
Member

@delvedor delvedor left a 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!!

@JulianBerger
Copy link
Author

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! :)

@github-actions
Copy link

github-actions bot commented Feb 6, 2022

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants