checkOne method

Future<({bool found, Map<String, Object?> urlParams})> checkOne(
  1. WebRoute route,
  2. String key
)

Checks a single route to see if it matches the given key.

The route parameter is the WebRoute object to check, and the key parameter is the route path to match.

Returns a Future with a tuple containing:

  • found: A boolean indicating whether a matching route was found.
  • urlParams: A map of URL parameters extracted from the route.

Implementation

Future<({bool found, Map<String, Object?> urlParams})> checkOne(
  WebRoute route,
  final String key,
) async {
  var urlParams = <String, Object?>{};
  final pathClient = route.rq.uri.path;
  var endpoint = endpointNorm([key]);
  var path = endpointNorm([pathClient]);
  rq.addParams(route.params);

  // Check is param or not
  if (key.contains("{")) {
    var paramPath = getParamsPath(path, endpoint);
    urlParams = paramPath.$2;
    endpoint = paramPath.$1;
  }

  if (endpoint.endsWith('*/')) {
    endpoint = endpoint.replaceAll('*/', '');

    var isTarget = true;
    final starPath = path.replaceFirst(endpoint, '');
    for (var excluded in route.excludePaths) {
      final special = endpointNorm([starPath]);
      excluded = endpointNorm([excluded]);

      if (special.startsWith(excluded)) {
        isTarget = false;
        break;
      }
    }

    if (isTarget && path.endsWith(starPath)) {
      path = path.replaceFirst(starPath, '');
    }
  }

  if (endpoint == path) {
    if (!route.allowMethod()) {
      return (found: false, urlParams: urlParams);
    }

    if (route.widget.isNotEmpty) {
      if (!await checkPermission(seenAuth)) {
        return (found: false, urlParams: urlParams);
      }
      rq.addParams(urlParams);
      renderWidget(route.widget);
      return (found: true, urlParams: urlParams);
    }

    if (route.auth != null) {
      seenAuth = route.auth;
      var res = await route.auth!.auth();
      if (res == false) return (found: false, urlParams: urlParams);
    }

    if (route.index == null) {
      if (route.controller != null) {
        if (!await checkPermission(seenAuth)) {
          return (found: false, urlParams: urlParams);
        }
        rq.addParams(urlParams);

        await route.controller!.index();
        return (found: true, urlParams: urlParams);
      } else {
        return (found: false, urlParams: urlParams);
      }
    } else {
      if (!await checkPermission(seenAuth)) {
        return (found: false, urlParams: urlParams);
      }
      rq.addParams(urlParams);
      await route.index!();
      return (found: true, urlParams: urlParams);
    }
  } else if (route.children.isNotEmpty && path.contains(endpoint)) {
    if (route.auth != null) {
      seenAuth = route.auth;
      var res = await route.auth!.auth();
      if (res == false) return (found: false, urlParams: urlParams);
    }

    if (await checkAll(route.children, parentPath: key)) {
      return (found: true, urlParams: urlParams);
    }
  }
  return (found: false, urlParams: urlParams);
}