getProjectDomains method

Future<List<DomainConfig>> getProjectDomains(
  1. String projectId
)

Get project domains

Implementation

Future<List<DomainConfig>> getProjectDomains(String projectId) async {
  final url = Uri.parse('$baseUrl/domains/projects/$projectId');

  Map<String, String> headers;
  try {
    headers = await _authHeaders();
  } catch (_) {
    // For domains, return empty list instead of throwing
    return [];
  }

  try {
    final response = await _getWithRetry(url, headers);

    if (response.statusCode == 200) {
      final body = response.body;
      List<dynamic> domainsList;

      // Response might be a list directly or wrapped in an object
      if (body.trim().startsWith('[')) {
        // Direct array response
        domainsList = jsonDecode(body) as List<dynamic>;
      } else {
        // Wrapped in object
        final json = jsonDecode(body) as Map<String, dynamic>;
        domainsList = json['data'] as List<dynamic>? ??
            json['domains'] as List<dynamic>? ??
            [];
      }

      return domainsList.map((d) {
        final domainJson = d as Map<String, dynamic>;
        // Map API response format to DomainConfig format
        // API returns 'verified' (boolean), CLI expects 'status' (string)
        final verified = domainJson['verified'] as bool? ?? false;
        final status = verified ? 'verified' : 'pending';

        return DomainConfig.fromJson({
          'id': domainJson['id'],
          'host': domainJson['host'],
          'status': status,
          'isPrimary': domainJson['isPrimary'] ?? false,
        });
      }).toList();
    } else if (response.statusCode == 401) {
      throw Exception(
        'Authentication failed. Please run "ulink login" to re-authenticate.',
      );
    } else if (response.statusCode == 403) {
      throw Exception(
        'Access forbidden. You may not have permission to access domains for this project.',
      );
    } else if (response.statusCode == 404) {
      // Project not found or no domains - return empty list
      return [];
    }
  } catch (e) {
    // Log error but don't fail verification
    // If it's an auth error, rethrow it
    if (e.toString().contains('Authentication') ||
        e.toString().contains('forbidden')) {
      rethrow;
    }
    // For other errors, return empty list
  }

  return [];
}