@umituz/react-native-subscription 2.14.11 → 2.14.14

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umituz/react-native-subscription",
3
- "version": "2.14.11",
3
+ "version": "2.14.14",
4
4
  "description": "Complete subscription management with RevenueCat, paywall UI, and credits system for React Native apps",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
@@ -0,0 +1,32 @@
1
+ /**
2
+ * InsufficientCreditsError
3
+ *
4
+ * Thrown when user doesn't have enough credits for an operation.
5
+ * Silently handled - triggers paywall display.
6
+ */
7
+
8
+ export class InsufficientCreditsError extends Error {
9
+ public readonly requiredCredits: number;
10
+ public readonly availableCredits: number;
11
+ public readonly operationType: string;
12
+
13
+ constructor(
14
+ requiredCredits: number,
15
+ availableCredits: number,
16
+ operationType: string,
17
+ ) {
18
+ super(
19
+ `Insufficient credits: ${availableCredits} available, ${requiredCredits} required`,
20
+ );
21
+ this.name = "InsufficientCreditsError";
22
+ this.requiredCredits = requiredCredits;
23
+ this.availableCredits = availableCredits;
24
+ this.operationType = operationType;
25
+
26
+ Object.setPrototypeOf(this, InsufficientCreditsError.prototype);
27
+ }
28
+
29
+ get deficit(): number {
30
+ return this.requiredCredits - this.availableCredits;
31
+ }
32
+ }
package/src/index.ts CHANGED
@@ -233,6 +233,12 @@ export type {
233
233
 
234
234
  export { DEFAULT_CREDITS_CONFIG } from "./domain/entities/Credits";
235
235
 
236
+ // =============================================================================
237
+ // CREDITS SYSTEM - Errors
238
+ // =============================================================================
239
+
240
+ export { InsufficientCreditsError } from "./domain/errors/InsufficientCreditsError";
241
+
236
242
  // CreditCost, Transaction types, Wallet types, Credit-cost types
237
243
  // are now exported from "./domains/wallet"
238
244
 
@@ -38,6 +38,8 @@ export interface UseCreditsResult {
38
38
  textCreditsPercent: number;
39
39
  imageCreditsPercent: number;
40
40
  refetch: () => void;
41
+ /** Check if user can afford a specific credit cost */
42
+ canAfford: (cost: number, type?: CreditType) => boolean;
41
43
  }
42
44
 
43
45
  export const useCredits = ({
@@ -86,6 +88,13 @@ export const useCredits = ({
86
88
  ? Math.round((credits.imageCredits / config.imageCreditLimit) * 100)
87
89
  : 0;
88
90
 
91
+ const canAfford = (cost: number, type: CreditType = "text"): boolean => {
92
+ if (!credits) return false;
93
+ return type === "text"
94
+ ? credits.textCredits >= cost
95
+ : credits.imageCredits >= cost;
96
+ };
97
+
89
98
  return {
90
99
  credits,
91
100
  isLoading,
@@ -95,6 +104,7 @@ export const useCredits = ({
95
104
  textCreditsPercent,
96
105
  imageCreditsPercent,
97
106
  refetch,
107
+ canAfford,
98
108
  };
99
109
  };
100
110