@nehorai/credits 2.0.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +384 -377
  3. package/dist/adapters/index.cjs +12 -8
  4. package/dist/adapters/index.cjs.map +1 -1
  5. package/dist/adapters/index.d.cts +3 -2
  6. package/dist/adapters/index.d.ts +3 -2
  7. package/dist/adapters/index.js +12 -8
  8. package/dist/adapters/index.js.map +1 -1
  9. package/dist/auth/index.cjs.map +1 -1
  10. package/dist/auth/index.js.map +1 -1
  11. package/dist/core/index.cjs +106 -4
  12. package/dist/core/index.cjs.map +1 -1
  13. package/dist/core/index.d.cts +92 -5
  14. package/dist/core/index.d.ts +92 -5
  15. package/dist/core/index.js +96 -4
  16. package/dist/core/index.js.map +1 -1
  17. package/dist/index.cjs +282 -45
  18. package/dist/index.cjs.map +1 -1
  19. package/dist/index.d.cts +9 -7
  20. package/dist/index.d.ts +9 -7
  21. package/dist/index.js +270 -45
  22. package/dist/index.js.map +1 -1
  23. package/dist/repository/index.cjs +251 -41
  24. package/dist/repository/index.cjs.map +1 -1
  25. package/dist/repository/index.d.cts +34 -6
  26. package/dist/repository/index.d.ts +34 -6
  27. package/dist/repository/index.js +248 -40
  28. package/dist/repository/index.js.map +1 -1
  29. package/dist/sdk/index.cjs.map +1 -1
  30. package/dist/sdk/index.d.cts +2 -2
  31. package/dist/sdk/index.d.ts +2 -2
  32. package/dist/sdk/index.js.map +1 -1
  33. package/dist/service/index.cjs +12 -6
  34. package/dist/service/index.cjs.map +1 -1
  35. package/dist/service/index.d.cts +2 -2
  36. package/dist/service/index.d.ts +2 -2
  37. package/dist/service/index.js +12 -6
  38. package/dist/service/index.js.map +1 -1
  39. package/dist/{types-BmG5Omzj.d.ts → types-3c-IAwZI.d.ts} +38 -3
  40. package/dist/{types-DJ8yqaVr.d.cts → types-MaViK5tD.d.cts} +23 -1
  41. package/dist/{types-DJ8yqaVr.d.ts → types-MaViK5tD.d.ts} +23 -1
  42. package/dist/{types-C8hZMe8a.d.cts → types-XSjNXO5l.d.cts} +38 -3
  43. package/package.json +1 -1
@@ -1,5 +1,5 @@
1
- import { c as PortableUserCredits, g as CreditCheckResult, C as CreditOperationType, a as PortableReservation, d as PortableUsageLog, D as DeductCreditsResult, S as SubscriptionTier, j as UsageHistoryResponse, f as PortableJournalEntry } from '../types-DJ8yqaVr.cjs';
2
- import { I as ICreditRepository, n as ReserveOutcome, l as ReservationTransitionOptions, C as CommitOutcome, R as ReleaseOutcome, A as AddCreditsAtomicOptions, a as AddCreditsOutcome, J as JournalEntryQuery } from '../types-C8hZMe8a.cjs';
1
+ import { c as PortableUserCredits, g as CreditCheckResult, C as CreditOperationType, a as PortableReservation, d as PortableUsageLog, D as DeductCreditsResult, S as SubscriptionTier, j as UsageHistoryResponse, f as PortableJournalEntry } from '../types-MaViK5tD.cjs';
2
+ import { I as ICreditRepository, o as ReserveOutcome, m as ReservationTransitionOptions, C as CommitOutcome, R as ReleaseOutcome, A as AddCreditsAtomicOptions, a as AddCreditsOutcome, J as JournalEntryQuery } from '../types-XSjNXO5l.cjs';
3
3
 
4
4
  /**
5
5
  * Notification callback type for low balance notifications
@@ -1,5 +1,5 @@
1
- import { c as PortableUserCredits, g as CreditCheckResult, C as CreditOperationType, a as PortableReservation, d as PortableUsageLog, D as DeductCreditsResult, S as SubscriptionTier, j as UsageHistoryResponse, f as PortableJournalEntry } from '../types-DJ8yqaVr.js';
2
- import { I as ICreditRepository, n as ReserveOutcome, l as ReservationTransitionOptions, C as CommitOutcome, R as ReleaseOutcome, A as AddCreditsAtomicOptions, a as AddCreditsOutcome, J as JournalEntryQuery } from '../types-BmG5Omzj.js';
1
+ import { c as PortableUserCredits, g as CreditCheckResult, C as CreditOperationType, a as PortableReservation, d as PortableUsageLog, D as DeductCreditsResult, S as SubscriptionTier, j as UsageHistoryResponse, f as PortableJournalEntry } from '../types-MaViK5tD.js';
2
+ import { I as ICreditRepository, o as ReserveOutcome, m as ReservationTransitionOptions, C as CommitOutcome, R as ReleaseOutcome, A as AddCreditsAtomicOptions, a as AddCreditsOutcome, J as JournalEntryQuery } from '../types-3c-IAwZI.js';
3
3
 
4
4
  /**
5
5
  * Notification callback type for low balance notifications
@@ -199,11 +199,13 @@ var creditSystemConfigSchema = z.object({
199
199
  /**
200
200
  * Operation costs in credits
201
201
  * Keys are operation type names (any non-empty string)
202
- * Values are positive numbers representing credit cost
202
+ * Values are non-negative numbers representing credit cost. 0 marks a free
203
+ * operation: `withCredits` runs it without touching the ledger. Amounts that
204
+ * move credits stay strictly positive, so `reserveCredits` refuses a 0.
203
205
  */
204
206
  operationCosts: z.record(
205
207
  z.string().min(1),
206
- z.number().positive()
208
+ z.number().nonnegative()
207
209
  ),
208
210
  /**
209
211
  * Display labels for operation types (English canonical labels).
@@ -225,9 +227,9 @@ var creditSystemConfigSchema = z.object({
225
227
  */
226
228
  reservationExpiryMs: z.number().positive().default(5 * 60 * 1e3),
227
229
  /**
228
- * Default credits for new users (free tier)
230
+ * Default credits for new users (free tier). 0 means no signup grant.
229
231
  */
230
- defaultFreeCredits: z.number().positive().default(25),
232
+ defaultFreeCredits: z.number().min(0).default(25),
231
233
  /**
232
234
  * Grace period in days after subscription expires before downgrade
233
235
  */
@@ -496,6 +498,8 @@ function toClientUserCredits(credits) {
496
498
  monthlyUsed: credits.monthlyUsed,
497
499
  monthlyResetAt: toISOString(credits.monthlyResetAt),
498
500
  subscriptionExpiresAt: credits.subscriptionExpiresAt ? toISOString(credits.subscriptionExpiresAt) : null,
501
+ ...credits.expiredHoldBacking !== void 0 ? { expiredHoldBacking: credits.expiredHoldBacking } : {},
502
+ ...credits.signupGrant !== void 0 ? { signupGrant: credits.signupGrant } : {},
499
503
  createdAt: toISOString(credits.createdAt),
500
504
  updatedAt: toISOString(credits.updatedAt)
501
505
  };
@@ -743,9 +747,11 @@ var CreditsService = class {
743
747
  source: "monthly_reset",
744
748
  referenceId: `reset-${Date.now()}`,
745
749
  referenceType: "reset",
746
- description: `Monthly credit reset for ${data.tier} tier.`,
750
+ // The tier the reset ran on, which a concurrent downgrade may have
751
+ // changed since `data` was read.
752
+ description: `Monthly credit reset for ${resetResult.credits.tier} tier.`,
747
753
  metadata: {
748
- tier: data.tier,
754
+ tier: resetResult.credits.tier,
749
755
  previousBalance: data.balance,
750
756
  newBalance: resetResult.credits.balance
751
757
  }