register method

Future<Customer> register({
  1. required CustomerCreateRequest request,
})

Register a new customer account.

Creates a new customer account in the Magento system with the provided information. The customer will need to authenticate separately after registration.

request the customer registration request containing all required fields

Returns a Customer object representing the newly created customer. Throws an exception if registration fails.

Implementation

Future<Customer> register({required CustomerCreateRequest request}) async {
  try {
    final response = await _client.guestRequest<Map<String, dynamic>>(
      '/rest/V1/customers',
      data: request.toJson(),
    );

    if (response.statusCode == 200) {
      return Customer.fromJson(response.data!);
    } else {
      throw Exception('Registration failed: ${response.statusMessage}');
    }
  } on DioException catch (e) {
    if (e.response?.statusCode == 400) {
      final errorData = e.response?.data;
      if (errorData is Map<String, dynamic>) {
        final message = errorData['message'] ?? 'Registration failed';
        throw Exception(message);
      }
    }
    throw Exception('Registration failed: ${e.message}');
  } catch (e) {
    throw Exception('Registration failed: $e');
  }
}