kfAddAddress static method

Future kfAddAddress({
  1. required String phone,
  2. required String userAuthToken,
  3. required Map<String, dynamic> address,
  4. String? email,
  5. bool consent = true,
})

Saves a new address for a phone number. Sets muToken + x-idempotency-key on global client headers (mirrors RN), then removes x-idempotency-key after the call to avoid polluting other requests.

Implementation

static Future<dynamic> kfAddAddress({
  required String phone,
  required String userAuthToken,
  required Map<String, dynamic> address,
  String? email,
  bool consent = true,
}) async {
  // Gate before any header mutation, so a blocked call leaves the shared
  // client untouched.
  await checkKwikFormsHealth();
  return _withRetry(() async {
    final gokwik = DioClient().getClient();
    final idempotencyKey = _generateUuid();

    gokwik.options.headers[APIHeader.muToken] = userAuthToken;
    gokwik.options.headers[APIHeader.xIdempotencyKey] = idempotencyKey;

    try {
      final response = await gokwik.post(
        cdnConfigInstance.getEndpoint(APIEndpointKeys.kfAddresses)!,
        data: {
          'phone': phone,
          'consent': consent,
          'address': address,
          if (email != null && email.isNotEmpty) 'email': email,
        },
      );
      return response.data;
    } finally {
      // remove x-idempotency-key to avoid affecting other requests
      gokwik.options.headers.remove(APIHeader.xIdempotencyKey);
    }
  });
}