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

Add getParams method to router.dart #453

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

CLNMR
Copy link

@CLNMR CLNMR commented Oct 26, 2024

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 a Router.


  • I’ve reviewed the contributor guide and applied the relevant portions to this PR.
Contribution guidelines:

Note that many Dart repos have a weekly cadence for reviewing PRs - please allow for some latency before initial review feedback.

Copy link

google-cla bot commented Oct 26, 2024

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.

@kevmoo
Copy link
Member

kevmoo commented Oct 29, 2024

@CLNMR – add tests and an entry to the changelog, please!

@CLNMR
Copy link
Author

CLNMR commented Oct 31, 2024

@CLNMR – add tests and an entry to the changelog, please!

Sorry @kevmoo, I added them!

@kevmoo
Copy link
Member

kevmoo commented Nov 1, 2024

@devoncarew @natebosch – thoughts here?

@kevmoo
Copy link
Member

kevmoo commented Nov 1, 2024

@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) {
Copy link
Member

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

@natebosch
Copy link
Member

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?

Copy link
Member

@jonasfj jonasfj left a 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.

Copy link
Member

@jonasfj jonasfj left a 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?

Comment on lines +193 to +194
/// Get URL parameters captured by the [Router].
/// Returns `null` if no parameters are captured.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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.

https://dart.dev/effective-dart/documentation#do-separate-the-first-sentence-of-a-doc-comment-into-its-own-paragraph

}
var params = route.match('/${request.url.path}');
if (params != null) {
return params;
Copy link
Member

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.

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

Successfully merging this pull request may close these issues.

4 participants