regexparam icon indicating copy to clipboard operation
regexparam copied to clipboard

Path params that contain slashes

Open justingrant opened this issue 1 year ago • 0 comments

What's the recommended way to handle path parameters that may contain slashes?

For example, we have a /docs/ path in our app that contains nested paths or arbitrary length. For example:

/docs/foo
/docs/foo/
/docs/foo/bar
/docs/foo/bar/baz

We can match these paths in regexparam using /docs/* which works fine if all we want to do is match the path without extracting the param value, but if we want to extract that param:

  1. Using a * parameter name is weird DX because it requires using params['*'] syntax instead of the more familiar params.rest syntax. This also makes IDE autocomplete problematic; in VSCode, autocomplete won't work when you type params. so it's harder for developers to discover that param, although if you know to type params.[' then it may work. (BTW, worse autocomplete was a tradeoffs of changing from wild to * for the wildcard parameter name.)
  2. I must manually strip trailing slashes, which makes the behavior of wildcard params different from all other params where slashes are automatically stripped by default.

Is there a solution for slashful params other than building our own Regex? (Where we'll also lose the nice DX and autocomplete because TS literal types won't work with regexes)

justingrant avatar Aug 15 '24 20:08 justingrant