getMatchRoute method

RouteData? getMatchRoute(
  1. String inputRoute,
  2. String method
)

Implementation

RouteData? getMatchRoute(String inputRoute, String method) {
  List<RouteData> methodMatchedRoutes = routes.where((route) {
    return route.method.toLowerCase() == method.toLowerCase();
  }).toList();

  RouteData? matchRoute;
  for (var route in methodMatchedRoutes) {
    route.path = Route.sanitizeRoutePath(route.path);
    inputRoute = Route.sanitizeRoutePath(inputRoute);

    /// when route is the same route exactly same route
    /// route without params, eg. /api/example
    if (route.path == inputRoute) {
      matchRoute = route;
      break;
    }

    /// when route have params
    /// eg. /api/admin/{adminId}
    var parameterNames = _getParameterNameFromRoute(route);
    var matches = _getPatternMatches(inputRoute, route);

    if (matches.isNotEmpty) {
      matchRoute = route;
      matchRoute.params = _getParameterMap(matches, parameterNames);
      break;
    }
  }
  return matchRoute;
}