getParamsPath method

(String, Map<String, Object?>) getParamsPath(
  1. String clientPath,
  2. String serverPath
)

Extracts parameters from the client path and server path.

The clientPath parameter is the path from the client's request. The serverPath parameter is the path defined in the route.

Returns a tuple where:

  • The first element is the processed server path.
  • The second element is a map of URL parameters extracted from the client path.

Implementation

(String, Map<String, Object?>) getParamsPath(
  String clientPath,
  String serverPath,
) {
  String resultKey = serverPath;
  Map<String, Object?> resultParams = {};

  var serverUri = Uri(path: serverPath);
  var clientUri = Uri(path: clientPath);

  if (serverUri.pathSegments.length != clientUri.pathSegments.length) {
    return (resultKey, resultParams);
  }

  for (int i = 0; i < clientUri.pathSegments.length; i++) {
    var key = Uri.decodeFull(serverUri.pathSegments[i]);
    if (!key.startsWith("{") || !key.endsWith("}")) {
      continue;
    } else {
      key = key.replaceAll("{", "").replaceAll("}", "");
      resultKey = resultKey.replaceFirst("{$key}", clientUri.pathSegments[i]);
      resultParams[key] = clientUri.pathSegments[i];
    }
  }

  return (resultKey, resultParams);
}