Access to original route string?
See original GitHub issueI’m making some tooling that maps an OpenAPI definition to a Trouter (specifically Polka) router/app thing, but what I’m missing when calling the handler is the route
, e.g. /things/:thingId
, so that I can map that back to a path definition.
It looks like that info is not passed to routes
on line 29 so that part would be easy enough to add:
- this.routes.push({ keys, pattern, method, handlers });
+ this.routes.push({ keys, pattern, method, handlers, route });
but… I’m not sure how that would be handed back to the handlers
array, since that’s just an array of functions from fns
, so the only way to do that is something horrible like tmp.handlers[0].route = route
or whatever.
The only other way that comes to mind is changing the returned object:
- return { params, handlers };
+ return { params, handlers, routes };
where the routes
would probably have to be a list of route strings, e.g. /things/:thingId
that was index-matched to the handlers
list. That doesn’t feel super great, but it’s … not horrible? 🤔
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
Good thoughts there, thanks. I’ll explore these ideas a bit and see where I land, but in any case I agree that changing Trouter internals isn’t the solution. Thanks!
I’m not particularly keen on changing anything internal here, tbh. How about the first
handler
writes itself? You can overload theadd
method and do it that way:Works with polka:
You can do the same thing in a Trouter app, but given that the
Trouter
class is exposed directly, you can just extend it as your own & callsuper.add
instead. Of course, that ends up being exactly the same thing, just a syntax difference.The benefit of this versus your proposed changes, is that you can put the information exactly where you want it and still at time of definition. Making it part of the return object (from Trouter.find) we’ll still require you to massage the values into place anyway, and would be ignored / useless by the majority of users