normalizeHubUri static method

Uri normalizeHubUri(
  1. String input
)

Normalizes a user-entered Hub address into a wss:// Uri.

Accepts host, host:port, wss://host:port[/path]. A bare ws:// scheme is preserved (for local dev), anything else becomes wss://. Throws an AppError for empty or unparseable input.

Implementation

static Uri normalizeHubUri(String input) {
  final trimmed = input.trim();
  if (trimmed.isEmpty) {
    throw const AppError(AppErrorKind.transport, 'Enter a Hub address.');
  }
  final hasScheme = trimmed.contains('://');
  final candidate = hasScheme ? trimmed : 'wss://$trimmed';
  final Uri uri;
  try {
    uri = Uri.parse(candidate);
  } on FormatException {
    throw AppError(AppErrorKind.transport, 'Invalid Hub address: $input');
  }
  if (uri.host.isEmpty) {
    throw AppError(AppErrorKind.transport, 'Invalid Hub address: $input');
  }
  if (uri.scheme != 'wss' && uri.scheme != 'ws') {
    return uri.replace(scheme: 'wss');
  }
  return uri;
}