@sudobility/subscription_lib 0.0.6 → 0.0.8
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"revenuecat-web.d.ts","sourceRoot":"","sources":["../../src/adapters/revenuecat-web.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAKV,mBAAmB,EACpB,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"revenuecat-web.d.ts","sourceRoot":"","sources":["../../src/adapters/revenuecat-web.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAKV,mBAAmB,EACpB,MAAM,kBAAkB,CAAC;AAY1B;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAEzE;AA0BD;;;GAGG;AACH,wBAAsB,iBAAiB,CACrC,MAAM,EAAE,MAAM,EACd,KAAK,CAAC,EAAE,MAAM,GACb,OAAO,CAAC,IAAI,CAAC,CA6Cf;AAED;;GAEG;AACH,wBAAgB,mBAAmB,IAAI,IAAI,CAM1C;AAED;;GAEG;AACH,wBAAgB,iBAAiB,IAAI,OAAO,CAE3C;AAED;;GAEG;AACH,wBAAgB,uBAAuB,IAAI,mBAAmB,CAyM7D"}
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
let purchasesInstance = null;
|
|
9
9
|
let currentUserId = null;
|
|
10
10
|
let apiKey = null;
|
|
11
|
+
let pendingUserSetup = null; // Track in-flight user setup
|
|
11
12
|
/**
|
|
12
13
|
* Configure the RevenueCat adapter with API key.
|
|
13
14
|
* Call this before initializing subscription_lib.
|
|
@@ -16,32 +17,23 @@ let apiKey = null;
|
|
|
16
17
|
export function configureRevenueCatAdapter(revenueCatApiKey) {
|
|
17
18
|
apiKey = revenueCatApiKey;
|
|
18
19
|
}
|
|
19
|
-
/**
|
|
20
|
-
* Get or create an anonymous user ID for RevenueCat.
|
|
21
|
-
* RevenueCat web SDK requires a user ID, so we generate one for anonymous users.
|
|
22
|
-
*/
|
|
23
|
-
function getAnonymousUserId() {
|
|
24
|
-
const STORAGE_KEY = 'rc_anonymous_user_id';
|
|
25
|
-
let anonId = localStorage.getItem(STORAGE_KEY);
|
|
26
|
-
if (!anonId) {
|
|
27
|
-
anonId = `anon_${globalThis.crypto.randomUUID()}`;
|
|
28
|
-
localStorage.setItem(STORAGE_KEY, anonId);
|
|
29
|
-
}
|
|
30
|
-
return anonId;
|
|
31
|
-
}
|
|
32
20
|
/**
|
|
33
21
|
* Lazily initialize RevenueCat SDK when first needed.
|
|
34
|
-
*
|
|
22
|
+
* Requires a user to be set first via setRevenueCatUser().
|
|
35
23
|
*/
|
|
36
24
|
async function ensureInitialized() {
|
|
37
25
|
if (!apiKey) {
|
|
38
26
|
throw new Error('RevenueCat not configured');
|
|
39
27
|
}
|
|
28
|
+
if (!currentUserId) {
|
|
29
|
+
throw new Error('RevenueCat user not set. Call setRevenueCatUser() first.');
|
|
30
|
+
}
|
|
40
31
|
if (!purchasesInstance) {
|
|
41
32
|
const SDK = await import('@revenuecat/purchases-js');
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
33
|
+
purchasesInstance = SDK.Purchases.configure({
|
|
34
|
+
apiKey,
|
|
35
|
+
appUserId: currentUserId,
|
|
36
|
+
});
|
|
45
37
|
}
|
|
46
38
|
return purchasesInstance;
|
|
47
39
|
}
|
|
@@ -54,30 +46,40 @@ export async function setRevenueCatUser(userId, email) {
|
|
|
54
46
|
console.warn('[subscription] RevenueCat not configured');
|
|
55
47
|
return;
|
|
56
48
|
}
|
|
57
|
-
// Skip if same user
|
|
49
|
+
// Skip if same user already set up
|
|
58
50
|
if (currentUserId === userId && purchasesInstance) {
|
|
59
51
|
return;
|
|
60
52
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (purchasesInstance) {
|
|
65
|
-
purchasesInstance.close();
|
|
53
|
+
// Skip if already setting up this user (prevent concurrent calls)
|
|
54
|
+
if (pendingUserSetup === userId) {
|
|
55
|
+
return;
|
|
66
56
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
if (email) {
|
|
74
|
-
try {
|
|
75
|
-
await purchasesInstance.setAttributes({ email });
|
|
57
|
+
pendingUserSetup = userId;
|
|
58
|
+
try {
|
|
59
|
+
const SDK = await import('@revenuecat/purchases-js');
|
|
60
|
+
// Close existing instance
|
|
61
|
+
if (purchasesInstance) {
|
|
62
|
+
purchasesInstance.close();
|
|
76
63
|
}
|
|
77
|
-
|
|
78
|
-
|
|
64
|
+
// Configure new instance with user
|
|
65
|
+
purchasesInstance = SDK.Purchases.configure({
|
|
66
|
+
apiKey,
|
|
67
|
+
appUserId: userId,
|
|
68
|
+
});
|
|
69
|
+
currentUserId = userId;
|
|
70
|
+
// Set email attribute
|
|
71
|
+
if (email) {
|
|
72
|
+
try {
|
|
73
|
+
await purchasesInstance.setAttributes({ email });
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
// Ignore attribute errors
|
|
77
|
+
}
|
|
79
78
|
}
|
|
80
79
|
}
|
|
80
|
+
finally {
|
|
81
|
+
pendingUserSetup = null;
|
|
82
|
+
}
|
|
81
83
|
}
|
|
82
84
|
/**
|
|
83
85
|
* Clear the current user (on logout).
|
|
@@ -109,6 +111,11 @@ export function createRevenueCatAdapter() {
|
|
|
109
111
|
}
|
|
110
112
|
},
|
|
111
113
|
async getOfferings() {
|
|
114
|
+
// Return empty offerings if no user is set
|
|
115
|
+
if (!currentUserId) {
|
|
116
|
+
console.log('[revenuecat-web] No user set, returning empty offerings');
|
|
117
|
+
return { all: {}, current: null };
|
|
118
|
+
}
|
|
112
119
|
try {
|
|
113
120
|
const purchases = await ensureInitialized();
|
|
114
121
|
const offerings = await purchases.getOfferings();
|
|
@@ -195,6 +202,10 @@ export function createRevenueCatAdapter() {
|
|
|
195
202
|
}
|
|
196
203
|
},
|
|
197
204
|
async getCustomerInfo() {
|
|
205
|
+
// Return empty customer info if no user is set
|
|
206
|
+
if (!currentUserId) {
|
|
207
|
+
return { activeSubscriptions: [], entitlements: { active: {} } };
|
|
208
|
+
}
|
|
198
209
|
try {
|
|
199
210
|
const purchases = await ensureInitialized();
|
|
200
211
|
const customerInfo = await purchases.getCustomerInfo();
|