question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access to original route string?

See original GitHub issue

I’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:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
saibotsivadcommented, Aug 31, 2021

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!

1reaction
lukeedcommented, Aug 31, 2021

I’m not particularly keen on changing anything internal here, tbh. How about the first handler writes itself? You can overload the add method and do it that way:

Works with polka:

let app = polka();

let toAdd = app.add.bind(app);

app.add = function (m, r, ...h) {
  let route = String(r);
  // Custom function to save route
  // where & how you want it
  let custom = (req, res, next) => {
    req.$route = route;
    next(); // done
  };
  return toAdd(m, r, custom, ...h);
}

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 & call super.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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do you access the query string in Flask routes?
First there is args which is "a dictionary with all the arguments passed in the query string of the URL." So if you...
Read more >
Working with routes for HTTP APIs - Amazon API Gateway
Routes direct incoming API requests to backend resources. Routes consist of two parts: an HTTP method and a resource path—for example, GET /pets...
Read more >
Routing - Laravel - The PHP Framework For Web Artisans
The Default Route Files ... For example, you may access the following route by navigating to ... By default, Route::redirect returns a 302...
Read more >
Access route path parameters from context #2983 - GitHub
I was looking for a way to access the url path parameters (eg. ... (List<String>)foo).get())); // To access first param bar: request.attrs().
Read more >
Routing in ASP.NET Core - Microsoft Learn
Routing is responsible for matching incoming HTTP requests and dispatching those requests to the app's executable endpoints.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found