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

feat: Support typed params in routes #2756

Open
diosney opened this issue Sep 16, 2015 · 6 comments
Open

feat: Support typed params in routes #2756

diosney opened this issue Sep 16, 2015 · 6 comments

Comments

@diosney
Copy link

diosney commented Sep 16, 2015

I mean, do something like:

app.route('/:an_id{number}');
app.route('/:another_id{string}')

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

@aredridel
Copy link
Contributor

Is there prior art this derives from?

@diosney
Copy link
Author

diosney commented Sep 16, 2015

@aredridel Yes, but I don't recall where I saw it.

@diosney
Copy link
Author

diosney commented Sep 18, 2015

@aredridel angular ui-router module has this feature 😄 I know is front-end but at the backend we can give it a good use of it (thought the syntax is different that the one I added above).

https://github.com/angular-ui/ui-router/wiki/URL-Routing

@gabeio
Copy link
Member

gabeio commented Sep 18, 2015

I have seen this in other express-similar libraries like bottlepy & flask.

vladvelici added a commit to vladvelici/path-to-regexp that referenced this issue Dec 1, 2015
…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.
@vladvelici
Copy link

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.

@blakeembrey
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants