-
Notifications
You must be signed in to change notification settings - Fork 125
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
Add getParams method to router.dart #453
base: master
Are you sure you want to change the base?
Conversation
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
@CLNMR – add tests and an entry to the changelog, please! |
Co-authored-by: Kevin Moore <[email protected]>
@devoncarew @natebosch – thoughts here? |
@CLNMR – need to change the pubspec version to match the changelog! |
@@ -190,6 +190,21 @@ class Router { | |||
return _notFoundHandler(request); | |||
} | |||
|
|||
/// Get URL parameters captured by the [Router]. | |||
/// Returns `null` if no parameters are captured. | |||
Map<String, String>? getParams(Request request) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we decide to land this, how about the name urlParameters
.
https://dart.dev/effective-dart/design#avoid-starting-a-method-name-with-get
I think @jonasfj might have the most context on this package and whether this is a good solution. I'm not sure I fully understand the problem. What is the use case where URL parameters must be read without handling the request? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Functionality is already there:
https://pub.dev/documentation/shelf_router/latest/shelf_router/params.html
update, I missed this: 🤣
This allows to extract the params of a URI from a request without actually handling the request in a middleware.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's kind of intentional that internals of this package are not public. It makes it possible to refactor the implementation, add new features without doing breaking changes.
We could make _routes
public, but if you want fast moving APIs that can do everything, perhaps it better to build it as a package in the community.
What is the intended use case for this?
/// Get URL parameters captured by the [Router]. | ||
/// Returns `null` if no parameters are captured. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Get URL parameters captured by the [Router]. | |
/// Returns `null` if no parameters are captured. | |
/// Get URL parameters captured by the [Router]. | |
/// | |
/// Returns `null` if no parameters are captured. |
} | ||
var params = route.match('/${request.url.path}'); | ||
if (params != null) { | ||
return params; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically, the route is not necessarily matched.
The Handler
for the route may return Router.routeNotFound
, in which case the Router
will try all subsequent routes.
See documentation for Router.routeNotFound
.
It's kind of weird to look at matched parameters from different routes and mounted routers.
This PR adds a
getParams
method to the handler. This allows to extract the params of a URI from a request without actually handling the request in a middleware.As far as I am aware, this was not possible in any other way before, as the
routes
are private in aRouter
.Contribution guidelines:
dart format
.Note that many Dart repos have a weekly cadence for reviewing PRs - please allow for some latency before initial review feedback.