@umituz/react-native-subscription 2.9.6 → 2.9.7
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 +1 -1
- package/src/index.ts +6 -0
- package/src/utils/aiCreditHelpers.ts +114 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/react-native-subscription",
|
|
3
|
-
"version": "2.9.
|
|
3
|
+
"version": "2.9.7",
|
|
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",
|
package/src/index.ts
CHANGED
|
@@ -250,6 +250,12 @@ export {
|
|
|
250
250
|
type CreditChecker,
|
|
251
251
|
} from "./utils/creditChecker";
|
|
252
252
|
|
|
253
|
+
export {
|
|
254
|
+
createAICreditHelpers,
|
|
255
|
+
type AICreditHelpersConfig,
|
|
256
|
+
type AICreditHelpers,
|
|
257
|
+
} from "./utils/aiCreditHelpers";
|
|
258
|
+
|
|
253
259
|
// =============================================================================
|
|
254
260
|
// REVENUECAT - Errors
|
|
255
261
|
// =============================================================================
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AI Credit Helpers
|
|
3
|
+
*
|
|
4
|
+
* Common patterns for AI generation apps to handle credits.
|
|
5
|
+
* Provides ready-to-use functions for credit checking and deduction.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* import { createAICreditHelpers } from '@umituz/react-native-subscription';
|
|
9
|
+
*
|
|
10
|
+
* const helpers = createAICreditHelpers({
|
|
11
|
+
* repository,
|
|
12
|
+
* imageGenerationTypes: ['future_image', 'santa_transform'],
|
|
13
|
+
* onCreditDeducted: (userId) => invalidateCache(userId)
|
|
14
|
+
* });
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type { CreditType } from "../domain/entities/Credits";
|
|
18
|
+
import type { CreditsRepository } from "../infrastructure/repositories/CreditsRepository";
|
|
19
|
+
import { createCreditChecker } from "./creditChecker";
|
|
20
|
+
|
|
21
|
+
export interface AICreditHelpersConfig {
|
|
22
|
+
/**
|
|
23
|
+
* Credits repository instance
|
|
24
|
+
*/
|
|
25
|
+
repository: CreditsRepository;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* List of operation types that should use "image" credits.
|
|
29
|
+
* All other types will use "text" credits.
|
|
30
|
+
* @example ['future_image', 'santa_transform', 'photo_generation']
|
|
31
|
+
*/
|
|
32
|
+
imageGenerationTypes: string[];
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Optional callback called after successful credit deduction.
|
|
36
|
+
* Use this to invalidate TanStack Query cache or trigger UI updates.
|
|
37
|
+
*/
|
|
38
|
+
onCreditDeducted?: (userId: string, creditType: CreditType) => void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface AICreditHelpers {
|
|
42
|
+
/**
|
|
43
|
+
* Check if user has credits for a specific generation type
|
|
44
|
+
* @param userId - User ID
|
|
45
|
+
* @param generationType - Type of generation (e.g., 'future_image', 'text_summary')
|
|
46
|
+
* @returns boolean indicating if credits are available
|
|
47
|
+
*/
|
|
48
|
+
checkCreditsForGeneration: (
|
|
49
|
+
userId: string | undefined,
|
|
50
|
+
generationType: string
|
|
51
|
+
) => Promise<boolean>;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Deduct credits after successful generation
|
|
55
|
+
* @param userId - User ID
|
|
56
|
+
* @param generationType - Type of generation that was performed
|
|
57
|
+
*/
|
|
58
|
+
deductCreditsForGeneration: (
|
|
59
|
+
userId: string | undefined,
|
|
60
|
+
generationType: string
|
|
61
|
+
) => Promise<void>;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Get credit type for a generation type (useful for UI display)
|
|
65
|
+
* @param generationType - Type of generation
|
|
66
|
+
* @returns "image" or "text"
|
|
67
|
+
*/
|
|
68
|
+
getCreditType: (generationType: string) => CreditType;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Creates AI-specific credit helper functions
|
|
73
|
+
*/
|
|
74
|
+
export function createAICreditHelpers(
|
|
75
|
+
config: AICreditHelpersConfig
|
|
76
|
+
): AICreditHelpers {
|
|
77
|
+
const { repository, imageGenerationTypes, onCreditDeducted } = config;
|
|
78
|
+
|
|
79
|
+
// Map generation type to credit type
|
|
80
|
+
const getCreditType = (generationType: string): CreditType => {
|
|
81
|
+
return imageGenerationTypes.includes(generationType) ? "image" : "text";
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// Create credit checker with the mapping
|
|
85
|
+
const checker = createCreditChecker({
|
|
86
|
+
repository,
|
|
87
|
+
getCreditType,
|
|
88
|
+
onCreditDeducted,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Check if credits are available for generation
|
|
92
|
+
const checkCreditsForGeneration = async (
|
|
93
|
+
userId: string | undefined,
|
|
94
|
+
generationType: string
|
|
95
|
+
): Promise<boolean> => {
|
|
96
|
+
const result = await checker.checkCreditsAvailable(userId, generationType);
|
|
97
|
+
return result.success;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// Deduct credits after successful generation
|
|
101
|
+
const deductCreditsForGeneration = async (
|
|
102
|
+
userId: string | undefined,
|
|
103
|
+
generationType: string
|
|
104
|
+
): Promise<void> => {
|
|
105
|
+
const creditType = getCreditType(generationType);
|
|
106
|
+
await checker.deductCreditsAfterSuccess(userId, creditType);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
checkCreditsForGeneration,
|
|
111
|
+
deductCreditsForGeneration,
|
|
112
|
+
getCreditType,
|
|
113
|
+
};
|
|
114
|
+
}
|