kwhitley / itty-router

A little router.
MIT License
1.75k stars 78 forks source link

feat: add support for greedy params #78

Closed markusahlstrand closed 1 year ago

markusahlstrand commented 2 years ago

Hi,

Thanks for this awesome router for cloudflare :)

The only feature I was missing was the support for greedy params, which I find pretty useful when doing things like proxying. So, for instance if I want to fetch static files from S3 on subpath you could use a path like /static/:file+ as path and that would pass everything after static in the file param.

There doesn't seem to be a real standard for this across different routers, so maybe there's a better way of doing it? Anyway.. this PR makes it possible to do the routing like the example above.

Cheers!

kwhitley commented 2 years ago

Hey @markusahlstrand - sorry, I was out of the country and didn't spot this till just now. Quick question though:

  1. Is that + used elsewhere (another popular routing API)? I'd expect something like a * qualifier instead...

In fact, I see that test in there, right above yours... maybe that should be modified to capture it all?

image

markusahlstrand commented 2 years ago

Hi @kwhitley, sorry for the slow response.

There are a few different ways to do it and I'm not sure what's most standard:

I think we would need both tests and maybe even a third to make it complete. So the test on lin 478 would only return test as param even if the path was /x/test/test. Maybe we should add this as a test as well to make the behaviour clear?

kwhitley commented 2 years ago

Still considering this one... hang tight!

danbars commented 1 year ago

+1 for this feature Very useful for file-structure APIs

kwhitley commented 1 year ago

UPDATE: itty-router is undergoing a full TS conversion (available as itty-router@next), and as such, I can't merge this directly. I'll be adding in this feature today though (and @markusahlstrand into the credits regardless), and will update this thread with the details.

Conversely @markusahlstrand, if you want to beat me too it (I'd love to merge your code in directly so you get code contribution), branch off of (and PR into) the ts branch if you would!

kwhitley commented 1 year ago

Leaving this open to remind myself to give @markusahlstrand credit for this feature despite the lack of a merge!

kwhitley commented 1 year ago

Support for greedy params (with this syntax) went live with v3.x :)

Thanks again @markusahlstrand!!!