@umituz/react-native-subscription 2.27.10 → 2.27.12
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.27.
|
|
3
|
+
"version": "2.27.12",
|
|
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",
|
|
@@ -74,10 +74,10 @@ export interface CreditsConfig {
|
|
|
74
74
|
creditPackageAmounts?: Record<string, number>;
|
|
75
75
|
/** Credit allocations for different subscription types (weekly, monthly, yearly) */
|
|
76
76
|
packageAllocations?: PackageAllocationMap;
|
|
77
|
-
/**
|
|
77
|
+
/** Enable free credits for new users (default: false) */
|
|
78
|
+
enableFreeCredits?: boolean;
|
|
79
|
+
/** Free credits given to new users on registration (only used when enableFreeCredits: true) */
|
|
78
80
|
freeCredits?: number;
|
|
79
|
-
/** Whether to auto-initialize free credits when user has no credits document (default: true if freeCredits > 0) */
|
|
80
|
-
autoInitializeFreeCredits?: boolean;
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
export interface CreditsResult<T = UserCredits> {
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Subscription Init Module Factory
|
|
3
|
+
* Creates a ready-to-use InitModule for app initialization
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { initializeSubscription, type SubscriptionInitConfig } from '../infrastructure/services/SubscriptionInitializer';
|
|
7
|
+
|
|
8
|
+
declare const __DEV__: boolean;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* InitModule interface (from @umituz/react-native-design-system)
|
|
12
|
+
*/
|
|
13
|
+
export interface InitModule {
|
|
14
|
+
name: string;
|
|
15
|
+
init: () => Promise<boolean>;
|
|
16
|
+
critical?: boolean;
|
|
17
|
+
dependsOn?: string[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface SubscriptionInitModuleConfig extends Omit<SubscriptionInitConfig, 'apiKey'> {
|
|
21
|
+
/**
|
|
22
|
+
* RevenueCat API key getter function
|
|
23
|
+
* Returns the API key or undefined if not available
|
|
24
|
+
*/
|
|
25
|
+
getApiKey: () => string | undefined;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Optional RevenueCat test store key getter
|
|
29
|
+
*/
|
|
30
|
+
getTestStoreKey?: () => string | undefined;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Whether this module is critical for app startup
|
|
34
|
+
* @default true
|
|
35
|
+
*/
|
|
36
|
+
critical?: boolean;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Module dependencies
|
|
40
|
+
* @default ["auth"]
|
|
41
|
+
*/
|
|
42
|
+
dependsOn?: string[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Creates a Subscription initialization module for use with createAppInitializer
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* import { createAppInitializer } from "@umituz/react-native-design-system";
|
|
51
|
+
* import { createFirebaseInitModule } from "@umituz/react-native-firebase";
|
|
52
|
+
* import { createAuthInitModule } from "@umituz/react-native-auth";
|
|
53
|
+
* import { createSubscriptionInitModule } from "@umituz/react-native-subscription";
|
|
54
|
+
*
|
|
55
|
+
* export const initializeApp = createAppInitializer({
|
|
56
|
+
* modules: [
|
|
57
|
+
* createFirebaseInitModule(),
|
|
58
|
+
* createAuthInitModule({ userCollection: "users" }),
|
|
59
|
+
* createSubscriptionInitModule({
|
|
60
|
+
* getApiKey: () => getRevenueCatApiKey(),
|
|
61
|
+
* entitlementId: "premium",
|
|
62
|
+
* credits: {
|
|
63
|
+
* collectionName: "credits",
|
|
64
|
+
* creditLimit: 500,
|
|
65
|
+
* enableFreeCredits: true,
|
|
66
|
+
* freeCredits: 1,
|
|
67
|
+
* },
|
|
68
|
+
* }),
|
|
69
|
+
* ],
|
|
70
|
+
* });
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export function createSubscriptionInitModule(
|
|
74
|
+
config: SubscriptionInitModuleConfig
|
|
75
|
+
): InitModule {
|
|
76
|
+
const {
|
|
77
|
+
getApiKey,
|
|
78
|
+
getTestStoreKey,
|
|
79
|
+
critical = true,
|
|
80
|
+
dependsOn = ['auth'],
|
|
81
|
+
...subscriptionConfig
|
|
82
|
+
} = config;
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
name: 'subscription',
|
|
86
|
+
critical,
|
|
87
|
+
dependsOn,
|
|
88
|
+
init: async () => {
|
|
89
|
+
try {
|
|
90
|
+
const apiKey = getApiKey();
|
|
91
|
+
|
|
92
|
+
if (!apiKey) {
|
|
93
|
+
if (typeof __DEV__ !== 'undefined' && __DEV__) {
|
|
94
|
+
console.log('[createSubscriptionInitModule] No API key - skipping');
|
|
95
|
+
}
|
|
96
|
+
return true; // Not an error, just skip
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const testStoreKey = getTestStoreKey?.();
|
|
100
|
+
|
|
101
|
+
await initializeSubscription({
|
|
102
|
+
apiKey,
|
|
103
|
+
testStoreKey,
|
|
104
|
+
...subscriptionConfig,
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
if (typeof __DEV__ !== 'undefined' && __DEV__) {
|
|
108
|
+
console.log('[createSubscriptionInitModule] Subscription initialized');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return true;
|
|
112
|
+
} catch (error) {
|
|
113
|
+
if (typeof __DEV__ !== 'undefined' && __DEV__) {
|
|
114
|
+
console.error('[createSubscriptionInitModule] Error:', error);
|
|
115
|
+
}
|
|
116
|
+
// Continue on error - subscription is not critical for app launch
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
|
@@ -115,7 +115,8 @@ export function useFreeCreditsInit(params: UseFreeCreditsInitParams): UseFreeCre
|
|
|
115
115
|
const isConfigured = isCreditsRepositoryConfigured();
|
|
116
116
|
const config = getCreditsConfig();
|
|
117
117
|
const freeCredits = config.freeCredits ?? 0;
|
|
118
|
-
|
|
118
|
+
// Free credits only enabled when explicitly set to true AND freeCredits > 0
|
|
119
|
+
const isFreeCreditsEnabled = config.enableFreeCredits === true && freeCredits > 0;
|
|
119
120
|
|
|
120
121
|
// Check if THIS user's init is in progress (shared across all hook instances)
|
|
121
122
|
const isInitializing = userId ? inProgressSet.has(userId) : false;
|
|
@@ -127,7 +128,7 @@ export function useFreeCreditsInit(params: UseFreeCreditsInitParams): UseFreeCre
|
|
|
127
128
|
isRegisteredUser &&
|
|
128
129
|
isConfigured &&
|
|
129
130
|
!hasCredits &&
|
|
130
|
-
|
|
131
|
+
isFreeCreditsEnabled &&
|
|
131
132
|
!freeCreditsInitAttempted.has(userId);
|
|
132
133
|
|
|
133
134
|
// Stable callback reference
|
|
@@ -143,12 +144,12 @@ export function useFreeCreditsInit(params: UseFreeCreditsInitParams): UseFreeCre
|
|
|
143
144
|
if (!freeCreditsInitAttempted.has(userId)) {
|
|
144
145
|
initializeFreeCreditsForUser(userId, stableOnComplete);
|
|
145
146
|
}
|
|
146
|
-
} else if (querySuccess && isAnonymous && !hasCredits &&
|
|
147
|
+
} else if (querySuccess && isAnonymous && !hasCredits && isFreeCreditsEnabled) {
|
|
147
148
|
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
148
149
|
console.log("[useFreeCreditsInit] Skipping - anonymous user must register first");
|
|
149
150
|
}
|
|
150
151
|
}
|
|
151
|
-
}, [needsInit, userId, querySuccess, isAnonymous, hasCredits,
|
|
152
|
+
}, [needsInit, userId, querySuccess, isAnonymous, hasCredits, isFreeCreditsEnabled, stableOnComplete]);
|
|
152
153
|
|
|
153
154
|
return {
|
|
154
155
|
isInitializing: isInitializing || needsInit,
|