@nehorai/credits 1.5.0 → 1.7.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.
- package/LICENSE +21 -21
- package/README.md +153 -153
- package/dist/adapters/index.cjs.map +1 -1
- package/dist/adapters/index.d.cts +2 -2
- package/dist/adapters/index.d.ts +2 -2
- package/dist/adapters/index.js.map +1 -1
- package/dist/auth/index.cjs.map +1 -1
- package/dist/auth/index.js.map +1 -1
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.cts +3 -3
- package/dist/core/index.d.ts +3 -3
- package/dist/core/index.js.map +1 -1
- package/dist/index.cjs +102 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +102 -7
- package/dist/index.js.map +1 -1
- package/dist/repository/index.cjs +21 -5
- package/dist/repository/index.cjs.map +1 -1
- package/dist/repository/index.d.cts +5 -5
- package/dist/repository/index.d.ts +5 -5
- package/dist/repository/index.js +21 -5
- package/dist/repository/index.js.map +1 -1
- package/dist/sdk/index.cjs.map +1 -1
- package/dist/sdk/index.d.cts +2 -2
- package/dist/sdk/index.d.ts +2 -2
- package/dist/sdk/index.js.map +1 -1
- package/dist/service/index.cjs +81 -2
- package/dist/service/index.cjs.map +1 -1
- package/dist/service/index.d.cts +46 -4
- package/dist/service/index.d.ts +46 -4
- package/dist/service/index.js +81 -2
- package/dist/service/index.js.map +1 -1
- package/dist/{types-DgQtamEe.d.cts → types-CjLj3GW5.d.cts} +22 -1
- package/dist/{types-DgQtamEe.d.ts → types-CjLj3GW5.d.ts} +22 -1
- package/dist/{types-fW2JNmmr.d.ts → types-DFe5BoJY.d.cts} +27 -3
- package/dist/{types-KgNdBuEF.d.cts → types-_bSybQnb.d.ts} +27 -3
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/repository/types.ts","../../src/repository/utils.ts","../../src/config/index.ts","../../src/config/provider.ts","../../src/repository/memory/index.ts"],"sourcesContent":["import type {\r\n PortableUserCredits,\r\n PortableReservation,\r\n PortableTransaction,\r\n PortableUsageLog,\r\n PortableJournalEntry,\r\n CreditOperationType,\r\n SubscriptionTier,\r\n ReservationStatus,\r\n AIProviderType,\r\n MonthlyResetResult,\r\n SubscriptionExpiryResult,\r\n CreditSource,\r\n JournalReferenceType,\r\n} from \"../core/types.js\";\r\n\r\n/**\r\n * Input for creating a credit reservation\r\n */\r\nexport interface CreateReservationInput {\r\n userId: string;\r\n amount: number;\r\n operationType: CreditOperationType;\r\n expiresAt: Date;\r\n}\r\n\r\n/**\r\n * Input for creating a credit transaction\r\n */\r\nexport interface CreateTransactionInput {\r\n userId: string;\r\n type: PortableTransaction[\"type\"];\r\n amount: number;\r\n description: string;\r\n paymentRef?: string;\r\n previousBalance: number;\r\n newBalance: number;\r\n}\r\n\r\n/**\r\n * Input for logging usage\r\n */\r\nexport interface CreateUsageLogInput {\r\n userId: string;\r\n operationType: CreditOperationType;\r\n provider: AIProviderType;\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 metadata?: Record<string, unknown>;\r\n}\r\n\r\n/**\r\n * Query options for usage logs\r\n */\r\nexport interface UsageLogQuery {\r\n userId?: string;\r\n operationType?: CreditOperationType;\r\n success?: boolean;\r\n limit?: number;\r\n offset?: number;\r\n startDate?: Date;\r\n endDate?: Date;\r\n}\r\n\r\n/**\r\n * Input for creating a journal entry\r\n */\r\nexport interface CreateJournalEntryInput {\r\n userId: string;\r\n entryType: \"debit\" | \"credit\";\r\n amount: number;\r\n balanceAfter: number;\r\n source: CreditSource;\r\n referenceId: string;\r\n referenceType: JournalReferenceType;\r\n description: string;\r\n metadata?: Record<string, unknown>;\r\n}\r\n\r\n/**\r\n * Query options for journal entries\r\n */\r\nexport interface JournalEntryQuery {\r\n userId: string;\r\n source?: CreditSource;\r\n referenceType?: JournalReferenceType;\r\n limit?: number;\r\n offset?: number;\r\n startDate?: Date;\r\n endDate?: Date;\r\n}\r\n\r\n/**\r\n * Partial update for user credits balance\r\n */\r\nexport interface CreditBalanceUpdate {\r\n balance?: number;\r\n bonusCredits?: number;\r\n reserved?: number;\r\n tier?: SubscriptionTier;\r\n monthlyLimit?: number;\r\n monthlyUsed?: number;\r\n monthlyResetAt?: Date | string;\r\n subscriptionExpiresAt?: Date | string | null;\r\n /** Increment balance by this amount (alternative to absolute value) */\r\n balanceIncrement?: number;\r\n /** Increment bonusCredits by this amount (alternative to absolute value) */\r\n bonusCreditsIncrement?: number;\r\n /** Increment reserved by this amount (alternative to absolute value) */\r\n reservedIncrement?: number;\r\n /** Increment monthlyUsed by this amount */\r\n monthlyUsedIncrement?: number;\r\n}\r\n\r\n/**\r\n * Input for tier update\r\n */\r\nexport interface TierUpdateInput {\r\n tier: SubscriptionTier;\r\n monthlyLimit: number;\r\n balance?: number;\r\n monthlyUsed?: number;\r\n subscriptionExpiresAt?: Date | string | null;\r\n}\r\n\r\n/**\r\n * Repository interface for credits database operations\r\n *\r\n * Implementations can use any database (Firestore, PostgreSQL, etc.)\r\n * All methods should handle their own error handling and transactions\r\n */\r\nexport interface ICreditRepository {\r\n // ==================== User Credits ====================\r\n\r\n /**\r\n * Get user credits balance\r\n * @param userId - User ID\r\n * @returns User credits or null if not found\r\n */\r\n getUserCredits(userId: string): Promise<PortableUserCredits | null>;\r\n\r\n /**\r\n * Initialize credits for a new user\r\n * @param userId - User ID\r\n * @param tier - Initial subscription tier\r\n * @param initialBalance - Initial credit balance\r\n * @returns Initialized user credits\r\n */\r\n initializeUserCredits(\r\n userId: string,\r\n tier: SubscriptionTier,\r\n initialBalance: number\r\n ): Promise<PortableUserCredits>;\r\n\r\n /**\r\n * Update user credits balance\r\n * @param userId - User ID\r\n * @param updates - Partial updates to apply\r\n */\r\n updateUserCredits(userId: string, updates: CreditBalanceUpdate): Promise<void>;\r\n\r\n /**\r\n * Update user subscription tier\r\n * @param userId - User ID\r\n * @param input - Tier update data\r\n */\r\n updateUserTier(userId: string, input: TierUpdateInput): Promise<void>;\r\n\r\n // ==================== Reservations ====================\r\n\r\n /**\r\n * Create a credit reservation (phase 1 of two-phase commit)\r\n * @param input - Reservation data\r\n * @returns Created reservation\r\n */\r\n createReservation(input: CreateReservationInput): Promise<PortableReservation>;\r\n\r\n /**\r\n * Get a reservation by ID\r\n * @param userId - User ID\r\n * @param reservationId - Reservation ID\r\n * @returns Reservation or null\r\n */\r\n getReservation(userId: string, reservationId: string): Promise<PortableReservation | null>;\r\n\r\n /**\r\n * Update reservation status\r\n * @param userId - User ID\r\n * @param reservationId - Reservation ID\r\n * @param status - New status\r\n * @param completedAt - Completion timestamp\r\n */\r\n updateReservationStatus(\r\n userId: string,\r\n reservationId: string,\r\n status: ReservationStatus,\r\n completedAt?: Date\r\n ): Promise<void>;\r\n\r\n // ==================== Atomic Operations ====================\r\n\r\n /**\r\n * Reserve credits atomically (creates reservation + updates balance in transaction)\r\n * @param userId - User ID\r\n * @param amount - Credits to reserve\r\n * @param operationType - Operation type for tracking\r\n * @param expiresAt - Reservation expiry time\r\n * @returns Created reservation\r\n * @throws Error if insufficient credits\r\n */\r\n reserveCreditsAtomic(\r\n userId: string,\r\n amount: number,\r\n operationType: CreditOperationType,\r\n expiresAt: Date\r\n ): Promise<PortableReservation>;\r\n\r\n /**\r\n * Commit a reservation atomically (deducts credits + marks reservation committed)\r\n * @param userId - User ID\r\n * @param reservationId - Reservation ID\r\n * @throws Error if reservation not found or not in reserved state\r\n */\r\n commitReservationAtomic(userId: string, reservationId: string): Promise<void>;\r\n\r\n /**\r\n * Release a reservation atomically (releases reserved credits + marks reservation released)\r\n * @param userId - User ID\r\n * @param reservationId - Reservation ID\r\n */\r\n releaseReservationAtomic(userId: string, reservationId: string): Promise<void>;\r\n\r\n /**\r\n * Add credits atomically (creates transaction + updates balance)\r\n * @param userId - User ID\r\n * @param amount - Credits to add\r\n * @param description - Transaction description\r\n * @param paymentRef - Optional payment reference\r\n */\r\n addCreditsAtomic(\r\n userId: string,\r\n amount: number,\r\n description: string,\r\n paymentRef?: string\r\n ): Promise<void>;\r\n\r\n /**\r\n * Deduct credits from a user's balance atomically.\r\n *\r\n * Splits the deduction across `balance` and `bonusCredits`, draining\r\n * `balance` (monthly, resets each cycle) first so persistent bonus\r\n * credits survive longer. Runs in a single atomic transaction so\r\n * concurrent deducts cannot drive either field negative.\r\n *\r\n * Callers are responsible for writing any audit record (journal /\r\n * transaction) — this method only moves credits.\r\n *\r\n * @param userId - User ID\r\n * @param amount - Credits to deduct (positive)\r\n * @returns Combined totals (balance + bonusCredits) before and after.\r\n * @throws If user has no credits doc or insufficient credits\r\n */\r\n deductCreditsAtomic(\r\n userId: string,\r\n amount: number\r\n ): Promise<{ previousBalance: number; newBalance: number }>;\r\n\r\n // ==================== Transactions ====================\r\n\r\n /**\r\n * Create a credit transaction record\r\n * @param input - Transaction data\r\n * @returns Created transaction\r\n */\r\n createTransaction(input: CreateTransactionInput): Promise<PortableTransaction>;\r\n\r\n /**\r\n * Get user's transaction history\r\n * @param userId - User ID\r\n * @param limit - Max results\r\n * @param offset - Skip results\r\n * @returns List of transactions\r\n */\r\n getTransactions(\r\n userId: string,\r\n limit?: number,\r\n offset?: number\r\n ): Promise<PortableTransaction[]>;\r\n\r\n // ==================== Usage Logs ====================\r\n\r\n /**\r\n * Log a usage event\r\n * @param input - Usage log data\r\n * @returns Created usage log\r\n */\r\n logUsage(input: CreateUsageLogInput): Promise<PortableUsageLog>;\r\n\r\n /**\r\n * Query usage logs\r\n * @param query - Query parameters\r\n * @returns List of usage logs\r\n */\r\n getUsageLogs(query: UsageLogQuery): Promise<PortableUsageLog[]>;\r\n\r\n /**\r\n * Get usage log count (for pagination)\r\n * @param query - Query parameters (without limit/offset)\r\n * @returns Count of matching logs\r\n */\r\n getUsageLogsCount(query: Omit<UsageLogQuery, \"limit\" | \"offset\">): Promise<number>;\r\n\r\n // ==================== Cleanup Operations ====================\r\n\r\n /**\r\n * Find and expire reservations past their expiration time\r\n * Used by cron job to clean up stale reservations\r\n * @param batchSize - Maximum number of reservations to process per batch (default: 100)\r\n * @param maxIterations - Maximum number of pagination iterations to prevent infinite loops (default: 100)\r\n * @returns Cleanup results with counts and errors\r\n */\r\n findAndExpireReservations(batchSize?: number, maxIterations?: number): Promise<{\r\n expiredCount: number;\r\n creditsReleased: number;\r\n errors: string[];\r\n }>;\r\n\r\n // ==================== Atomic Monthly Reset ====================\r\n\r\n /**\r\n * Atomically perform monthly reset if needed\r\n * Uses optimistic locking to prevent race conditions\r\n *\r\n * @param userId - User ID\r\n * @param tier - User's current subscription tier (for determining new balance)\r\n * @param expectedResetAt - The expected monthlyResetAt value (for optimistic locking)\r\n * @returns Result indicating whether reset was performed and updated credits\r\n */\r\n atomicMonthlyReset(\r\n userId: string,\r\n tier: SubscriptionTier,\r\n expectedResetAt: Date | string\r\n ): Promise<MonthlyResetResult>;\r\n\r\n // ==================== Subscription Expiry ====================\r\n\r\n /**\r\n * Check and handle subscription expiry with grace period\r\n * Auto-downgrades expired subscriptions after grace period\r\n *\r\n * @param userId - User ID\r\n * @param gracePeriodDays - Days to allow after expiry before downgrade (default: 3)\r\n * @returns Result indicating whether downgrade occurred\r\n */\r\n checkAndHandleSubscriptionExpiry(\r\n userId: string,\r\n gracePeriodDays?: number\r\n ): Promise<SubscriptionExpiryResult>;\r\n\r\n // ==================== Journal Entries ====================\r\n\r\n /**\r\n * Create a journal entry for audit trail\r\n * @param input - Journal entry data\r\n * @returns Created journal entry\r\n */\r\n createJournalEntry(input: CreateJournalEntryInput): Promise<PortableJournalEntry>;\r\n\r\n /**\r\n * Get journal entries for a user\r\n * @param query - Query parameters\r\n * @returns List of journal entries\r\n */\r\n getJournalEntries(query: JournalEntryQuery): Promise<PortableJournalEntry[]>;\r\n\r\n /**\r\n * Get journal entry count for pagination\r\n * @param query - Query parameters (without limit/offset)\r\n * @returns Count of matching entries\r\n */\r\n getJournalEntriesCount(query: Omit<JournalEntryQuery, \"limit\" | \"offset\">): Promise<number>;\r\n}\r\n\r\n/**\r\n * Factory type for creating repository instances\r\n */\r\nexport type CreditRepositoryFactory = () => ICreditRepository;\r\n\r\n/**\r\n * Convert PortableUserCredits to client-safe format\r\n * Utility function that implementations can use\r\n */\r\nexport function toClientUserCredits(credits: PortableUserCredits): PortableUserCredits {\r\n // Already in portable format, just ensure all timestamps are ISO strings\r\n return {\r\n userId: credits.userId,\r\n balance: credits.balance,\r\n bonusCredits: credits.bonusCredits ?? 0,\r\n reserved: credits.reserved,\r\n tier: credits.tier,\r\n monthlyLimit: credits.monthlyLimit,\r\n monthlyUsed: credits.monthlyUsed,\r\n monthlyResetAt: toISOString(credits.monthlyResetAt),\r\n subscriptionExpiresAt: credits.subscriptionExpiresAt\r\n ? toISOString(credits.subscriptionExpiresAt)\r\n : null,\r\n createdAt: toISOString(credits.createdAt),\r\n updatedAt: toISOString(credits.updatedAt),\r\n };\r\n}\r\n\r\n/**\r\n * Convert any timestamp-like value to ISO string\r\n */\r\nfunction toISOString(value: unknown): string {\r\n if (!value) return new Date().toISOString();\r\n if (typeof value === \"string\") return value;\r\n if (value instanceof Date) return value.toISOString();\r\n // Handle Firestore Timestamp-like objects\r\n if (typeof value === \"object\" && \"toDate\" in value && typeof (value as { toDate: () => Date }).toDate === \"function\") {\r\n return (value as { toDate: () => Date }).toDate().toISOString();\r\n }\r\n return new Date().toISOString();\r\n}\r\n","/**\r\n * Shared utilities for credit repository implementations\r\n *\r\n * These are pure functions with no server-only dependencies,\r\n * making them safe to use in any repository implementation.\r\n */\r\n\r\n/**\r\n * Generate a unique ID\r\n * Uses timestamp + random string for uniqueness\r\n */\r\nexport function generateId(): string {\r\n return `${Date.now()}-${Math.random().toString(36).substring(2, 11)}`;\r\n}\r\n\r\n/**\r\n * Convert date-like value to Date object\r\n * Handles: Date, string, Firestore Timestamp\r\n */\r\nexport function toDate(value: Date | string | unknown): Date {\r\n if (value instanceof Date) return value;\r\n if (typeof value === \"string\") return new Date(value);\r\n // Handle Firestore Timestamp\r\n if (typeof value === \"object\" && value !== null && \"toDate\" in value) {\r\n return (value as { toDate: () => Date }).toDate();\r\n }\r\n return new Date();\r\n}\r\n\r\n/**\r\n * Get the first day of next month\r\n * Used for calculating monthly reset dates\r\n */\r\nexport function getNextMonthlyReset(from: Date = new Date()): Date {\r\n const next = new Date(from);\r\n next.setMonth(next.getMonth() + 1);\r\n next.setDate(1);\r\n next.setHours(0, 0, 0, 0);\r\n return next;\r\n}\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 * In-Memory Credit Repository Implementation\r\n *\r\n * A database-agnostic implementation of ICreditRepository for testing and prototyping.\r\n * All data is stored in memory and lost when the process restarts.\r\n *\r\n * Usage:\r\n * - Unit tests without database dependency\r\n * - Local development and prototyping\r\n * - Reference implementation for custom repositories\r\n */\r\n\r\nimport type {\r\n PortableUserCredits,\r\n PortableReservation,\r\n PortableTransaction,\r\n PortableJournalEntry,\r\n PortableUsageLog,\r\n SubscriptionTier,\r\n ReservationStatus,\r\n MonthlyResetResult,\r\n SubscriptionExpiryResult,\r\n} from \"../../core/types.js\";\r\nimport type {\r\n ICreditRepository,\r\n CreateReservationInput,\r\n CreateTransactionInput,\r\n CreateUsageLogInput,\r\n CreateJournalEntryInput,\r\n UsageLogQuery,\r\n JournalEntryQuery,\r\n CreditBalanceUpdate,\r\n TierUpdateInput,\r\n} from \"../types.js\";\r\nimport { generateId, toDate, getNextMonthlyReset } from \"../utils.js\";\r\nimport {\r\n getConfigMonthlyLimit,\r\n getConfigTierConfig,\r\n getDefaultTier,\r\n isFreeTier,\r\n} from \"../../config/index.js\";\r\n\r\n/**\r\n * In-Memory implementation of ICreditRepository\r\n *\r\n * Implements all repository methods using Map-based storage.\r\n * Useful for testing and as a reference implementation.\r\n */\r\nexport class InMemoryCreditRepository implements ICreditRepository {\r\n private users = new Map<string, PortableUserCredits>();\r\n private reservations = new Map<string, Map<string, PortableReservation>>();\r\n private transactions = new Map<string, PortableTransaction[]>();\r\n private usageLogs: PortableUsageLog[] = [];\r\n private journalEntries = new Map<string, PortableJournalEntry[]>();\r\n\r\n // ==================== User Credits ====================\r\n\r\n async getUserCredits(userId: string): Promise<PortableUserCredits | null> {\r\n return this.users.get(userId) ?? null;\r\n }\r\n\r\n async initializeUserCredits(\r\n userId: string,\r\n tier: SubscriptionTier,\r\n initialBalance: number\r\n ): Promise<PortableUserCredits> {\r\n const now = new Date().toISOString();\r\n const credits: PortableUserCredits = {\r\n userId,\r\n balance: initialBalance,\r\n bonusCredits: 0,\r\n reserved: 0,\r\n tier,\r\n monthlyLimit: getConfigMonthlyLimit(tier),\r\n monthlyUsed: 0,\r\n monthlyResetAt: getNextMonthlyReset().toISOString(),\r\n subscriptionExpiresAt: null,\r\n createdAt: now,\r\n updatedAt: now,\r\n };\r\n this.users.set(userId, credits);\r\n return credits;\r\n }\r\n\r\n async updateUserCredits(userId: string, updates: CreditBalanceUpdate): Promise<void> {\r\n const credits = this.users.get(userId);\r\n if (!credits) {\r\n throw new Error(`User ${userId} not found`);\r\n }\r\n\r\n const now = new Date().toISOString();\r\n\r\n // Apply absolute updates\r\n if (updates.balance !== undefined) credits.balance = updates.balance;\r\n if (updates.bonusCredits !== undefined) credits.bonusCredits = updates.bonusCredits;\r\n if (updates.reserved !== undefined) credits.reserved = updates.reserved;\r\n if (updates.tier !== undefined) credits.tier = updates.tier;\r\n if (updates.monthlyLimit !== undefined) credits.monthlyLimit = updates.monthlyLimit;\r\n if (updates.monthlyUsed !== undefined) credits.monthlyUsed = updates.monthlyUsed;\r\n if (updates.monthlyResetAt !== undefined) {\r\n credits.monthlyResetAt = updates.monthlyResetAt instanceof Date\r\n ? updates.monthlyResetAt.toISOString()\r\n : updates.monthlyResetAt;\r\n }\r\n if (updates.subscriptionExpiresAt !== undefined) {\r\n if (updates.subscriptionExpiresAt === null) {\r\n credits.subscriptionExpiresAt = null;\r\n } else {\r\n credits.subscriptionExpiresAt = updates.subscriptionExpiresAt instanceof Date\r\n ? updates.subscriptionExpiresAt.toISOString()\r\n : updates.subscriptionExpiresAt;\r\n }\r\n }\r\n\r\n // Apply increments\r\n if (updates.balanceIncrement !== undefined) {\r\n credits.balance += updates.balanceIncrement;\r\n }\r\n if (updates.bonusCreditsIncrement !== undefined) {\r\n credits.bonusCredits += updates.bonusCreditsIncrement;\r\n }\r\n if (updates.reservedIncrement !== undefined) {\r\n credits.reserved += updates.reservedIncrement;\r\n }\r\n if (updates.monthlyUsedIncrement !== undefined) {\r\n credits.monthlyUsed += updates.monthlyUsedIncrement;\r\n }\r\n\r\n credits.updatedAt = now;\r\n this.users.set(userId, credits);\r\n }\r\n\r\n async updateUserTier(userId: string, input: TierUpdateInput): Promise<void> {\r\n const credits = this.users.get(userId);\r\n if (!credits) {\r\n throw new Error(`User ${userId} not found`);\r\n }\r\n\r\n credits.tier = input.tier;\r\n credits.monthlyLimit = input.monthlyLimit;\r\n if (input.balance !== undefined) credits.balance = input.balance;\r\n if (input.monthlyUsed !== undefined) credits.monthlyUsed = input.monthlyUsed;\r\n if (input.subscriptionExpiresAt !== undefined) {\r\n if (input.subscriptionExpiresAt === null) {\r\n credits.subscriptionExpiresAt = null;\r\n } else {\r\n credits.subscriptionExpiresAt = input.subscriptionExpiresAt instanceof Date\r\n ? input.subscriptionExpiresAt.toISOString()\r\n : input.subscriptionExpiresAt;\r\n }\r\n }\r\n credits.updatedAt = new Date().toISOString();\r\n\r\n this.users.set(userId, credits);\r\n }\r\n\r\n // ==================== Reservations ====================\r\n\r\n async createReservation(input: CreateReservationInput): Promise<PortableReservation> {\r\n const now = new Date().toISOString();\r\n const reservation: PortableReservation = {\r\n id: generateId(),\r\n userId: input.userId,\r\n amount: input.amount,\r\n operationType: input.operationType,\r\n status: \"reserved\",\r\n createdAt: now,\r\n expiresAt: input.expiresAt.toISOString(),\r\n };\r\n\r\n if (!this.reservations.has(input.userId)) {\r\n this.reservations.set(input.userId, new Map());\r\n }\r\n this.reservations.get(input.userId)!.set(reservation.id, reservation);\r\n\r\n return reservation;\r\n }\r\n\r\n async getReservation(\r\n userId: string,\r\n reservationId: string\r\n ): Promise<PortableReservation | null> {\r\n const userReservations = this.reservations.get(userId);\r\n if (!userReservations) return null;\r\n return userReservations.get(reservationId) ?? null;\r\n }\r\n\r\n async updateReservationStatus(\r\n userId: string,\r\n reservationId: string,\r\n status: ReservationStatus,\r\n completedAt?: Date\r\n ): Promise<void> {\r\n const userReservations = this.reservations.get(userId);\r\n if (!userReservations) {\r\n throw new Error(`No reservations found for user ${userId}`);\r\n }\r\n\r\n const reservation = userReservations.get(reservationId);\r\n if (!reservation) {\r\n throw new Error(`Reservation ${reservationId} not found`);\r\n }\r\n\r\n reservation.status = status;\r\n if (completedAt) reservation.completedAt = completedAt.toISOString();\r\n\r\n userReservations.set(reservationId, reservation);\r\n }\r\n\r\n // ==================== Atomic Operations ====================\r\n\r\n async reserveCreditsAtomic(\r\n userId: string,\r\n amount: number,\r\n operationType: string,\r\n expiresAt: Date\r\n ): Promise<PortableReservation> {\r\n const credits = this.users.get(userId);\r\n if (!credits) {\r\n throw new Error(`User ${userId} not found`);\r\n }\r\n\r\n // Calculate available credits (balance + bonusCredits - reserved)\r\n const available = credits.balance + credits.bonusCredits - credits.reserved;\r\n if (available < amount) {\r\n throw new Error(\r\n `Insufficient credits. Available: ${available}, Required: ${amount}`\r\n );\r\n }\r\n\r\n // Create reservation\r\n const reservation = await this.createReservation({\r\n userId,\r\n amount,\r\n operationType,\r\n expiresAt,\r\n });\r\n\r\n // Update reserved amount\r\n credits.reserved += amount;\r\n credits.updatedAt = new Date().toISOString();\r\n this.users.set(userId, credits);\r\n\r\n return reservation;\r\n }\r\n\r\n async commitReservationAtomic(userId: string, reservationId: string): Promise<void> {\r\n const credits = this.users.get(userId);\r\n if (!credits) {\r\n throw new Error(`User ${userId} not found`);\r\n }\r\n\r\n const reservation = await this.getReservation(userId, reservationId);\r\n if (!reservation) {\r\n throw new Error(`Reservation ${reservationId} not found`);\r\n }\r\n\r\n // Idempotent: re-committing an already-committed reservation is a no-op\r\n // (retry-safe). Committing a released/expired one is a genuine conflict.\r\n if (reservation.status === \"committed\") {\r\n return;\r\n }\r\n\r\n if (reservation.status !== \"reserved\") {\r\n throw new Error(\r\n `Cannot commit reservation in ${reservation.status} state`\r\n );\r\n }\r\n\r\n const amount = reservation.amount;\r\n\r\n // Deduct from balance (bonus credits first logic can be added if needed)\r\n credits.balance -= amount;\r\n credits.reserved -= amount;\r\n credits.monthlyUsed += amount;\r\n credits.updatedAt = new Date().toISOString();\r\n this.users.set(userId, credits);\r\n\r\n // Update reservation status\r\n await this.updateReservationStatus(userId, reservationId, \"committed\", new Date());\r\n }\r\n\r\n async releaseReservationAtomic(userId: string, reservationId: string): Promise<void> {\r\n const credits = this.users.get(userId);\r\n if (!credits) {\r\n throw new Error(`User ${userId} not found`);\r\n }\r\n\r\n const reservation = await this.getReservation(userId, reservationId);\r\n if (!reservation) {\r\n throw new Error(`Reservation ${reservationId} not found`);\r\n }\r\n\r\n if (reservation.status !== \"reserved\") {\r\n // Already processed, no-op\r\n return;\r\n }\r\n\r\n // Release reserved credits\r\n credits.reserved -= reservation.amount;\r\n credits.updatedAt = new Date().toISOString();\r\n this.users.set(userId, credits);\r\n\r\n // Update reservation status\r\n await this.updateReservationStatus(userId, reservationId, \"released\", new Date());\r\n }\r\n\r\n async addCreditsAtomic(\r\n userId: string,\r\n amount: number,\r\n description: string,\r\n paymentRef?: string\r\n ): Promise<void> {\r\n const credits = this.users.get(userId);\r\n if (!credits) {\r\n throw new Error(`User ${userId} not found`);\r\n }\r\n\r\n const previousBalance = credits.bonusCredits;\r\n credits.bonusCredits += amount;\r\n credits.updatedAt = new Date().toISOString();\r\n this.users.set(userId, credits);\r\n\r\n // Create transaction\r\n await this.createTransaction({\r\n userId,\r\n type: \"purchase\",\r\n amount,\r\n description,\r\n paymentRef,\r\n previousBalance,\r\n newBalance: credits.bonusCredits,\r\n });\r\n }\r\n\r\n async deductCreditsAtomic(\r\n userId: string,\r\n amount: number\r\n ): Promise<{ previousBalance: number; newBalance: number }> {\r\n if (amount <= 0) {\r\n throw new Error(`deductCreditsAtomic amount must be positive (got ${amount})`);\r\n }\r\n\r\n const credits = this.users.get(userId);\r\n if (!credits) {\r\n throw new Error(`User credits not found for userId: ${userId}`);\r\n }\r\n\r\n const available = credits.balance + credits.bonusCredits - credits.reserved;\r\n if (available < amount) {\r\n throw new Error(\r\n `Insufficient credits. Available: ${available}, requested: ${amount}`\r\n );\r\n }\r\n\r\n // Drain balance first, then bonusCredits — same policy as commit.\r\n const balanceDeduction = Math.min(credits.balance, amount);\r\n const bonusDeduction = amount - balanceDeduction;\r\n\r\n const previousBalance = credits.balance + credits.bonusCredits;\r\n credits.balance -= balanceDeduction;\r\n credits.bonusCredits -= bonusDeduction;\r\n credits.updatedAt = new Date().toISOString();\r\n this.users.set(userId, credits);\r\n\r\n return { previousBalance, newBalance: previousBalance - amount };\r\n }\r\n\r\n // ==================== Transactions ====================\r\n\r\n async createTransaction(input: CreateTransactionInput): Promise<PortableTransaction> {\r\n const transaction: PortableTransaction = {\r\n id: generateId(),\r\n userId: input.userId,\r\n type: input.type,\r\n amount: input.amount,\r\n description: input.description,\r\n paymentRef: input.paymentRef,\r\n previousBalance: input.previousBalance,\r\n newBalance: input.newBalance,\r\n createdAt: new Date().toISOString(),\r\n };\r\n\r\n if (!this.transactions.has(input.userId)) {\r\n this.transactions.set(input.userId, []);\r\n }\r\n this.transactions.get(input.userId)!.push(transaction);\r\n\r\n return transaction;\r\n }\r\n\r\n async getTransactions(\r\n userId: string,\r\n limit = 50,\r\n offset = 0\r\n ): Promise<PortableTransaction[]> {\r\n const userTransactions = this.transactions.get(userId) ?? [];\r\n // Sort by createdAt descending (most recent first)\r\n const sorted = [...userTransactions].sort((a, b) => {\r\n const aDate = toDate(a.createdAt).getTime();\r\n const bDate = toDate(b.createdAt).getTime();\r\n return bDate - aDate;\r\n });\r\n return sorted.slice(offset, offset + limit);\r\n }\r\n\r\n // ==================== Usage Logs ====================\r\n\r\n async logUsage(input: CreateUsageLogInput): Promise<PortableUsageLog> {\r\n const log: PortableUsageLog = {\r\n id: generateId(),\r\n userId: input.userId,\r\n operationType: input.operationType,\r\n provider: input.provider,\r\n creditsUsed: input.creditsUsed,\r\n success: input.success,\r\n errorMessage: input.errorMessage,\r\n resourceId: input.resourceId,\r\n resourceType: input.resourceType,\r\n requestId: input.requestId,\r\n metadata: input.metadata,\r\n createdAt: new Date().toISOString(),\r\n };\r\n\r\n this.usageLogs.push(log);\r\n return log;\r\n }\r\n\r\n async getUsageLogs(query: UsageLogQuery): Promise<PortableUsageLog[]> {\r\n let results = [...this.usageLogs];\r\n\r\n // Apply filters\r\n if (query.userId) {\r\n results = results.filter((log) => log.userId === query.userId);\r\n }\r\n if (query.operationType) {\r\n results = results.filter((log) => log.operationType === query.operationType);\r\n }\r\n if (query.success !== undefined) {\r\n results = results.filter((log) => log.success === query.success);\r\n }\r\n if (query.startDate) {\r\n const startTime = query.startDate.getTime();\r\n results = results.filter(\r\n (log) => toDate(log.createdAt).getTime() >= startTime\r\n );\r\n }\r\n if (query.endDate) {\r\n const endTime = query.endDate.getTime();\r\n results = results.filter(\r\n (log) => toDate(log.createdAt).getTime() <= endTime\r\n );\r\n }\r\n\r\n // Sort by createdAt descending\r\n results.sort((a, b) => {\r\n return toDate(b.createdAt).getTime() - toDate(a.createdAt).getTime();\r\n });\r\n\r\n // Apply pagination\r\n const offset = query.offset ?? 0;\r\n const limit = query.limit ?? 50;\r\n return results.slice(offset, offset + limit);\r\n }\r\n\r\n async getUsageLogsCount(\r\n query: Omit<UsageLogQuery, \"limit\" | \"offset\">\r\n ): Promise<number> {\r\n const results = await this.getUsageLogs({ ...query, limit: Infinity, offset: 0 });\r\n return results.length;\r\n }\r\n\r\n // ==================== Journal Entries ====================\r\n\r\n async createJournalEntry(input: CreateJournalEntryInput): Promise<PortableJournalEntry> {\r\n const entry: PortableJournalEntry = {\r\n id: generateId(),\r\n userId: input.userId,\r\n entryType: input.entryType,\r\n amount: input.amount,\r\n balanceAfter: input.balanceAfter,\r\n source: input.source,\r\n referenceId: input.referenceId,\r\n referenceType: input.referenceType,\r\n description: input.description,\r\n metadata: input.metadata,\r\n createdAt: new Date().toISOString(),\r\n };\r\n\r\n if (!this.journalEntries.has(input.userId)) {\r\n this.journalEntries.set(input.userId, []);\r\n }\r\n this.journalEntries.get(input.userId)!.push(entry);\r\n\r\n return entry;\r\n }\r\n\r\n async getJournalEntries(query: JournalEntryQuery): Promise<PortableJournalEntry[]> {\r\n let results = this.journalEntries.get(query.userId) ?? [];\r\n\r\n // Apply filters\r\n if (query.source) {\r\n results = results.filter((entry) => entry.source === query.source);\r\n }\r\n if (query.referenceType) {\r\n results = results.filter((entry) => entry.referenceType === query.referenceType);\r\n }\r\n if (query.startDate) {\r\n const startTime = query.startDate.getTime();\r\n results = results.filter(\r\n (entry) => toDate(entry.createdAt).getTime() >= startTime\r\n );\r\n }\r\n if (query.endDate) {\r\n const endTime = query.endDate.getTime();\r\n results = results.filter(\r\n (entry) => toDate(entry.createdAt).getTime() <= endTime\r\n );\r\n }\r\n\r\n // Sort by createdAt descending\r\n results = [...results].sort((a, b) => {\r\n return toDate(b.createdAt).getTime() - toDate(a.createdAt).getTime();\r\n });\r\n\r\n // Apply pagination\r\n const offset = query.offset ?? 0;\r\n const limit = query.limit ?? 50;\r\n return results.slice(offset, offset + limit);\r\n }\r\n\r\n async getJournalEntriesCount(\r\n query: Omit<JournalEntryQuery, \"limit\" | \"offset\">\r\n ): Promise<number> {\r\n const results = await this.getJournalEntries({\r\n ...query,\r\n limit: Infinity,\r\n offset: 0,\r\n });\r\n return results.length;\r\n }\r\n\r\n // ==================== Cleanup Operations ====================\r\n\r\n async findAndExpireReservations(\r\n _batchSize = 100,\r\n _maxIterations = 100\r\n ): Promise<{\r\n expiredCount: number;\r\n creditsReleased: number;\r\n errors: string[];\r\n }> {\r\n const now = new Date();\r\n let expiredCount = 0;\r\n let creditsReleased = 0;\r\n const errors: string[] = [];\r\n\r\n for (const [userId, userReservations] of this.reservations) {\r\n for (const [reservationId, reservation] of userReservations) {\r\n if (\r\n reservation.status === \"reserved\" &&\r\n toDate(reservation.expiresAt).getTime() < now.getTime()\r\n ) {\r\n try {\r\n // Release the reservation\r\n await this.releaseReservationAtomic(userId, reservationId);\r\n // Mark as expired instead of released\r\n reservation.status = \"expired\";\r\n userReservations.set(reservationId, reservation);\r\n\r\n expiredCount++;\r\n creditsReleased += reservation.amount;\r\n } catch (error) {\r\n errors.push(\r\n `Failed to expire reservation ${reservationId}: ${error}`\r\n );\r\n }\r\n }\r\n }\r\n }\r\n\r\n return { expiredCount, creditsReleased, errors };\r\n }\r\n\r\n // ==================== Atomic Monthly Reset ====================\r\n\r\n async atomicMonthlyReset(\r\n userId: string,\r\n tier: SubscriptionTier,\r\n expectedResetAt: Date | string\r\n ): Promise<MonthlyResetResult> {\r\n const credits = this.users.get(userId);\r\n if (!credits) {\r\n throw new Error(`User ${userId} not found`);\r\n }\r\n\r\n // Optimistic locking: check if expectedResetAt matches current value\r\n const currentResetAt = toDate(credits.monthlyResetAt).getTime();\r\n const expected = toDate(expectedResetAt).getTime();\r\n\r\n if (currentResetAt !== expected) {\r\n // Another request already performed the reset\r\n return { wasReset: false, credits };\r\n }\r\n\r\n // Perform the reset\r\n const newBalance = getConfigMonthlyLimit(tier);\r\n const nextReset = getNextMonthlyReset();\r\n\r\n credits.balance = newBalance === Infinity ? credits.balance : newBalance;\r\n credits.monthlyUsed = 0;\r\n credits.monthlyResetAt = nextReset.toISOString();\r\n credits.updatedAt = new Date().toISOString();\r\n\r\n this.users.set(userId, credits);\r\n\r\n return { wasReset: true, credits };\r\n }\r\n\r\n // ==================== Subscription Expiry ====================\r\n\r\n async checkAndHandleSubscriptionExpiry(\r\n userId: string,\r\n gracePeriodDays = 3\r\n ): Promise<SubscriptionExpiryResult> {\r\n const credits = this.users.get(userId);\r\n if (!credits) {\r\n throw new Error(`User ${userId} not found`);\r\n }\r\n\r\n // Free tier doesn't expire\r\n if (isFreeTier(credits.tier) || !credits.subscriptionExpiresAt) {\r\n return {\r\n wasDowngraded: false,\r\n inGracePeriod: false,\r\n graceDaysRemaining: 0,\r\n credits,\r\n };\r\n }\r\n\r\n const now = new Date();\r\n const expiresAt = toDate(credits.subscriptionExpiresAt);\r\n const daysSinceExpiry =\r\n (now.getTime() - expiresAt.getTime()) / (1000 * 60 * 60 * 24);\r\n\r\n if (daysSinceExpiry <= 0) {\r\n // Not expired yet\r\n return {\r\n wasDowngraded: false,\r\n inGracePeriod: false,\r\n graceDaysRemaining: 0,\r\n credits,\r\n };\r\n }\r\n\r\n if (daysSinceExpiry <= gracePeriodDays) {\r\n // In grace period\r\n return {\r\n wasDowngraded: false,\r\n inGracePeriod: true,\r\n graceDaysRemaining: Math.ceil(gracePeriodDays - daysSinceExpiry),\r\n credits,\r\n };\r\n }\r\n\r\n // Grace period expired - downgrade to the default (free) tier\r\n const defaultTier = getDefaultTier();\r\n const defaultTierConfig = getConfigTierConfig(defaultTier);\r\n\r\n credits.tier = defaultTier;\r\n credits.monthlyLimit = defaultTierConfig.monthlyCredits;\r\n credits.balance = Math.min(credits.balance, defaultTierConfig.monthlyCredits);\r\n credits.subscriptionExpiresAt = null;\r\n credits.updatedAt = new Date().toISOString();\r\n\r\n this.users.set(userId, credits);\r\n\r\n return {\r\n wasDowngraded: true,\r\n inGracePeriod: false,\r\n graceDaysRemaining: 0,\r\n credits,\r\n };\r\n }\r\n\r\n // ==================== Testing Utilities ====================\r\n\r\n /**\r\n * Clear all data (useful for testing)\r\n */\r\n clear(): void {\r\n this.users.clear();\r\n this.reservations.clear();\r\n this.transactions.clear();\r\n this.usageLogs = [];\r\n this.journalEntries.clear();\r\n }\r\n\r\n /**\r\n * Get all users (useful for testing/debugging)\r\n */\r\n getAllUsers(): PortableUserCredits[] {\r\n return Array.from(this.users.values());\r\n }\r\n\r\n /**\r\n * Get all reservations for a user (useful for testing)\r\n */\r\n getAllReservations(userId: string): PortableReservation[] {\r\n const userReservations = this.reservations.get(userId);\r\n if (!userReservations) return [];\r\n return Array.from(userReservations.values());\r\n }\r\n}\r\n\r\n/**\r\n * Create a new in-memory repository instance\r\n * Each call creates a fresh, isolated instance\r\n */\r\nexport function createInMemoryC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AG;AAExB,aAAO;AAAA,QACL,eAAe;AAAA,QACf,eAAe;AAAA,QACf,oBAAoB;AAAA,QACpB;AAAA,MACF;AAAA,IACF;AAEA,QAAI,mBAAmB,iBAAiB;AAEtC,aAAO;AAAA,QACL,eAAe;AAAA,QACf,eAAe;AAAA,QACf,oBAAoB,KAAK,KAAK,kBAAkB,eAAe;AAAA,QAC/D;AAAA,MACF;AAAA,IACF;AAGA,UAAM,cAAc,eAAe;AACnC,UAAM,oBAAoB,oBAAoB,WAAW;AAEzD,YAAQ,OAAO;AACf,YAAQ,eAAe,kBAAkB;AACzC,YAAQ,UAAU,KAAK,IAAI,QAAQ,SAAS,kBAAkB,cAAc;AAC5E,YAAQ,wBAAwB;AAChC,YAAQ,aAAY,oBAAI,KAAK,GAAE,YAAY;AAE3C,SAAK,MAAM,IAAI,QAAQ,OAAO;AAE9B,WAAO;AAAA,MACL,eAAe;AAAA,MACf,eAAe;AAAA,MACf,oBAAoB;AAAA,MACpB;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,QAAc;AACZ,SAAK,MAAM,MAAM;AACjB,SAAK,aAAa,MAAM;AACxB,SAAK,aAAa,MAAM;AACxB,SAAK,YAAY,CAAC;AAClB,SAAK,eAAe,MAAM;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA,EAKA,cAAqC;AACnC,WAAO,MAAM,KAAK,KAAK,MAAM,OAAO,CAAC;AAAA,EACvC;AAAA;AAAA;AAAA;AAAA,EAKA,mBAAmB,QAAuC;AACxD,UAAM,mBAAmB,KAAK,aAAa,IAAI,MAAM;AACrD,QAAI,CAAC,iBAAkB,QAAO,CAAC;AAC/B,WAAO,MAAM,KAAK,iBAAiB,OAAO,CAAC;AAAA,EAC7C;AACF;AAMO,SAAS,iCAA2D;AACzE,SAAO,IAAI,yBAAyB;AACtC;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/repository/types.ts","../../src/repository/utils.ts","../../src/config/index.ts","../../src/config/provider.ts","../../src/repository/memory/index.ts"],"sourcesContent":["import type {\n PortableUserCredits,\n PortableReservation,\n PortableTransaction,\n PortableUsageLog,\n PortableJournalEntry,\n CreditOperationType,\n SubscriptionTier,\n ReservationStatus,\n AIProviderType,\n MonthlyResetResult,\n SubscriptionExpiryResult,\n CreditSource,\n JournalReferenceType,\n} from \"../core/types.js\";\n\n/**\n * Input for creating a credit reservation\n */\nexport interface CreateReservationInput {\n userId: string;\n amount: number;\n operationType: CreditOperationType;\n expiresAt: Date;\n}\n\n/**\n * Input for creating a credit transaction\n */\nexport interface CreateTransactionInput {\n userId: string;\n type: PortableTransaction[\"type\"];\n amount: number;\n description: string;\n paymentRef?: string;\n previousBalance: number;\n newBalance: number;\n}\n\n/**\n * Input for logging usage\n */\nexport interface CreateUsageLogInput {\n userId: string;\n operationType: CreditOperationType;\n provider: AIProviderType;\n creditsUsed: number;\n success: boolean;\n errorMessage?: string;\n resourceId?: string;\n resourceType?: string;\n requestId?: string;\n metadata?: Record<string, unknown>;\n}\n\n/**\n * Query options for usage logs\n */\nexport interface UsageLogQuery {\n userId?: string;\n operationType?: CreditOperationType;\n success?: boolean;\n limit?: number;\n offset?: number;\n startDate?: Date;\n endDate?: Date;\n}\n\n/**\n * Input for creating a journal entry\n */\nexport interface CreateJournalEntryInput {\n userId: string;\n entryType: \"debit\" | \"credit\";\n amount: number;\n balanceAfter: number;\n source: CreditSource;\n referenceId: string;\n referenceType: JournalReferenceType;\n description: string;\n metadata?: Record<string, unknown>;\n}\n\n/**\n * Query options for journal entries\n */\nexport interface JournalEntryQuery {\n userId: string;\n source?: CreditSource;\n referenceType?: JournalReferenceType;\n limit?: number;\n offset?: number;\n startDate?: Date;\n endDate?: Date;\n}\n\n/**\n * Partial update for user credits balance\n */\nexport interface CreditBalanceUpdate {\n balance?: number;\n bonusCredits?: number;\n reserved?: number;\n tier?: SubscriptionTier;\n monthlyLimit?: number;\n monthlyUsed?: number;\n monthlyResetAt?: Date | string;\n subscriptionExpiresAt?: Date | string | null;\n /** Increment balance by this amount (alternative to absolute value) */\n balanceIncrement?: number;\n /** Increment bonusCredits by this amount (alternative to absolute value) */\n bonusCreditsIncrement?: number;\n /** Increment reserved by this amount (alternative to absolute value) */\n reservedIncrement?: number;\n /** Increment monthlyUsed by this amount */\n monthlyUsedIncrement?: number;\n}\n\n/**\n * Input for tier update\n */\nexport interface TierUpdateInput {\n tier: SubscriptionTier;\n monthlyLimit: number;\n balance?: number;\n monthlyUsed?: number;\n subscriptionExpiresAt?: Date | string | null;\n}\n\n/**\n * Optional journal customization for `addCreditsAtomic`.\n *\n * By default a credit add records a journal entry with `source: \"purchase\"`,\n * `referenceType: \"transaction\"`, and `{ paymentRef }` metadata. These options\n * let callers override that — e.g. tag a signup grant as `source: \"bonus\"`, or\n * carry revenue-attribution fields (gross amount, credits granted) so a single\n * journal can serve as the app's revenue ledger.\n */\nexport interface AddCreditsAtomicOptions {\n /** Journal entry source (defaults to `\"purchase\"`). */\n source?: CreditSource;\n /** Journal reference type (defaults to `\"transaction\"`). */\n referenceType?: JournalReferenceType;\n /**\n * Extra metadata merged onto the journal entry. When `paymentRef` is present\n * it is merged in too (explicit keys here win on collision).\n */\n metadata?: Record<string, unknown>;\n}\n\n/**\n * Repository interface for credits database operations\n *\n * Implementations can use any database (Firestore, PostgreSQL, etc.)\n * All methods should handle their own error handling and transactions\n */\nexport interface ICreditRepository {\n // ==================== User Credits ====================\n\n /**\n * Get user credits balance\n * @param userId - User ID\n * @returns User credits or null if not found\n */\n getUserCredits(userId: string): Promise<PortableUserCredits | null>;\n\n /**\n * Initialize credits for a new user\n * @param userId - User ID\n * @param tier - Initial subscription tier\n * @param initialBalance - Initial credit balance\n * @returns Initialized user credits\n */\n initializeUserCredits(\n userId: string,\n tier: SubscriptionTier,\n initialBalance: number\n ): Promise<PortableUserCredits>;\n\n /**\n * Update user credits balance\n * @param userId - User ID\n * @param updates - Partial updates to apply\n */\n updateUserCredits(userId: string, updates: CreditBalanceUpdate): Promise<void>;\n\n /**\n * Update user subscription tier\n * @param userId - User ID\n * @param input - Tier update data\n */\n updateUserTier(userId: string, input: TierUpdateInput): Promise<void>;\n\n // ==================== Reservations ====================\n\n /**\n * Create a credit reservation (phase 1 of two-phase commit)\n * @param input - Reservation data\n * @returns Created reservation\n */\n createReservation(input: CreateReservationInput): Promise<PortableReservation>;\n\n /**\n * Get a reservation by ID\n * @param userId - User ID\n * @param reservationId - Reservation ID\n * @returns Reservation or null\n */\n getReservation(userId: string, reservationId: string): Promise<PortableReservation | null>;\n\n /**\n * Update reservation status\n * @param userId - User ID\n * @param reservationId - Reservation ID\n * @param status - New status\n * @param completedAt - Completion timestamp\n */\n updateReservationStatus(\n userId: string,\n reservationId: string,\n status: ReservationStatus,\n completedAt?: Date\n ): Promise<void>;\n\n // ==================== Atomic Operations ====================\n\n /**\n * Reserve credits atomically (creates reservation + updates balance in transaction)\n * @param userId - User ID\n * @param amount - Credits to reserve\n * @param operationType - Operation type for tracking\n * @param expiresAt - Reservation expiry time\n * @returns Created reservation\n * @throws Error if insufficient credits\n */\n reserveCreditsAtomic(\n userId: string,\n amount: number,\n operationType: CreditOperationType,\n expiresAt: Date\n ): Promise<PortableReservation>;\n\n /**\n * Commit a reservation atomically (deducts credits + marks reservation committed)\n * @param userId - User ID\n * @param reservationId - Reservation ID\n * @throws Error if reservation not found or not in reserved state\n */\n commitReservationAtomic(userId: string, reservationId: string): Promise<void>;\n\n /**\n * Release a reservation atomically (releases reserved credits + marks reservation released)\n * @param userId - User ID\n * @param reservationId - Reservation ID\n */\n releaseReservationAtomic(userId: string, reservationId: string): Promise<void>;\n\n /**\n * Add credits atomically (creates transaction + updates balance)\n * @param userId - User ID\n * @param amount - Credits to add\n * @param description - Transaction description\n * @param paymentRef - Optional payment reference\n * @param options - Optional journal source / reference type / extra metadata\n * for the credit journal entry (e.g. revenue attribution fields). Lets\n * callers record a purchase's gross amount, a bonus/refund source, etc.,\n * instead of the default `source: \"purchase\"` + `{ paymentRef }` metadata.\n */\n addCreditsAtomic(\n userId: string,\n amount: number,\n description: string,\n paymentRef?: string,\n options?: AddCreditsAtomicOptions\n ): Promise<void>;\n\n /**\n * Deduct credits from a user's balance atomically.\n *\n * Splits the deduction across `balance` and `bonusCredits`, draining\n * `balance` (monthly, resets each cycle) first so persistent bonus\n * credits survive longer. Runs in a single atomic transaction so\n * concurrent deducts cannot drive either field negative.\n *\n * Callers are responsible for writing any audit record (journal /\n * transaction) — this method only moves credits.\n *\n * @param userId - User ID\n * @param amount - Credits to deduct (positive)\n * @returns Combined totals (balance + bonusCredits) before and after.\n * @throws If user has no credits doc or insufficient credits\n */\n deductCreditsAtomic(\n userId: string,\n amount: number\n ): Promise<{ previousBalance: number; newBalance: number }>;\n\n // ==================== Transactions ====================\n\n /**\n * Create a credit transaction record\n * @param input - Transaction data\n * @returns Created transaction\n */\n createTransaction(input: CreateTransactionInput): Promise<PortableTransaction>;\n\n /**\n * Get user's transaction history\n * @param userId - User ID\n * @param limit - Max results\n * @param offset - Skip results\n * @returns List of transactions\n */\n getTransactions(\n userId: string,\n limit?: number,\n offset?: number\n ): Promise<PortableTransaction[]>;\n\n // ==================== Usage Logs ====================\n\n /**\n * Log a usage event\n * @param input - Usage log data\n * @returns Created usage log\n */\n logUsage(input: CreateUsageLogInput): Promise<PortableUsageLog>;\n\n /**\n * Query usage logs\n * @param query - Query parameters\n * @returns List of usage logs\n */\n getUsageLogs(query: UsageLogQuery): Promise<PortableUsageLog[]>;\n\n /**\n * Get usage log count (for pagination)\n * @param query - Query parameters (without limit/offset)\n * @returns Count of matching logs\n */\n getUsageLogsCount(query: Omit<UsageLogQuery, \"limit\" | \"offset\">): Promise<number>;\n\n // ==================== Cleanup Operations ====================\n\n /**\n * Find and expire reservations past their expiration time\n * Used by cron job to clean up stale reservations\n * @param batchSize - Maximum number of reservations to process per batch (default: 100)\n * @param maxIterations - Maximum number of pagination iterations to prevent infinite loops (default: 100)\n * @returns Cleanup results with counts and errors\n */\n findAndExpireReservations(batchSize?: number, maxIterations?: number): Promise<{\n expiredCount: number;\n creditsReleased: number;\n errors: string[];\n }>;\n\n // ==================== Atomic Monthly Reset ====================\n\n /**\n * Atomically perform monthly reset if needed\n * Uses optimistic locking to prevent race conditions\n *\n * @param userId - User ID\n * @param tier - User's current subscription tier (for determining new balance)\n * @param expectedResetAt - The expected monthlyResetAt value (for optimistic locking)\n * @returns Result indicating whether reset was performed and updated credits\n */\n atomicMonthlyReset(\n userId: string,\n tier: SubscriptionTier,\n expectedResetAt: Date | string\n ): Promise<MonthlyResetResult>;\n\n // ==================== Subscription Expiry ====================\n\n /**\n * Check and handle subscription expiry with grace period\n * Auto-downgrades expired subscriptions after grace period\n *\n * @param userId - User ID\n * @param gracePeriodDays - Days to allow after expiry before downgrade (default: 3)\n * @returns Result indicating whether downgrade occurred\n */\n checkAndHandleSubscriptionExpiry(\n userId: string,\n gracePeriodDays?: number\n ): Promise<SubscriptionExpiryResult>;\n\n // ==================== Journal Entries ====================\n\n /**\n * Create a journal entry for audit trail\n * @param input - Journal entry data\n * @returns Created journal entry\n */\n createJournalEntry(input: CreateJournalEntryInput): Promise<PortableJournalEntry>;\n\n /**\n * Get journal entries for a user\n * @param query - Query parameters\n * @returns List of journal entries\n */\n getJournalEntries(query: JournalEntryQuery): Promise<PortableJournalEntry[]>;\n\n /**\n * Get journal entry count for pagination\n * @param query - Query parameters (without limit/offset)\n * @returns Count of matching entries\n */\n getJournalEntriesCount(query: Omit<JournalEntryQuery, \"limit\" | \"offset\">): Promise<number>;\n}\n\n/**\n * Factory type for creating repository instances\n */\nexport type CreditRepositoryFactory = () => ICreditRepository;\n\n/**\n * Convert PortableUserCredits to client-safe format\n * Utility function that implementations can use\n */\nexport function toClientUserCredits(credits: PortableUserCredits): PortableUserCredits {\n // Already in portable format, just ensure all timestamps are ISO strings\n return {\n userId: credits.userId,\n balance: credits.balance,\n bonusCredits: credits.bonusCredits ?? 0,\n reserved: credits.reserved,\n tier: credits.tier,\n monthlyLimit: credits.monthlyLimit,\n monthlyUsed: credits.monthlyUsed,\n monthlyResetAt: toISOString(credits.monthlyResetAt),\n subscriptionExpiresAt: credits.subscriptionExpiresAt\n ? toISOString(credits.subscriptionExpiresAt)\n : null,\n createdAt: toISOString(credits.createdAt),\n updatedAt: toISOString(credits.updatedAt),\n };\n}\n\n/**\n * Convert any timestamp-like value to ISO string\n */\nfunction toISOString(value: unknown): string {\n if (!value) return new Date().toISOString();\n if (typeof value === \"string\") return value;\n if (value instanceof Date) return value.toISOString();\n // Handle Firestore Timestamp-like objects\n if (typeof value === \"object\" && \"toDate\" in value && typeof (value as { toDate: () => Date }).toDate === \"function\") {\n return (value as { toDate: () => Date }).toDate().toISOString();\n }\n return new Date().toISOString();\n}\n","/**\n * Shared utilities for credit repository implementations\n *\n * These are pure functions with no server-only dependencies,\n * making them safe to use in any repository implementation.\n */\n\n/**\n * Generate a unique ID\n * Uses timestamp + random string for uniqueness\n */\nexport function generateId(): string {\n return `${Date.now()}-${Math.random().toString(36).substring(2, 11)}`;\n}\n\n/**\n * Convert date-like value to Date object\n * Handles: Date, string, Firestore Timestamp\n */\nexport function toDate(value: Date | string | unknown): Date {\n if (value instanceof Date) return value;\n if (typeof value === \"string\") return new Date(value);\n // Handle Firestore Timestamp\n if (typeof value === \"object\" && value !== null && \"toDate\" in value) {\n return (value as { toDate: () => Date }).toDate();\n }\n return new Date();\n}\n\n/**\n * Get the first day of next month\n * Used for calculating monthly reset dates\n */\nexport function getNextMonthlyReset(from: Date = new Date()): Date {\n const next = new Date(from);\n next.setMonth(next.getMonth() + 1);\n next.setDate(1);\n next.setHours(0, 0, 0, 0);\n return next;\n}\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.string().min(1),\n monthlyCredits: z.number().min(0),\n priceUsd: z.number().min(0),\n features: z.array(z.string()),\n isFree: z.boolean().optional(),\n unlimited: z.boolean().optional(),\n isDefault: z.boolean().optional(),\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.string().min(1),\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 isFree: true,\n isDefault: true,\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 unlimited: true,\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 all valid tier ids from configuration\n * Returns the keys of the tierConfigs object\n */\nexport function getValidTiers(): string[] {\n return Object.keys(getConfig().tierConfigs);\n}\n\n/**\n * Check if a tier id is valid (configured in the system)\n */\nexport function isValidTier(tier: string): boolean {\n return tier in getConfig().tierConfigs;\n}\n\n/**\n * Check if a tier is the free/default tier.\n * Falls back to priceUsd === 0 when the isFree flag is not set.\n */\nexport function isFreeTier(tier: SubscriptionTier): boolean {\n const c = getConfig().tierConfigs[tier];\n if (!c) return false;\n return c.isFree ?? c.priceUsd === 0;\n}\n\n/**\n * Check if a tier is unlimited.\n * Falls back to monthlyCredits === 0 when the unlimited flag is not set.\n */\nexport function isUnlimitedTier(tier: SubscriptionTier): boolean {\n const c = getConfig().tierConfigs[tier];\n if (!c) return false;\n return c.unlimited ?? c.monthlyCredits === 0;\n}\n\n/**\n * Get the tier assigned to brand-new users.\n * Prefers the tier flagged isDefault, then the first isFree tier, then \"free\".\n */\nexport function getDefaultTier(): SubscriptionTier {\n const entries = Object.entries(getConfig().tierConfigs);\n const def =\n entries.find(([, c]) => c.isDefault)?.[0] ??\n entries.find(([, c]) => c.isFree ?? c.priceUsd === 0)?.[0];\n return def ?? \"free\";\n}\n\n/**\n * Magic balance assigned to unlimited tiers on upgrade.\n * Kept at 999999 for back-compat with stored/displayed balances.\n */\nexport const UNLIMITED_BALANCE_SENTINEL = 999999;\n\n/**\n * Get the magic balance assigned to unlimited tiers on upgrade.\n */\nexport function getUnlimitedSentinelBalance(): number {\n return UNLIMITED_BALANCE_SENTINEL;\n}\n\n/**\n * Get tier configuration from configuration.\n * Falls back to the default tier (with a warning) if the tier is unknown.\n */\nexport function getConfigTierConfig(tier: SubscriptionTier): TierConfig {\n const cfg = getConfig().tierConfigs[tier];\n if (cfg) return cfg;\n const fallback = getDefaultTier();\n console.warn(\n `[Credits Config] Unknown tier \"${tier}\". Falling back to \"${fallback}\". Valid: ${getValidTiers().join(\", \")}`\n );\n return getConfig().tierConfigs[fallback]!;\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 return isUnlimitedTier(tier) ? Infinity : getConfigTierConfig(tier).monthlyCredits;\n}\n\n/**\n * Runtime zod validator for tier ids. Validates against the LIVE config keys\n * at parse time, so apps that add tiers via config get correct validation.\n */\nexport const tierSchema = z\n .string()\n .refine(isValidTier, { message: \"Unknown subscription tier\" }) as unknown as z.ZodType<SubscriptionTier>;\n\n/**\n * Parse/validate a tier id against the live config keys.\n * @throws ZodError if the tier is not configured\n */\nexport function parseTier(value: unknown): SubscriptionTier {\n return tierSchema.parse(value);\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 * In-Memory Credit Repository Implementation\n *\n * A database-agnostic implementation of ICreditRepository for testing and prototyping.\n * All data is stored in memory and lost when the process restarts.\n *\n * Usage:\n * - Unit tests without database dependency\n * - Local development and prototyping\n * - Reference implementation for custom repositories\n */\n\nimport type {\n PortableUserCredits,\n PortableReservation,\n PortableTransaction,\n PortableJournalEntry,\n PortableUsageLog,\n SubscriptionTier,\n ReservationStatus,\n MonthlyResetResult,\n SubscriptionExpiryResult,\n} from \"../../core/types.js\";\nimport type {\n ICreditRepository,\n CreateReservationInput,\n CreateTransactionInput,\n CreateUsageLogInput,\n CreateJournalEntryInput,\n UsageLogQuery,\n JournalEntryQuery,\n CreditBalanceUpdate,\n TierUpdateInput,\n AddCreditsAtomicOptions,\n} from \"../types.js\";\nimport { generateId, toDate, getNextMonthlyReset } from \"../utils.js\";\nimport {\n getConfigMonthlyLimit,\n getConfigTierConfig,\n getDefaultTier,\n isFreeTier,\n} from \"../../config/index.js\";\n\n/**\n * In-Memory implementation of ICreditRepository\n *\n * Implements all repository methods using Map-based storage.\n * Useful for testing and as a reference implementation.\n */\nexport class InMemoryCreditRepository implements ICreditRepository {\n private users = new Map<string, PortableUserCredits>();\n private reservations = new Map<string, Map<string, PortableReservation>>();\n private transactions = new Map<string, PortableTransaction[]>();\n private usageLogs: PortableUsageLog[] = [];\n private journalEntries = new Map<string, PortableJournalEntry[]>();\n\n // ==================== User Credits ====================\n\n async getUserCredits(userId: string): Promise<PortableUserCredits | null> {\n return this.users.get(userId) ?? null;\n }\n\n async initializeUserCredits(\n userId: string,\n tier: SubscriptionTier,\n initialBalance: number\n ): Promise<PortableUserCredits> {\n const now = new Date().toISOString();\n const credits: PortableUserCredits = {\n userId,\n balance: initialBalance,\n bonusCredits: 0,\n reserved: 0,\n tier,\n monthlyLimit: getConfigMonthlyLimit(tier),\n monthlyUsed: 0,\n monthlyResetAt: getNextMonthlyReset().toISOString(),\n subscriptionExpiresAt: null,\n createdAt: now,\n updatedAt: now,\n };\n this.users.set(userId, credits);\n return credits;\n }\n\n async updateUserCredits(userId: string, updates: CreditBalanceUpdate): Promise<void> {\n const credits = this.users.get(userId);\n if (!credits) {\n throw new Error(`User ${userId} not found`);\n }\n\n const now = new Date().toISOString();\n\n // Apply absolute updates\n if (updates.balance !== undefined) credits.balance = updates.balance;\n if (updates.bonusCredits !== undefined) credits.bonusCredits = updates.bonusCredits;\n if (updates.reserved !== undefined) credits.reserved = updates.reserved;\n if (updates.tier !== undefined) credits.tier = updates.tier;\n if (updates.monthlyLimit !== undefined) credits.monthlyLimit = updates.monthlyLimit;\n if (updates.monthlyUsed !== undefined) credits.monthlyUsed = updates.monthlyUsed;\n if (updates.monthlyResetAt !== undefined) {\n credits.monthlyResetAt = updates.monthlyResetAt instanceof Date\n ? updates.monthlyResetAt.toISOString()\n : updates.monthlyResetAt;\n }\n if (updates.subscriptionExpiresAt !== undefined) {\n if (updates.subscriptionExpiresAt === null) {\n credits.subscriptionExpiresAt = null;\n } else {\n credits.subscriptionExpiresAt = updates.subscriptionExpiresAt instanceof Date\n ? updates.subscriptionExpiresAt.toISOString()\n : updates.subscriptionExpiresAt;\n }\n }\n\n // Apply increments\n if (updates.balanceIncrement !== undefined) {\n credits.balance += updates.balanceIncrement;\n }\n if (updates.bonusCreditsIncrement !== undefined) {\n credits.bonusCredits += updates.bonusCreditsIncrement;\n }\n if (updates.reservedIncrement !== undefined) {\n credits.reserved += updates.reservedIncrement;\n }\n if (updates.monthlyUsedIncrement !== undefined) {\n credits.monthlyUsed += updates.monthlyUsedIncrement;\n }\n\n credits.updatedAt = now;\n this.users.set(userId, credits);\n }\n\n async updateUserTier(userId: string, input: TierUpdateInput): Promise<void> {\n const credits = this.users.get(userId);\n if (!credits) {\n throw new Error(`User ${userId} not found`);\n }\n\n credits.tier = input.tier;\n credits.monthlyLimit = input.monthlyLimit;\n if (input.balance !== undefined) credits.balance = input.balance;\n if (input.monthlyUsed !== undefined) credits.monthlyUsed = input.monthlyUsed;\n if (input.subscriptionExpiresAt !== undefined) {\n if (input.subscriptionExpiresAt === null) {\n credits.subscriptionExpiresAt = null;\n } else {\n credits.subscriptionExpiresAt = input.subscriptionExpiresAt instanceof Date\n ? input.subscriptionExpiresAt.toISOString()\n : input.subscriptionExpiresAt;\n }\n }\n credits.updatedAt = new Date().toISOString();\n\n this.users.set(userId, credits);\n }\n\n // ==================== Reservations ====================\n\n async createReservation(input: CreateReservationInput): Promise<PortableReservation> {\n const now = new Date().toISOString();\n const reservation: PortableReservation = {\n id: generateId(),\n userId: input.userId,\n amount: input.amount,\n operationType: input.operationType,\n status: \"reserved\",\n createdAt: now,\n expiresAt: input.expiresAt.toISOString(),\n };\n\n if (!this.reservations.has(input.userId)) {\n this.reservations.set(input.userId, new Map());\n }\n this.reservations.get(input.userId)!.set(reservation.id, reservation);\n\n return reservation;\n }\n\n async getReservation(\n userId: string,\n reservationId: string\n ): Promise<PortableReservation | null> {\n const userReservations = this.reservations.get(userId);\n if (!userReservations) return null;\n return userReservations.get(reservationId) ?? null;\n }\n\n async updateReservationStatus(\n userId: string,\n reservationId: string,\n status: ReservationStatus,\n completedAt?: Date\n ): Promise<void> {\n const userReservations = this.reservations.get(userId);\n if (!userReservations) {\n throw new Error(`No reservations found for user ${userId}`);\n }\n\n const reservation = userReservations.get(reservationId);\n if (!reservation) {\n throw new Error(`Reservation ${reservationId} not found`);\n }\n\n reservation.status = status;\n if (completedAt) reservation.completedAt = completedAt.toISOString();\n\n userReservations.set(reservationId, reservation);\n }\n\n // ==================== Atomic Operations ====================\n\n async reserveCreditsAtomic(\n userId: string,\n amount: number,\n operationType: string,\n expiresAt: Date\n ): Promise<PortableReservation> {\n const credits = this.users.get(userId);\n if (!credits) {\n throw new Error(`User ${userId} not found`);\n }\n\n // Calculate available credits (balance + bonusCredits - reserved)\n const available = credits.balance + credits.bonusCredits - credits.reserved;\n if (available < amount) {\n throw new Error(\n `Insufficient credits. Available: ${available}, Required: ${amount}`\n );\n }\n\n // Create reservation\n const reservation = await this.createReservation({\n userId,\n amount,\n operationType,\n expiresAt,\n });\n\n // Update reserved amount\n credits.reserved += amount;\n credits.updatedAt = new Date().toISOString();\n this.users.set(userId, credits);\n\n return reservation;\n }\n\n async commitReservationAtomic(userId: string, reservationId: string): Promise<void> {\n const credits = this.users.get(userId);\n if (!credits) {\n throw new Error(`User ${userId} not found`);\n }\n\n const reservation = await this.getReservation(userId, reservationId);\n if (!reservation) {\n throw new Error(`Reservation ${reservationId} not found`);\n }\n\n // Idempotent: re-committing an already-committed reservation is a no-op\n // (retry-safe). Committing a released/expired one is a genuine conflict.\n if (reservation.status === \"committed\") {\n return;\n }\n\n if (reservation.status !== \"reserved\") {\n throw new Error(\n `Cannot commit reservation in ${reservation.status} state`\n );\n }\n\n const amount = reservation.amount;\n\n // Deduct from balance (bonus credits first logic can be added if needed)\n credits.balance -= amount;\n credits.reserved -= amount;\n credits.monthlyUsed += amount;\n credits.updatedAt = new Date().toISOString();\n this.users.set(userId, credits);\n\n // Update reservation status\n await this.updateReservationStatus(userId, reservationId, \"committed\", new Date());\n }\n\n async releaseReservationAtomic(userId: string, reservationId: string): Promise<void> {\n const credits = this.users.get(userId);\n if (!credits) {\n throw new Error(`User ${userId} not found`);\n }\n\n const reservation = await this.getReservation(userId, reservationId);\n if (!reservation) {\n throw new Error(`Reservation ${reservationId} not found`);\n }\n\n if (reservation.status !== \"reserved\") {\n // Already processed, no-op\n return;\n }\n\n // Release reserved credits\n credits.reserved -= reservation.amount;\n credits.updatedAt = new Date().toISOString();\n this.users.set(userId, credits);\n\n // Update reservation status\n await this.updateReservationStatus(userId, reservationId, \"released\", new Date());\n }\n\n async addCreditsAtomic(\n userId: string,\n amount: number,\n description: string,\n paymentRef?: string,\n options?: AddCreditsAtomicOptions\n ): Promise<void> {\n const credits = this.users.get(userId);\n if (!credits) {\n throw new Error(`User ${userId} not found`);\n }\n\n const previousTotal = credits.balance + credits.bonusCredits;\n credits.bonusCredits += amount;\n credits.updatedAt = new Date().toISOString();\n this.users.set(userId, credits);\n const newTotal = credits.balance + credits.bonusCredits;\n\n // Create transaction\n const transaction = await this.createTransaction({\n userId,\n type: \"purchase\",\n amount,\n description,\n paymentRef,\n previousBalance: previousTotal,\n newBalance: newTotal,\n });\n\n // Journal entry (parity with the Drizzle adapter): a single journal can then\n // serve as the app's credit ledger, including revenue-attribution metadata.\n const journalMetadata = {\n ...(paymentRef ? { paymentRef } : {}),\n ...(options?.metadata ?? {}),\n };\n await this.createJournalEntry({\n userId,\n entryType: \"credit\",\n amount,\n balanceAfter: newTotal,\n source: options?.source ?? \"purchase\",\n referenceId: transaction.id ?? paymentRef ?? \"unknown\",\n referenceType: options?.referenceType ?? \"transaction\",\n description,\n metadata: Object.keys(journalMetadata).length > 0 ? journalMetadata : undefined,\n });\n }\n\n async deductCreditsAtomic(\n userId: string,\n amount: number\n ): Promise<{ previousBalance: number; newBalance: number }> {\n if (amount <= 0) {\n throw new Error(`deductCreditsAtomic amount must be positive (got ${amount})`);\n }\n\n const credits = this.users.get(userId);\n if (!credits) {\n throw new Error(`User credits not found for userId: ${userId}`);\n }\n\n const available = credits.balance + credits.bonusCredits - credits.reserved;\n if (available < amount) {\n throw new Error(\n `Insufficient credits. Available: ${available}, requested: ${amount}`\n );\n }\n\n // Drain balance first, then bonusCredits — same policy as commit.\n const balanceDeduction = Math.min(credits.balance, amount);\n const bonusDeduction = amount - balanceDeduction;\n\n const previousBalance = credits.balance + credits.bonusCredits;\n credits.balance -= balanceDeduction;\n credits.bonusCredits -= bonusDeduction;\n credits.updatedAt = new Date().toISOString();\n this.users.set(userId, credits);\n\n return { previousBalance, newBalance: previousBalance - amount };\n }\n\n // ==================== Transactions ====================\n\n async createTransaction(input: CreateTransactionInput): Promise<PortableTransaction> {\n const transaction: PortableTransaction = {\n id: generateId(),\n userId: input.userId,\n type: input.type,\n amount: input.amount,\n description: input.description,\n paymentRef: input.paymentRef,\n previousBalance: input.previousBalance,\n newBalance: input.newBalance,\n createdAt: new Date().toISOString(),\n };\n\n if (!this.transactions.has(input.userId)) {\n this.transactions.set(input.userId, []);\n }\n this.transactions.get(input.userId)!.push(transaction);\n\n return transaction;\n }\n\n async getTransactions(\n userId: string,\n limit = 50,\n offset = 0\n ): Promise<PortableTransaction[]> {\n const userTransactions = this.transactions.get(userId) ?? [];\n // Sort by createdAt descending (most recent first)\n const sorted = [...userTransactions].sort((a, b) => {\n const aDate = toDate(a.createdAt).getTime();\n const bDate = toDate(b.createdAt).getTime();\n return bDate - aDate;\n });\n return sorted.slice(offset, offset + limit);\n }\n\n // ==================== Usage Logs ====================\n\n async logUsage(input: CreateUsageLogInput): Promise<PortableUsageLog> {\n const log: PortableUsageLog = {\n id: generateId(),\n userId: input.userId,\n operationType: input.operationType,\n provider: input.provider,\n creditsUsed: input.creditsUsed,\n success: input.success,\n errorMessage: input.errorMessage,\n resourceId: input.resourceId,\n resourceType: input.resourceType,\n requestId: input.requestId,\n metadata: input.metadata,\n createdAt: new Date().toISOString(),\n };\n\n this.usageLogs.push(log);\n return log;\n }\n\n async getUsageLogs(query: UsageLogQuery): Promise<PortableUsageLog[]> {\n let results = [...this.usageLogs];\n\n // Apply filters\n if (query.userId) {\n results = results.filter((log) => log.userId === query.userId);\n }\n if (query.operationType) {\n results = results.filter((log) => log.operationType === query.operationType);\n }\n if (query.success !== undefined) {\n results = results.filter((log) => log.success === query.success);\n }\n if (query.startDate) {\n const startTime = query.startDate.getTime();\n results = results.filter(\n (log) => toDate(log.createdAt).getTime() >= startTime\n );\n }\n if (query.endDate) {\n const endTime = query.endDate.getTime();\n results = results.filter(\n (log) => toDate(log.createdAt).getTime() <= endTime\n );\n }\n\n // Sort by createdAt descending\n results.sort((a, b) => {\n return toDate(b.createdAt).getTime() - toDate(a.createdAt).getTime();\n });\n\n // Apply pagination\n const offset = query.offset ?? 0;\n const limit = query.limit ?? 50;\n return results.slice(offset, offset + limit);\n }\n\n async getUsageLogsCount(\n query: Omit<UsageLogQuery, \"limit\" | \"offset\">\n ): Promise<number> {\n const results = await this.getUsageLogs({ ...query, limit: Infinity, offset: 0 });\n return results.length;\n }\n\n // ==================== Journal Entries ====================\n\n async createJournalEntry(input: CreateJournalEntryInput): Promise<PortableJournalEntry> {\n const entry: PortableJournalEntry = {\n id: generateId(),\n userId: input.userId,\n entryType: input.entryType,\n amount: input.amount,\n balanceAfter: input.balanceAfter,\n source: input.source,\n referenceId: input.referenceId,\n referenceType: input.referenceType,\n description: input.description,\n metadata: input.metadata,\n createdAt: new Date().toISOString(),\n };\n\n if (!this.journalEntries.has(input.userId)) {\n this.journalEntries.set(input.userId, []);\n }\n this.journalEntries.get(input.userId)!.push(entry);\n\n return entry;\n }\n\n async getJournalEntries(query: JournalEntryQuery): Promise<PortableJournalEntry[]> {\n let results = this.journalEntries.get(query.userId) ?? [];\n\n // Apply filters\n if (query.source) {\n results = results.filter((entry) => entry.source === query.source);\n }\n if (query.referenceType) {\n results = results.filter((entry) => entry.referenceType === query.referenceType);\n }\n if (query.startDate) {\n const startTime = query.startDate.getTime();\n results = results.filter(\n (entry) => toDate(entry.createdAt).getTime() >= startTime\n );\n }\n if (query.endDate) {\n const endTime = query.endDate.getTime();\n results = results.filter(\n (entry) => toDate(entry.createdAt).getTime() <= endTime\n );\n }\n\n // Sort by createdAt descending\n results = [...results].sort((a, b) => {\n return toDate(b.createdAt).getTime() - toDate(a.createdAt).getTime();\n });\n\n // Apply pagination\n const offset = query.offset ?? 0;\n const limit = query.limit ?? 50;\n return results.slice(offset, offset + limit);\n }\n\n async getJournalEntriesCount(\n query: Omit<JournalEntryQuery, \"limit\" | \"offset\">\n ): Promise<number> {\n const results = await this.getJournalEntries({\n ...query,\n limit: Infinity,\n offset: 0,\n });\n return results.length;\n }\n\n // ==================== Cleanup Operations ====================\n\n async findAndExpireReservations(\n _batchSize = 100,\n _maxIterations = 100\n ): Promise<{\n expiredCount: number;\n creditsReleased: number;\n errors: string[];\n }> {\n const now = new Date();\n let expiredCount = 0;\n let creditsReleased = 0;\n const errors: string[] = [];\n\n for (const [userId, userReservations] of this.reservations) {\n for (const [reservationId, reservation] of userReservations) {\n if (\n reservation.status === \"reserved\" &&\n toDate(reservation.expiresAt).getTime() < now.getTime()\n ) {\n try {\n // Release the reservation\n await this.releaseReservationAtomic(userId, reservationId);\n // Mark as expired instead of released\n reservation.status = \"expired\";\n userReservations.set(reservationId, reservation);\n\n expiredCount++;\n creditsReleased += reservation.amount;\n } catch (error) {\n errors.push(\n `Failed to expire reservation ${reservationId}: ${error}`\n );\n }\n }\n }\n }\n\n return { expiredCount, creditsReleased, errors };\n }\n\n // ==================== Atomic Monthly Reset ====================\n\n async atomicMonthlyReset(\n userId: string,\n tier: SubscriptionTier,\n expectedResetAt: Date | string\n ): Promise<MonthlyResetResult> {\n const credits = this.users.get(userId);\n if (!credits) {\n throw new Error(`User ${userId} not found`);\n }\n\n // Optimistic locking: check if expectedResetAt matches current value\n const currentResetAt = toDate(credits.monthlyResetAt).getTime();\n const expected = toDate(expectedResetAt).getTime();\n\n if (currentResetAt !== expected) {\n // Another request already performed the reset\n return { wasReset: false, credits };\n }\n\n // Perform the reset\n const newBalance = getConfigMonthlyLimit(tier);\n const nextReset = getNextMonthlyReset();\n\n credits.balance = newBalance === Infinity ? credits.balance : newBalance;\n credits.monthlyUsed = 0;\n credits.monthlyResetAt = nextReset.toISOString();\n credits.updatedAt = new Date().toISOString();\n\n this.users.set(userId, credits);\n\n return { wasReset: true, credits };\n }\n\n // ==================== Subscription Expiry ====================\n\n async checkAndHandleSubscriptionExpiry(\n userId: string,\n gracePeriodDays = 3\n ): Promise<SubscriptionExpiryResult> {\n const credits = this.users.get(userId);\n if (!credits) {\n throw new Error(`User ${userId} not found`);\n }\n\n // Free tier doesn't expire\n if (isFreeTier(credits.tier) || !credits.subscriptionExpiresAt) {\n return {\n wasDowngraded: false,\n inGracePeriod: false,\n graceDaysRemaining: 0,\n credits,\n };\n }\n\n const now = new Date();\n const expiresAt = toDate(credits.subscriptionExpiresAt);\n const daysSinceExpiry =\n (now.getTime() - expiresAt.getTime()) / (1000 * 60 * 60 * 24);\n\n if (daysSinceExpiry <= 0) {\n // Not expired yet\n return {\n wasDowngraded: false,\n inGracePeriod: false,\n graceDaysRemaining: 0,\n credits,\n };\n }\n\n if (daysSinceExpiry <= gracePeriodDays) {\n // In grace period\n return {\n wasDowngraded: false,\n inGracePeriod: true,\n graceDaysRemaining: Math.ceil(gracePeriodDays - daysSinceExpiry),\n credits,\n };\n }\n\n // Grace period expired - downgrade to the default (free) tier\n const defaultTier = getDefaultTier();\n const defaultTierConfig = getConfigTierConfig(defaultTier);\n\n credits.tier = defaultTier;\n credits.monthlyLimit = defaultTierConfig.monthlyCredits;\n credits.balance = Math.min(credits.balance, defaultTierConfig.monthlyCredits);\n credits.subscriptionExpiresAt = null;\n credits.updatedAt = new Date().toISOString();\n\n this.users.set(userId, credits);\n\n return {\n wasDowngraded: true,\n inGracePeriod: false,\n graceDaysRemaining: 0,\n credits,\n };\n }\n\n // ==================== Testing Utilities ====================\n\n /**\n * Clear all data (useful for testing)\n */\n clear(): void {\n this.users.clear();\n this.reservations.clear();\n this.transactions.clear();\n this.usageLogs = [];\n this.journalEntries.clear();\n }\n\n /**\n * Get all users (useful for testing/debugging)\n */\n getAllUsers(): PortableUserCredits[] {\n return Array.from(this.users.values());\n }\n\n /**\n * Get all reservations for a user (useful for testing)\n */\n getAllReservations(userId: string): PortableReservation[] {\n const userReservations = this.reservations.get(userId);\n if (!userReservations) return [];\n return Array.from(userReservations.values());\n }\n}\n\n/**\n * Create a new in-memory repository instance\n * Each call creates a fresh, isolated instance\n */\nexport function createInMemoryCreditRepository(): InMemoryCreditRepository {\n return new InMemoryCreditRepository();\n}\n"],"mappings":";AAsaO,SAAS,oBAAoB,SAAmD;AAErF,SAAO;AAAA,IACL,QAAQ,QAAQ;AAAA,IAChB,SAAS,QAAQ;AAAA,IACjB,cAAc,QAAQ,gBAAgB;AAAA,IACtC,UAAU,QAAQ;AAAA,IAClB,MAAM,QAAQ;AAAA,IACd,cAAc,QAAQ;AAAA,IACtB,aAAa,QAAQ;AAAA,IACrB,gBAAgB,YAAY,QAAQ,cAAc;AAAA,IAClD,uBAAuB,QAAQ,wBAC3B,YAAY,QAAQ,qBAAqB,IACzC;AAAA,IACJ,WAAW,YAAY,QAAQ,SAAS;AAAA,IACxC,WAAW,YAAY,QAAQ,SAAS;AAAA,EAC1C;AACF;AAKA,SAAS,YAAY,OAAwB;AAC3C,MAAI,CAAC,MAAO,SAAO,oBAAI,KAAK,GAAE,YAAY;AAC1C,MAAI,OAAO,UAAU,SAAU,QAAO;A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O;AAAA,QACL,eAAe;AAAA,QACf,eAAe;AAAA,QACf,oBAAoB,KAAK,KAAK,kBAAkB,eAAe;AAAA,QAC/D;AAAA,MACF;AAAA,IACF;AAGA,UAAM,cAAc,eAAe;AACnC,UAAM,oBAAoB,oBAAoB,WAAW;AAEzD,YAAQ,OAAO;AACf,YAAQ,eAAe,kBAAkB;AACzC,YAAQ,UAAU,KAAK,IAAI,QAAQ,SAAS,kBAAkB,cAAc;AAC5E,YAAQ,wBAAwB;AAChC,YAAQ,aAAY,oBAAI,KAAK,GAAE,YAAY;AAE3C,SAAK,MAAM,IAAI,QAAQ,OAAO;AAE9B,WAAO;AAAA,MACL,eAAe;AAAA,MACf,eAAe;AAAA,MACf,oBAAoB;AAAA,MACpB;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,QAAc;AACZ,SAAK,MAAM,MAAM;AACjB,SAAK,aAAa,MAAM;AACxB,SAAK,aAAa,MAAM;AACxB,SAAK,YAAY,CAAC;AAClB,SAAK,eAAe,MAAM;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA,EAKA,cAAqC;AACnC,WAAO,MAAM,KAAK,KAAK,MAAM,OAAO,CAAC;AAAA,EACvC;AAAA;AAAA;AAAA;AAAA,EAKA,mBAAmB,QAAuC;AACxD,UAAM,mBAAmB,KAAK,aAAa,IAAI,MAAM;AACrD,QAAI,CAAC,iBAAkB,QAAO,CAAC;AAC/B,WAAO,MAAM,KAAK,iBAAiB,OAAO,CAAC;AAAA,EAC7C;AACF;AAMO,SAAS,iCAA2D;AACzE,SAAO,IAAI,yBAAyB;AACtC;","names":[]}
|
package/dist/sdk/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/sdk/index.ts","../../src/sdk/types.ts","../../src/sdk/errors.ts","../../src/sdk/client.ts","../../src/sdk/admin-client.ts"],"sourcesContent":["/**\r\n * Credits SDK - barrel exports\r\n *\r\n * Provides TypeScript SDK for consuming the credits REST API\r\n * from external services or other applications.\r\n */\r\n\r\n// Clients\r\nexport { CreditsClient } from \"./client.js\";\r\nexport { AdminCreditsClient } from \"./admin-client.js\";\r\n\r\n// Types\r\nexport type {\r\n CreditsClientConfig,\r\n AdminCreditsClientConfig,\r\n UserCredits,\r\n CreditReservation,\r\n CreditCheckResult,\r\n ReservationResult,\r\n PaginationOptions,\r\n UsageHistoryEntry,\r\n UsageHistoryResponse,\r\n ApiResponse,\r\n SDKErrorCodeType,\r\n SubscriptionTier,\r\n} from \"./types.js\";\r\n\r\nexport { SDKErrorCode } from \"./types.js\";\r\n\r\n// Errors\r\nexport {\r\n CreditsSDKError,\r\n NetworkError,\r\n AuthenticationError,\r\n AuthorizationError,\r\n InsufficientCreditsError,\r\n ReservationNotFoundError,\r\n ReservationExpiredError,\r\n ValidationError,\r\n ServerError,\r\n parseApiError,\r\n} from \"./errors.js\";\r\n\r\n// Admin client types\r\nexport type {\r\n CreditsConfig,\r\n ListUsersResponse,\r\n ListUsersOptions,\r\n} from \"./admin-client.js\";\r\n","/**\r\n * SDK types for the credits system\r\n *\r\n * These types are used by the client SDKs for REST API consumption.\r\n */\r\n\r\n// Re-export core types for SDK consumers\r\nexport type {\r\n PortableUserCredits as UserCredits,\r\n PortableReservation as CreditReservation,\r\n CreditCheckResult,\r\n SubscriptionTier,\r\n} from \"../core/types.js\";\r\n\r\n/**\r\n * Configuration for CreditsClient\r\n */\r\nexport interface CreditsClientConfig {\r\n /** Base URL of the credits API (e.g., \"https://api.example.com\") */\r\n baseUrl: string;\r\n /** Function to get the auth token for session-based auth */\r\n getAuthToken?: () => Promise<string>;\r\n /** Optional custom fetch function for testing */\r\n fetch?: typeof fetch;\r\n}\r\n\r\n/**\r\n * Configuration for AdminCreditsClient\r\n */\r\nexport interface AdminCreditsClientConfig {\r\n /** Base URL of the credits API */\r\n baseUrl: string;\r\n /** Admin API key */\r\n apiKey: string;\r\n /** Optional custom fetch function for testing */\r\n fetch?: typeof fetch;\r\n}\r\n\r\n/**\r\n * Result of a credit reservation\r\n */\r\nexport interface ReservationResult {\r\n /** The reservation ID */\r\n reservationId: string;\r\n /** Amount of credits reserved */\r\n amount: number;\r\n /** When the reservation expires (ISO 8601) */\r\n expiresAt: string;\r\n}\r\n\r\n/**\r\n * Pagination options for API requests\r\n */\r\nexport interface PaginationOptions {\r\n /** Page number (1-indexed) */\r\n page?: number;\r\n /** Number of items per page */\r\n limit?: number;\r\n}\r\n\r\n/**\r\n * Usage history entry\r\n */\r\nexport interface UsageHistoryEntry {\r\n /** Unique ID of the usage log */\r\n id: string;\r\n /** Type of operation */\r\n operationType: string;\r\n /** AI provider used */\r\n provider: string;\r\n /** Credits consumed */\r\n creditsUsed: number;\r\n /** Whether the operation was successful */\r\n success: boolean;\r\n /** Error message if unsuccessful */\r\n errorMessage?: string;\r\n /** ID of the related resource */\r\n resourceId?: string;\r\n /** Type of the related resource */\r\n resourceType?: string;\r\n /** When the operation occurred (ISO 8601) */\r\n createdAt: string;\r\n}\r\n\r\n/**\r\n * Response from usage history endpoint\r\n */\r\nexport interface UsageHistoryResponse {\r\n /** List of usage entries */\r\n entries: UsageHistoryEntry[];\r\n /** Whether there are more results */\r\n hasMore: boolean;\r\n /** Total count (if available) */\r\n total?: number;\r\n}\r\n\r\n/**\r\n * Generic API response wrapper\r\n */\r\nexport interface ApiResponse<T> {\r\n success: boolean;\r\n data?: T;\r\n error?: {\r\n code: string;\r\n message: string;\r\n details?: unknown;\r\n };\r\n}\r\n\r\n/**\r\n * SDK error codes\r\n */\r\nexport const SDKErrorCode = {\r\n NETWORK_ERROR: \"NETWORK_ERROR\",\r\n AUTHENTICATION_ERROR: \"AUTHENTICATION_ERROR\",\r\n AUTHORIZATION_ERROR: \"AUTHORIZATION_ERROR\",\r\n INSUFFICIENT_CREDITS: \"INSUFFICIENT_CREDITS\",\r\n RESERVATION_NOT_FOUND: \"RESERVATION_NOT_FOUND\",\r\n RESERVATION_EXPIRED: \"RESERVATION_EXPIRED\",\r\n VALIDATION_ERROR: \"VALIDATION_ERROR\",\r\n SERVER_ERROR: \"SERVER_ERROR\",\r\n UNKNOWN_ERROR: \"UNKNOWN_ERROR\",\r\n} as const;\r\n\r\nexport type SDKErrorCodeType = (typeof SDKErrorCode)[keyof typeof SDKErrorCode];\r\n","/**\r\n * SDK error classes\r\n *\r\n * Provides typed error handling for SDK operations.\r\n */\r\n\r\nimport { SDKErrorCode, type SDKErrorCodeType } from \"./types.js\";\r\n\r\n/**\r\n * Base error class for SDK errors\r\n */\r\nexport class CreditsSDKError extends Error {\r\n public readonly code: SDKErrorCodeType;\r\n public readonly details?: Record<string, unknown>;\r\n public readonly statusCode?: number;\r\n\r\n constructor(\r\n message: string,\r\n code: SDKErrorCodeType,\r\n options?: {\r\n details?: Record<string, unknown>;\r\n statusCode?: number;\r\n cause?: Error;\r\n }\r\n ) {\r\n super(message, { cause: options?.cause });\r\n this.name = \"CreditsSDKError\";\r\n this.code = code;\r\n this.details = options?.details;\r\n this.statusCode = options?.statusCode;\r\n }\r\n\r\n /**\r\n * Check if this is a specific error type\r\n */\r\n is(code: SDKErrorCodeType): boolean {\r\n return this.code === code;\r\n }\r\n\r\n /**\r\n * Create a JSON representation for logging\r\n */\r\n toJSON(): Record<string, unknown> {\r\n return {\r\n name: this.name,\r\n message: this.message,\r\n code: this.code,\r\n details: this.details,\r\n statusCode: this.statusCode,\r\n };\r\n }\r\n}\r\n\r\n/**\r\n * Error thrown when a network request fails\r\n */\r\nexport class NetworkError extends CreditsSDKError {\r\n constructor(message: string, cause?: Error) {\r\n super(message, SDKErrorCode.NETWORK_ERROR, { cause });\r\n this.name = \"NetworkError\";\r\n }\r\n}\r\n\r\n/**\r\n * Error thrown when authentication fails\r\n */\r\nexport class AuthenticationError extends CreditsSDKError {\r\n constructor(message: string = \"Authentication required\") {\r\n super(message, SDKErrorCode.AUTHENTICATION_ERROR, { statusCode: 401 });\r\n this.name = \"AuthenticationError\";\r\n }\r\n}\r\n\r\n/**\r\n * Error thrown when authorization fails\r\n */\r\nexport class AuthorizationError extends CreditsSDKError {\r\n constructor(message: string = \"Access denied\") {\r\n super(message, SDKErrorCode.AUTHORIZATION_ERROR, { statusCode: 403 });\r\n this.name = \"AuthorizationError\";\r\n }\r\n}\r\n\r\n/**\r\n * Error thrown when credits are insufficient\r\n */\r\nexport class InsufficientCreditsError extends CreditsSDKError {\r\n public readonly available: number;\r\n public readonly required: number;\r\n\r\n constructor(available: number, required: number) {\r\n super(\r\n `Insufficient credits: available ${available}, required ${required}`,\r\n SDKErrorCode.INSUFFICIENT_CREDITS,\r\n {\r\n statusCode: 402,\r\n details: { available, required },\r\n }\r\n );\r\n this.name = \"InsufficientCreditsError\";\r\n this.available = available;\r\n this.required = required;\r\n }\r\n}\r\n\r\n/**\r\n * Error thrown when a reservation is not found\r\n */\r\nexport class ReservationNotFoundError extends CreditsSDKError {\r\n public readonly reservationId: string;\r\n\r\n constructor(reservationId: string) {\r\n super(\r\n `Reservation not found: ${reservationId}`,\r\n SDKErrorCode.RESERVATION_NOT_FOUND,\r\n {\r\n statusCode: 404,\r\n details: { reservationId },\r\n }\r\n );\r\n this.name = \"ReservationNotFoundError\";\r\n this.reservationId = reservationId;\r\n }\r\n}\r\n\r\n/**\r\n * Error thrown when a reservation has expired\r\n */\r\nexport class ReservationExpiredError extends CreditsSDKError {\r\n public readonly reservationId: string;\r\n\r\n constructor(reservationId: string) {\r\n super(\r\n `Reservation expired: ${reservationId}`,\r\n SDKErrorCode.RESERVATION_EXPIRED,\r\n {\r\n statusCode: 410,\r\n details: { reservationId },\r\n }\r\n );\r\n this.name = \"ReservationExpiredError\";\r\n this.reservationId = reservationId;\r\n }\r\n}\r\n\r\n/**\r\n * Error thrown for validation failures\r\n */\r\nexport class ValidationError extends CreditsSDKError {\r\n public readonly field?: string;\r\n\r\n constructor(message: string, field?: string) {\r\n super(message, SDKErrorCode.VALIDATION_ERROR, {\r\n statusCode: 400,\r\n details: field ? { field } : undefined,\r\n });\r\n this.name = \"ValidationError\";\r\n this.field = field;\r\n }\r\n}\r\n\r\n/**\r\n * Error thrown for server errors\r\n */\r\nexport class ServerError extends CreditsSDKError {\r\n constructor(message: string = \"Internal server error\") {\r\n super(message, SDKErrorCode.SERVER_ERROR, { statusCode: 500 });\r\n this.name = \"ServerError\";\r\n }\r\n}\r\n\r\n/**\r\n * Parse an API error response into the appropriate SDK error\r\n */\r\nexport function parseApiError(\r\n statusCode: number,\r\n body: { error?: { code?: string; message?: string; details?: unknown } }\r\n): CreditsSDKError {\r\n const errorCode = body.error?.code || \"\";\r\n const errorMessage = body.error?.message || \"An error occurred\";\r\n const details = body.error?.details as Record<string, unknown> | undefined;\r\n\r\n switch (statusCode) {\r\n case 401:\r\n return new AuthenticationError(errorMessage);\r\n case 403:\r\n return new AuthorizationError(errorMessage);\r\n case 402:\r\n // Parse insufficient credits details\r\n if (details?.available !== undefined && details?.required !== undefined) {\r\n return new InsufficientCreditsError(\r\n details.available as number,\r\n details.required as number\r\n );\r\n }\r\n return new CreditsSDKError(errorMessage, SDKErrorCode.INSUFFICIENT_CREDITS, {\r\n statusCode,\r\n details,\r\n });\r\n case 404:\r\n if (errorCode === \"RESERVATION_NOT_FOUND\" && details?.reservationId) {\r\n return new ReservationNotFoundError(details.reservationId as string);\r\n }\r\n return new CreditsSDKError(errorMessage, SDKErrorCode.RESERVATION_NOT_FOUND, {\r\n statusCode,\r\n details,\r\n });\r\n case 410:\r\n if (errorCode === \"RESERVATION_EXPIRED\" && details?.reservationId) {\r\n return new ReservationExpiredError(details.reservationId as string);\r\n }\r\n return new CreditsSDKError(errorMessage, SDKErrorCode.RESERVATION_EXPIRED, {\r\n statusCode,\r\n details,\r\n });\r\n case 400:\r\n case 422:\r\n return new ValidationError(errorMessage, details?.field as string | undefined);\r\n case 500:\r\n default:\r\n if (statusCode >= 500) {\r\n return new ServerError(errorMessage);\r\n }\r\n return new CreditsSDKError(errorMessage, SDKErrorCode.UNKNOWN_ERROR, {\r\n statusCode,\r\n details,\r\n });\r\n }\r\n}\r\n","/**\r\n * CreditsClient - REST API client for the credits system\r\n *\r\n * Provides a type-safe interface for consuming credits API endpoints\r\n * from external services or other applications.\r\n *\r\n * @example\r\n * ```typescript\r\n * const client = new CreditsClient({\r\n * baseUrl: process.env.CREDITS_API_URL,\r\n * getAuthToken: async () => session.accessToken,\r\n * });\r\n *\r\n * // Check if user has sufficient credits\r\n * const check = await client.checkAvailability(\"story_generation\");\r\n * if (!check.hasCredits) {\r\n * console.log(\"Insufficient credits\");\r\n * return;\r\n * }\r\n *\r\n * // Two-phase commit pattern\r\n * const reservation = await client.reserve(\"story_generation\");\r\n * try {\r\n * const result = await doExpensiveWork();\r\n * await client.commit(reservation.reservationId);\r\n * return result;\r\n * } catch (error) {\r\n * await client.release(reservation.reservationId);\r\n * throw error;\r\n * }\r\n * ```\r\n */\r\n\r\nimport type {\r\n CreditsClientConfig,\r\n UserCredits,\r\n CreditCheckResult,\r\n ReservationResult,\r\n UsageHistoryResponse,\r\n PaginationOptions,\r\n} from \"./types.js\";\r\nimport { parseApiError, NetworkError } from \"./errors.js\";\r\n\r\nexport class CreditsClient {\r\n private readonly baseUrl: string;\r\n private readonly getAuthToken?: () => Promise<string>;\r\n private readonly fetchFn: typeof fetch;\r\n\r\n constructor(config: CreditsClientConfig) {\r\n // Ensure baseUrl doesn't have trailing slash\r\n this.baseUrl = config.baseUrl.replace(/\\/$/, \"\");\r\n this.getAuthToken = config.getAuthToken;\r\n this.fetchFn = config.fetch ?? fetch;\r\n }\r\n\r\n /**\r\n * Make an authenticated request to the API\r\n */\r\n private async request<T>(\r\n path: string,\r\n options: {\r\n method?: \"GET\" | \"POST\" | \"PUT\" | \"DELETE\";\r\n body?: unknown;\r\n params?: Record<string, string | number | undefined>;\r\n } = {}\r\n ): Promise<T> {\r\n const { method = \"GET\", body, params } = options;\r\n\r\n // Build URL with query params\r\n let url = `${this.baseUrl}${path}`;\r\n if (params) {\r\n const searchParams = new URLSearchParams();\r\n for (const [key, value] of Object.entries(params)) {\r\n if (value !== undefined) {\r\n searchParams.set(key, String(value));\r\n }\r\n }\r\n const queryString = searchParams.toString();\r\n if (queryString) {\r\n url += `?${queryString}`;\r\n }\r\n }\r\n\r\n // Build headers\r\n const headers: Record<string, string> = {\r\n \"Content-Type\": \"application/json\",\r\n };\r\n\r\n // Add auth token if available\r\n if (this.getAuthToken) {\r\n try {\r\n const token = await this.getAuthToken();\r\n if (token) {\r\n headers[\"Authorization\"] = `Bearer ${token}`;\r\n }\r\n } catch {\r\n // Auth token not available, proceed without it\r\n }\r\n }\r\n\r\n // Make the request\r\n let response: Response;\r\n try {\r\n response = await this.fetchFn(url, {\r\n method,\r\n headers,\r\n body: body ? JSON.stringify(body) : undefined,\r\n });\r\n } catch (error) {\r\n throw new NetworkError(\r\n error instanceof Error ? error.message : \"Network request failed\",\r\n error instanceof Error ? error : undefined\r\n );\r\n }\r\n\r\n // Parse response\r\n const responseBody = (await response.json()) as {\r\n data?: T;\r\n error?: { code?: string; message?: string; details?: unknown };\r\n };\r\n\r\n // Handle errors\r\n if (!response.ok) {\r\n throw parseApiError(response.status, responseBody);\r\n }\r\n\r\n // Return data\r\n return responseBody.data as T;\r\n }\r\n\r\n /**\r\n * Get the current user's credit balance\r\n *\r\n * @returns User's credit information\r\n * @throws AuthenticationError if not authenticated\r\n */\r\n async getBalance(): Promise<UserCredits> {\r\n return this.request<UserCredits>(\"/api/v1/credits/balance\");\r\n }\r\n\r\n /**\r\n * Check if the user has sufficient credits for an operation\r\n *\r\n * @param operationType - Type of operation to check\r\n * @param customCost - Optional custom cost to check against\r\n * @returns Credit availability status\r\n * @throws AuthenticationError if not authenticated\r\n */\r\n async checkAvailability(\r\n operationType: string,\r\n customCost?: number\r\n ): Promise<CreditCheckResult> {\r\n return this.request<CreditCheckResult>(\"/api/v1/credits/availability\", {\r\n params: {\r\n operationType,\r\n customCost: customCost?.toString(),\r\n },\r\n });\r\n }\r\n\r\n /**\r\n * Reserve credits for an operation\r\n *\r\n * This is the first step in the two-phase commit pattern.\r\n * Reserved credits are held for 5 minutes before expiring.\r\n *\r\n * @param operationType - Type of operation\r\n * @param options - Optional reservation options\r\n * @returns Reservation details including ID and expiry\r\n * @throws InsufficientCreditsError if not enough credits\r\n * @throws AuthenticationError if not authenticated\r\n */\r\n async reserve(\r\n operationType: string,\r\n options?: {\r\n customCost?: number;\r\n resourceId?: string;\r\n resourceType?: string;\r\n }\r\n ): Promise<ReservationResult> {\r\n return this.request<ReservationResult>(\"/api/v1/credits/reserve\", {\r\n method: \"POST\",\r\n body: {\r\n operationType,\r\n ...options,\r\n },\r\n });\r\n }\r\n\r\n /**\r\n * Commit a reservation (deduct credits)\r\n *\r\n * This is the second step in the two-phase commit pattern.\r\n * Call this after your operation succeeds.\r\n *\r\n * @param reservationId - ID of the reservation to commit\r\n * @throws ReservationNotFoundError if reservation doesn't exist\r\n * @throws ReservationExpiredError if reservation has expired\r\n * @throws AuthenticationError if not authenticated\r\n */\r\n async commit(reservationId: string): Promise<void> {\r\n await this.request<{ message: string }>(\"/api/v1/credits/commit\", {\r\n method: \"POST\",\r\n body: { reservationId },\r\n });\r\n }\r\n\r\n /**\r\n * Release a reservation (refund credits)\r\n *\r\n * Call this if your operation fails after reserving credits.\r\n *\r\n * @param reservationId - ID of the reservation to release\r\n * @throws ReservationNotFoundError if reservation doesn't exist\r\n * @throws AuthenticationError if not authenticated\r\n */\r\n async release(reservationId: string): Promise<void> {\r\n await this.request<{ message: string }>(\"/api/v1/credits/release\", {\r\n method: \"POST\",\r\n body: { reservationId },\r\n });\r\n }\r\n\r\n /**\r\n * Get usage history for the current user\r\n *\r\n * @param options - Optional pagination options\r\n * @returns Paginated list of usage entries\r\n * @throws AuthenticationError if not authenticated\r\n */\r\n async getHistory(options?: PaginationOptions): Promise<UsageHistoryResponse> {\r\n return this.request<UsageHistoryResponse>(\"/api/v1/credits/history\", {\r\n params: {\r\n page: options?.page,\r\n limit: options?.limit,\r\n },\r\n });\r\n }\r\n\r\n /**\r\n * Get the available credits (balance + bonus - reserved)\r\n *\r\n * Convenience method that calculates available credits from balance.\r\n *\r\n * @returns Number of available credits\r\n * @throws AuthenticationError if not authenticated\r\n */\r\n async getAvailableCredits(): Promise<number> {\r\n const balance = await this.getBalance();\r\n return balance.balance + balance.bonusCredits - balance.reserved;\r\n }\r\n\r\n /**\r\n * Execute an operation with automatic credit handling\r\n *\r\n * This is a convenience method that handles the full two-phase commit\r\n * pattern automatically.\r\n *\r\n * @param operationType - Type of operation\r\n * @param operation - The operation to execute\r\n * @param options - Optional reservation options\r\n * @returns Result of the operation\r\n * @throws InsufficientCreditsError if not enough credits\r\n * @throws AuthenticationError if not authenticated\r\n *\r\n * @example\r\n * ```typescript\r\n * const result = await client.withCredits(\r\n * \"story_generation\",\r\n * async () => {\r\n * return generateStory(data);\r\n * }\r\n * );\r\n * ```\r\n */\r\n async withCredits<T>(\r\n operationType: string,\r\n operation: () => Promise<T>,\r\n options?: {\r\n customCost?: number;\r\n resourceId?: string;\r\n resourceType?: string;\r\n }\r\n ): Promise<T> {\r\n const reservation = await this.reserve(operationType, options);\r\n\r\n try {\r\n const result = await operation();\r\n await this.commit(reservation.reservationId);\r\n return result;\r\n } catch (error) {\r\n // Always attempt to release on failure\r\n try {\r\n await this.release(reservation.reservationId);\r\n } catch {\r\n // Ignore release errors, original error is more important\r\n }\r\n throw error;\r\n }\r\n }\r\n}\r\n","/**\r\n * AdminCreditsClient - Admin REST API client for the credits system\r\n *\r\n * Provides administrative access to the credits system for managing\r\n * user credits, configurations, and subscriptions.\r\n *\r\n * @example\r\n * ```typescript\r\n * const adminClient = new AdminCreditsClient({\r\n * baseUrl: process.env.CREDITS_API_URL,\r\n * apiKey: process.env.CREDITS_ADMIN_API_KEY,\r\n * });\r\n *\r\n * // Add credits to a user\r\n * await adminClient.addCredits(\"user-123\", 100, \"Support credit grant\");\r\n *\r\n * // Update subscription tier\r\n * await adminClient.updateSubscription(\"user-123\", \"premium\", \"2027-01-01T00:00:00Z\");\r\n *\r\n * // Update operation costs\r\n * await adminClient.updateCosts({\r\n * story_generation: 8,\r\n * image_generation: 12,\r\n * });\r\n * ```\r\n */\r\n\r\nimport type { AdminCreditsClientConfig, UserCredits } from \"./types.js\";\r\nimport { parseApiError, NetworkError } from \"./errors.js\";\r\n\r\n/**\r\n * Credits configuration returned by the API\r\n */\r\nexport interface CreditsConfig {\r\n operationCosts: Record<string, number>;\r\n tierConfigs: Record<\r\n string,\r\n {\r\n monthlyLimit: number;\r\n features: string[];\r\n }\r\n >;\r\n}\r\n\r\n/**\r\n * Response from listing users\r\n */\r\nexport interface ListUsersResponse {\r\n users: Array<{\r\n userId: string;\r\n balance: number;\r\n bonusCredits?: number;\r\n tier: string;\r\n monthlyUsed?: number;\r\n }>;\r\n hasMore: boolean;\r\n total?: number;\r\n}\r\n\r\n/**\r\n * Options for listing users\r\n */\r\nexport interface ListUsersOptions {\r\n page?: number;\r\n limit?: number;\r\n tier?: string;\r\n search?: string;\r\n}\r\n\r\nexport class AdminCreditsClient {\r\n private readonly baseUrl: string;\r\n private readonly apiKey: string;\r\n private readonly fetchFn: typeof fetch;\r\n\r\n constructor(config: AdminCreditsClientConfig) {\r\n // Ensure baseUrl doesn't have trailing slash\r\n this.baseUrl = config.baseUrl.replace(/\\/$/, \"\");\r\n this.apiKey = config.apiKey;\r\n this.fetchFn = config.fetch ?? fetch;\r\n }\r\n\r\n /**\r\n * Make an authenticated admin request to the API\r\n */\r\n private async request<T>(\r\n path: string,\r\n options: {\r\n method?: \"GET\" | \"POST\" | \"PUT\" | \"DELETE\";\r\n body?: unknown;\r\n params?: Record<string, string | number | undefined>;\r\n } = {}\r\n ): Promise<T> {\r\n const { method = \"GET\", body, params } = options;\r\n\r\n // Build URL with query params\r\n let url = `${this.baseUrl}${path}`;\r\n if (params) {\r\n const searchParams = new URLSearchParams();\r\n for (const [key, value] of Object.entries(params)) {\r\n if (value !== undefined) {\r\n searchParams.set(key, String(value));\r\n }\r\n }\r\n const queryString = searchParams.toString();\r\n if (queryString) {\r\n url += `?${queryString}`;\r\n }\r\n }\r\n\r\n // Build headers with admin API key\r\n const headers: Record<string, string> = {\r\n \"Content-Type\": \"application/json\",\r\n \"x-api-key\": this.apiKey,\r\n };\r\n\r\n // Make the request\r\n let response: Response;\r\n try {\r\n response = await this.fetchFn(url, {\r\n method,\r\n headers,\r\n body: body ? JSON.stringify(body) : undefined,\r\n });\r\n } catch (error) {\r\n throw new NetworkError(\r\n error instanceof Error ? error.message : \"Network request failed\",\r\n error instanceof Error ? error : undefined\r\n );\r\n }\r\n\r\n // Parse response\r\n const responseBody = (await response.json()) as {\r\n data?: T;\r\n error?: { code?: string; message?: string; details?: unknown };\r\n };\r\n\r\n // Handle errors\r\n if (!response.ok) {\r\n throw parseApiError(response.status, responseBody);\r\n }\r\n\r\n // Return data\r\n return responseBody.data as T;\r\n }\r\n\r\n /**\r\n * Get a specific user's credits\r\n *\r\n * @param userId - ID of the user\r\n * @returns User's credit information\r\n * @throws AuthorizationError if API key is invalid\r\n */\r\n async getUserCredits(userId: string): Promise<UserCredits> {\r\n return this.request<UserCredits>(`/api/admin/credits/users/${userId}`);\r\n }\r\n\r\n /**\r\n * Add credits to a user's account\r\n *\r\n * Credits are added to bonusCredits, which don't expire on monthly reset.\r\n *\r\n * @param userId - ID of the user\r\n * @param amount - Amount of credits to add\r\n * @param description - Reason for adding credits\r\n * @throws AuthorizationError if API key is invalid\r\n */\r\n async addCredits(\r\n userId: string,\r\n amount: number,\r\n description: string\r\n ): Promise<void> {\r\n await this.request<{ message: string; newBalance: number }>(\r\n `/api/admin/credits/users/${userId}/add`,\r\n {\r\n method: \"POST\",\r\n body: { amount, description },\r\n }\r\n );\r\n }\r\n\r\n /**\r\n * Update a user's subscription tier\r\n *\r\n * @param userId - ID of the user\r\n * @param tier - New subscription tier\r\n * @param expiresAt - Optional expiration date (ISO 8601)\r\n * @throws AuthorizationError if API key is invalid\r\n */\r\n async updateSubscription(\r\n userId: string,\r\n tier: string,\r\n expiresAt?: string\r\n ): Promise<void> {\r\n const body: { tier: string; expiresAt?: string } = { tier };\r\n if (expiresAt) {\r\n body.expiresAt = expiresAt;\r\n }\r\n\r\n await this.request<{ message: string }>(\r\n `/api/admin/credits/users/${userId}/subscription`,\r\n {\r\n method: \"PUT\",\r\n body,\r\n }\r\n );\r\n }\r\n\r\n /**\r\n * Get the current credits configuration\r\n *\r\n * @returns Credits system configuration\r\n * @throws AuthorizationError if API key is invalid\r\n */\r\n async getConfig(): Promise<CreditsConfig> {\r\n return this.request<CreditsConfig>(\"/api/admin/credits/config\");\r\n }\r\n\r\n /**\r\n * Update operation costs\r\n *\r\n * @param costs - New operation costs (operation type -> cost)\r\n * @throws AuthorizationError if API key is invalid\r\n */\r\n async updateCosts(costs: Record<string, number>): Promise<void> {\r\n await this.request<{ message: string }>(\"/api/admin/credits/config/costs\", {\r\n method: \"PUT\",\r\n body: { operationCosts: costs },\r\n });\r\n }\r\n\r\n /**\r\n * List all users with their credits\r\n *\r\n * @param options - Pagination and filtering options\r\n * @returns Paginated list of users\r\n * @throws AuthorizationError if API key is invalid\r\n */\r\n async listUsers(options?: ListUsersOptions): Promise<ListUsersResponse> {\r\n return this.request<ListUsersResponse>(\"/api/admin/credits/users\", {\r\n params: {\r\n page: options?.page,\r\n limit: options?.limit,\r\n tier: options?.tier,\r\n search: options?.search,\r\n },\r\n });\r\n }\r\n\r\n /**\r\n * Deduct credits from a user's account\r\n *\r\n * @param userId - ID of the user\r\n * @param amount - Amount of credits to deduct\r\n * @param description - Reason for deducting credits\r\n * @throws AuthorizationError if API key is invalid\r\n */\r\n async deductCredits(\r\n userId: string,\r\n amount: number,\r\n description: string\r\n ): Promise<void> {\r\n await this.request<{ message: string; newBalance: number }>(\r\n `/api/admin/credits/users/${userId}/deduct`,\r\n {\r\n method: \"POST\",\r\n body: { amount, description },\r\n }\r\n );\r\n }\r\n\r\n /**\r\n * Get usage statistics for a user\r\n *\r\n * @param userId - ID of the user\r\n * @param options - Optional date range\r\n * @returns Usage statistics\r\n * @throws AuthorizationError if API key is invalid\r\n */\r\n async getUserStats(\r\n userId: string,\r\n options?: { startDate?: string; endDate?: string }\r\n ): Promise<{\r\n totalCreditsUsed: number;\r\n operationBreakdown: Record<string, number>;\r\n successRate: number;\r\n }> {\r\n return this.request<{\r\n totalCreditsUsed: number;\r\n operationBreakdown: Record<string, number>;\r\n successRate: number;\r\n }>(`/api/admin/credits/users/${userId}/stats`, {\r\n params: {\r\n startDate: options?.startDate,\r\n endDate: options?.endDate,\r\n },\r\n });\r\n }\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACgHO,IAAM,eAAe;AAAA,EAC1B,eAAe;AAAA,EACf,sBAAsB;AAAA,EACtB,qBAAqB;AAAA,EACrB,sBAAsB;AAAA,EACtB,uBAAuB;AAAA,EACvB,qBAAqB;AAAA,EACrB,kBAAkB;AAAA,EAClB,cAAc;AAAA,EACd,eAAe;AACjB;;;AC/GO,IAAM,kBAAN,cAA8B,MAAM;AAAA,EACzB;AAAA,EACA;AAAA,EACA;AAAA,EAEhB,YACE,SACA,MACA,SAKA;AACA,UAAM,SAAS,EAAE,OAAO,SAAS,MAAM,CAAC;AACxC,SAAK,OAAO;AACZ,SAAK,OAAO;AACZ,SAAK,UAAU,SAAS;AACxB,SAAK,aAAa,SAAS;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA,EAKA,GAAG,MAAiC;AAClC,WAAO,KAAK,SAAS;AAAA,EACvB;AAAA;AAAA;AAAA;AAAA,EAKA,SAAkC;AAChC,WAAO;AAAA,MACL,MAAM,KAAK;AAAA,MACX,SAAS,KAAK;AAAA,MACd,MAAM,KAAK;AAAA,MACX,SAAS,KAAK;AAAA,MACd,YAAY,KAAK;AAAA,IACnB;AAAA,EACF;AACF;AAKO,IAAM,eAAN,cAA2B,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AAA;AAAA;AAAA,EAUA,MAAM,aACJ,QACA,SAKC;AACD,WAAO,KAAK,QAIT,4BAA4B,MAAM,UAAU;AAAA,MAC7C,QAAQ;AAAA,QACN,WAAW,SAAS;AAAA,QACpB,SAAS,SAAS;AAAA,MACpB;AAAA,IACF,CAAC;AAAA,EACH;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/sdk/index.ts","../../src/sdk/types.ts","../../src/sdk/errors.ts","../../src/sdk/client.ts","../../src/sdk/admin-client.ts"],"sourcesContent":["/**\n * Credits SDK - barrel exports\n *\n * Provides TypeScript SDK for consuming the credits REST API\n * from external services or other applications.\n */\n\n// Clients\nexport { CreditsClient } from \"./client.js\";\nexport { AdminCreditsClient } from \"./admin-client.js\";\n\n// Types\nexport type {\n CreditsClientConfig,\n AdminCreditsClientConfig,\n UserCredits,\n CreditReservation,\n CreditCheckResult,\n ReservationResult,\n PaginationOptions,\n UsageHistoryEntry,\n UsageHistoryResponse,\n ApiResponse,\n SDKErrorCodeType,\n SubscriptionTier,\n} from \"./types.js\";\n\nexport { SDKErrorCode } from \"./types.js\";\n\n// Errors\nexport {\n CreditsSDKError,\n NetworkError,\n AuthenticationError,\n AuthorizationError,\n InsufficientCreditsError,\n ReservationNotFoundError,\n ReservationExpiredError,\n ValidationError,\n ServerError,\n parseApiError,\n} from \"./errors.js\";\n\n// Admin client types\nexport type {\n CreditsConfig,\n ListUsersResponse,\n ListUsersOptions,\n} from \"./admin-client.js\";\n","/**\n * SDK types for the credits system\n *\n * These types are used by the client SDKs for REST API consumption.\n */\n\n// Re-export core types for SDK consumers\nexport type {\n PortableUserCredits as UserCredits,\n PortableReservation as CreditReservation,\n CreditCheckResult,\n SubscriptionTier,\n} from \"../core/types.js\";\n\n/**\n * Configuration for CreditsClient\n */\nexport interface CreditsClientConfig {\n /** Base URL of the credits API (e.g., \"https://api.example.com\") */\n baseUrl: string;\n /** Function to get the auth token for session-based auth */\n getAuthToken?: () => Promise<string>;\n /** Optional custom fetch function for testing */\n fetch?: typeof fetch;\n}\n\n/**\n * Configuration for AdminCreditsClient\n */\nexport interface AdminCreditsClientConfig {\n /** Base URL of the credits API */\n baseUrl: string;\n /** Admin API key */\n apiKey: string;\n /** Optional custom fetch function for testing */\n fetch?: typeof fetch;\n}\n\n/**\n * Result of a credit reservation\n */\nexport interface ReservationResult {\n /** The reservation ID */\n reservationId: string;\n /** Amount of credits reserved */\n amount: number;\n /** When the reservation expires (ISO 8601) */\n expiresAt: string;\n}\n\n/**\n * Pagination options for API requests\n */\nexport interface PaginationOptions {\n /** Page number (1-indexed) */\n page?: number;\n /** Number of items per page */\n limit?: number;\n}\n\n/**\n * Usage history entry\n */\nexport interface UsageHistoryEntry {\n /** Unique ID of the usage log */\n id: string;\n /** Type of operation */\n operationType: string;\n /** AI provider used */\n provider: string;\n /** Credits consumed */\n creditsUsed: number;\n /** Whether the operation was successful */\n success: boolean;\n /** Error message if unsuccessful */\n errorMessage?: string;\n /** ID of the related resource */\n resourceId?: string;\n /** Type of the related resource */\n resourceType?: string;\n /** When the operation occurred (ISO 8601) */\n createdAt: string;\n}\n\n/**\n * Response from usage history endpoint\n */\nexport interface UsageHistoryResponse {\n /** List of usage entries */\n entries: UsageHistoryEntry[];\n /** Whether there are more results */\n hasMore: boolean;\n /** Total count (if available) */\n total?: number;\n}\n\n/**\n * Generic API response wrapper\n */\nexport interface ApiResponse<T> {\n success: boolean;\n data?: T;\n error?: {\n code: string;\n message: string;\n details?: unknown;\n };\n}\n\n/**\n * SDK error codes\n */\nexport const SDKErrorCode = {\n NETWORK_ERROR: \"NETWORK_ERROR\",\n AUTHENTICATION_ERROR: \"AUTHENTICATION_ERROR\",\n AUTHORIZATION_ERROR: \"AUTHORIZATION_ERROR\",\n INSUFFICIENT_CREDITS: \"INSUFFICIENT_CREDITS\",\n RESERVATION_NOT_FOUND: \"RESERVATION_NOT_FOUND\",\n RESERVATION_EXPIRED: \"RESERVATION_EXPIRED\",\n VALIDATION_ERROR: \"VALIDATION_ERROR\",\n SERVER_ERROR: \"SERVER_ERROR\",\n UNKNOWN_ERROR: \"UNKNOWN_ERROR\",\n} as const;\n\nexport type SDKErrorCodeType = (typeof SDKErrorCode)[keyof typeof SDKErrorCode];\n","/**\n * SDK error classes\n *\n * Provides typed error handling for SDK operations.\n */\n\nimport { SDKErrorCode, type SDKErrorCodeType } from \"./types.js\";\n\n/**\n * Base error class for SDK errors\n */\nexport class CreditsSDKError extends Error {\n public readonly code: SDKErrorCodeType;\n public readonly details?: Record<string, unknown>;\n public readonly statusCode?: number;\n\n constructor(\n message: string,\n code: SDKErrorCodeType,\n options?: {\n details?: Record<string, unknown>;\n statusCode?: number;\n cause?: Error;\n }\n ) {\n super(message, { cause: options?.cause });\n this.name = \"CreditsSDKError\";\n this.code = code;\n this.details = options?.details;\n this.statusCode = options?.statusCode;\n }\n\n /**\n * Check if this is a specific error type\n */\n is(code: SDKErrorCodeType): boolean {\n return this.code === code;\n }\n\n /**\n * Create a JSON representation for logging\n */\n toJSON(): Record<string, unknown> {\n return {\n name: this.name,\n message: this.message,\n code: this.code,\n details: this.details,\n statusCode: this.statusCode,\n };\n }\n}\n\n/**\n * Error thrown when a network request fails\n */\nexport class NetworkError extends CreditsSDKError {\n constructor(message: string, cause?: Error) {\n super(message, SDKErrorCode.NETWORK_ERROR, { cause });\n this.name = \"NetworkError\";\n }\n}\n\n/**\n * Error thrown when authentication fails\n */\nexport class AuthenticationError extends CreditsSDKError {\n constructor(message: string = \"Authentication required\") {\n super(message, SDKErrorCode.AUTHENTICATION_ERROR, { statusCode: 401 });\n this.name = \"AuthenticationError\";\n }\n}\n\n/**\n * Error thrown when authorization fails\n */\nexport class AuthorizationError extends CreditsSDKError {\n constructor(message: string = \"Access denied\") {\n super(message, SDKErrorCode.AUTHORIZATION_ERROR, { statusCode: 403 });\n this.name = \"AuthorizationError\";\n }\n}\n\n/**\n * Error thrown when credits are insufficient\n */\nexport class InsufficientCreditsError extends CreditsSDKError {\n public readonly available: number;\n public readonly required: number;\n\n constructor(available: number, required: number) {\n super(\n `Insufficient credits: available ${available}, required ${required}`,\n SDKErrorCode.INSUFFICIENT_CREDITS,\n {\n statusCode: 402,\n details: { available, required },\n }\n );\n this.name = \"InsufficientCreditsError\";\n this.available = available;\n this.required = required;\n }\n}\n\n/**\n * Error thrown when a reservation is not found\n */\nexport class ReservationNotFoundError extends CreditsSDKError {\n public readonly reservationId: string;\n\n constructor(reservationId: string) {\n super(\n `Reservation not found: ${reservationId}`,\n SDKErrorCode.RESERVATION_NOT_FOUND,\n {\n statusCode: 404,\n details: { reservationId },\n }\n );\n this.name = \"ReservationNotFoundError\";\n this.reservationId = reservationId;\n }\n}\n\n/**\n * Error thrown when a reservation has expired\n */\nexport class ReservationExpiredError extends CreditsSDKError {\n public readonly reservationId: string;\n\n constructor(reservationId: string) {\n super(\n `Reservation expired: ${reservationId}`,\n SDKErrorCode.RESERVATION_EXPIRED,\n {\n statusCode: 410,\n details: { reservationId },\n }\n );\n this.name = \"ReservationExpiredError\";\n this.reservationId = reservationId;\n }\n}\n\n/**\n * Error thrown for validation failures\n */\nexport class ValidationError extends CreditsSDKError {\n public readonly field?: string;\n\n constructor(message: string, field?: string) {\n super(message, SDKErrorCode.VALIDATION_ERROR, {\n statusCode: 400,\n details: field ? { field } : undefined,\n });\n this.name = \"ValidationError\";\n this.field = field;\n }\n}\n\n/**\n * Error thrown for server errors\n */\nexport class ServerError extends CreditsSDKError {\n constructor(message: string = \"Internal server error\") {\n super(message, SDKErrorCode.SERVER_ERROR, { statusCode: 500 });\n this.name = \"ServerError\";\n }\n}\n\n/**\n * Parse an API error response into the appropriate SDK error\n */\nexport function parseApiError(\n statusCode: number,\n body: { error?: { code?: string; message?: string; details?: unknown } }\n): CreditsSDKError {\n const errorCode = body.error?.code || \"\";\n const errorMessage = body.error?.message || \"An error occurred\";\n const details = body.error?.details as Record<string, unknown> | undefined;\n\n switch (statusCode) {\n case 401:\n return new AuthenticationError(errorMessage);\n case 403:\n return new AuthorizationError(errorMessage);\n case 402:\n // Parse insufficient credits details\n if (details?.available !== undefined && details?.required !== undefined) {\n return new InsufficientCreditsError(\n details.available as number,\n details.required as number\n );\n }\n return new CreditsSDKError(errorMessage, SDKErrorCode.INSUFFICIENT_CREDITS, {\n statusCode,\n details,\n });\n case 404:\n if (errorCode === \"RESERVATION_NOT_FOUND\" && details?.reservationId) {\n return new ReservationNotFoundError(details.reservationId as string);\n }\n return new CreditsSDKError(errorMessage, SDKErrorCode.RESERVATION_NOT_FOUND, {\n statusCode,\n details,\n });\n case 410:\n if (errorCode === \"RESERVATION_EXPIRED\" && details?.reservationId) {\n return new ReservationExpiredError(details.reservationId as string);\n }\n return new CreditsSDKError(errorMessage, SDKErrorCode.RESERVATION_EXPIRED, {\n statusCode,\n details,\n });\n case 400:\n case 422:\n return new ValidationError(errorMessage, details?.field as string | undefined);\n case 500:\n default:\n if (statusCode >= 500) {\n return new ServerError(errorMessage);\n }\n return new CreditsSDKError(errorMessage, SDKErrorCode.UNKNOWN_ERROR, {\n statusCode,\n details,\n });\n }\n}\n","/**\n * CreditsClient - REST API client for the credits system\n *\n * Provides a type-safe interface for consuming credits API endpoints\n * from external services or other applications.\n *\n * @example\n * ```typescript\n * const client = new CreditsClient({\n * baseUrl: process.env.CREDITS_API_URL,\n * getAuthToken: async () => session.accessToken,\n * });\n *\n * // Check if user has sufficient credits\n * const check = await client.checkAvailability(\"story_generation\");\n * if (!check.hasCredits) {\n * console.log(\"Insufficient credits\");\n * return;\n * }\n *\n * // Two-phase commit pattern\n * const reservation = await client.reserve(\"story_generation\");\n * try {\n * const result = await doExpensiveWork();\n * await client.commit(reservation.reservationId);\n * return result;\n * } catch (error) {\n * await client.release(reservation.reservationId);\n * throw error;\n * }\n * ```\n */\n\nimport type {\n CreditsClientConfig,\n UserCredits,\n CreditCheckResult,\n ReservationResult,\n UsageHistoryResponse,\n PaginationOptions,\n} from \"./types.js\";\nimport { parseApiError, NetworkError } from \"./errors.js\";\n\nexport class CreditsClient {\n private readonly baseUrl: string;\n private readonly getAuthToken?: () => Promise<string>;\n private readonly fetchFn: typeof fetch;\n\n constructor(config: CreditsClientConfig) {\n // Ensure baseUrl doesn't have trailing slash\n this.baseUrl = config.baseUrl.replace(/\\/$/, \"\");\n this.getAuthToken = config.getAuthToken;\n this.fetchFn = config.fetch ?? fetch;\n }\n\n /**\n * Make an authenticated request to the API\n */\n private async request<T>(\n path: string,\n options: {\n method?: \"GET\" | \"POST\" | \"PUT\" | \"DELETE\";\n body?: unknown;\n params?: Record<string, string | number | undefined>;\n } = {}\n ): Promise<T> {\n const { method = \"GET\", body, params } = options;\n\n // Build URL with query params\n let url = `${this.baseUrl}${path}`;\n if (params) {\n const searchParams = new URLSearchParams();\n for (const [key, value] of Object.entries(params)) {\n if (value !== undefined) {\n searchParams.set(key, String(value));\n }\n }\n const queryString = searchParams.toString();\n if (queryString) {\n url += `?${queryString}`;\n }\n }\n\n // Build headers\n const headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n };\n\n // Add auth token if available\n if (this.getAuthToken) {\n try {\n const token = await this.getAuthToken();\n if (token) {\n headers[\"Authorization\"] = `Bearer ${token}`;\n }\n } catch {\n // Auth token not available, proceed without it\n }\n }\n\n // Make the request\n let response: Response;\n try {\n response = await this.fetchFn(url, {\n method,\n headers,\n body: body ? JSON.stringify(body) : undefined,\n });\n } catch (error) {\n throw new NetworkError(\n error instanceof Error ? error.message : \"Network request failed\",\n error instanceof Error ? error : undefined\n );\n }\n\n // Parse response\n const responseBody = (await response.json()) as {\n data?: T;\n error?: { code?: string; message?: string; details?: unknown };\n };\n\n // Handle errors\n if (!response.ok) {\n throw parseApiError(response.status, responseBody);\n }\n\n // Return data\n return responseBody.data as T;\n }\n\n /**\n * Get the current user's credit balance\n *\n * @returns User's credit information\n * @throws AuthenticationError if not authenticated\n */\n async getBalance(): Promise<UserCredits> {\n return this.request<UserCredits>(\"/api/v1/credits/balance\");\n }\n\n /**\n * Check if the user has sufficient credits for an operation\n *\n * @param operationType - Type of operation to check\n * @param customCost - Optional custom cost to check against\n * @returns Credit availability status\n * @throws AuthenticationError if not authenticated\n */\n async checkAvailability(\n operationType: string,\n customCost?: number\n ): Promise<CreditCheckResult> {\n return this.request<CreditCheckResult>(\"/api/v1/credits/availability\", {\n params: {\n operationType,\n customCost: customCost?.toString(),\n },\n });\n }\n\n /**\n * Reserve credits for an operation\n *\n * This is the first step in the two-phase commit pattern.\n * Reserved credits are held for 5 minutes before expiring.\n *\n * @param operationType - Type of operation\n * @param options - Optional reservation options\n * @returns Reservation details including ID and expiry\n * @throws InsufficientCreditsError if not enough credits\n * @throws AuthenticationError if not authenticated\n */\n async reserve(\n operationType: string,\n options?: {\n customCost?: number;\n resourceId?: string;\n resourceType?: string;\n }\n ): Promise<ReservationResult> {\n return this.request<ReservationResult>(\"/api/v1/credits/reserve\", {\n method: \"POST\",\n body: {\n operationType,\n ...options,\n },\n });\n }\n\n /**\n * Commit a reservation (deduct credits)\n *\n * This is the second step in the two-phase commit pattern.\n * Call this after your operation succeeds.\n *\n * @param reservationId - ID of the reservation to commit\n * @throws ReservationNotFoundError if reservation doesn't exist\n * @throws ReservationExpiredError if reservation has expired\n * @throws AuthenticationError if not authenticated\n */\n async commit(reservationId: string): Promise<void> {\n await this.request<{ message: string }>(\"/api/v1/credits/commit\", {\n method: \"POST\",\n body: { reservationId },\n });\n }\n\n /**\n * Release a reservation (refund credits)\n *\n * Call this if your operation fails after reserving credits.\n *\n * @param reservationId - ID of the reservation to release\n * @throws ReservationNotFoundError if reservation doesn't exist\n * @throws AuthenticationError if not authenticated\n */\n async release(reservationId: string): Promise<void> {\n await this.request<{ message: string }>(\"/api/v1/credits/release\", {\n method: \"POST\",\n body: { reservationId },\n });\n }\n\n /**\n * Get usage history for the current user\n *\n * @param options - Optional pagination options\n * @returns Paginated list of usage entries\n * @throws AuthenticationError if not authenticated\n */\n async getHistory(options?: PaginationOptions): Promise<UsageHistoryResponse> {\n return this.request<UsageHistoryResponse>(\"/api/v1/credits/history\", {\n params: {\n page: options?.page,\n limit: options?.limit,\n },\n });\n }\n\n /**\n * Get the available credits (balance + bonus - reserved)\n *\n * Convenience method that calculates available credits from balance.\n *\n * @returns Number of available credits\n * @throws AuthenticationError if not authenticated\n */\n async getAvailableCredits(): Promise<number> {\n const balance = await this.getBalance();\n return balance.balance + balance.bonusCredits - balance.reserved;\n }\n\n /**\n * Execute an operation with automatic credit handling\n *\n * This is a convenience method that handles the full two-phase commit\n * pattern automatically.\n *\n * @param operationType - Type of operation\n * @param operation - The operation to execute\n * @param options - Optional reservation options\n * @returns Result of the operation\n * @throws InsufficientCreditsError if not enough credits\n * @throws AuthenticationError if not authenticated\n *\n * @example\n * ```typescript\n * const result = await client.withCredits(\n * \"story_generation\",\n * async () => {\n * return generateStory(data);\n * }\n * );\n * ```\n */\n async withCredits<T>(\n operationType: string,\n operation: () => Promise<T>,\n options?: {\n customCost?: number;\n resourceId?: string;\n resourceType?: string;\n }\n ): Promise<T> {\n const reservation = await this.reserve(operationType, options);\n\n try {\n const result = await operation();\n await this.commit(reservation.reservationId);\n return result;\n } catch (error) {\n // Always attempt to release on failure\n try {\n await this.release(reservation.reservationId);\n } catch {\n // Ignore release errors, original error is more important\n }\n throw error;\n }\n }\n}\n","/**\n * AdminCreditsClient - Admin REST API client for the credits system\n *\n * Provides administrative access to the credits system for managing\n * user credits, configurations, and subscriptions.\n *\n * @example\n * ```typescript\n * const adminClient = new AdminCreditsClient({\n * baseUrl: process.env.CREDITS_API_URL,\n * apiKey: process.env.CREDITS_ADMIN_API_KEY,\n * });\n *\n * // Add credits to a user\n * await adminClient.addCredits(\"user-123\", 100, \"Support credit grant\");\n *\n * // Update subscription tier\n * await adminClient.updateSubscription(\"user-123\", \"premium\", \"2027-01-01T00:00:00Z\");\n *\n * // Update operation costs\n * await adminClient.updateCosts({\n * story_generation: 8,\n * image_generation: 12,\n * });\n * ```\n */\n\nimport type { AdminCreditsClientConfig, UserCredits } from \"./types.js\";\nimport { parseApiError, NetworkError } from \"./errors.js\";\n\n/**\n * Credits configuration returned by the API\n */\nexport interface CreditsConfig {\n operationCosts: Record<string, number>;\n tierConfigs: Record<\n string,\n {\n monthlyLimit: number;\n features: string[];\n }\n >;\n}\n\n/**\n * Response from listing users\n */\nexport interface ListUsersResponse {\n users: Array<{\n userId: string;\n balance: number;\n bonusCredits?: number;\n tier: string;\n monthlyUsed?: number;\n }>;\n hasMore: boolean;\n total?: number;\n}\n\n/**\n * Options for listing users\n */\nexport interface ListUsersOptions {\n page?: number;\n limit?: number;\n tier?: string;\n search?: string;\n}\n\nexport class AdminCreditsClient {\n private readonly baseUrl: string;\n private readonly apiKey: string;\n private readonly fetchFn: typeof fetch;\n\n constructor(config: AdminCreditsClientConfig) {\n // Ensure baseUrl doesn't have trailing slash\n this.baseUrl = config.baseUrl.replace(/\\/$/, \"\");\n this.apiKey = config.apiKey;\n this.fetchFn = config.fetch ?? fetch;\n }\n\n /**\n * Make an authenticated admin request to the API\n */\n private async request<T>(\n path: string,\n options: {\n method?: \"GET\" | \"POST\" | \"PUT\" | \"DELETE\";\n body?: unknown;\n params?: Record<string, string | number | undefined>;\n } = {}\n ): Promise<T> {\n const { method = \"GET\", body, params } = options;\n\n // Build URL with query params\n let url = `${this.baseUrl}${path}`;\n if (params) {\n const searchParams = new URLSearchParams();\n for (const [key, value] of Object.entries(params)) {\n if (value !== undefined) {\n searchParams.set(key, String(value));\n }\n }\n const queryString = searchParams.toString();\n if (queryString) {\n url += `?${queryString}`;\n }\n }\n\n // Build headers with admin API key\n const headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n \"x-api-key\": this.apiKey,\n };\n\n // Make the request\n let response: Response;\n try {\n response = await this.fetchFn(url, {\n method,\n headers,\n body: body ? JSON.stringify(body) : undefined,\n });\n } catch (error) {\n throw new NetworkError(\n error instanceof Error ? error.message : \"Network request failed\",\n error instanceof Error ? error : undefined\n );\n }\n\n // Parse response\n const responseBody = (await response.json()) as {\n data?: T;\n error?: { code?: string; message?: string; details?: unknown };\n };\n\n // Handle errors\n if (!response.ok) {\n throw parseApiError(response.status, responseBody);\n }\n\n // Return data\n return responseBody.data as T;\n }\n\n /**\n * Get a specific user's credits\n *\n * @param userId - ID of the user\n * @returns User's credit information\n * @throws AuthorizationError if API key is invalid\n */\n async getUserCredits(userId: string): Promise<UserCredits> {\n return this.request<UserCredits>(`/api/admin/credits/users/${userId}`);\n }\n\n /**\n * Add credits to a user's account\n *\n * Credits are added to bonusCredits, which don't expire on monthly reset.\n *\n * @param userId - ID of the user\n * @param amount - Amount of credits to add\n * @param description - Reason for adding credits\n * @throws AuthorizationError if API key is invalid\n */\n async addCredits(\n userId: string,\n amount: number,\n description: string\n ): Promise<void> {\n await this.request<{ message: string; newBalance: number }>(\n `/api/admin/credits/users/${userId}/add`,\n {\n method: \"POST\",\n body: { amount, description },\n }\n );\n }\n\n /**\n * Update a user's subscription tier\n *\n * @param userId - ID of the user\n * @param tier - New subscription tier\n * @param expiresAt - Optional expiration date (ISO 8601)\n * @throws AuthorizationError if API key is invalid\n */\n async updateSubscription(\n userId: string,\n tier: string,\n expiresAt?: string\n ): Promise<void> {\n const body: { tier: string; expiresAt?: string } = { tier };\n if (expiresAt) {\n body.expiresAt = expiresAt;\n }\n\n await this.request<{ message: string }>(\n `/api/admin/credits/users/${userId}/subscription`,\n {\n method: \"PUT\",\n body,\n }\n );\n }\n\n /**\n * Get the current credits configuration\n *\n * @returns Credits system configuration\n * @throws AuthorizationError if API key is invalid\n */\n async getConfig(): Promise<CreditsConfig> {\n return this.request<CreditsConfig>(\"/api/admin/credits/config\");\n }\n\n /**\n * Update operation costs\n *\n * @param costs - New operation costs (operation type -> cost)\n * @throws AuthorizationError if API key is invalid\n */\n async updateCosts(costs: Record<string, number>): Promise<void> {\n await this.request<{ message: string }>(\"/api/admin/credits/config/costs\", {\n method: \"PUT\",\n body: { operationCosts: costs },\n });\n }\n\n /**\n * List all users with their credits\n *\n * @param options - Pagination and filtering options\n * @returns Paginated list of users\n * @throws AuthorizationError if API key is invalid\n */\n async listUsers(options?: ListUsersOptions): Promise<ListUsersResponse> {\n return this.request<ListUsersResponse>(\"/api/admin/credits/users\", {\n params: {\n page: options?.page,\n limit: options?.limit,\n tier: options?.tier,\n search: options?.search,\n },\n });\n }\n\n /**\n * Deduct credits from a user's account\n *\n * @param userId - ID of the user\n * @param amount - Amount of credits to deduct\n * @param description - Reason for deducting credits\n * @throws AuthorizationError if API key is invalid\n */\n async deductCredits(\n userId: string,\n amount: number,\n description: string\n ): Promise<void> {\n await this.request<{ message: string; newBalance: number }>(\n `/api/admin/credits/users/${userId}/deduct`,\n {\n method: \"POST\",\n body: { amount, description },\n }\n );\n }\n\n /**\n * Get usage statistics for a user\n *\n * @param userId - ID of the user\n * @param options - Optional date range\n * @returns Usage statistics\n * @throws AuthorizationError if API key is invalid\n */\n async getUserStats(\n userId: string,\n options?: { startDate?: string; endDate?: string }\n ): Promise<{\n totalCreditsUsed: number;\n operationBreakdown: Record<string, number>;\n successRate: number;\n }> {\n return this.request<{\n totalCreditsUsed: number;\n operationBreakdown: Record<string, number>;\n successRate: number;\n }>(`/api/admin/credits/users/${userId}/stats`, {\n params: {\n startDate: options?.startDate,\n endDate: options?.endDate,\n },\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACgHO,IAAM,eAAe;AAAA,EAC1B,eAAe;AAAA,EACf,sBAAsB;AAAA,EACtB,qBAAqB;AAAA,EACrB,sBAAsB;AAAA,EACtB,uBAAuB;AAAA,EACvB,qBAAqB;AAAA,EACrB,kBAAkB;AAAA,EAClB,cAAc;AAAA,EACd,eAAe;AACjB;;;AC/GO,IAAM,kBAAN,cAA8B,MAAM;AAAA,EACzB;AAAA,EACA;AAAA,EACA;AAAA,EAEhB,YACE,SACA,MACA,SAKA;AACA,UAAM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B,4BAA4B;AAAA,MACjE,QAAQ;AAAA,QACN,MAAM,SAAS;AAAA,QACf,OAAO,SAAS;AAAA,QAChB,MAAM,SAAS;AAAA,QACf,QAAQ,SAAS;AAAA,MACnB;AAAA,IACF,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,MAAM,cACJ,QACA,QACA,aACe;AACf,UAAM,KAAK;AAAA,MACT,4BAA4B,MAAM;AAAA,MAClC;AAAA,QACE,QAAQ;AAAA,QACR,MAAM,EAAE,QAAQ,YAAY;AAAA,MAC9B;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,MAAM,aACJ,QACA,SAKC;AACD,WAAO,KAAK,QAIT,4BAA4B,MAAM,UAAU;AAAA,MAC7C,QAAQ;AAAA,QACN,WAAW,SAAS;AAAA,QACpB,SAAS,SAAS;AAAA,MACpB;AAAA,IACF,CAAC;AAAA,EACH;AACF;","names":[]}
|
package/dist/sdk/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { b as PortableUserCredits, g as CreditCheckResult } from '../types-
|
|
2
|
-
export { c as CreditReservation, S as SubscriptionTier } from '../types-
|
|
1
|
+
import { b as PortableUserCredits, g as CreditCheckResult } from '../types-CjLj3GW5.cjs';
|
|
2
|
+
export { c as CreditReservation, S as SubscriptionTier } from '../types-CjLj3GW5.cjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* SDK types for the credits system
|
package/dist/sdk/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { b as PortableUserCredits, g as CreditCheckResult } from '../types-
|
|
2
|
-
export { c as CreditReservation, S as SubscriptionTier } from '../types-
|
|
1
|
+
import { b as PortableUserCredits, g as CreditCheckResult } from '../types-CjLj3GW5.js';
|
|
2
|
+
export { c as CreditReservation, S as SubscriptionTier } from '../types-CjLj3GW5.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* SDK types for the credits system
|