From 9c3c064269a88cac58856784e5495aacac1238f6 Mon Sep 17 00:00:00 2001 From: Colin Maier - MBA Date: Sat, 26 Oct 2024 15:42:46 +0200 Subject: [PATCH 1/4] Add getParams method to router.dart --- pkgs/shelf_router/lib/src/router.dart | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pkgs/shelf_router/lib/src/router.dart b/pkgs/shelf_router/lib/src/router.dart index 720d3c3e..fb8f295d 100644 --- a/pkgs/shelf_router/lib/src/router.dart +++ b/pkgs/shelf_router/lib/src/router.dart @@ -190,6 +190,21 @@ class Router { return _notFoundHandler(request); } + /// Get URL parameters captured by the [Router]. + /// Returns `null` if no parameters are captured. + Map? getParams(Request request) { + 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; + } + } + return null; + } + // Handlers for all methods /// Handle `GET` request to [route] using [handler]. From 084d7222f2c2f457d664891fc0ca70afb8ead9de Mon Sep 17 00:00:00 2001 From: Colin Maier - MBA Date: Thu, 31 Oct 2024 16:33:51 +0100 Subject: [PATCH 2/4] Added Tests and Changelog --- pkgs/shelf_router/CHANGELOG.md | 4 ++++ pkgs/shelf_router/test/router_test.dart | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/pkgs/shelf_router/CHANGELOG.md b/pkgs/shelf_router/CHANGELOG.md index 6e88703e..7449f960 100644 --- a/pkgs/shelf_router/CHANGELOG.md +++ b/pkgs/shelf_router/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.1.6 + +* Added function `getParams` to the `Router` class to get the parameters from a request + ## 1.1.5-wip * Require Dart `^3.3.0`. diff --git a/pkgs/shelf_router/test/router_test.dart b/pkgs/shelf_router/test/router_test.dart index fdffa51a..d932b129 100644 --- a/pkgs/shelf_router/test/router_test.dart +++ b/pkgs/shelf_router/test/router_test.dart @@ -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//groups/', + (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(); From 276fecf2f42503ad8200759c8193871e24374032 Mon Sep 17 00:00:00 2001 From: Colin Ihlenfeldt Date: Fri, 1 Nov 2024 16:09:08 +0100 Subject: [PATCH 3/4] Update pkgs/shelf_router/CHANGELOG.md Co-authored-by: Kevin Moore --- pkgs/shelf_router/CHANGELOG.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkgs/shelf_router/CHANGELOG.md b/pkgs/shelf_router/CHANGELOG.md index 7449f960..d07b2fbc 100644 --- a/pkgs/shelf_router/CHANGELOG.md +++ b/pkgs/shelf_router/CHANGELOG.md @@ -1,9 +1,7 @@ -## 1.1.6 +## 1.2.0-wip * Added function `getParams` to the `Router` class to get the parameters from a request -## 1.1.5-wip - * Require Dart `^3.3.0`. ## 1.1.4 From 489f4fcf61c7ff56a9a62e076f5f990b76ccc3f2 Mon Sep 17 00:00:00 2001 From: Colin Ihlenfeldt Date: Thu, 14 Nov 2024 16:19:28 +0100 Subject: [PATCH 4/4] Update pkgs/shelf_router/lib/src/router.dart Co-authored-by: Jonas Finnemann Jensen --- pkgs/shelf_router/lib/src/router.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/pkgs/shelf_router/lib/src/router.dart b/pkgs/shelf_router/lib/src/router.dart index fb8f295d..f2bf7bba 100644 --- a/pkgs/shelf_router/lib/src/router.dart +++ b/pkgs/shelf_router/lib/src/router.dart @@ -191,6 +191,7 @@ class Router { } /// Get URL parameters captured by the [Router]. + /// /// Returns `null` if no parameters are captured. Map? getParams(Request request) { for (var route in _routes) {