normalizeToAppPath static method

Uri normalizeToAppPath(
  1. Uri incoming, {
  2. Iterable<String> hosts = const [],
})

Convert a deep link URI into an internal, path-only URI.

  • HTTPS URIs whose host appears in hosts keep their path/query.
  • Custom scheme URIs promote the host to the first segment.
  • Query parameters are preserved.

Implementation

static Uri normalizeToAppPath(
  Uri incoming, {
  Iterable<String> hosts = const [],
}) {
  if (!incoming.hasScheme) {
    return incoming;
  }

  final lowerHosts = hosts.map((host) => host.toLowerCase());
  final scheme = incoming.scheme.toLowerCase();
  final params =
      incoming.queryParameters.isEmpty ? null : incoming.queryParameters;

  if ((scheme == 'http' || scheme == 'https') &&
      lowerHosts.any((host) => incoming.host.toLowerCase().contains(host))) {
    final path = incoming.path.isEmpty ? '/' : incoming.path;
    return Uri(path: path, queryParameters: params);
  }

  if (incoming.host.isEmpty) {
    final segments = incoming.pathSegments;
    final path = segments.isEmpty ? '/' : '/${segments.join('/')}';
    return Uri(path: path, queryParameters: params);
  }

  final segments = [incoming.host, ...incoming.pathSegments];
  return Uri(path: '/${segments.join('/')}', queryParameters: params);
}