@nehorai/credits 1.3.0 → 1.5.0

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.
Files changed (43) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +153 -153
  3. package/dist/adapters/index.cjs +13 -3
  4. package/dist/adapters/index.cjs.map +1 -1
  5. package/dist/adapters/index.d.cts +2 -2
  6. package/dist/adapters/index.d.ts +2 -2
  7. package/dist/adapters/index.js +13 -3
  8. package/dist/adapters/index.js.map +1 -1
  9. package/dist/auth/index.cjs.map +1 -1
  10. package/dist/auth/index.js.map +1 -1
  11. package/dist/core/index.cjs +13 -3
  12. package/dist/core/index.cjs.map +1 -1
  13. package/dist/core/index.d.cts +3 -3
  14. package/dist/core/index.d.ts +3 -3
  15. package/dist/core/index.js +13 -3
  16. package/dist/core/index.js.map +1 -1
  17. package/dist/index.cjs +88 -19
  18. package/dist/index.cjs.map +1 -1
  19. package/dist/index.d.cts +74 -15
  20. package/dist/index.d.ts +74 -15
  21. package/dist/index.js +79 -19
  22. package/dist/index.js.map +1 -1
  23. package/dist/repository/index.cjs +50 -11
  24. package/dist/repository/index.cjs.map +1 -1
  25. package/dist/repository/index.d.cts +3 -3
  26. package/dist/repository/index.d.ts +3 -3
  27. package/dist/repository/index.js +50 -11
  28. package/dist/repository/index.js.map +1 -1
  29. package/dist/sdk/index.cjs.map +1 -1
  30. package/dist/sdk/index.d.cts +2 -2
  31. package/dist/sdk/index.d.ts +2 -2
  32. package/dist/sdk/index.js.map +1 -1
  33. package/dist/service/index.cjs +60 -12
  34. package/dist/service/index.cjs.map +1 -1
  35. package/dist/service/index.d.cts +18 -3
  36. package/dist/service/index.d.ts +18 -3
  37. package/dist/service/index.js +60 -12
  38. package/dist/service/index.js.map +1 -1
  39. package/dist/{types-BBxIWHK2.d.cts → types-DgQtamEe.d.cts} +16 -3
  40. package/dist/{types-BBxIWHK2.d.ts → types-DgQtamEe.d.ts} +16 -3
  41. package/dist/{types-D1vdeBcA.d.cts → types-KgNdBuEF.d.cts} +1 -1
  42. package/dist/{types-aJsyv8df.d.ts → types-fW2JNmmr.d.ts} +1 -1
  43. package/package.json +1 -1
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 Nehorai Hadad
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Nehorai Hadad
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,153 +1,153 @@
1
- # @nehorai/credits
2
-
3
- Framework-agnostic credits and billing system with two-phase commit for safe credit operations.
4
-
5
- ## Features
6
-
7
- - **Two-phase commit** -- Reserve credits before executing operations, then commit or release
8
- - **Journal-based audit trail** -- Every credit change is logged with source, reference, and metadata
9
- - **Subscription tier management** -- Free, basic, premium, and unlimited tiers with monthly resets
10
- - **Notification hooks** -- Low balance and subscription expiry notifications
11
- - **REST API SDK** -- Client and admin SDK for external integrations
12
- - **In-memory repository** -- Included for testing and development
13
- - **Type-safe** -- Full TypeScript with portable types (no framework dependencies)
14
-
15
- ## Installation
16
-
17
- ```bash
18
- pnpm add @nehorai/credits
19
- ```
20
-
21
- ## Quick Start
22
-
23
- ```typescript
24
- import {
25
- CreditsService,
26
- createInMemoryCreditRepository,
27
- } from "@nehorai/credits";
28
-
29
- const repository = createInMemoryCreditRepository();
30
- const service = new CreditsService(repository);
31
-
32
- // Initialize a user with free tier
33
- await service.initializeUserCredits("user-123");
34
-
35
- // Check if user has enough credits
36
- const check = await service.checkCredits("user-123", 10);
37
- if (!check.hasCredits) {
38
- console.log(`Need ${check.shortfall} more credits`);
39
- }
40
-
41
- // Reserve-Execute-Commit pattern
42
- const reservation = await service.reserveCredits("user-123", 10, "story_generation");
43
- try {
44
- await doExpensiveWork();
45
- await service.commitCredits("user-123", reservation.id); // Deducts credits
46
- } catch (error) {
47
- await service.releaseCredits("user-123", reservation.id); // Refunds credits
48
- throw error;
49
- }
50
- ```
51
-
52
- ## API Reference
53
-
54
- ### `CreditsService`
55
-
56
- The main service class. Accepts any `ICreditRepository` implementation.
57
-
58
- | Method | Description |
59
- |--------|-------------|
60
- | `getUserCredits(userId)` | Get user balance (auto-handles monthly reset and subscription expiry) |
61
- | `initializeUserCredits(userId)` | Create a new user with free tier |
62
- | `getOrCreateUserCredits(userId)` | Get or initialize user credits |
63
- | `checkCredits(userId, amount)` | Check if user has sufficient credits |
64
- | `reserveCredits(userId, amount, operationType)` | Reserve credits (phase 1) |
65
- | `commitCredits(userId, reservationId)` | Commit reservation (phase 2 -- success) |
66
- | `releaseCredits(userId, reservationId)` | Release reservation (phase 2 -- failure) |
67
- | `addCredits(userId, amount, description)` | Add credits (purchases, bonuses) |
68
- | `updateTier(userId, tier, expiresAt?)` | Change subscription tier |
69
- | `getUsageHistory(userId, limit?, offset?)` | Paginated usage history |
70
- | `logUsage(log)` | Log a usage event for auditing |
71
-
72
- ### `ICreditRepository`
73
-
74
- Interface for database implementations. Implement this to use any database backend.
75
-
76
- Key methods: `getUserCredits`, `initializeUserCredits`, `reserveCreditsAtomic`, `commitReservationAtomic`, `releaseReservationAtomic`, `addCreditsAtomic`, `atomicMonthlyReset`, `createJournalEntry`, `findAndExpireReservations`.
77
-
78
- ### `InMemoryCreditRepository`
79
-
80
- In-memory implementation of `ICreditRepository` for testing:
81
-
82
- ```typescript
83
- import { createInMemoryCreditRepository } from "@nehorai/credits";
84
-
85
- const repo = createInMemoryCreditRepository();
86
- ```
87
-
88
- ### Error Handling
89
-
90
- ```typescript
91
- import { CreditError, isCreditError, CreditErrorCode } from "@nehorai/credits";
92
-
93
- try {
94
- await service.reserveCredits(userId, 100, "expensive_op");
95
- } catch (error) {
96
- if (isCreditError(error) && error.code === CreditErrorCode.INSUFFICIENT_CREDITS) {
97
- // Handle insufficient credits
98
- }
99
- }
100
- ```
101
-
102
- ### SDK Clients
103
-
104
- REST API clients for external integrations:
105
-
106
- ```typescript
107
- import { CreditsClient, AdminCreditsClient } from "@nehorai/credits";
108
-
109
- // User-facing client
110
- const client = new CreditsClient({ baseUrl: "/api/v1/credits", getToken });
111
-
112
- // Admin client
113
- const admin = new AdminCreditsClient({ baseUrl: "/api/v1/admin", getToken });
114
- ```
115
-
116
- ### Core Types
117
-
118
- | Type | Description |
119
- |------|-------------|
120
- | `PortableUserCredits` | User balance with tier, monthly limits, timestamps |
121
- | `PortableReservation` | Credit reservation for two-phase commit |
122
- | `PortableTransaction` | Purchase/refund transaction record |
123
- | `PortableJournalEntry` | Audit trail entry |
124
- | `PortableUsageLog` | Operation usage log |
125
- | `CreditCheckResult` | Result of credit sufficiency check |
126
- | `SubscriptionTier` | `"free" \| "basic" \| "premium" \| "unlimited"` |
127
- | `WithCreditsOptions` | Options for the `withCredits` HOF |
128
-
129
- ### Sub-path Exports
130
-
131
- ```typescript
132
- import { ... } from "@nehorai/credits/core"; // Core types and errors
133
- import { ... } from "@nehorai/credits/repository"; // Repository types and in-memory impl
134
- import { ... } from "@nehorai/credits/auth"; // Auth provider types
135
- import { ... } from "@nehorai/credits/service"; // CreditsService
136
- import { ... } from "@nehorai/credits/adapters"; // Adapter types
137
- import { ... } from "@nehorai/credits/sdk"; // REST API clients
138
- ```
139
-
140
- ## Related Packages
141
-
142
- | Package | Description |
143
- |---------|-------------|
144
- | [`@nehorai/credits-firestore`](https://www.npmjs.com/package/@nehorai/credits-firestore) | Firestore implementation of `ICreditRepository` |
145
- | [`@nehorai/credits-nextjs`](https://www.npmjs.com/package/@nehorai/credits-nextjs) | Next.js adapter with NextAuth integration |
146
-
147
- ## Repository
148
-
149
- [https://github.com/NehoraiHadad/nehorai-plugins](https://github.com/NehoraiHadad/nehorai-plugins)
150
-
151
- ## License
152
-
153
- MIT
1
+ # @nehorai/credits
2
+
3
+ Framework-agnostic credits and billing system with two-phase commit for safe credit operations.
4
+
5
+ ## Features
6
+
7
+ - **Two-phase commit** -- Reserve credits before executing operations, then commit or release
8
+ - **Journal-based audit trail** -- Every credit change is logged with source, reference, and metadata
9
+ - **Subscription tier management** -- Free, basic, premium, and unlimited tiers with monthly resets
10
+ - **Notification hooks** -- Low balance and subscription expiry notifications
11
+ - **REST API SDK** -- Client and admin SDK for external integrations
12
+ - **In-memory repository** -- Included for testing and development
13
+ - **Type-safe** -- Full TypeScript with portable types (no framework dependencies)
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ pnpm add @nehorai/credits
19
+ ```
20
+
21
+ ## Quick Start
22
+
23
+ ```typescript
24
+ import {
25
+ CreditsService,
26
+ createInMemoryCreditRepository,
27
+ } from "@nehorai/credits";
28
+
29
+ const repository = createInMemoryCreditRepository();
30
+ const service = new CreditsService(repository);
31
+
32
+ // Initialize a user with free tier
33
+ await service.initializeUserCredits("user-123");
34
+
35
+ // Check if user has enough credits
36
+ const check = await service.checkCredits("user-123", 10);
37
+ if (!check.hasCredits) {
38
+ console.log(`Need ${check.shortfall} more credits`);
39
+ }
40
+
41
+ // Reserve-Execute-Commit pattern
42
+ const reservation = await service.reserveCredits("user-123", 10, "story_generation");
43
+ try {
44
+ await doExpensiveWork();
45
+ await service.commitCredits("user-123", reservation.id); // Deducts credits
46
+ } catch (error) {
47
+ await service.releaseCredits("user-123", reservation.id); // Refunds credits
48
+ throw error;
49
+ }
50
+ ```
51
+
52
+ ## API Reference
53
+
54
+ ### `CreditsService`
55
+
56
+ The main service class. Accepts any `ICreditRepository` implementation.
57
+
58
+ | Method | Description |
59
+ |--------|-------------|
60
+ | `getUserCredits(userId)` | Get user balance (auto-handles monthly reset and subscription expiry) |
61
+ | `initializeUserCredits(userId)` | Create a new user with free tier |
62
+ | `getOrCreateUserCredits(userId)` | Get or initialize user credits |
63
+ | `checkCredits(userId, amount)` | Check if user has sufficient credits |
64
+ | `reserveCredits(userId, amount, operationType)` | Reserve credits (phase 1) |
65
+ | `commitCredits(userId, reservationId)` | Commit reservation (phase 2 -- success) |
66
+ | `releaseCredits(userId, reservationId)` | Release reservation (phase 2 -- failure) |
67
+ | `addCredits(userId, amount, description)` | Add credits (purchases, bonuses) |
68
+ | `updateTier(userId, tier, expiresAt?)` | Change subscription tier |
69
+ | `getUsageHistory(userId, limit?, offset?)` | Paginated usage history |
70
+ | `logUsage(log)` | Log a usage event for auditing |
71
+
72
+ ### `ICreditRepository`
73
+
74
+ Interface for database implementations. Implement this to use any database backend.
75
+
76
+ Key methods: `getUserCredits`, `initializeUserCredits`, `reserveCreditsAtomic`, `commitReservationAtomic`, `releaseReservationAtomic`, `addCreditsAtomic`, `atomicMonthlyReset`, `createJournalEntry`, `findAndExpireReservations`.
77
+
78
+ ### `InMemoryCreditRepository`
79
+
80
+ In-memory implementation of `ICreditRepository` for testing:
81
+
82
+ ```typescript
83
+ import { createInMemoryCreditRepository } from "@nehorai/credits";
84
+
85
+ const repo = createInMemoryCreditRepository();
86
+ ```
87
+
88
+ ### Error Handling
89
+
90
+ ```typescript
91
+ import { CreditError, isCreditError, CreditErrorCode } from "@nehorai/credits";
92
+
93
+ try {
94
+ await service.reserveCredits(userId, 100, "expensive_op");
95
+ } catch (error) {
96
+ if (isCreditError(error) && error.code === CreditErrorCode.INSUFFICIENT_CREDITS) {
97
+ // Handle insufficient credits
98
+ }
99
+ }
100
+ ```
101
+
102
+ ### SDK Clients
103
+
104
+ REST API clients for external integrations:
105
+
106
+ ```typescript
107
+ import { CreditsClient, AdminCreditsClient } from "@nehorai/credits";
108
+
109
+ // User-facing client
110
+ const client = new CreditsClient({ baseUrl: "/api/v1/credits", getToken });
111
+
112
+ // Admin client
113
+ const admin = new AdminCreditsClient({ baseUrl: "/api/v1/admin", getToken });
114
+ ```
115
+
116
+ ### Core Types
117
+
118
+ | Type | Description |
119
+ |------|-------------|
120
+ | `PortableUserCredits` | User balance with tier, monthly limits, timestamps |
121
+ | `PortableReservation` | Credit reservation for two-phase commit |
122
+ | `PortableTransaction` | Purchase/refund transaction record |
123
+ | `PortableJournalEntry` | Audit trail entry |
124
+ | `PortableUsageLog` | Operation usage log |
125
+ | `CreditCheckResult` | Result of credit sufficiency check |
126
+ | `SubscriptionTier` | `"free" \| "basic" \| "premium" \| "unlimited"` |
127
+ | `WithCreditsOptions` | Options for the `withCredits` HOF |
128
+
129
+ ### Sub-path Exports
130
+
131
+ ```typescript
132
+ import { ... } from "@nehorai/credits/core"; // Core types and errors
133
+ import { ... } from "@nehorai/credits/repository"; // Repository types and in-memory impl
134
+ import { ... } from "@nehorai/credits/auth"; // Auth provider types
135
+ import { ... } from "@nehorai/credits/service"; // CreditsService
136
+ import { ... } from "@nehorai/credits/adapters"; // Adapter types
137
+ import { ... } from "@nehorai/credits/sdk"; // REST API clients
138
+ ```
139
+
140
+ ## Related Packages
141
+
142
+ | Package | Description |
143
+ |---------|-------------|
144
+ | [`@nehorai/credits-firestore`](https://www.npmjs.com/package/@nehorai/credits-firestore) | Firestore implementation of `ICreditRepository` |
145
+ | [`@nehorai/credits-nextjs`](https://www.npmjs.com/package/@nehorai/credits-nextjs) | Next.js adapter with NextAuth integration |
146
+
147
+ ## Repository
148
+
149
+ [https://github.com/NehoraiHadad/nehorai-plugins](https://github.com/NehoraiHadad/nehorai-plugins)
150
+
151
+ ## License
152
+
153
+ MIT
@@ -35,10 +35,13 @@ function getConfigProvider() {
35
35
 
36
36
  // src/config/index.ts
37
37
  var tierConfigSchema = import_zod.z.object({
38
- tier: import_zod.z.enum(["free", "basic", "premium", "unlimited"]),
38
+ tier: import_zod.z.string().min(1),
39
39
  monthlyCredits: import_zod.z.number().min(0),
40
40
  priceUsd: import_zod.z.number().min(0),
41
- features: import_zod.z.array(import_zod.z.string())
41
+ features: import_zod.z.array(import_zod.z.string()),
42
+ isFree: import_zod.z.boolean().optional(),
43
+ unlimited: import_zod.z.boolean().optional(),
44
+ isDefault: import_zod.z.boolean().optional()
42
45
  });
43
46
  var creditSystemConfigSchema = import_zod.z.object({
44
47
  /**
@@ -62,7 +65,7 @@ var creditSystemConfigSchema = import_zod.z.object({
62
65
  * Tier configurations
63
66
  */
64
67
  tierConfigs: import_zod.z.record(
65
- import_zod.z.enum(["free", "basic", "premium", "unlimited"]),
68
+ import_zod.z.string().min(1),
66
69
  tierConfigSchema
67
70
  ),
68
71
  /**
@@ -118,6 +121,8 @@ var DEFAULT_CONFIG = {
118
121
  tier: "free",
119
122
  monthlyCredits: 25,
120
123
  priceUsd: 0,
124
+ isFree: true,
125
+ isDefault: true,
121
126
  features: [
122
127
  "25 credits per month",
123
128
  "Basic story generation",
@@ -151,6 +156,7 @@ var DEFAULT_CONFIG = {
151
156
  tier: "unlimited",
152
157
  monthlyCredits: 0,
153
158
  // 0 = unlimited
159
+ unlimited: true,
154
160
  priceUsd: 49.99,
155
161
  features: [
156
162
  "Unlimited credits",
@@ -263,6 +269,10 @@ function getConfig() {
263
269
  }
264
270
  return currentConfig;
265
271
  }
272
+ function isValidTier(tier) {
273
+ return tier in getConfig().tierConfigs;
274
+ }
275
+ var tierSchema = import_zod.z.string().refine(isValidTier, { message: "Unknown subscription tier" });
266
276
  function snakeCaseToTitleCase(str) {
267
277
  return str.replace(/_/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
268
278
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/adapters/index.ts","../../src/config/index.ts","../../src/config/provider.ts","../../src/core/operations.ts","../../src/adapters/generic.ts"],"sourcesContent":["export type {\n ActionResult,\n CreditActionHandler,\n CreditsAdapterConfig,\n CreditsAdapter,\n} from \"./types.js\";\n\nexport { createGenericAdapter } from \"./generic.js\";\n","import { z } from \"zod\";\nimport type { SubscriptionTier, TierConfig } from \"../core/types.js\";\nimport { getConfigProvider } from \"./provider.js\";\n\n/**\n * Schema for tier configuration\n */\nconst tierConfigSchema = z.object({\n tier: z.enum([\"free\", \"basic\", \"premium\", \"unlimited\"]),\n monthlyCredits: z.number().min(0),\n priceUsd: z.number().min(0),\n features: z.array(z.string()),\n});\n\n/**\n * Schema for credit system configuration\n */\nconst creditSystemConfigSchema = z.object({\n /**\n * Operation costs in credits\n * Keys are operation type names (any non-empty string)\n * Values are positive numbers representing credit cost\n */\n operationCosts: z.record(\n z.string().min(1),\n z.number().positive()\n ),\n\n /**\n * Display labels for operation types (English canonical labels).\n * Falls back to auto-generating from snake_case keys if not provided.\n */\n operationLabels: z.record(\n z.string().min(1),\n z.string().min(1)\n ).optional().default({}),\n\n /**\n * Tier configurations\n */\n tierConfigs: z.record(\n z.enum([\"free\", \"basic\", \"premium\", \"unlimited\"]),\n tierConfigSchema\n ),\n\n /**\n * Reservation expiry time in milliseconds\n */\n reservationExpiryMs: z.number().positive().default(5 * 60 * 1000),\n\n /**\n * Default credits for new users (free tier)\n */\n defaultFreeCredits: z.number().positive().default(25),\n\n /**\n * Grace period in days after subscription expires before downgrade\n */\n subscriptionGracePeriodDays: z.number().min(0).default(3),\n\n /**\n * Low balance notification threshold\n */\n lowBalanceThreshold: z.number().min(0).default(10),\n\n /**\n * Cooldown in hours between low balance notifications\n */\n lowBalanceNotificationCooldownHours: z.number().positive().default(24),\n\n /**\n * Feature flags for optional features\n */\n features: z.object({\n journalEntries: z.boolean().default(true),\n notifications: z.boolean().default(true),\n subscriptionExpiry: z.boolean().default(true),\n usageHistory: z.boolean().default(true),\n }).default({\n journalEntries: true,\n notifications: true,\n subscriptionExpiry: true,\n usageHistory: true,\n }),\n});\n\n/**\n * Credit system configuration type\n */\nexport type CreditSystemConfig = z.infer<typeof creditSystemConfigSchema>;\n\n/**\n * Default configuration\n */\nconst DEFAULT_CONFIG: CreditSystemConfig = {\n operationCosts: {\n story_generation: 5,\n conversation: 2,\n image_generation: 10,\n template_generation: 10,\n },\n operationLabels: {\n story_generation: \"Story generation\",\n conversation: \"Conversation\",\n image_generation: \"Image generation\",\n template_generation: \"Template generation\",\n },\n tierConfigs: {\n free: {\n tier: \"free\",\n monthlyCredits: 25,\n priceUsd: 0,\n features: [\n \"25 credits per month\",\n \"Basic story generation\",\n \"Standard image quality\",\n ],\n },\n basic: {\n tier: \"basic\",\n monthlyCredits: 200,\n priceUsd: 9.99,\n features: [\n \"200 credits per month\",\n \"All story features\",\n \"High quality images\",\n \"Priority support\",\n ],\n },\n premium: {\n tier: \"premium\",\n monthlyCredits: 500,\n priceUsd: 19.99,\n features: [\n \"500 credits per month\",\n \"All features\",\n \"Highest quality images\",\n \"Early access to new features\",\n \"Priority support\",\n ],\n },\n unlimited: {\n tier: \"unlimited\",\n monthlyCredits: 0, // 0 = unlimited\n priceUsd: 49.99,\n features: [\n \"Unlimited credits\",\n \"All features\",\n \"Highest quality images\",\n \"Early access to new features\",\n \"Dedicated support\",\n ],\n },\n },\n reservationExpiryMs: 5 * 60 * 1000, // 5 minutes\n defaultFreeCredits: 25,\n subscriptionGracePeriodDays: 3,\n lowBalanceThreshold: 10,\n lowBalanceNotificationCooldownHours: 24,\n features: {\n journalEntries: true,\n notifications: true,\n subscriptionExpiry: true,\n usageHistory: true,\n },\n};\n\n/**\n * Current configuration (can be overridden)\n */\nlet currentConfig: CreditSystemConfig = DEFAULT_CONFIG;\n\n/**\n * Load configuration from environment variables\n * Can be extended to load from other sources (file, remote config, etc.)\n *\n * Operation costs are loaded from CREDITS_OPERATION_COSTS as JSON:\n * CREDITS_OPERATION_COSTS={\"story_generation\":5,\"image_generation\":10}\n */\nexport function loadConfigFromEnv(): Partial<CreditSystemConfig> {\n const envConfig: Partial<CreditSystemConfig> = {};\n\n // Load operation costs from env as JSON\n const operationCostsEnv = process.env.CREDITS_OPERATION_COSTS;\n if (operationCostsEnv) {\n try {\n const parsed = JSON.parse(operationCostsEnv);\n if (typeof parsed === \"object\" && parsed !== null) {\n envConfig.operationCosts = {\n ...DEFAULT_CONFIG.operationCosts,\n ...parsed,\n };\n }\n } catch {\n console.warn(\"[Credits Config] Failed to parse CREDITS_OPERATION_COSTS env var as JSON\");\n }\n }\n\n // Load other settings from env\n const reservationExpiry = process.env.CREDITS_RESERVATION_EXPIRY_MS;\n if (reservationExpiry) {\n envConfig.reservationExpiryMs = parseInt(reservationExpiry, 10);\n }\n\n const defaultCredits = process.env.CREDITS_DEFAULT_FREE;\n if (defaultCredits) {\n envConfig.defaultFreeCredits = parseInt(defaultCredits, 10);\n }\n\n const gracePeriod = process.env.CREDITS_GRACE_PERIOD_DAYS;\n if (gracePeriod) {\n envConfig.subscriptionGracePeriodDays = parseInt(gracePeriod, 10);\n }\n\n const lowBalanceThreshold = process.env.CREDITS_LOW_BALANCE_THRESHOLD;\n if (lowBalanceThreshold) {\n envConfig.lowBalanceThreshold = parseInt(lowBalanceThreshold, 10);\n }\n\n // Load feature flags\n const featuresEnv = process.env.CREDITS_FEATURES;\n if (featuresEnv) {\n try {\n const features = JSON.parse(featuresEnv);\n envConfig.features = {\n ...DEFAULT_CONFIG.features,\n ...features,\n };\n } catch {\n console.warn(\"[Credits Config] Failed to parse CREDITS_FEATURES env var\");\n }\n }\n\n return envConfig;\n}\n\n/**\n * Initialize configuration\n * Merges default config with environment overrides\n */\nexport function initializeConfig(overrides?: Partial<CreditSystemConfig>): CreditSystemConfig {\n const envConfig = loadConfigFromEnv();\n\n const mergedConfig = {\n ...DEFAULT_CONFIG,\n ...envConfig,\n ...overrides,\n operationCosts: {\n ...DEFAULT_CONFIG.operationCosts,\n ...envConfig.operationCosts,\n ...overrides?.operationCosts,\n },\n operationLabels: {\n ...DEFAULT_CONFIG.operationLabels,\n ...envConfig.operationLabels,\n ...overrides?.operationLabels,\n },\n tierConfigs: {\n ...DEFAULT_CONFIG.tierConfigs,\n ...envConfig.tierConfigs,\n ...overrides?.tierConfigs,\n },\n features: {\n ...DEFAULT_CONFIG.features,\n ...envConfig.features,\n ...overrides?.features,\n },\n };\n\n // Validate configuration\n const result = creditSystemConfigSchema.safeParse(mergedConfig);\n if (!result.success) {\n console.error(\"[Credits Config] Invalid configuration:\", result.error.issues);\n throw new Error(\"Invalid credit system configuration\");\n }\n\n currentConfig = result.data;\n return currentConfig;\n}\n\n/**\n * Get the current configuration.\n * If an external provider is registered, delegates to it.\n * Otherwise falls back to the local currentConfig.\n */\nexport function getConfig(): CreditSystemConfig {\n const provider = getConfigProvider();\n if (provider) {\n return provider.getConfig();\n }\n return currentConfig;\n}\n\n/**\n * Get all valid operation types from configuration\n * Returns the keys of the operationCosts object\n */\nexport function getValidOperationTypes(): string[] {\n return Object.keys(getConfig().operationCosts);\n}\n\n/**\n * Check if an operation type is valid (configured in the system)\n * @param type - The operation type to check\n * @returns true if the operation type is configured\n */\nexport function isValidOperationType(type: string): boolean {\n return type in getConfig().operationCosts;\n}\n\n/**\n * Get operation cost from configuration\n * @throws Error if the operation type is not configured\n */\nexport function getConfigOperationCost(operationType: string): number {\n const cost = getConfig().operationCosts[operationType];\n if (cost === undefined) {\n throw new Error(\n `Unknown operation type: ${operationType}. Valid types: ${getValidOperationTypes().join(\", \")}`\n );\n }\n return cost;\n}\n\n/**\n * Get tier configuration from configuration\n */\nexport function getConfigTierConfig(tier: SubscriptionTier): TierConfig {\n return getConfig().tierConfigs[tier]!;\n}\n\n/**\n * Get monthly limit for a tier from configuration\n * Returns Infinity for unlimited tier\n */\nexport function getConfigMonthlyLimit(tier: SubscriptionTier): number {\n const config = getConfig().tierConfigs[tier]!;\n return config.monthlyCredits === 0 ? Infinity : config.monthlyCredits;\n}\n\n/**\n * Check if a feature is enabled\n */\nexport function isFeatureEnabled(feature: keyof CreditSystemConfig[\"features\"]): boolean {\n return getConfig().features[feature];\n}\n\n/**\n * Convert a snake_case string to Title Case.\n * E.g., \"story_generation\" -> \"Story Generation\"\n */\nfunction snakeCaseToTitleCase(str: string): string {\n return str\n .replace(/_/g, \" \")\n .replace(/\\b\\w/g, (c) => c.toUpperCase());\n}\n\n/**\n * Get the display label for an operation type.\n * Falls back to converting snake_case to Title Case if no label is configured.\n */\nexport function getOperationLabel(operationType: string): string {\n const labels = getConfig().operationLabels;\n if (labels && operationType in labels) {\n return labels[operationType];\n }\n return snakeCaseToTitleCase(operationType);\n}\n\n/**\n * Get all operation labels as a record.\n * For any operation in operationCosts that lacks a label, generates one from the key.\n */\nexport function getOperationLabels(): Record<string, string> {\n const config = getConfig();\n const result: Record<string, string> = {};\n for (const key of Object.keys(config.operationCosts)) {\n result[key] = config.operationLabels?.[key]\n ?? snakeCaseToTitleCase(key);\n }\n return result;\n}\n\n/**\n * Reset configuration to defaults (for testing)\n */\nexport function resetConfig(): void {\n currentConfig = DEFAULT_CONFIG;\n}\n\n// Initialize configuration on module load\ninitializeConfig();\n","import type { CreditSystemConfig } from \"./index.js\";\n\n/** Interface for external config providers (e.g. Firestore-backed) */\nexport interface ICreditConfigProvider {\n getConfig(): CreditSystemConfig;\n}\n\nlet configProvider: ICreditConfigProvider | null = null;\n\n/** Register an external config provider. Called once at app startup. */\nexport function registerConfigProvider(provider: ICreditConfigProvider): void {\n configProvider = provider;\n}\n\n/** Get the registered provider, or null if none registered. */\nexport function getConfigProvider(): ICreditConfigProvider | null {\n return configProvider;\n}\n\n/** Clear registered provider (for testing). */\nexport function clearConfigProvider(): void {\n configProvider = null;\n}\n","/**\n * Core credit operations - framework agnostic\n *\n * Contains the business logic for credit operations that can be\n * used by any adapter or service implementation.\n */\n\nimport type { ICreditRepository } from \"../repository/types.js\";\nimport type { PortableReservation } from \"./types.js\";\nimport { getOperationLabel } from \"../config/index.js\";\n\n/**\n * Commit a reservation with journal entry\n *\n * @param repository - The credit repository\n * @param userId - User ID\n * @param reservationId - Reservation to commit\n */\nexport async function commitReservationWithJournal(\n repository: ICreditRepository,\n userId: string,\n reservationId: string\n): Promise<void> {\n // Get the reservation to know the amount\n const reservation = await repository.getReservation(userId, reservationId);\n if (!reservation) {\n throw new Error(`Reservation ${reservationId} not found`);\n }\n\n // Commit the reservation atomically\n await repository.commitReservationAtomic(userId, reservationId);\n\n // Create journal entry\n const credits = await repository.getUserCredits(userId);\n if (credits) {\n await repository.createJournalEntry({\n userId,\n entryType: \"debit\",\n amount: reservation.amount,\n balanceAfter: credits.balance,\n source: \"operation_commit\",\n referenceId: reservationId,\n referenceType: \"reservation\",\n description: `Committed ${reservation.amount} credits for ${getOperationLabel(reservation.operationType)}`,\n metadata: {\n operationType: reservation.operationType,\n },\n });\n }\n}\n\n/**\n * Release a reservation with journal entry\n *\n * @param repository - The credit repository\n * @param userId - User ID\n * @param reservationId - Reservation to release\n */\nexport async function releaseReservationWithJournal(\n repository: ICreditRepository,\n userId: string,\n reservationId: string\n): Promise<void> {\n // Get the reservation to check its state\n const reservation = await repository.getReservation(userId, reservationId);\n\n // Release the reservation atomically\n await repository.releaseReservationAtomic(userId, reservationId);\n\n // Create journal entry only if reservation was in reserved state\n if (reservation?.status === \"reserved\") {\n const credits = await repository.getUserCredits(userId);\n if (credits) {\n await repository.createJournalEntry({\n userId,\n entryType: \"credit\",\n amount: 0, // No actual credits returned (they were reserved, not spent)\n balanceAfter: credits.balance,\n source: \"operation_release\",\n referenceId: reservationId,\n referenceType: \"reservation\",\n description: `Released ${reservation.amount} reserved credits for ${getOperationLabel(reservation.operationType)}`,\n metadata: {\n operationType: reservation.operationType,\n amount: reservation.amount,\n },\n });\n }\n }\n}\n\n/**\n * Reserve credits for an operation\n *\n * @param repository - The credit repository\n * @param userId - User ID\n * @param amount - Credits to reserve\n * @param operationType - Type of operation\n * @param expiryMs - Reservation expiry time in milliseconds\n * @returns The reservation\n */\nexport async function reserveCreditsForOperation(\n repository: ICreditRepository,\n userId: string,\n amount: number,\n operationType: string,\n expiryMs: number = 5 * 60 * 1000\n): Promise<PortableReservation> {\n const expiresAt = new Date(Date.now() + expiryMs);\n return repository.reserveCreditsAtomic(userId, amount, operationType, expiresAt);\n}\n","/**\n * Generic credits adapter - framework agnostic\n *\n * This adapter uses standard JavaScript APIs and works in any\n * JavaScript environment (Node.js, browser, etc.).\n */\n\nimport type { PortableReservation, WithCreditsOptions } from \"../core/types.js\";\nimport type { ActionResult, CreditsAdapter, CreditsAdapterConfig, CreditActionHandler } from \"./types.js\";\nimport {\n commitReservationWithJournal,\n releaseReservationWithJournal,\n reserveCreditsForOperation,\n} from \"../core/operations.js\";\n\n/**\n * Generate a unique request ID\n */\nfunction generateRequestId(): string {\n const timestamp = Date.now();\n const randomPart = Math.random().toString(36).substring(2, 14);\n return `req_${timestamp}_${randomPart}`;\n}\n\n/**\n * Create a generic credits adapter\n *\n * This adapter:\n * - Authenticates users via the provided auth provider\n * - Reserves credits before action execution\n * - Commits credits on success, releases on failure\n * - Logs usage asynchronously via the deferred executor\n */\nexport function createGenericAdapter(config: CreditsAdapterConfig): CreditsAdapter {\n const { repository, authProvider, deferred, operationCosts } = config;\n\n function getOperationCost(operationType: string): number {\n const cost = operationCosts[operationType];\n if (cost === undefined) {\n throw new Error(\n `Unknown operation type: \"${operationType}\". ` +\n `Configured types: ${Object.keys(operationCosts).join(\", \")}`\n );\n }\n return cost;\n }\n\n function logUsage(params: {\n userId: string;\n operationType: string;\n creditsUsed: number;\n success: boolean;\n errorMessage?: string;\n resourceId?: string;\n resourceType?: string;\n requestId: string;\n }): void {\n deferred.defer(async () => {\n await repository.logUsage({\n userId: params.userId,\n operationType: params.operationType,\n provider: \"gemini\",\n creditsUsed: params.creditsUsed,\n success: params.success,\n errorMessage: params.errorMessage,\n resourceId: params.resourceId,\n resourceType: params.resourceType,\n requestId: params.requestId,\n });\n });\n }\n\n return {\n withCredits<TInput, TOutput>(\n options: WithCreditsOptions,\n handler: CreditActionHandler<TInput, TOutput>\n ): (data: TInput) => Promise<ActionResult<TOutput>> {\n return async (data: TInput): Promise<ActionResult<TOutput>> => {\n const requestId = generateRequestId();\n\n // Authenticate\n const user = await authProvider.getCurrentUser();\n if (!user?.id) {\n return { success: false, error: \"Authentication required\" };\n }\n\n // Handle preview mode - skip credit handling entirely\n if (isPreviewMode(data)) {\n const dummyReservation = createDummyReservation(user.id, options.operationType);\n return handler(user, data, dummyReservation);\n }\n\n // Calculate cost and reserve credits\n let reservation: PortableReservation;\n try {\n const cost = options.customCost ?? getOperationCost(options.operationType);\n reservation = await reserveCreditsForOperation(\n repository,\n user.id,\n cost,\n options.operationType\n );\n } catch (error) {\n const message = error instanceof Error ? error.message : \"Failed to reserve credits\";\n logUsage({\n userId: user.id,\n operationType: options.operationType,\n creditsUsed: 0,\n success: false,\n errorMessage: message,\n resourceId: options.resourceId,\n resourceType: options.resourceType,\n requestId,\n });\n return { success: false, error: message };\n }\n\n // Execute action\n try {\n const result = await handler(user, data, reservation);\n\n if (result.success) {\n await commitReservationWithJournal(repository, user.id, reservation.id);\n logUsage({\n userId: user.id,\n operationType: options.operationType,\n creditsUsed: reservation.amount,\n success: true,\n resourceId: options.resourceId,\n resourceType: options.resourceType,\n requestId,\n });\n } else {\n await releaseReservationWithJournal(repository, user.id, reservation.id);\n logUsage({\n userId: user.id,\n operationType: options.operationType,\n creditsUsed: 0,\n success: false,\n errorMessage: result.error,\n resourceId: options.resourceId,\n resourceType: options.resourceType,\n requestId,\n });\n }\n\n return result;\n } catch (error) {\n const message = error instanceof Error ? error.message : \"An unexpected error occurred\";\n await releaseReservationWithJournal(repository, user.id, reservation.id);\n logUsage({\n userId: user.id,\n operationType: options.operationType,\n creditsUsed: 0,\n success: false,\n errorMessage: message,\n resourceId: options.resourceId,\n resourceType: options.resourceType,\n requestId,\n });\n console.error(\"Action error:\", error);\n return { success: false, error: message };\n }\n };\n },\n };\n}\n\nfunction isPreviewMode(data: unknown): boolean {\n return (\n typeof data === \"object\" &&\n data !== null &&\n \"preview\" in data &&\n (data as Record<string, unknown>).preview === true\n );\n}\n\nfunction createDummyReservation(userId: string, operationType: string): PortableReservation {\n return {\n id: \"preview-mode\",\n userId,\n amount: 0,\n operationType,\n status: \"released\",\n createdAt: new Date().toISOString(),\n expiresAt: new Date().toISOString(),\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,iBAAkB;;;ACOlB,IAAI,iBAA+C;AAQ5C,SAAS,oBAAkD;AAChE,SAAO;AACT;;;ADVA,IAAM,mBAAmB,aAAE,OAAO;AAAA,EAChC,MAAM,aAAE,KAAK,CAAC,QAAQ,SAAS,WAAW,WAAW,CAAC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AAQ;AAAA,YACpB,cAAc,QAAQ;AAAA,YACtB;AAAA,UACF,CAAC;AACD,kBAAQ,MAAM,iBAAiB,KAAK;AACpC,iBAAO,EAAE,SAAS,OAAO,OAAO,QAAQ;AAAA,QAC1C;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAEA,SAAS,cAAc,MAAwB;AAC7C,SACE,OAAO,SAAS,YAChB,SAAS,QACT,aAAa,QACZ,KAAiC,YAAY;AAElD;AAEA,SAAS,uBAAuB,QAAgB,eAA4C;AAC1F,SAAO;AAAA,IACL,IAAI;AAAA,IACJ;AAAA,IACA,QAAQ;AAAA,IACR;AAAA,IACA,QAAQ;AAAA,IACR,YAAW,oBAAI,KAAK,GAAE,YAAY;AAAA,IAClC,YAAW,oBAAI,KAAK,GAAE,YAAY;AAAA,EACpC;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/adapters/index.ts","../../src/config/index.ts","../../src/config/provider.ts","../../src/core/operations.ts","../../src/adapters/generic.ts"],"sourcesContent":["export type {\r\n ActionResult,\r\n CreditActionHandler,\r\n CreditsAdapterConfig,\r\n CreditsAdapter,\r\n} from \"./types.js\";\r\n\r\nexport { createGenericAdapter } from \"./generic.js\";\r\n","import { z } from \"zod\";\r\nimport type { SubscriptionTier, TierConfig } from \"../core/types.js\";\r\nimport { getConfigProvider } from \"./provider.js\";\r\n\r\n/**\r\n * Schema for tier configuration\r\n */\r\nconst tierConfigSchema = z.object({\r\n tier: z.string().min(1),\r\n monthlyCredits: z.number().min(0),\r\n priceUsd: z.number().min(0),\r\n features: z.array(z.string()),\r\n isFree: z.boolean().optional(),\r\n unlimited: z.boolean().optional(),\r\n isDefault: z.boolean().optional(),\r\n});\r\n\r\n/**\r\n * Schema for credit system configuration\r\n */\r\nconst creditSystemConfigSchema = z.object({\r\n /**\r\n * Operation costs in credits\r\n * Keys are operation type names (any non-empty string)\r\n * Values are positive numbers representing credit cost\r\n */\r\n operationCosts: z.record(\r\n z.string().min(1),\r\n z.number().positive()\r\n ),\r\n\r\n /**\r\n * Display labels for operation types (English canonical labels).\r\n * Falls back to auto-generating from snake_case keys if not provided.\r\n */\r\n operationLabels: z.record(\r\n z.string().min(1),\r\n z.string().min(1)\r\n ).optional().default({}),\r\n\r\n /**\r\n * Tier configurations\r\n */\r\n tierConfigs: z.record(\r\n z.string().min(1),\r\n tierConfigSchema\r\n ),\r\n\r\n /**\r\n * Reservation expiry time in milliseconds\r\n */\r\n reservationExpiryMs: z.number().positive().default(5 * 60 * 1000),\r\n\r\n /**\r\n * Default credits for new users (free tier)\r\n */\r\n defaultFreeCredits: z.number().positive().default(25),\r\n\r\n /**\r\n * Grace period in days after subscription expires before downgrade\r\n */\r\n subscriptionGracePeriodDays: z.number().min(0).default(3),\r\n\r\n /**\r\n * Low balance notification threshold\r\n */\r\n lowBalanceThreshold: z.number().min(0).default(10),\r\n\r\n /**\r\n * Cooldown in hours between low balance notifications\r\n */\r\n lowBalanceNotificationCooldownHours: z.number().positive().default(24),\r\n\r\n /**\r\n * Feature flags for optional features\r\n */\r\n features: z.object({\r\n journalEntries: z.boolean().default(true),\r\n notifications: z.boolean().default(true),\r\n subscriptionExpiry: z.boolean().default(true),\r\n usageHistory: z.boolean().default(true),\r\n }).default({\r\n journalEntries: true,\r\n notifications: true,\r\n subscriptionExpiry: true,\r\n usageHistory: true,\r\n }),\r\n});\r\n\r\n/**\r\n * Credit system configuration type\r\n */\r\nexport type CreditSystemConfig = z.infer<typeof creditSystemConfigSchema>;\r\n\r\n/**\r\n * Default configuration\r\n */\r\nconst DEFAULT_CONFIG: CreditSystemConfig = {\r\n operationCosts: {\r\n story_generation: 5,\r\n conversation: 2,\r\n image_generation: 10,\r\n template_generation: 10,\r\n },\r\n operationLabels: {\r\n story_generation: \"Story generation\",\r\n conversation: \"Conversation\",\r\n image_generation: \"Image generation\",\r\n template_generation: \"Template generation\",\r\n },\r\n tierConfigs: {\r\n free: {\r\n tier: \"free\",\r\n monthlyCredits: 25,\r\n priceUsd: 0,\r\n isFree: true,\r\n isDefault: true,\r\n features: [\r\n \"25 credits per month\",\r\n \"Basic story generation\",\r\n \"Standard image quality\",\r\n ],\r\n },\r\n basic: {\r\n tier: \"basic\",\r\n monthlyCredits: 200,\r\n priceUsd: 9.99,\r\n features: [\r\n \"200 credits per month\",\r\n \"All story features\",\r\n \"High quality images\",\r\n \"Priority support\",\r\n ],\r\n },\r\n premium: {\r\n tier: \"premium\",\r\n monthlyCredits: 500,\r\n priceUsd: 19.99,\r\n features: [\r\n \"500 credits per month\",\r\n \"All features\",\r\n \"Highest quality images\",\r\n \"Early access to new features\",\r\n \"Priority support\",\r\n ],\r\n },\r\n unlimited: {\r\n tier: \"unlimited\",\r\n monthlyCredits: 0, // 0 = unlimited\r\n unlimited: true,\r\n priceUsd: 49.99,\r\n features: [\r\n \"Unlimited credits\",\r\n \"All features\",\r\n \"Highest quality images\",\r\n \"Early access to new features\",\r\n \"Dedicated support\",\r\n ],\r\n },\r\n },\r\n reservationExpiryMs: 5 * 60 * 1000, // 5 minutes\r\n defaultFreeCredits: 25,\r\n subscriptionGracePeriodDays: 3,\r\n lowBalanceThreshold: 10,\r\n lowBalanceNotificationCooldownHours: 24,\r\n features: {\r\n journalEntries: true,\r\n notifications: true,\r\n subscriptionExpiry: true,\r\n usageHistory: true,\r\n },\r\n};\r\n\r\n/**\r\n * Current configuration (can be overridden)\r\n */\r\nlet currentConfig: CreditSystemConfig = DEFAULT_CONFIG;\r\n\r\n/**\r\n * Load configuration from environment variables\r\n * Can be extended to load from other sources (file, remote config, etc.)\r\n *\r\n * Operation costs are loaded from CREDITS_OPERATION_COSTS as JSON:\r\n * CREDITS_OPERATION_COSTS={\"story_generation\":5,\"image_generation\":10}\r\n */\r\nexport function loadConfigFromEnv(): Partial<CreditSystemConfig> {\r\n const envConfig: Partial<CreditSystemConfig> = {};\r\n\r\n // Load operation costs from env as JSON\r\n const operationCostsEnv = process.env.CREDITS_OPERATION_COSTS;\r\n if (operationCostsEnv) {\r\n try {\r\n const parsed = JSON.parse(operationCostsEnv);\r\n if (typeof parsed === \"object\" && parsed !== null) {\r\n envConfig.operationCosts = {\r\n ...DEFAULT_CONFIG.operationCosts,\r\n ...parsed,\r\n };\r\n }\r\n } catch {\r\n console.warn(\"[Credits Config] Failed to parse CREDITS_OPERATION_COSTS env var as JSON\");\r\n }\r\n }\r\n\r\n // Load other settings from env\r\n const reservationExpiry = process.env.CREDITS_RESERVATION_EXPIRY_MS;\r\n if (reservationExpiry) {\r\n envConfig.reservationExpiryMs = parseInt(reservationExpiry, 10);\r\n }\r\n\r\n const defaultCredits = process.env.CREDITS_DEFAULT_FREE;\r\n if (defaultCredits) {\r\n envConfig.defaultFreeCredits = parseInt(defaultCredits, 10);\r\n }\r\n\r\n const gracePeriod = process.env.CREDITS_GRACE_PERIOD_DAYS;\r\n if (gracePeriod) {\r\n envConfig.subscriptionGracePeriodDays = parseInt(gracePeriod, 10);\r\n }\r\n\r\n const lowBalanceThreshold = process.env.CREDITS_LOW_BALANCE_THRESHOLD;\r\n if (lowBalanceThreshold) {\r\n envConfig.lowBalanceThreshold = parseInt(lowBalanceThreshold, 10);\r\n }\r\n\r\n // Load feature flags\r\n const featuresEnv = process.env.CREDITS_FEATURES;\r\n if (featuresEnv) {\r\n try {\r\n const features = JSON.parse(featuresEnv);\r\n envConfig.features = {\r\n ...DEFAULT_CONFIG.features,\r\n ...features,\r\n };\r\n } catch {\r\n console.warn(\"[Credits Config] Failed to parse CREDITS_FEATURES env var\");\r\n }\r\n }\r\n\r\n return envConfig;\r\n}\r\n\r\n/**\r\n * Initialize configuration\r\n * Merges default config with environment overrides\r\n */\r\nexport function initializeConfig(overrides?: Partial<CreditSystemConfig>): CreditSystemConfig {\r\n const envConfig = loadConfigFromEnv();\r\n\r\n const mergedConfig = {\r\n ...DEFAULT_CONFIG,\r\n ...envConfig,\r\n ...overrides,\r\n operationCosts: {\r\n ...DEFAULT_CONFIG.operationCosts,\r\n ...envConfig.operationCosts,\r\n ...overrides?.operationCosts,\r\n },\r\n operationLabels: {\r\n ...DEFAULT_CONFIG.operationLabels,\r\n ...envConfig.operationLabels,\r\n ...overrides?.operationLabels,\r\n },\r\n tierConfigs: {\r\n ...DEFAULT_CONFIG.tierConfigs,\r\n ...envConfig.tierConfigs,\r\n ...overrides?.tierConfigs,\r\n },\r\n features: {\r\n ...DEFAULT_CONFIG.features,\r\n ...envConfig.features,\r\n ...overrides?.features,\r\n },\r\n };\r\n\r\n // Validate configuration\r\n const result = creditSystemConfigSchema.safeParse(mergedConfig);\r\n if (!result.success) {\r\n console.error(\"[Credits Config] Invalid configuration:\", result.error.issues);\r\n throw new Error(\"Invalid credit system configuration\");\r\n }\r\n\r\n currentConfig = result.data;\r\n return currentConfig;\r\n}\r\n\r\n/**\r\n * Get the current configuration.\r\n * If an external provider is registered, delegates to it.\r\n * Otherwise falls back to the local currentConfig.\r\n */\r\nexport function getConfig(): CreditSystemConfig {\r\n const provider = getConfigProvider();\r\n if (provider) {\r\n return provider.getConfig();\r\n }\r\n return currentConfig;\r\n}\r\n\r\n/**\r\n * Get all valid operation types from configuration\r\n * Returns the keys of the operationCosts object\r\n */\r\nexport function getValidOperationTypes(): string[] {\r\n return Object.keys(getConfig().operationCosts);\r\n}\r\n\r\n/**\r\n * Check if an operation type is valid (configured in the system)\r\n * @param type - The operation type to check\r\n * @returns true if the operation type is configured\r\n */\r\nexport function isValidOperationType(type: string): boolean {\r\n return type in getConfig().operationCosts;\r\n}\r\n\r\n/**\r\n * Get operation cost from configuration\r\n * @throws Error if the operation type is not configured\r\n */\r\nexport function getConfigOperationCost(operationType: string): number {\r\n const cost = getConfig().operationCosts[operationType];\r\n if (cost === undefined) {\r\n throw new Error(\r\n `Unknown operation type: ${operationType}. Valid types: ${getValidOperationTypes().join(\", \")}`\r\n );\r\n }\r\n return cost;\r\n}\r\n\r\n/**\r\n * Get all valid tier ids from configuration\r\n * Returns the keys of the tierConfigs object\r\n */\r\nexport function getValidTiers(): string[] {\r\n return Object.keys(getConfig().tierConfigs);\r\n}\r\n\r\n/**\r\n * Check if a tier id is valid (configured in the system)\r\n */\r\nexport function isValidTier(tier: string): boolean {\r\n return tier in getConfig().tierConfigs;\r\n}\r\n\r\n/**\r\n * Check if a tier is the free/default tier.\r\n * Falls back to priceUsd === 0 when the isFree flag is not set.\r\n */\r\nexport function isFreeTier(tier: SubscriptionTier): boolean {\r\n const c = getConfig().tierConfigs[tier];\r\n if (!c) return false;\r\n return c.isFree ?? c.priceUsd === 0;\r\n}\r\n\r\n/**\r\n * Check if a tier is unlimited.\r\n * Falls back to monthlyCredits === 0 when the unlimited flag is not set.\r\n */\r\nexport function isUnlimitedTier(tier: SubscriptionTier): boolean {\r\n const c = getConfig().tierConfigs[tier];\r\n if (!c) return false;\r\n return c.unlimited ?? c.monthlyCredits === 0;\r\n}\r\n\r\n/**\r\n * Get the tier assigned to brand-new users.\r\n * Prefers the tier flagged isDefault, then the first isFree tier, then \"free\".\r\n */\r\nexport function getDefaultTier(): SubscriptionTier {\r\n const entries = Object.entries(getConfig().tierConfigs);\r\n const def =\r\n entries.find(([, c]) => c.isDefault)?.[0] ??\r\n entries.find(([, c]) => c.isFree ?? c.priceUsd === 0)?.[0];\r\n return def ?? \"free\";\r\n}\r\n\r\n/**\r\n * Magic balance assigned to unlimited tiers on upgrade.\r\n * Kept at 999999 for back-compat with stored/displayed balances.\r\n */\r\nexport const UNLIMITED_BALANCE_SENTINEL = 999999;\r\n\r\n/**\r\n * Get the magic balance assigned to unlimited tiers on upgrade.\r\n */\r\nexport function getUnlimitedSentinelBalance(): number {\r\n return UNLIMITED_BALANCE_SENTINEL;\r\n}\r\n\r\n/**\r\n * Get tier configuration from configuration.\r\n * Falls back to the default tier (with a warning) if the tier is unknown.\r\n */\r\nexport function getConfigTierConfig(tier: SubscriptionTier): TierConfig {\r\n const cfg = getConfig().tierConfigs[tier];\r\n if (cfg) return cfg;\r\n const fallback = getDefaultTier();\r\n console.warn(\r\n `[Credits Config] Unknown tier \"${tier}\". Falling back to \"${fallback}\". Valid: ${getValidTiers().join(\", \")}`\r\n );\r\n return getConfig().tierConfigs[fallback]!;\r\n}\r\n\r\n/**\r\n * Get monthly limit for a tier from configuration\r\n * Returns Infinity for unlimited tier\r\n */\r\nexport function getConfigMonthlyLimit(tier: SubscriptionTier): number {\r\n return isUnlimitedTier(tier) ? Infinity : getConfigTierConfig(tier).monthlyCredits;\r\n}\r\n\r\n/**\r\n * Runtime zod validator for tier ids. Validates against the LIVE config keys\r\n * at parse time, so apps that add tiers via config get correct validation.\r\n */\r\nexport const tierSchema = z\r\n .string()\r\n .refine(isValidTier, { message: \"Unknown subscription tier\" }) as unknown as z.ZodType<SubscriptionTier>;\r\n\r\n/**\r\n * Parse/validate a tier id against the live config keys.\r\n * @throws ZodError if the tier is not configured\r\n */\r\nexport function parseTier(value: unknown): SubscriptionTier {\r\n return tierSchema.parse(value);\r\n}\r\n\r\n/**\r\n * Check if a feature is enabled\r\n */\r\nexport function isFeatureEnabled(feature: keyof CreditSystemConfig[\"features\"]): boolean {\r\n return getConfig().features[feature];\r\n}\r\n\r\n/**\r\n * Convert a snake_case string to Title Case.\r\n * E.g., \"story_generation\" -> \"Story Generation\"\r\n */\r\nfunction snakeCaseToTitleCase(str: string): string {\r\n return str\r\n .replace(/_/g, \" \")\r\n .replace(/\\b\\w/g, (c) => c.toUpperCase());\r\n}\r\n\r\n/**\r\n * Get the display label for an operation type.\r\n * Falls back to converting snake_case to Title Case if no label is configured.\r\n */\r\nexport function getOperationLabel(operationType: string): string {\r\n const labels = getConfig().operationLabels;\r\n if (labels && operationType in labels) {\r\n return labels[operationType];\r\n }\r\n return snakeCaseToTitleCase(operationType);\r\n}\r\n\r\n/**\r\n * Get all operation labels as a record.\r\n * For any operation in operationCosts that lacks a label, generates one from the key.\r\n */\r\nexport function getOperationLabels(): Record<string, string> {\r\n const config = getConfig();\r\n const result: Record<string, string> = {};\r\n for (const key of Object.keys(config.operationCosts)) {\r\n result[key] = config.operationLabels?.[key]\r\n ?? snakeCaseToTitleCase(key);\r\n }\r\n return result;\r\n}\r\n\r\n/**\r\n * Reset configuration to defaults (for testing)\r\n */\r\nexport function resetConfig(): void {\r\n currentConfig = DEFAULT_CONFIG;\r\n}\r\n\r\n// Initialize configuration on module load\r\ninitializeConfig();\r\n","import type { CreditSystemConfig } from \"./index.js\";\r\n\r\n/** Interface for external config providers (e.g. Firestore-backed) */\r\nexport interface ICreditConfigProvider {\r\n getConfig(): CreditSystemConfig;\r\n}\r\n\r\nlet configProvider: ICreditConfigProvider | null = null;\r\n\r\n/** Register an external config provider. Called once at app startup. */\r\nexport function registerConfigProvider(provider: ICreditConfigProvider): void {\r\n configProvider = provider;\r\n}\r\n\r\n/** Get the registered provider, or null if none registered. */\r\nexport function getConfigProvider(): ICreditConfigProvider | null {\r\n return configProvider;\r\n}\r\n\r\n/** Clear registered provider (for testing). */\r\nexport function clearConfigProvider(): void {\r\n configProvider = null;\r\n}\r\n","/**\r\n * Core credit operations - framework agnostic\r\n *\r\n * Contains the business logic for credit operations that can be\r\n * used by any adapter or service implementation.\r\n */\r\n\r\nimport type { ICreditRepository } from \"../repository/types.js\";\r\nimport type { PortableReservation } from \"./types.js\";\r\nimport { getOperationLabel } from \"../config/index.js\";\r\n\r\n/**\r\n * Commit a reservation with journal entry\r\n *\r\n * @param repository - The credit repository\r\n * @param userId - User ID\r\n * @param reservationId - Reservation to commit\r\n */\r\nexport async function commitReservationWithJournal(\r\n repository: ICreditRepository,\r\n userId: string,\r\n reservationId: string\r\n): Promise<void> {\r\n // Get the reservation to know the amount\r\n const reservation = await repository.getReservation(userId, reservationId);\r\n if (!reservation) {\r\n throw new Error(`Reservation ${reservationId} not found`);\r\n }\r\n\r\n // Commit the reservation atomically\r\n await repository.commitReservationAtomic(userId, reservationId);\r\n\r\n // Create journal entry\r\n const credits = await repository.getUserCredits(userId);\r\n if (credits) {\r\n await repository.createJournalEntry({\r\n userId,\r\n entryType: \"debit\",\r\n amount: reservation.amount,\r\n balanceAfter: credits.balance,\r\n source: \"operation_commit\",\r\n referenceId: reservationId,\r\n referenceType: \"reservation\",\r\n description: `Committed ${reservation.amount} credits for ${getOperationLabel(reservation.operationType)}`,\r\n metadata: {\r\n operationType: reservation.operationType,\r\n },\r\n });\r\n }\r\n}\r\n\r\n/**\r\n * Release a reservation with journal entry\r\n *\r\n * @param repository - The credit repository\r\n * @param userId - User ID\r\n * @param reservationId - Reservation to release\r\n */\r\nexport async function releaseReservationWithJournal(\r\n repository: ICreditRepository,\r\n userId: string,\r\n reservationId: string\r\n): Promise<void> {\r\n // Get the reservation to check its state\r\n const reservation = await repository.getReservation(userId, reservationId);\r\n\r\n // Release the reservation atomically\r\n await repository.releaseReservationAtomic(userId, reservationId);\r\n\r\n // Create journal entry only if reservation was in reserved state\r\n if (reservation?.status === \"reserved\") {\r\n const credits = await repository.getUserCredits(userId);\r\n if (credits) {\r\n await repository.createJournalEntry({\r\n userId,\r\n entryType: \"credit\",\r\n amount: 0, // No actual credits returned (they were reserved, not spent)\r\n balanceAfter: credits.balance,\r\n source: \"operation_release\",\r\n referenceId: reservationId,\r\n referenceType: \"reservation\",\r\n description: `Released ${reservation.amount} reserved credits for ${getOperationLabel(reservation.operationType)}`,\r\n metadata: {\r\n operationType: reservation.operationType,\r\n amount: reservation.amount,\r\n },\r\n });\r\n }\r\n }\r\n}\r\n\r\n/**\r\n * Reserve credits for an operation\r\n *\r\n * @param repository - The credit repository\r\n * @param userId - User ID\r\n * @param amount - Credits to reserve\r\n * @param operationType - Type of operation\r\n * @param expiryMs - Reservation expiry time in milliseconds\r\n * @returns The reservation\r\n */\r\nexport async function reserveCreditsForOperation(\r\n repository: ICreditRepository,\r\n userId: string,\r\n amount: number,\r\n operationType: string,\r\n expiryMs: number = 5 * 60 * 1000\r\n): Promise<PortableReservation> {\r\n const expiresAt = new Date(Date.now() + expiryMs);\r\n return repository.reserveCreditsAtomic(userId, amount, operationType, expiresAt);\r\n}\r\n","/**\r\n * Generic credits adapter - framework agnostic\r\n *\r\n * This adapter uses standard JavaScript APIs and works in any\r\n * JavaScript environment (Node.js, browser, etc.).\r\n */\r\n\r\nimport type { PortableReservation, WithCreditsOptions } from \"../core/types.js\";\r\nimport type { ActionResult, CreditsAdapter, CreditsAdapterConfig, CreditActionHandler } from \"./types.js\";\r\nimport {\r\n commitReservationWithJournal,\r\n releaseReservationWithJournal,\r\n reserveCreditsForOperation,\r\n} from \"../core/operations.js\";\r\n\r\n/**\r\n * Generate a unique request ID\r\n */\r\nfunction generateRequestId(): string {\r\n const timestamp = Date.now();\r\n const randomPart = Math.random().toString(36).substring(2, 14);\r\n return `req_${timestamp}_${randomPart}`;\r\n}\r\n\r\n/**\r\n * Create a generic credits adapter\r\n *\r\n * This adapter:\r\n * - Authenticates users via the provided auth provider\r\n * - Reserves credits before action execution\r\n * - Commits credits on success, releases on failure\r\n * - Logs usage asynchronously via the deferred executor\r\n */\r\nexport function createGenericAdapter(config: CreditsAdapterConfig): CreditsAdapter {\r\n const { repository, authProvider, deferred, operationCosts } = config;\r\n\r\n function getOperationCost(operationType: string): number {\r\n const cost = operationCosts[operationType];\r\n if (cost === undefined) {\r\n throw new Error(\r\n `Unknown operation type: \"${operationType}\". ` +\r\n `Configured types: ${Object.keys(operationCosts).join(\", \")}`\r\n );\r\n }\r\n return cost;\r\n }\r\n\r\n function logUsage(params: {\r\n userId: string;\r\n operationType: string;\r\n creditsUsed: number;\r\n success: boolean;\r\n errorMessage?: string;\r\n resourceId?: string;\r\n resourceType?: string;\r\n requestId: string;\r\n }): void {\r\n deferred.defer(async () => {\r\n await repository.logUsage({\r\n userId: params.userId,\r\n operationType: params.operationType,\r\n provider: \"gemini\",\r\n creditsUsed: params.creditsUsed,\r\n success: params.success,\r\n errorMessage: params.errorMessage,\r\n resourceId: params.resourceId,\r\n resourceType: params.resourceType,\r\n requestId: params.requestId,\r\n });\r\n });\r\n }\r\n\r\n return {\r\n withCredits<TInput, TOutput>(\r\n options: WithCreditsOptions,\r\n handler: CreditActionHandler<TInput, TOutput>\r\n ): (data: TInput) => Promise<ActionResult<TOutput>> {\r\n return async (data: TInput): Promise<ActionResult<TOutput>> => {\r\n const requestId = generateRequestId();\r\n\r\n // Authenticate\r\n const user = await authProvider.getCurrentUser();\r\n if (!user?.id) {\r\n return { success: false, error: \"Authentication required\" };\r\n }\r\n\r\n // Handle preview mode - skip credit handling entirely\r\n if (isPreviewMode(data)) {\r\n const dummyReservation = createDummyReservation(user.id, options.operationType);\r\n return handler(user, data, dummyReservation);\r\n }\r\n\r\n // Calculate cost and reserve credits\r\n let reservation: PortableReservation;\r\n try {\r\n const cost = options.customCost ?? getOperationCost(options.operationType);\r\n reservation = await reserveCreditsForOperation(\r\n repository,\r\n user.id,\r\n cost,\r\n options.operationType\r\n );\r\n } catch (error) {\r\n const message = error instanceof Error ? error.message : \"Failed to reserve credits\";\r\n logUsage({\r\n userId: user.id,\r\n operationType: options.operationType,\r\n creditsUsed: 0,\r\n success: false,\r\n errorMessage: message,\r\n resourceId: options.resourceId,\r\n resourceType: options.resourceType,\r\n requestId,\r\n });\r\n return { success: false, error: message };\r\n }\r\n\r\n // Execute action\r\n try {\r\n const result = await handler(user, data, reservation);\r\n\r\n if (result.success) {\r\n await commitReservationWithJournal(repository, user.id, reservation.id);\r\n logUsage({\r\n userId: user.id,\r\n operationType: options.operationType,\r\n creditsUsed: reservation.amount,\r\n success: true,\r\n resourceId: options.resourceId,\r\n resourceType: options.resourceType,\r\n requestId,\r\n });\r\n } else {\r\n await releaseReservationWithJournal(repository, user.id, reservation.id);\r\n logUsage({\r\n userId: user.id,\r\n operationType: options.operationType,\r\n creditsUsed: 0,\r\n success: false,\r\n errorMessage: result.error,\r\n resourceId: options.resourceId,\r\n resourceType: options.resourceType,\r\n requestId,\r\n });\r\n }\r\n\r\n return result;\r\n } catch (error) {\r\n const message = error instanceof Error ? error.message : \"An unexpected error occurred\";\r\n await releaseReservationWithJournal(repository, user.id, reservation.id);\r\n logUsage({\r\n userId: user.id,\r\n operationType: options.operationType,\r\n creditsUsed: 0,\r\n success: false,\r\n errorMessage: message,\r\n resourceId: options.resourceId,\r\n resourceType: options.resourceType,\r\n requestId,\r\n });\r\n console.error(\"Action error:\", error);\r\n return { success: false, error: message };\r\n }\r\n };\r\n },\r\n };\r\n}\r\n\r\nfunction isPreviewMode(data: unknown): boolean {\r\n return (\r\n typeof data === \"object\" &&\r\n data !== null &&\r\n \"preview\" in data &&\r\n (data as Record<string, unknown>).preview === true\r\n );\r\n}\r\n\r\nfunction createDummyReservation(userId: string, operationType: string): PortableReservation {\r\n return {\r\n id: \"preview-mode\",\r\n userId,\r\n amount: 0,\r\n operationType,\r\n status: \"released\",\r\n createdAt: new Date().toISOString(),\r\n expiresAt: new Date().toISOString(),\r\n };\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,iBAAkB;;;ACOlB,IAAI,iBAA+C;AAQ5C,SAAS,oBAAkD;AAChE,SAAO;AACT;;;ADVA,IAAM,mBAAmB,aAAE,OAAO;AAAA,EAChC,MAAM,aAAE,OAAO,EAAE,IAAI,CAAC;AAAA,EACtB,gBAAgB,aAAE,OAAO,EAAE,IAAI,CAAC;AAAA,EAChC,UAAU,aAAE,OAAO,EAAE,IAAI,CAAC;AAAA,EAC1B,UAAU,aAAE,MAAM,aAAE,OAAO,CAAC;AAAA,EAC5B,QAAQ,aAAE,QAAQ,EAAE,SAAS;AAAA,EAC7B,WAAW,aAAE,QAAQ,EAAE,SAAS;AAAA,EAChC,WAAW,aAAE,QAAQ,EAAE,SAAS;AAClC,CAAC;AAKD,IAAM,2BAA2B,aAAE,OAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMxC,gBAAgB,aAAE;AAAA,IAChB,aAAE,OAAO,EAAE,IAAI,CAAC;AAAA,IAChB,aAAE,OAAO,EAAE,SAAS;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,iBAAiB,aAAE;AAAA,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,OAAO,SAAS,YAChB,SAAS,QACT,aAAa,QACZ,KAAiC,YAAY;AAElD;AAEA,SAAS,uBAAuB,QAAgB,eAA4C;AAC1F,SAAO;AAAA,IACL,IAAI;AAAA,IACJ;AAAA,IACA,QAAQ;AAAA,IACR;AAAA,IACA,QAAQ;AAAA,IACR,YAAW,oBAAI,KAAK,GAAE,YAAY;AAAA,IAClC,YAAW,oBAAI,KAAK,GAAE,YAAY;AAAA,EACpC;AACF;","names":[]}
@@ -1,6 +1,6 @@
1
- import { c as PortableReservation, W as WithCreditsOptions } from '../types-BBxIWHK2.cjs';
1
+ import { c as PortableReservation, W as WithCreditsOptions } from '../types-DgQtamEe.cjs';
2
2
  import { a as CreditsUser, I as ICreditsAuthProvider } from '../types-OZU3I5OM.cjs';
3
- import { I as ICreditRepository } from '../types-D1vdeBcA.cjs';
3
+ import { I as ICreditRepository } from '../types-KgNdBuEF.cjs';
4
4
  import { D as DeferredExecutor } from '../deferred-C7rrRfkm.cjs';
5
5
 
6
6
  /**
@@ -1,6 +1,6 @@
1
- import { c as PortableReservation, W as WithCreditsOptions } from '../types-BBxIWHK2.js';
1
+ import { c as PortableReservation, W as WithCreditsOptions } from '../types-DgQtamEe.js';
2
2
  import { a as CreditsUser, I as ICreditsAuthProvider } from '../types-OZU3I5OM.js';
3
- import { I as ICreditRepository } from '../types-aJsyv8df.js';
3
+ import { I as ICreditRepository } from '../types-fW2JNmmr.js';
4
4
  import { D as DeferredExecutor } from '../deferred-C7rrRfkm.js';
5
5
 
6
6
  /**
@@ -9,10 +9,13 @@ function getConfigProvider() {
9
9
 
10
10
  // src/config/index.ts
11
11
  var tierConfigSchema = z.object({
12
- tier: z.enum(["free", "basic", "premium", "unlimited"]),
12
+ tier: z.string().min(1),
13
13
  monthlyCredits: z.number().min(0),
14
14
  priceUsd: z.number().min(0),
15
- features: z.array(z.string())
15
+ features: z.array(z.string()),
16
+ isFree: z.boolean().optional(),
17
+ unlimited: z.boolean().optional(),
18
+ isDefault: z.boolean().optional()
16
19
  });
17
20
  var creditSystemConfigSchema = z.object({
18
21
  /**
@@ -36,7 +39,7 @@ var creditSystemConfigSchema = z.object({
36
39
  * Tier configurations
37
40
  */
38
41
  tierConfigs: z.record(
39
- z.enum(["free", "basic", "premium", "unlimited"]),
42
+ z.string().min(1),
40
43
  tierConfigSchema
41
44
  ),
42
45
  /**
@@ -92,6 +95,8 @@ var DEFAULT_CONFIG = {
92
95
  tier: "free",
93
96
  monthlyCredits: 25,
94
97
  priceUsd: 0,
98
+ isFree: true,
99
+ isDefault: true,
95
100
  features: [
96
101
  "25 credits per month",
97
102
  "Basic story generation",
@@ -125,6 +130,7 @@ var DEFAULT_CONFIG = {
125
130
  tier: "unlimited",
126
131
  monthlyCredits: 0,
127
132
  // 0 = unlimited
133
+ unlimited: true,
128
134
  priceUsd: 49.99,
129
135
  features: [
130
136
  "Unlimited credits",
@@ -237,6 +243,10 @@ function getConfig() {
237
243
  }
238
244
  return currentConfig;
239
245
  }
246
+ function isValidTier(tier) {
247
+ return tier in getConfig().tierConfigs;
248
+ }
249
+ var tierSchema = z.string().refine(isValidTier, { message: "Unknown subscription tier" });
240
250
  function snakeCaseToTitleCase(str) {
241
251
  return str.replace(/_/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
242
252
  }