getRouteName function

String getRouteName(
  1. String path
)

Extracts a route name from the given path.

  • Cleans the path using cleanPath (removes /api/{something}/ and braces).
  • Removes prefixes defined in ConstantsHelper.allPrefixesToRemove.
  • Splits remaining parts into entity and action.
  • Maps common API verbs (Get, Create, Add, etc.) to readable method names.
  • Ensures entity names are pluralized for GetAll.
  • Returns a PascalCase route name like GetOrders, CreateStudent.

If the path is empty after cleaning, it falls back to ConstantsHelper.generalCategory.

Implementation

String getRouteName(String path) {
  path = cleanPath(path);
  List<String> parts = path.split('/')..removeWhere((p) => p.isEmpty);

  if (parts.isEmpty) return ConstantsHelper.generalCategory;

  // Special handling for Hub routes
  if (parts.first.toLowerCase() == 'hub' && parts.length >= 3) {
    String entity = _toPascalCase(parts[1]); // e.g., SamePageWarning
    String action = _toPascalCase(parts[2]); // e.g., CheckPage, PageLogout
    return action + entity; // => CheckPageSamePageWarning
  }

  // Remove defined prefixes
  for (var prefix in ConstantsHelper.allPrefixesToRemove) {
    if (parts.isNotEmpty && parts.first.toLowerCase() == prefix.toLowerCase()) {
      parts.removeAt(0);
      break;
    }
  }

  if (parts.isEmpty) return ConstantsHelper.generalCategory;

  String entity = _toPascalCase(parts[0]);
  String action = parts.length > 1 ? _toPascalCase(parts[1]) : "";

  final Map<String, String> verbMap = {
    "GetAll": "GetAll",
    "GetById": "GetById",
    "Get": "Get",
    "Create": "Create",
    "Add": "Add",
    "Modify": "Modify",
    "Reset": "Reset",
    "Send": "Send",
    "Verify": "Verify",
    "Resend": "Resend",
    "Change": "Change",
    "Refresh": "Refresh",
    "LogIn": "Login",
    "LoginAsGuest": "LoginAsGuest",
  };

  String mappedAction = verbMap[action] ?? action;
  if (!mappedAction.contains(entity)) {
    mappedAction = mappedAction + entity;
  }

  if (mappedAction.startsWith("GetAll") && !entity.endsWith("s")) {
    entity += "s";
  }

  return mappedAction;
}