-
-
Notifications
You must be signed in to change notification settings - Fork 386
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
Typed parameters in routes #67
Comments
Seems reasonable. This would be a breaking change release, but it's a nice feature. There are some caveats that need to be considered for the implementation though. So, Does type coercion introduce an unexpected issue here though, when your IDs get too big? For example, JS handles 2^53 safely, but beyond that things will start being weird (edit: then we hit Does that just get documented, or do we just always return strings so people can then use libraries like Does |
If the numbers too big issue is addressed as always returning strings then Documenting it is certainly necessary. I suggest also having a way to provide custom parsers for different types, as an optional config step after require, not necessarily as a per-route thing (but this can work as well). It can also be a way to define new types, e.g. Date. Regarding the '0x10' situation, it probably fits in the idea of allowing configurable parsers for types.
|
@vladvelici Once you bring in configurable parsers, however cool, this sort of leaves As for numbers too big, it should either always be a string or a number. So does it coerce or not? That was mostly my concern there, and if it does parse when does it fail. |
By configurable parsers I was thinking somethings like configurable global types, like:
Which then can be used like
But I agree, it goes a bit beyond the scope of this project.
|
The problem with that and why it's out of scope is that you'll be modifying any other usages of Anyway, I understand what you want now. How would a plugin syntax like that be optimized, for instance? Numbers don't need to match strings, but here we don't understand the syntax used if the parser is a function. If we pass the function everything matched, the implementation would need to signal back that it's either valid or not (for instance, to go to the next route and/or 404 - in Express-land). |
Perhaps this is something best left up to json schema? |
JSON schema is a whole other beast - it's a complete schema implementation is generally quite complex. Most parameters tend to be little more than types like |
IMHO, the basic route matching semantics should be as slim as possible. I think the issue @blakeembrey mentioned here is a better solution because every new feature is a performance hit. So if you need that feature I would think implementing a routing engine would be best. |
I second what @wesleytodd says. I don't find it that hard for manually cast to whatever type you need. Having most of the available types will bring a hell of a mess (just imagine how you set Date type, not to speak of encoding). Don't forget that url is a string, and the way you interpret it is your own job. The routing system itself does (and should) not care if this is a Number or a String. |
👎 could easily be a separate module that wraps this module. i'd take a PR here: https://github.com/pillarjs/path-match but it isn't used by express so... |
@blakeembrey Optimisation - it can be made such that each parser has a regexp and the parsing function. The syntax might not be as nice as @wesleytodd Agree with the fact that every new feature is a performance hit. General: I don't particularly need this feature. I just played around and I thought it's a nice thing to have. Sometimes I find it annoying to do @jonathanong Seems like path-match is a better fit for this suggestion. I'll send a PR there when I get a bit of time (I assume it needs some adjustments?). I'm closing this issue now. Thanks for the responses everybody! |
@vladvelici It definitely should exist. I've commented on a similar issue in Express and I thought I'd left comments here, but I actually did similar work a year ago. For example, to make this work altogether I created pillarjs/router#29 and https://github.com/mulesoft-labs/raml-path-match. @jonathanong Can I make the PR on Edit: Here's the router I built that has a simple type system based on an API specification language: https://github.com/mulesoft-labs/osprey-router. |
@blakeembrey np as long as you don't break backwards compat :) |
It would, it'd need a major version bump. The output from |
hm not sure i follow. open a new issue? i don't really mind a breaking change though as long as there is added benefit, just prefer not to break anything |
As mentioned in expressjs/express#2756, it would be nice to have
and
an_id
to actually be a number instead of string.I implemented something here afebe50, but I would like to ask for feedback, and if this is something that is desirable.
I think the way I put the
typedMatch
function on theregexp
object is not quite nice, so I'd like to hear some feedback.Also I know it lacks documentation in the README but that should come when and if this is something desirable.
The text was updated successfully, but these errors were encountered: