From 9c3c064269a88cac58856784e5495aacac1238f6 Mon Sep 17 00:00:00 2001 From: Colin Maier - MBA Date: Sat, 26 Oct 2024 15:42:46 +0200 Subject: [PATCH] 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].