path_to_regexp icon indicating copy to clipboard operation
path_to_regexp copied to clipboard

Optional matching?

Open esDotDev opened this issue 3 years ago • 5 comments

Curious if there is a way to define '/user/:id' so that it will match /user /user/ and /user/1 ?

Similarly, could we define billing/:foo/:bar so that only :foo is required and bar is optional?

esDotDev avatar Apr 14 '21 01:04 esDotDev

you can do it by setting prefix to true...

what we need is a 'any' match for example: /path/to.* so everything that starts with /path/to go there...

shovelmn12 avatar Apr 21 '21 15:04 shovelmn12

No, this package doesn't currently support optional parameters, although it seems like a reasonable feature.

As a workaround you could potentially just define multiple matchers per route (or multiple routes) to support this.

leonsenft avatar May 07 '21 00:05 leonsenft

you can do it by setting prefix to true...

I don't think that would meet the requirements requested here. That approach means something like /user/anything would match as well, which is probably not desired.

leonsenft avatar May 07 '21 00:05 leonsenft

Right, the "aliases" approach is basically how we deal with this now, so you would need to define a path value of: ["user", "user/", "user/:id"]

This request is basically asking to not have to do that, but instead some streamlined syntax to express this.

esDotDev avatar May 07 '21 01:05 esDotDev

are there any changes we will eventually get this feature?

clragon avatar May 15 '22 12:05 clragon