-
-
Notifications
You must be signed in to change notification settings - Fork 16.4k
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
feat: Support typed params in routes #2756
Comments
Is there prior art this derives from? |
@aredridel Yes, but I don't recall where I saw it. |
@aredridel angular |
…jects. This is the first step towards achieving expressjs/express#2756. /:foo{<type>} - adds a valType=<type> property to the key object - sets the pattern to match floats for 'number' and 'float' - sets the pattern to match integers for 'integer' <type> ::= string | float | number | integer A new function is added as typedMatch(str) to the regexp objects returned by pathToRegexp(...). It is a wrapper on top of String.match(regexp), and it uses the keys property to change the types of the matching groups accordingly. The changes happen for "float", "number", and "integer". The pattern is only changed when a different pattern is not defined. For example, /:foo([123]){integer} will have the pattern [123], but will also benefit from automatic type changes when using typedMatch.
I implemented some support for this in path-to-regexp, the library that parses the paths into regular expressions. My commit is here. I didn't sent a pull request yet as there is no issue related to it in path-to-regexp. I will open one and try to get some feedback. |
Interesting. On a (partially related) side note, I did something similar for pillarjs/router#29 (the standalone router, not in Express 4.x) which added support for the RAML type system using https://github.com/mulesoft-labs/raml-path-match. If something like that lands, it would be straightforward to add a new path matching implementation with types (I know URI templates is of some interest too). Edit: That router is here: https://github.com/mulesoft-labs/osprey-router |
I mean, do something like:
which is roughly equivalent to use regex in each case, but the shorthand above is clearer and allows
req.params
to properly type the params, fi:req.params.an_id
would be a number instead of a string.Thanks
The text was updated successfully, but these errors were encountered: