expo-iap 3.0.2 → 3.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,21 +5,27 @@
5
5
 
6
6
  import {ErrorCode} from '../ExpoIap.types';
7
7
 
8
- /**
9
- * Checks if an error is a user cancellation
10
- * @param error Error object or error code
11
- * @returns True if the error represents user cancellation
12
- */
13
- export function isUserCancelledError(error: any): boolean {
8
+ type ErrorLike = string | {code?: ErrorCode | string; message?: string};
9
+
10
+ function extractCode(error: unknown): string | undefined {
14
11
  if (typeof error === 'string') {
15
- return error === ErrorCode.E_USER_CANCELLED;
12
+ return error;
16
13
  }
17
14
 
18
- if (error && error.code) {
19
- return error.code === ErrorCode.E_USER_CANCELLED;
15
+ if (error && typeof error === 'object' && 'code' in error) {
16
+ return (error as {code?: string}).code;
20
17
  }
21
18
 
22
- return false;
19
+ return undefined;
20
+ }
21
+
22
+ /**
23
+ * Checks if an error is a user cancellation
24
+ * @param error Error object or error code
25
+ * @returns True if the error represents user cancellation
26
+ */
27
+ export function isUserCancelledError(error: unknown): boolean {
28
+ return extractCode(error) === ErrorCode.UserCancelled;
23
29
  }
24
30
 
25
31
  /**
@@ -27,17 +33,17 @@ export function isUserCancelledError(error: any): boolean {
27
33
  * @param error Error object or error code
28
34
  * @returns True if the error is network-related
29
35
  */
30
- export function isNetworkError(error: any): boolean {
31
- const networkErrors = [
32
- ErrorCode.E_NETWORK_ERROR,
33
- ErrorCode.E_REMOTE_ERROR,
34
- ErrorCode.E_SERVICE_ERROR,
35
- ErrorCode.E_SERVICE_DISCONNECTED,
36
- ErrorCode.E_BILLING_UNAVAILABLE,
36
+ export function isNetworkError(error: unknown): boolean {
37
+ const networkErrors: ErrorCode[] = [
38
+ ErrorCode.NetworkError,
39
+ ErrorCode.RemoteError,
40
+ ErrorCode.ServiceError,
41
+ ErrorCode.ServiceDisconnected,
42
+ ErrorCode.BillingUnavailable,
37
43
  ];
38
44
 
39
- const errorCode = typeof error === 'string' ? error : error?.code;
40
- return networkErrors.includes(errorCode);
45
+ const code = extractCode(error);
46
+ return !!code && (networkErrors as string[]).includes(code);
41
47
  }
42
48
 
43
49
  /**
@@ -45,20 +51,20 @@ export function isNetworkError(error: any): boolean {
45
51
  * @param error Error object or error code
46
52
  * @returns True if the error is potentially recoverable
47
53
  */
48
- export function isRecoverableError(error: any): boolean {
49
- const recoverableErrors = [
50
- ErrorCode.E_NETWORK_ERROR,
51
- ErrorCode.E_REMOTE_ERROR,
52
- ErrorCode.E_SERVICE_ERROR,
53
- ErrorCode.E_INTERRUPTED,
54
- ErrorCode.E_SERVICE_DISCONNECTED,
55
- ErrorCode.E_BILLING_UNAVAILABLE,
56
- ErrorCode.E_QUERY_PRODUCT,
57
- ErrorCode.E_INIT_CONNECTION,
54
+ export function isRecoverableError(error: unknown): boolean {
55
+ const recoverableErrors: ErrorCode[] = [
56
+ ErrorCode.NetworkError,
57
+ ErrorCode.RemoteError,
58
+ ErrorCode.ServiceError,
59
+ ErrorCode.Interrupted,
60
+ ErrorCode.ServiceDisconnected,
61
+ ErrorCode.BillingUnavailable,
62
+ ErrorCode.QueryProduct,
63
+ ErrorCode.InitConnection,
58
64
  ];
59
65
 
60
- const errorCode = typeof error === 'string' ? error : error?.code;
61
- return recoverableErrors.includes(errorCode);
66
+ const code = extractCode(error);
67
+ return !!code && (recoverableErrors as string[]).includes(code);
62
68
  }
63
69
 
64
70
  /**
@@ -66,49 +72,56 @@ export function isRecoverableError(error: any): boolean {
66
72
  * @param error Error object or error code
67
73
  * @returns User-friendly error message
68
74
  */
69
- export function getUserFriendlyErrorMessage(error: any): string {
70
- const errorCode = typeof error === 'string' ? error : error?.code;
75
+ export function getUserFriendlyErrorMessage(error: ErrorLike): string {
76
+ const errorCode = extractCode(error);
71
77
 
72
78
  switch (errorCode) {
73
- case ErrorCode.E_USER_CANCELLED:
79
+ case ErrorCode.UserCancelled:
74
80
  return 'Purchase was cancelled by user';
75
- case ErrorCode.E_NETWORK_ERROR:
81
+ case ErrorCode.NetworkError:
76
82
  return 'Network connection error. Please check your internet connection and try again.';
77
- case ErrorCode.E_RECEIPT_FINISHED:
83
+ case ErrorCode.ReceiptFinished:
78
84
  return 'Receipt already finished';
79
- case ErrorCode.E_SERVICE_DISCONNECTED:
85
+ case ErrorCode.ServiceDisconnected:
80
86
  return 'Billing service disconnected. Please try again.';
81
- case ErrorCode.E_BILLING_UNAVAILABLE:
87
+ case ErrorCode.BillingUnavailable:
82
88
  return 'Billing is unavailable on this device or account.';
83
- case ErrorCode.E_ITEM_UNAVAILABLE:
89
+ case ErrorCode.ItemUnavailable:
84
90
  return 'This item is not available for purchase';
85
- case ErrorCode.E_ITEM_NOT_OWNED:
91
+ case ErrorCode.ItemNotOwned:
86
92
  return "You don't own this item";
87
- case ErrorCode.E_ALREADY_OWNED:
93
+ case ErrorCode.AlreadyOwned:
88
94
  return 'You already own this item';
89
- case ErrorCode.E_SKU_NOT_FOUND:
95
+ case ErrorCode.SkuNotFound:
90
96
  return 'Requested product could not be found';
91
- case ErrorCode.E_SKU_OFFER_MISMATCH:
97
+ case ErrorCode.SkuOfferMismatch:
92
98
  return 'Selected offer does not match the SKU';
93
- case ErrorCode.E_DEFERRED_PAYMENT:
99
+ case ErrorCode.DeferredPayment:
94
100
  return 'Payment is pending approval';
95
- case ErrorCode.E_NOT_PREPARED:
101
+ case ErrorCode.NotPrepared:
96
102
  return 'In-app purchase is not ready. Please try again later.';
97
- case ErrorCode.E_SERVICE_ERROR:
103
+ case ErrorCode.ServiceError:
98
104
  return 'Store service error. Please try again later.';
99
- case ErrorCode.E_FEATURE_NOT_SUPPORTED:
105
+ case ErrorCode.FeatureNotSupported:
100
106
  return 'This feature is not supported on this device.';
101
- case ErrorCode.E_TRANSACTION_VALIDATION_FAILED:
107
+ case ErrorCode.TransactionValidationFailed:
102
108
  return 'Transaction could not be verified';
103
- case ErrorCode.E_RECEIPT_FAILED:
109
+ case ErrorCode.ReceiptFailed:
104
110
  return 'Receipt processing failed';
105
- case ErrorCode.E_EMPTY_SKU_LIST:
111
+ case ErrorCode.EmptySkuList:
106
112
  return 'No product IDs provided';
107
- case ErrorCode.E_INIT_CONNECTION:
113
+ case ErrorCode.InitConnection:
108
114
  return 'Failed to initialize billing connection';
109
- case ErrorCode.E_QUERY_PRODUCT:
115
+ case ErrorCode.QueryProduct:
110
116
  return 'Failed to query products. Please try again later.';
111
- default:
112
- return error?.message || 'An unexpected error occurred';
117
+ default: {
118
+ if (error && typeof error === 'object' && 'message' in error) {
119
+ return (
120
+ (error as {message?: string}).message ??
121
+ 'An unexpected error occurred'
122
+ );
123
+ }
124
+ return 'An unexpected error occurred';
125
+ }
113
126
  }
114
127
  }