cleanConfigUrl function

Uri cleanConfigUrl(
  1. Uri url,
  2. UrlCleaner? cleaner
)

Cleans URL based on cleaner

Implementation

Uri cleanConfigUrl(Uri url, UrlCleaner? cleaner) {
  //Set variables
  Map<String, String> params = {};

  //Empty cleaner, return path only, no params!
  if (cleaner == null) {
    return Uri.https(url.authority, url.path);
  }

  //Blacklisted params! Add all other params.
  if (cleaner.blacklistParams != null) {
    url.queryParameters.forEach((key, value) {
      if (!cleaner.blacklistParams!.contains(key)) {
        params[key] = value;
      }
    });
  } else {
    params.addAll(url.queryParameters);
  }

  //Let's remove non-whitelisted params
  if (cleaner.whitelistParams != null) {
    List<String> toRemove = [];
    params.forEach((key, value) {
      if (!cleaner.whitelistParams!.contains(key)) {
        toRemove.add(key);
      }
    });
    params.removeWhere((k, v) => toRemove.contains(k));
  } else {
    params.clear();
  }

  //Let's append defined parameters
  if (cleaner.appendParams != null) {
    cleaner.appendParams!.forEach((key, value) {
      params[key] = value;
    });
  }

  return Uri.https(url.authority, url.path, params);
}