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 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkgs/shelf_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## 1.1.5-wip
## 1.2.0-wip

* Added function `getParams` to the `Router` class to get the parameters from a request

* Require Dart `^3.3.0`.

Expand Down
15 changes: 15 additions & 0 deletions pkgs/shelf_router/lib/src/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,21 @@ class Router {
return _notFoundHandler(request);
}

/// Get URL parameters captured by the [Router].
/// Returns `null` if no parameters are captured.
CLNMR marked this conversation as resolved.
Show resolved Hide resolved
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

for (var route in _routes) {
if (route.verb != request.method.toUpperCase() && route.verb != 'ALL') {
continue;
}
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.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, that's true, but only when a route was matched and found are the params returned. Therefore you can deduce whether the router found a route or not (see also my comment below).

}
}
return null;
}

// Handlers for all methods

/// Handle `GET` request to [route] using [handler].
Expand Down
15 changes: 15 additions & 0 deletions pkgs/shelf_router/test/router_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ void main() {
expect(await get('/user/jonasfj/groups/42'), 'jonasfj / 42');
});

test('get params via getParams', () async {
var app = Router();

app.get(
r'/user/<user>/groups/<group|\d+>',
(Request request) => Response.ok(''),
);

final params =
app.getParams(Request('GET', Uri.http('', '/user/jonasfj/groups/42')))!;

expect(params['user'], 'jonasfj');
expect(params['group'], '42');
});

test('params by arguments', () async {
var app = Router();

Expand Down
Loading