@sudobility/subscription_lib 0.0.8 → 0.0.10
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/dist/adapters/index.d.ts +2 -6
- package/dist/adapters/index.d.ts.map +1 -1
- package/dist/adapters/index.js +3 -6
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/revenuecat-rn.d.ts +7 -0
- package/dist/adapters/revenuecat-rn.d.ts.map +1 -0
- package/dist/adapters/revenuecat-rn.js +230 -0
- package/dist/adapters/revenuecat-rn.js.map +1 -0
- package/dist/adapters/revenuecat-web.d.ts +0 -24
- package/dist/adapters/revenuecat-web.d.ts.map +1 -1
- package/dist/adapters/revenuecat-web.js +2 -42
- package/dist/adapters/revenuecat-web.js.map +1 -0
- package/dist/core/index.d.ts +0 -3
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +1 -3
- package/dist/core/index.js.map +1 -0
- package/dist/core/service.d.ts +0 -52
- package/dist/core/service.d.ts.map +1 -1
- package/dist/core/service.js +21 -103
- package/dist/core/service.js.map +1 -0
- package/dist/core/singleton.d.ts +0 -85
- package/dist/core/singleton.d.ts.map +1 -1
- package/dist/core/singleton.js +1 -85
- package/dist/core/singleton.js.map +1 -0
- package/dist/hooks/index.d.ts +0 -3
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +1 -3
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/useSubscribable.d.ts +0 -53
- package/dist/hooks/useSubscribable.d.ts.map +1 -1
- package/dist/hooks/useSubscribable.js +1 -51
- package/dist/hooks/useSubscribable.js.map +1 -0
- package/dist/hooks/useSubscriptionForPeriod.d.ts +0 -45
- package/dist/hooks/useSubscriptionForPeriod.d.ts.map +1 -1
- package/dist/hooks/useSubscriptionForPeriod.js +1 -42
- package/dist/hooks/useSubscriptionForPeriod.js.map +1 -0
- package/dist/hooks/useSubscriptionPeriods.d.ts +0 -36
- package/dist/hooks/useSubscriptionPeriods.d.ts.map +1 -1
- package/dist/hooks/useSubscriptionPeriods.js +1 -32
- package/dist/hooks/useSubscriptionPeriods.js.map +1 -0
- package/dist/hooks/useSubscriptions.d.ts +0 -44
- package/dist/hooks/useSubscriptions.d.ts.map +1 -1
- package/dist/hooks/useSubscriptions.js +1 -34
- package/dist/hooks/useSubscriptions.js.map +1 -0
- package/dist/hooks/useUserSubscription.d.ts +0 -44
- package/dist/hooks/useUserSubscription.d.ts.map +1 -1
- package/dist/hooks/useUserSubscription.js +1 -38
- package/dist/hooks/useUserSubscription.js.map +1 -0
- package/dist/index.d.ts +1 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -10
- package/dist/index.js.map +1 -0
- package/dist/types/adapter.d.ts +0 -94
- package/dist/types/adapter.d.ts.map +1 -1
- package/dist/types/adapter.js +1 -6
- package/dist/types/adapter.js.map +1 -0
- package/dist/types/index.d.ts +0 -3
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +1 -3
- package/dist/types/index.js.map +1 -0
- package/dist/types/period.d.ts +0 -14
- package/dist/types/period.d.ts.map +1 -1
- package/dist/types/period.js +1 -11
- package/dist/types/period.js.map +1 -0
- package/dist/types/subscription.d.ts +0 -53
- package/dist/types/subscription.d.ts.map +1 -1
- package/dist/types/subscription.js +1 -5
- package/dist/types/subscription.js.map +1 -0
- package/dist/utils/index.d.ts +0 -3
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -3
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/level-calculator.d.ts +0 -58
- package/dist/utils/level-calculator.d.ts.map +1 -1
- package/dist/utils/level-calculator.js +1 -68
- package/dist/utils/level-calculator.js.map +1 -0
- package/dist/utils/period-parser.d.ts +0 -39
- package/dist/utils/period-parser.d.ts.map +1 -1
- package/dist/utils/period-parser.js +3 -47
- package/dist/utils/period-parser.js.map +1 -0
- package/package.json +8 -3
|
@@ -1,52 +1,8 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* useSubscribable Hook
|
|
3
|
-
*
|
|
4
|
-
* Get packages that the user can subscribe to or upgrade to.
|
|
5
|
-
*/
|
|
6
1
|
import { useMemo } from 'react';
|
|
7
2
|
import { useSubscriptions, } from './useSubscriptions';
|
|
8
3
|
import { useUserSubscription } from './useUserSubscription';
|
|
9
4
|
import { findUpgradeablePackages } from '../utils/level-calculator';
|
|
10
5
|
import { getSubscriptionInstance, isSubscriptionInitialized, } from '../core/singleton';
|
|
11
|
-
/**
|
|
12
|
-
* Hook to get packages that the user can subscribe to or upgrade to
|
|
13
|
-
*
|
|
14
|
-
* Upgrade eligibility rules:
|
|
15
|
-
* - If no current subscription: all packages are subscribable
|
|
16
|
-
* - If on free tier: all paid packages are subscribable
|
|
17
|
-
* - If has subscription:
|
|
18
|
-
* - Package period must be >= current period (monthly → yearly OK, yearly → monthly NOT OK)
|
|
19
|
-
* - Package level must be >= current level (basic → pro OK, pro → basic NOT OK)
|
|
20
|
-
* - Level is determined by price comparison within the same period
|
|
21
|
-
*
|
|
22
|
-
* @param offerId Offer identifier
|
|
23
|
-
* @param options Optional configuration including userId
|
|
24
|
-
* @returns List of subscribable package IDs
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* ```typescript
|
|
28
|
-
* const { subscribablePackageIds } = useSubscribable('default');
|
|
29
|
-
*
|
|
30
|
-
* // With user ID
|
|
31
|
-
* const { subscribablePackageIds } = useSubscribable('default', {
|
|
32
|
-
* userId: user?.uid,
|
|
33
|
-
* });
|
|
34
|
-
*
|
|
35
|
-
* const { packages } = useSubscriptionForPeriod('default', 'monthly');
|
|
36
|
-
*
|
|
37
|
-
* return (
|
|
38
|
-
* <div>
|
|
39
|
-
* {packages.map(pkg => (
|
|
40
|
-
* <SubscriptionTile
|
|
41
|
-
* key={pkg.packageId}
|
|
42
|
-
* enabled={subscribablePackageIds.includes(pkg.packageId)}
|
|
43
|
-
* {...pkg}
|
|
44
|
-
* />
|
|
45
|
-
* ))}
|
|
46
|
-
* </div>
|
|
47
|
-
* );
|
|
48
|
-
* ```
|
|
49
|
-
*/
|
|
50
6
|
export function useSubscribable(offerId, options) {
|
|
51
7
|
const { offer, isLoading: loadingOffer, error: offerError, } = useSubscriptions(offerId, options);
|
|
52
8
|
const { subscription, isLoading: loadingSub, error: subError, } = useUserSubscription(options);
|
|
@@ -65,40 +21,34 @@ export function useSubscribable(offerId, options) {
|
|
|
65
21
|
console.log('[useSubscribable] No offer, returning empty array');
|
|
66
22
|
return [];
|
|
67
23
|
}
|
|
68
|
-
// Get all packages including free tier
|
|
69
24
|
let allPackages = [...offer.packages];
|
|
70
|
-
// Add free tier if initialized
|
|
71
25
|
if (isSubscriptionInitialized()) {
|
|
72
26
|
const service = getSubscriptionInstance();
|
|
73
27
|
const freeTier = service.getFreeTierPackage();
|
|
74
28
|
console.log('[useSubscribable] Free tier:', freeTier);
|
|
75
|
-
// Only add if not already in the list
|
|
76
29
|
if (!allPackages.some(p => p.packageId === freeTier.packageId)) {
|
|
77
30
|
allPackages = [freeTier, ...allPackages];
|
|
78
31
|
}
|
|
79
32
|
}
|
|
80
33
|
console.log('[useSubscribable] All packages:', allPackages.map(p => p.packageId));
|
|
81
|
-
// No subscription or inactive = all packages subscribable
|
|
82
34
|
if (!subscription || !subscription.isActive) {
|
|
83
35
|
const result = allPackages.map(p => p.packageId);
|
|
84
36
|
console.log('[useSubscribable] No active subscription, returning all:', result);
|
|
85
37
|
return result;
|
|
86
38
|
}
|
|
87
|
-
// Pass both packageId and productId for matching
|
|
88
39
|
const current = {
|
|
89
40
|
packageId: subscription.packageId,
|
|
90
41
|
productId: subscription.productId,
|
|
91
42
|
};
|
|
92
|
-
// If we don't know the current package or product, return all (shouldn't happen)
|
|
93
43
|
if (!current.packageId && !current.productId) {
|
|
94
44
|
const result = allPackages.map(p => p.packageId);
|
|
95
45
|
console.log('[useSubscribable] No current package/product, returning all:', result);
|
|
96
46
|
return result;
|
|
97
47
|
}
|
|
98
|
-
// Calculate upgradeable packages
|
|
99
48
|
const result = findUpgradeablePackages(current, allPackages);
|
|
100
49
|
console.log('[useSubscribable] Upgradeable packages:', result);
|
|
101
50
|
return result;
|
|
102
51
|
}, [offer, subscription]);
|
|
103
52
|
return { subscribablePackageIds, isLoading, error };
|
|
104
53
|
}
|
|
54
|
+
//# sourceMappingURL=useSubscribable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSubscribable.js","sourceRoot":"","sources":["../../src/hooks/useSubscribable.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAChC,OAAO,EACL,gBAAgB,GAEjB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AACpE,OAAO,EACL,uBAAuB,EACvB,yBAAyB,GAC1B,MAAM,mBAAmB,CAAC;AA0D3B,MAAM,UAAU,eAAe,CAC7B,OAAe,EACf,OAAgC;IAEhC,MAAM,EACJ,KAAK,EACL,SAAS,EAAE,YAAY,EACvB,KAAK,EAAE,UAAU,GAClB,GAAG,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IACvC,MAAM,EACJ,YAAY,EACZ,SAAS,EAAE,UAAU,EACrB,KAAK,EAAE,QAAQ,GAChB,GAAG,mBAAmB,CAAC,OAAO,CAAC,CAAC;IAEjC,MAAM,SAAS,GAAG,YAAY,IAAI,UAAU,CAAC;IAC7C,MAAM,KAAK,GAAG,UAAU,IAAI,QAAQ,CAAC;IAErC,MAAM,sBAAsB,GAAG,OAAO,CAAC,GAAG,EAAE;QAC1C,OAAO,CAAC,GAAG,CAAC,qDAAqD,EAAE;YACjE,OAAO;YACP,QAAQ,EAAE,CAAC,CAAC,KAAK;YACjB,iBAAiB,EAAE,KAAK,EAAE,QAAQ,CAAC,MAAM,IAAI,CAAC;YAC9C,eAAe,EAAE,KAAK,EAAE,QAAQ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S;SAClC,CAAC;QAGF,IAAI,CAAC,OAAO,CAAC,SAAS,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC;YAC7C,MAAM,MAAM,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;YACjD,OAAO,CAAC,GAAG,CACT,8DAA8D,EAC9D,MAAM,CACP,CAAC;YACF,OAAO,MAAM,CAAC;QAChB,CAAC;QAGD,MAAM,MAAM,GAAG,uBAAuB,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;QAC7D,OAAO,CAAC,GAAG,CAAC,yCAAyC,EAAE,MAAM,CAAC,CAAC;QAC/D,OAAO,MAAM,CAAC;IAChB,CAAC,EAAE,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC,CAAC;IAE1B,OAAO,EAAE,sBAAsB,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;AACtD,CAAC"}
|
|
@@ -1,55 +1,10 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* useSubscriptionForPeriod Hook
|
|
3
|
-
*
|
|
4
|
-
* Get packages filtered by billing period, sorted by price.
|
|
5
|
-
*/
|
|
6
1
|
import type { SubscriptionPackage } from '../types/subscription';
|
|
7
2
|
import type { SubscriptionPeriod } from '../types/period';
|
|
8
3
|
import { type UseSubscriptionsOptions } from './useSubscriptions';
|
|
9
|
-
/**
|
|
10
|
-
* Result of useSubscriptionForPeriod hook
|
|
11
|
-
*/
|
|
12
4
|
export interface UseSubscriptionForPeriodResult {
|
|
13
|
-
/** Packages for the period, sorted by price (ascending), includes free tier first */
|
|
14
5
|
packages: SubscriptionPackage[];
|
|
15
|
-
/** Whether data is being loaded */
|
|
16
6
|
isLoading: boolean;
|
|
17
|
-
/** Error if loading failed */
|
|
18
7
|
error: Error | null;
|
|
19
8
|
}
|
|
20
|
-
/**
|
|
21
|
-
* Hook to get packages filtered by billing period
|
|
22
|
-
*
|
|
23
|
-
* Returns packages sorted by price (ascending):
|
|
24
|
-
* - Free tier first (if configured)
|
|
25
|
-
* - Then paid packages from lowest to highest price
|
|
26
|
-
*
|
|
27
|
-
* @param offerId Offer identifier
|
|
28
|
-
* @param period Billing period to filter by
|
|
29
|
-
* @param options Optional configuration including userId
|
|
30
|
-
* @returns Filtered and sorted packages
|
|
31
|
-
*
|
|
32
|
-
* @example
|
|
33
|
-
* ```typescript
|
|
34
|
-
* const { packages, isLoading } = useSubscriptionForPeriod('default', 'monthly');
|
|
35
|
-
*
|
|
36
|
-
* // With user ID
|
|
37
|
-
* const { packages, isLoading } = useSubscriptionForPeriod('default', 'monthly', {
|
|
38
|
-
* userId: user?.uid,
|
|
39
|
-
* });
|
|
40
|
-
*
|
|
41
|
-
* return (
|
|
42
|
-
* <div className="grid">
|
|
43
|
-
* {packages.map(pkg => (
|
|
44
|
-
* <SubscriptionTile
|
|
45
|
-
* key={pkg.packageId}
|
|
46
|
-
* title={pkg.name}
|
|
47
|
-
* price={pkg.product?.priceString ?? 'Free'}
|
|
48
|
-
* />
|
|
49
|
-
* ))}
|
|
50
|
-
* </div>
|
|
51
|
-
* );
|
|
52
|
-
* ```
|
|
53
|
-
*/
|
|
54
9
|
export declare function useSubscriptionForPeriod(offerId: string, period: SubscriptionPeriod, options?: UseSubscriptionsOptions): UseSubscriptionForPeriodResult;
|
|
55
10
|
//# sourceMappingURL=useSubscriptionForPeriod.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSubscriptionForPeriod.d.ts","sourceRoot":"","sources":["../../src/hooks/useSubscriptionForPeriod.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useSubscriptionForPeriod.d.ts","sourceRoot":"","sources":["../../src/hooks/useSubscriptionForPeriod.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AACjE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAC1D,OAAO,EAEL,KAAK,uBAAuB,EAC7B,MAAM,oBAAoB,CAAC;AAS5B,MAAM,WAAW,8BAA8B;IAE7C,QAAQ,EAAE,mBAAmB,EAAE,CAAC;IAEhC,SAAS,EAAE,OAAO,CAAC;IAEnB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;CACrB;AAoCD,wBAAgB,wBAAwB,CACtC,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,kBAAkB,EAC1B,OAAO,CAAC,EAAE,uBAAuB,GAChC,8BAA8B,CA6BhC"}
|
|
@@ -1,59 +1,17 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* useSubscriptionForPeriod Hook
|
|
3
|
-
*
|
|
4
|
-
* Get packages filtered by billing period, sorted by price.
|
|
5
|
-
*/
|
|
6
1
|
import { useMemo } from 'react';
|
|
7
2
|
import { useSubscriptions, } from './useSubscriptions';
|
|
8
3
|
import { getSubscriptionInstance, isSubscriptionInitialized, } from '../core/singleton';
|
|
9
|
-
/**
|
|
10
|
-
* Hook to get packages filtered by billing period
|
|
11
|
-
*
|
|
12
|
-
* Returns packages sorted by price (ascending):
|
|
13
|
-
* - Free tier first (if configured)
|
|
14
|
-
* - Then paid packages from lowest to highest price
|
|
15
|
-
*
|
|
16
|
-
* @param offerId Offer identifier
|
|
17
|
-
* @param period Billing period to filter by
|
|
18
|
-
* @param options Optional configuration including userId
|
|
19
|
-
* @returns Filtered and sorted packages
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* ```typescript
|
|
23
|
-
* const { packages, isLoading } = useSubscriptionForPeriod('default', 'monthly');
|
|
24
|
-
*
|
|
25
|
-
* // With user ID
|
|
26
|
-
* const { packages, isLoading } = useSubscriptionForPeriod('default', 'monthly', {
|
|
27
|
-
* userId: user?.uid,
|
|
28
|
-
* });
|
|
29
|
-
*
|
|
30
|
-
* return (
|
|
31
|
-
* <div className="grid">
|
|
32
|
-
* {packages.map(pkg => (
|
|
33
|
-
* <SubscriptionTile
|
|
34
|
-
* key={pkg.packageId}
|
|
35
|
-
* title={pkg.name}
|
|
36
|
-
* price={pkg.product?.priceString ?? 'Free'}
|
|
37
|
-
* />
|
|
38
|
-
* ))}
|
|
39
|
-
* </div>
|
|
40
|
-
* );
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
4
|
export function useSubscriptionForPeriod(offerId, period, options) {
|
|
44
5
|
const { offer, isLoading, error } = useSubscriptions(offerId, options);
|
|
45
6
|
const packages = useMemo(() => {
|
|
46
7
|
if (!offer)
|
|
47
8
|
return [];
|
|
48
|
-
// Filter packages by period
|
|
49
9
|
const periodPackages = offer.packages.filter(pkg => pkg.product && pkg.product.period === period);
|
|
50
|
-
// Sort by price ascending
|
|
51
10
|
periodPackages.sort((a, b) => {
|
|
52
11
|
const priceA = a.product?.price ?? 0;
|
|
53
12
|
const priceB = b.product?.price ?? 0;
|
|
54
13
|
return priceA - priceB;
|
|
55
14
|
});
|
|
56
|
-
// Add free tier at the beginning if subscription is initialized
|
|
57
15
|
if (isSubscriptionInitialized()) {
|
|
58
16
|
const service = getSubscriptionInstance();
|
|
59
17
|
const freeTier = service.getFreeTierPackage();
|
|
@@ -63,3 +21,4 @@ export function useSubscriptionForPeriod(offerId, period, options) {
|
|
|
63
21
|
}, [offer, period]);
|
|
64
22
|
return { packages, isLoading, error };
|
|
65
23
|
}
|
|
24
|
+
//# sourceMappingURL=useSubscriptionForPeriod.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSubscriptionForPeriod.js","sourceRoot":"","sources":["../../src/hooks/useSubscriptionForPeriod.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAGhC,OAAO,EACL,gBAAgB,GAEjB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,uBAAuB,EACvB,yBAAyB,GAC1B,MAAM,mBAAmB,CAAC;AAgD3B,MAAM,UAAU,wBAAwB,CACtC,OAAe,EACf,MAA0B,EAC1B,OAAiC;IAEjC,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAEvE,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,EAAE;QAC5B,IAAI,CAAC,KAAK;YAAE,OAAO,EAAE,CAAC;QAGtB,MAAM,cAAc,GAAG,KAAK,CAAC,QAAQ,CAAC,MAAM,CAC1C,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,OAAO,IAAI,GAAG,CAAC,OAAO,CAAC,MAAM,KAAK,MAAM,CACpD,CAAC;QAGF,cAAc,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;YAC3B,MAAM,MAAM,GAAG,CAAC,CAAC,OAAO,EAAE,KAAK,IAAI,CAAC,CAAC;YACrC,MAAM,MAAM,GAAG,CAAC,CAAC,OAAO,EAAE,KAAK,IAAI,CAAC,CAAC;YACrC,OAAO,MAAM,GAAG,MAAM,CAAC;QACzB,CAAC,CAAC,CAAC;QAGH,IAAI,yBAAyB,EAAE,EAAE,CAAC;YAChC,MAAM,OAAO,GAAG,uBAAuB,EAAE,CAAC;YAC1C,MAAM,QAAQ,GAAG,OAAO,CAAC,kBAAkB,EAAE,CAAC;YAC9C,OAAO,CAAC,QAAQ,EAAE,GAAG,cAAc,CAAC,CAAC;QACvC,CAAC;QAED,OAAO,cAAc,CAAC;IACxB,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC;IAEpB,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;AACxC,CAAC"}
|
|
@@ -1,45 +1,9 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* useSubscriptionPeriods Hook
|
|
3
|
-
*
|
|
4
|
-
* Extract unique billing periods from an offer's packages.
|
|
5
|
-
*/
|
|
6
1
|
import type { SubscriptionPeriod } from '../types/period';
|
|
7
2
|
import { type UseSubscriptionsOptions } from './useSubscriptions';
|
|
8
|
-
/**
|
|
9
|
-
* Result of useSubscriptionPeriods hook
|
|
10
|
-
*/
|
|
11
3
|
export interface UseSubscriptionPeriodsResult {
|
|
12
|
-
/** Available periods sorted (weekly → monthly → quarterly → yearly → lifetime) */
|
|
13
4
|
periods: SubscriptionPeriod[];
|
|
14
|
-
/** Whether data is being loaded */
|
|
15
5
|
isLoading: boolean;
|
|
16
|
-
/** Error if loading failed */
|
|
17
6
|
error: Error | null;
|
|
18
7
|
}
|
|
19
|
-
/**
|
|
20
|
-
* Hook to get available billing periods from an offer
|
|
21
|
-
*
|
|
22
|
-
* @param offerId Offer identifier
|
|
23
|
-
* @param options Optional configuration including userId
|
|
24
|
-
* @returns Available periods, sorted from shortest to longest
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* ```typescript
|
|
28
|
-
* const { periods, isLoading } = useSubscriptionPeriods('default');
|
|
29
|
-
*
|
|
30
|
-
* // With user ID
|
|
31
|
-
* const { periods, isLoading } = useSubscriptionPeriods('default', {
|
|
32
|
-
* userId: user?.uid,
|
|
33
|
-
* });
|
|
34
|
-
*
|
|
35
|
-
* return (
|
|
36
|
-
* <SegmentedControl
|
|
37
|
-
* options={periods.map(p => ({ value: p, label: capitalize(p) }))}
|
|
38
|
-
* value={selectedPeriod}
|
|
39
|
-
* onChange={setSelectedPeriod}
|
|
40
|
-
* />
|
|
41
|
-
* );
|
|
42
|
-
* ```
|
|
43
|
-
*/
|
|
44
8
|
export declare function useSubscriptionPeriods(offerId: string, options?: UseSubscriptionsOptions): UseSubscriptionPeriodsResult;
|
|
45
9
|
//# sourceMappingURL=useSubscriptionPeriods.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSubscriptionPeriods.d.ts","sourceRoot":"","sources":["../../src/hooks/useSubscriptionPeriods.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useSubscriptionPeriods.d.ts","sourceRoot":"","sources":["../../src/hooks/useSubscriptionPeriods.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAE1D,OAAO,EAEL,KAAK,uBAAuB,EAC7B,MAAM,oBAAoB,CAAC;AAK5B,MAAM,WAAW,4BAA4B;IAE3C,OAAO,EAAE,kBAAkB,EAAE,CAAC;IAE9B,SAAS,EAAE,OAAO,CAAC;IAEnB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;CACrB;AA2BD,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,uBAAuB,GAChC,4BAA4B,CAoB9B"}
|
|
@@ -1,50 +1,19 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* useSubscriptionPeriods Hook
|
|
3
|
-
*
|
|
4
|
-
* Extract unique billing periods from an offer's packages.
|
|
5
|
-
*/
|
|
6
1
|
import { useMemo } from 'react';
|
|
7
2
|
import { ALL_PERIODS } from '../types/period';
|
|
8
3
|
import { useSubscriptions, } from './useSubscriptions';
|
|
9
|
-
/**
|
|
10
|
-
* Hook to get available billing periods from an offer
|
|
11
|
-
*
|
|
12
|
-
* @param offerId Offer identifier
|
|
13
|
-
* @param options Optional configuration including userId
|
|
14
|
-
* @returns Available periods, sorted from shortest to longest
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* const { periods, isLoading } = useSubscriptionPeriods('default');
|
|
19
|
-
*
|
|
20
|
-
* // With user ID
|
|
21
|
-
* const { periods, isLoading } = useSubscriptionPeriods('default', {
|
|
22
|
-
* userId: user?.uid,
|
|
23
|
-
* });
|
|
24
|
-
*
|
|
25
|
-
* return (
|
|
26
|
-
* <SegmentedControl
|
|
27
|
-
* options={periods.map(p => ({ value: p, label: capitalize(p) }))}
|
|
28
|
-
* value={selectedPeriod}
|
|
29
|
-
* onChange={setSelectedPeriod}
|
|
30
|
-
* />
|
|
31
|
-
* );
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
4
|
export function useSubscriptionPeriods(offerId, options) {
|
|
35
5
|
const { offer, isLoading, error } = useSubscriptions(offerId, options);
|
|
36
6
|
const periods = useMemo(() => {
|
|
37
7
|
if (!offer)
|
|
38
8
|
return [];
|
|
39
|
-
// Collect unique periods from packages that have products
|
|
40
9
|
const periodSet = new Set();
|
|
41
10
|
for (const pkg of offer.packages) {
|
|
42
11
|
if (pkg.product) {
|
|
43
12
|
periodSet.add(pkg.product.period);
|
|
44
13
|
}
|
|
45
14
|
}
|
|
46
|
-
// Sort by the standard order
|
|
47
15
|
return ALL_PERIODS.filter(p => periodSet.has(p));
|
|
48
16
|
}, [offer]);
|
|
49
17
|
return { periods, isLoading, error };
|
|
50
18
|
}
|
|
19
|
+
//# sourceMappingURL=useSubscriptionPeriods.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSubscriptionPeriods.js","sourceRoot":"","sources":["../../src/hooks/useSubscriptionPeriods.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAEhC,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EACL,gBAAgB,GAEjB,MAAM,oBAAoB,CAAC;AAuC5B,MAAM,UAAU,sBAAsB,CACpC,OAAe,EACf,OAAiC;IAEjC,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAEvE,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,EAAE;QAC3B,IAAI,CAAC,KAAK;YAAE,OAAO,EAAE,CAAC;QAGtB,MAAM,SAAS,GAAG,IAAI,GAAG,EAAsB,CAAC;QAEhD,KAAK,MAAM,GAAG,IAAI,KAAK,CAAC,QAAQ,EAAE,CAAC;YACjC,IAAI,GAAG,CAAC,OAAO,EAAE,CAAC;gBAChB,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;YACpC,CAAC;QACH,CAAC;QAGD,OAAO,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IACnD,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAEZ,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;AACvC,CAAC"}
|
|
@@ -1,57 +1,13 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* useSubscriptions Hook
|
|
3
|
-
*
|
|
4
|
-
* Fetch and manage subscription offer data.
|
|
5
|
-
* Note: Offerings (product catalog) don't change with user,
|
|
6
|
-
* so this hook doesn't reload when user changes.
|
|
7
|
-
*/
|
|
8
1
|
import type { SubscriptionOffer } from '../types/subscription';
|
|
9
|
-
/**
|
|
10
|
-
* Result of useSubscriptions hook
|
|
11
|
-
*/
|
|
12
2
|
export interface UseSubscriptionsResult {
|
|
13
|
-
/** The requested offer, null if not found or loading */
|
|
14
3
|
offer: SubscriptionOffer | null;
|
|
15
|
-
/** Whether data is being loaded */
|
|
16
4
|
isLoading: boolean;
|
|
17
|
-
/** Error if loading failed */
|
|
18
5
|
error: Error | null;
|
|
19
|
-
/** Manually refetch the data */
|
|
20
6
|
refetch: () => Promise<void>;
|
|
21
7
|
}
|
|
22
|
-
/**
|
|
23
|
-
* Options for useSubscriptions hook
|
|
24
|
-
* Note: userId is accepted for API consistency but offerings don't change with user.
|
|
25
|
-
*/
|
|
26
8
|
export interface UseSubscriptionsOptions {
|
|
27
|
-
/** Optional user ID (accepted for API consistency, but offerings don't change with user) */
|
|
28
9
|
userId?: string;
|
|
29
|
-
/** Optional email for the user */
|
|
30
10
|
userEmail?: string;
|
|
31
11
|
}
|
|
32
|
-
/**
|
|
33
|
-
* Hook to get subscription offer data (product catalog).
|
|
34
|
-
* Note: Offerings don't change with user, so this hook doesn't need userId.
|
|
35
|
-
*
|
|
36
|
-
* @param offerId Offer identifier to fetch
|
|
37
|
-
* @param _options Optional configuration (userId accepted but not used - offerings are user-independent)
|
|
38
|
-
* @returns Offer data, loading state, and error
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* ```typescript
|
|
42
|
-
* const { offer, isLoading, error } = useSubscriptions('default');
|
|
43
|
-
*
|
|
44
|
-
* if (isLoading) return <Spinner />;
|
|
45
|
-
* if (error) return <Error message={error.message} />;
|
|
46
|
-
*
|
|
47
|
-
* return (
|
|
48
|
-
* <div>
|
|
49
|
-
* {offer?.packages.map(pkg => (
|
|
50
|
-
* <SubscriptionTile key={pkg.packageId} package={pkg} />
|
|
51
|
-
* ))}
|
|
52
|
-
* </div>
|
|
53
|
-
* );
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
12
|
export declare function useSubscriptions(offerId: string, _options?: UseSubscriptionsOptions): UseSubscriptionsResult;
|
|
57
13
|
//# sourceMappingURL=useSubscriptions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSubscriptions.d.ts","sourceRoot":"","sources":["../../src/hooks/useSubscriptions.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useSubscriptions.d.ts","sourceRoot":"","sources":["../../src/hooks/useSubscriptions.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAS/D,MAAM,WAAW,sBAAsB;IAErC,KAAK,EAAE,iBAAiB,GAAG,IAAI,CAAC;IAEhC,SAAS,EAAE,OAAO,CAAC;IAEnB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IAEpB,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9B;AAMD,MAAM,WAAW,uBAAuB;IAEtC,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AA0BD,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,MAAM,EACf,QAAQ,CAAC,EAAE,uBAAuB,GACjC,sBAAsB,CA4ExB"}
|
|
@@ -1,36 +1,5 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* useSubscriptions Hook
|
|
3
|
-
*
|
|
4
|
-
* Fetch and manage subscription offer data.
|
|
5
|
-
* Note: Offerings (product catalog) don't change with user,
|
|
6
|
-
* so this hook doesn't reload when user changes.
|
|
7
|
-
*/
|
|
8
1
|
import { useCallback, useEffect, useState } from 'react';
|
|
9
2
|
import { getSubscriptionInstance, isSubscriptionInitialized, } from '../core/singleton';
|
|
10
|
-
/**
|
|
11
|
-
* Hook to get subscription offer data (product catalog).
|
|
12
|
-
* Note: Offerings don't change with user, so this hook doesn't need userId.
|
|
13
|
-
*
|
|
14
|
-
* @param offerId Offer identifier to fetch
|
|
15
|
-
* @param _options Optional configuration (userId accepted but not used - offerings are user-independent)
|
|
16
|
-
* @returns Offer data, loading state, and error
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```typescript
|
|
20
|
-
* const { offer, isLoading, error } = useSubscriptions('default');
|
|
21
|
-
*
|
|
22
|
-
* if (isLoading) return <Spinner />;
|
|
23
|
-
* if (error) return <Error message={error.message} />;
|
|
24
|
-
*
|
|
25
|
-
* return (
|
|
26
|
-
* <div>
|
|
27
|
-
* {offer?.packages.map(pkg => (
|
|
28
|
-
* <SubscriptionTile key={pkg.packageId} package={pkg} />
|
|
29
|
-
* ))}
|
|
30
|
-
* </div>
|
|
31
|
-
* );
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
3
|
export function useSubscriptions(offerId, _options) {
|
|
35
4
|
const [offer, setOffer] = useState(null);
|
|
36
5
|
const [isLoading, setIsLoading] = useState(true);
|
|
@@ -43,7 +12,6 @@ export function useSubscriptions(offerId, _options) {
|
|
|
43
12
|
return;
|
|
44
13
|
}
|
|
45
14
|
const service = getSubscriptionInstance();
|
|
46
|
-
// If we already have this offer cached, use it without refetching
|
|
47
15
|
const cachedOffer = service.getOffer(offerId);
|
|
48
16
|
if (cachedOffer) {
|
|
49
17
|
console.log('[useSubscriptions] Using cached offer:', {
|
|
@@ -57,7 +25,6 @@ export function useSubscriptions(offerId, _options) {
|
|
|
57
25
|
try {
|
|
58
26
|
setIsLoading(true);
|
|
59
27
|
setError(null);
|
|
60
|
-
// Load offerings only if not already loaded
|
|
61
28
|
if (!service.hasLoadedOfferings()) {
|
|
62
29
|
console.log('[useSubscriptions] Loading offerings...');
|
|
63
30
|
await service.loadOfferings();
|
|
@@ -79,7 +46,6 @@ export function useSubscriptions(offerId, _options) {
|
|
|
79
46
|
setIsLoading(false);
|
|
80
47
|
}
|
|
81
48
|
}, [offerId]);
|
|
82
|
-
// Load data on mount only - offerings don't change with user
|
|
83
49
|
useEffect(() => {
|
|
84
50
|
loadData();
|
|
85
51
|
}, [loadData]);
|
|
@@ -103,3 +69,4 @@ export function useSubscriptions(offerId, _options) {
|
|
|
103
69
|
}, [offerId]);
|
|
104
70
|
return { offer, isLoading, error, refetch };
|
|
105
71
|
}
|
|
72
|
+
//# sourceMappingURL=useSubscriptions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSubscriptions.js","sourceRoot":"","sources":["../../src/hooks/useSubscriptions.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAEzD,OAAO,EACL,uBAAuB,EACvB,yBAAyB,GAC1B,MAAM,mBAAmB,CAAC;AAmD3B,MAAM,UAAU,gBAAgB,CAC9B,OAAe,EACf,QAAkC;IAElC,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAA2B,IAAI,CAAC,CAAC;IACnE,MAAM,CAAC,SAAS,EAAE,YAAY,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;IACjD,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAe,IAAI,CAAC,CAAC;IA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
|
2
|
-
* useUserSubscription Hook
|
|
3
|
-
*
|
|
4
|
-
* Fetch and manage current user's subscription status.
|
|
5
|
-
*/
|
|
6
1
|
import type { CurrentSubscription } from '../types/subscription';
|
|
7
|
-
/**
|
|
8
|
-
* Result of useUserSubscription hook
|
|
9
|
-
*/
|
|
10
2
|
export interface UseUserSubscriptionResult {
|
|
11
|
-
/** Current subscription info, null if loading */
|
|
12
3
|
subscription: CurrentSubscription | null;
|
|
13
|
-
/** Whether data is being loaded */
|
|
14
4
|
isLoading: boolean;
|
|
15
|
-
/** Error if loading failed */
|
|
16
5
|
error: Error | null;
|
|
17
|
-
/** Force refresh customer subscription data from the server */
|
|
18
6
|
update: () => Promise<void>;
|
|
19
|
-
/** @deprecated Use update() instead */
|
|
20
7
|
refetch: () => Promise<void>;
|
|
21
8
|
}
|
|
22
|
-
/**
|
|
23
|
-
* Options for useUserSubscription hook
|
|
24
|
-
*/
|
|
25
9
|
export interface UseUserSubscriptionOptions {
|
|
26
|
-
/** Optional user ID. When provided, sets the subscription user and reloads on change. */
|
|
27
10
|
userId?: string;
|
|
28
|
-
/** Optional email for the user */
|
|
29
11
|
userEmail?: string;
|
|
30
12
|
}
|
|
31
|
-
/**
|
|
32
|
-
* Hook to get current user's subscription status
|
|
33
|
-
*
|
|
34
|
-
* @param options Optional configuration including userId
|
|
35
|
-
* @returns Current subscription data, loading state, and error
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```typescript
|
|
39
|
-
* // Without user ID (anonymous or use existing)
|
|
40
|
-
* const { subscription, isLoading } = useUserSubscription();
|
|
41
|
-
*
|
|
42
|
-
* // With user ID (will re-initialize when user changes)
|
|
43
|
-
* const { subscription, isLoading } = useUserSubscription({
|
|
44
|
-
* userId: user?.uid,
|
|
45
|
-
* userEmail: user?.email,
|
|
46
|
-
* });
|
|
47
|
-
*
|
|
48
|
-
* if (isLoading) return <Spinner />;
|
|
49
|
-
*
|
|
50
|
-
* if (subscription?.isActive) {
|
|
51
|
-
* return <div>Your plan: {subscription.entitlements.join(', ')}</div>;
|
|
52
|
-
* } else {
|
|
53
|
-
* return <div>No active subscription</div>;
|
|
54
|
-
* }
|
|
55
|
-
* ```
|
|
56
|
-
*/
|
|
57
13
|
export declare function useUserSubscription(options?: UseUserSubscriptionOptions): UseUserSubscriptionResult;
|
|
58
14
|
//# sourceMappingURL=useUserSubscription.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useUserSubscription.d.ts","sourceRoot":"","sources":["../../src/hooks/useUserSubscription.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useUserSubscription.d.ts","sourceRoot":"","sources":["../../src/hooks/useUserSubscription.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAajE,MAAM,WAAW,yBAAyB;IAExC,YAAY,EAAE,mBAAmB,GAAG,IAAI,CAAC;IAEzC,SAAS,EAAE,OAAO,CAAC;IAEnB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IAEpB,MAAM,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAE5B,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9B;AAKD,MAAM,WAAW,0BAA0B;IAEzC,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AA4BD,wBAAgB,mBAAmB,CACjC,OAAO,CAAC,EAAE,0BAA0B,GACnC,yBAAyB,CAoG3B"}
|
|
@@ -1,45 +1,12 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* useUserSubscription Hook
|
|
3
|
-
*
|
|
4
|
-
* Fetch and manage current user's subscription status.
|
|
5
|
-
*/
|
|
6
1
|
import { useCallback, useEffect, useState } from 'react';
|
|
7
2
|
import { getSubscriptionInstance, getSubscriptionUserId, isSubscriptionInitialized, onSubscriptionRefresh, onSubscriptionUserIdChange, setSubscriptionUserId, } from '../core/singleton';
|
|
8
|
-
/**
|
|
9
|
-
* Hook to get current user's subscription status
|
|
10
|
-
*
|
|
11
|
-
* @param options Optional configuration including userId
|
|
12
|
-
* @returns Current subscription data, loading state, and error
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```typescript
|
|
16
|
-
* // Without user ID (anonymous or use existing)
|
|
17
|
-
* const { subscription, isLoading } = useUserSubscription();
|
|
18
|
-
*
|
|
19
|
-
* // With user ID (will re-initialize when user changes)
|
|
20
|
-
* const { subscription, isLoading } = useUserSubscription({
|
|
21
|
-
* userId: user?.uid,
|
|
22
|
-
* userEmail: user?.email,
|
|
23
|
-
* });
|
|
24
|
-
*
|
|
25
|
-
* if (isLoading) return <Spinner />;
|
|
26
|
-
*
|
|
27
|
-
* if (subscription?.isActive) {
|
|
28
|
-
* return <div>Your plan: {subscription.entitlements.join(', ')}</div>;
|
|
29
|
-
* } else {
|
|
30
|
-
* return <div>No active subscription</div>;
|
|
31
|
-
* }
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
3
|
export function useUserSubscription(options) {
|
|
35
4
|
const { userId, userEmail } = options ?? {};
|
|
36
5
|
const [subscription, setSubscription] = useState(null);
|
|
37
6
|
const [isLoading, setIsLoading] = useState(true);
|
|
38
7
|
const [error, setError] = useState(null);
|
|
39
|
-
// Handle user ID changes
|
|
40
8
|
useEffect(() => {
|
|
41
9
|
const currentLibUserId = getSubscriptionUserId();
|
|
42
|
-
// Only update if userId is explicitly provided and different
|
|
43
10
|
if (userId !== undefined && userId !== currentLibUserId) {
|
|
44
11
|
console.log('[useUserSubscription] User ID changed, updating subscription user:', {
|
|
45
12
|
from: currentLibUserId,
|
|
@@ -58,7 +25,6 @@ export function useUserSubscription(options) {
|
|
|
58
25
|
try {
|
|
59
26
|
setIsLoading(true);
|
|
60
27
|
setError(null);
|
|
61
|
-
// Load customer info if not already loaded
|
|
62
28
|
if (!service.hasLoadedCustomerInfo()) {
|
|
63
29
|
await service.loadCustomerInfo();
|
|
64
30
|
}
|
|
@@ -72,18 +38,14 @@ export function useUserSubscription(options) {
|
|
|
72
38
|
setIsLoading(false);
|
|
73
39
|
}
|
|
74
40
|
}, []);
|
|
75
|
-
// Load data on mount and when userId changes
|
|
76
41
|
useEffect(() => {
|
|
77
42
|
loadData();
|
|
78
|
-
// Subscribe to user ID changes to reload
|
|
79
43
|
const unsubscribeUserId = onSubscriptionUserIdChange(() => {
|
|
80
44
|
console.log('[useUserSubscription] User ID changed, reloading...');
|
|
81
45
|
loadData();
|
|
82
46
|
});
|
|
83
|
-
// Subscribe to subscription refresh events (e.g., after purchase)
|
|
84
47
|
const unsubscribeRefresh = onSubscriptionRefresh(() => {
|
|
85
48
|
console.log('[useUserSubscription] Subscription refreshed, updating...');
|
|
86
|
-
// Get the updated subscription from the service
|
|
87
49
|
if (isSubscriptionInitialized()) {
|
|
88
50
|
const service = getSubscriptionInstance();
|
|
89
51
|
const currentSub = service.getCurrentSubscription();
|
|
@@ -115,3 +77,4 @@ export function useUserSubscription(options) {
|
|
|
115
77
|
}, []);
|
|
116
78
|
return { subscription, isLoading, error, update, refetch: update };
|
|
117
79
|
}
|
|
80
|
+
//# sourceMappingURL=useUserSubscription.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useUserSubscription.js","sourceRoot":"","sources":["../../src/hooks/useUserSubscription.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAEzD,OAAO,EACL,uBAAuB,EACvB,qBAAqB,EACrB,yBAAyB,EACzB,qBAAqB,EACrB,0BAA0B,EAC1B,qBAAqB,GACtB,MAAM,mBAAmB,CAAC;AAsD3B,MAAM,UAAU,mBAAmB,CACjC,OAAoC;IAEpC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,OAAO,IAAI,EAAE,CAAC;IAC5C,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,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;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,QAAQ,CACN,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,6BAA6B,CAAC,CACtE,CAAC;QACJ,CAAC;gBAAS,CAAC;YACT,YAAY,CAAC,KAAK,CAAC,CAAC;QACtB,CAAC;IACH,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;AACrE,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @sudobility/subscription_lib
|
|
3
|
-
*
|
|
4
|
-
* Cross-platform subscription management library with RevenueCat adapter pattern.
|
|
5
|
-
* Works with both React (web) and React Native.
|
|
6
|
-
*/
|
|
7
1
|
export { initializeSubscription, getSubscriptionInstance, isSubscriptionInitialized, resetSubscription, refreshSubscription, setSubscriptionUserId, getSubscriptionUserId, onSubscriptionUserIdChange, onSubscriptionRefresh, SubscriptionService, type SubscriptionConfig, type SubscriptionServiceConfig, } from './core';
|
|
8
2
|
export { useSubscriptions, useUserSubscription, useSubscriptionPeriods, useSubscriptionForPeriod, useSubscribable, type UseSubscriptionsResult, type UseSubscriptionsOptions, type UseUserSubscriptionResult, type UseUserSubscriptionOptions, type UseSubscriptionPeriodsResult, type UseSubscriptionForPeriodResult, type UseSubscribableResult, type UseSubscribableOptions, } from './hooks';
|
|
9
3
|
export type { SubscriptionAdapter, AdapterOfferings, AdapterOffering, AdapterPackage, AdapterProduct, AdapterSubscriptionOption, AdapterPricingPhase, AdapterCustomerInfo, AdapterEntitlementInfo, AdapterPurchaseParams, AdapterPurchaseResult, SubscriptionProduct, SubscriptionPackage, SubscriptionOffer, CurrentSubscription, FreeTierConfig, PackageWithLevel, SubscriptionPeriod, } from './types';
|
|
10
4
|
export { PERIOD_RANKS, ALL_PERIODS } from './types';
|
|
11
5
|
export { parseISO8601Period, getPeriodRank, comparePeriods, isPeriodGreaterOrEqual, calculatePackageLevels, addLevelsToPackages, getPackageLevel, findUpgradeablePackages, } from './utils';
|
|
12
6
|
export { configureRevenueCatAdapter, createRevenueCatAdapter, setRevenueCatUser, clearRevenueCatUser, hasRevenueCatUser, } from './adapters';
|
|
7
|
+
export { configureRevenueCatRNAdapter, createRevenueCatRNAdapter, setRevenueCatRNUser, clearRevenueCatRNUser, hasRevenueCatRNUser, } from './adapters';
|
|
13
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EACL,sBAAsB,EACtB,uBAAuB,EACvB,yBAAyB,EACzB,iBAAiB,EACjB,mBAAmB,EACnB,qBAAqB,EACrB,qBAAqB,EACrB,0BAA0B,EAC1B,qBAAqB,EACrB,mBAAmB,EACnB,KAAK,kBAAkB,EACvB,KAAK,yBAAyB,GAC/B,MAAM,QAAQ,CAAC;AAGhB,OAAO,EACL,gBAAgB,EAChB,mBAAmB,EACnB,sBAAsB,EACtB,wBAAwB,EACxB,eAAe,EACf,KAAK,sBAAsB,EAC3B,KAAK,uBAAuB,EAC5B,KAAK,yBAAyB,EAC9B,KAAK,0BAA0B,EAC/B,KAAK,4BAA4B,EACjC,KAAK,8BAA8B,EACnC,KAAK,qBAAqB,EAC1B,KAAK,sBAAsB,GAC5B,MAAM,SAAS,CAAC;AAGjB,YAAY,EAEV,mBAAmB,EACnB,gBAAgB,EAChB,eAAe,EACf,cAAc,EACd,cAAc,EACd,yBAAyB,EACzB,mBAAmB,EACnB,mBAAmB,EACnB,sBAAsB,EACtB,qBAAqB,EACrB,qBAAqB,EAErB,mBAA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