@nehorai/credits 1.4.0 → 1.6.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 (38) hide show
  1. package/dist/adapters/index.cjs +13 -3
  2. package/dist/adapters/index.cjs.map +1 -1
  3. package/dist/adapters/index.d.cts +2 -2
  4. package/dist/adapters/index.d.ts +2 -2
  5. package/dist/adapters/index.js +13 -3
  6. package/dist/adapters/index.js.map +1 -1
  7. package/dist/core/index.cjs +13 -3
  8. package/dist/core/index.cjs.map +1 -1
  9. package/dist/core/index.d.cts +3 -3
  10. package/dist/core/index.d.ts +3 -3
  11. package/dist/core/index.js +13 -3
  12. package/dist/core/index.js.map +1 -1
  13. package/dist/index.cjs +155 -17
  14. package/dist/index.cjs.map +1 -1
  15. package/dist/index.d.cts +75 -16
  16. package/dist/index.d.ts +75 -16
  17. package/dist/index.js +146 -17
  18. package/dist/index.js.map +1 -1
  19. package/dist/repository/index.cjs +47 -11
  20. package/dist/repository/index.cjs.map +1 -1
  21. package/dist/repository/index.d.cts +3 -3
  22. package/dist/repository/index.d.ts +3 -3
  23. package/dist/repository/index.js +47 -11
  24. package/dist/repository/index.js.map +1 -1
  25. package/dist/sdk/index.d.cts +2 -2
  26. package/dist/sdk/index.d.ts +2 -2
  27. package/dist/service/index.cjs +130 -10
  28. package/dist/service/index.cjs.map +1 -1
  29. package/dist/service/index.d.cts +43 -3
  30. package/dist/service/index.d.ts +43 -3
  31. package/dist/service/index.js +130 -10
  32. package/dist/service/index.js.map +1 -1
  33. package/dist/{types-D1vdeBcA.d.cts → types-CZNv1U3e.d.cts} +1 -1
  34. package/dist/{types-BBxIWHK2.d.cts → types-CjLj3GW5.d.cts} +37 -3
  35. package/dist/{types-BBxIWHK2.d.ts → types-CjLj3GW5.d.ts} +37 -3
  36. package/dist/{types-aJsyv8df.d.ts → types-G4WK0rM-.d.ts} +1 -1
  37. package/package.json +10 -9
  38. package/LICENSE +0 -21
@@ -45,9 +45,16 @@ declare function toPortableTimestamp(value: unknown): string;
45
45
  */
46
46
  declare function toDate(value: unknown): Date;
47
47
  /**
48
- * Subscription tiers for the credits system
48
+ * Built-in tier ids (autocomplete) — apps may add more via config.
49
49
  */
50
- type SubscriptionTier = "free" | "basic" | "premium" | "unlimited";
50
+ type BuiltinTier = "free" | "basic" | "premium" | "unlimited";
51
+ /**
52
+ * Subscription tiers for the credits system.
53
+ *
54
+ * Any built-in OR any config-defined tier id. The `(string & {})` keeps builtin
55
+ * autocomplete while accepting arbitrary configured tiers.
56
+ */
57
+ type SubscriptionTier = BuiltinTier | (string & {});
51
58
  /**
52
59
  * Operation types for cost tracking (provider-agnostic)
53
60
  *
@@ -129,6 +136,27 @@ interface CreditCheckResult {
129
136
  /** Shortfall if insufficient */
130
137
  shortfall: number;
131
138
  }
139
+ /**
140
+ * Result of a one-shot atomic credit deduction (see `CreditsService.deductCredits`).
141
+ *
142
+ * Discriminated on `success` so callers get exhaustive narrowing:
143
+ * - `success: true` -> `newBalance` is populated, `available`/`shortfall` are not present.
144
+ * - `success: false` -> `available`/`shortfall` describe the shortfall, no mutation occurred.
145
+ */
146
+ type DeductCreditsResult = {
147
+ success: true;
148
+ /** Combined balance (balance + bonusCredits) after the deduction */
149
+ newBalance: number;
150
+ } | {
151
+ success: false;
152
+ reason: "insufficient";
153
+ /** Combined balance (balance + bonusCredits - reserved) available before the attempt */
154
+ available: number;
155
+ /** Credits that were requested */
156
+ required: number;
157
+ /** How many more credits would have been needed */
158
+ shortfall: number;
159
+ };
132
160
  /**
133
161
  * Result of monthly reset operation
134
162
  */
@@ -257,6 +285,12 @@ interface TierConfig {
257
285
  monthlyCredits: number;
258
286
  priceUsd: number;
259
287
  features: string[];
288
+ /** Free/default tier marker — no subscription expiry, balance untouched on tier change. Defaults to priceUsd === 0. */
289
+ isFree?: boolean;
290
+ /** Unlimited tier marker. Defaults to monthlyCredits === 0. */
291
+ unlimited?: boolean;
292
+ /** Tier assigned to brand-new users. Defaults to the tier flagged isFree. */
293
+ isDefault?: boolean;
260
294
  }
261
295
  /**
262
296
  * Options for withCredits HOF
@@ -274,4 +308,4 @@ interface WithCreditsOptions {
274
308
  resourceType?: ResourceType;
275
309
  }
276
310
 
277
- export { type AIProviderType as A, type CreditSource as C, type JournalReferenceType as J, type MonthlyResetResult as M, type PortableTransaction as P, type ReservationStatus as R, type SubscriptionTier as S, type TierConfig as T, type UsageHistoryEntry as U, type WithCreditsOptions as W, type CreditOperationType as a, type PortableUserCredits as b, type PortableReservation as c, type PortableUsageLog as d, type SubscriptionExpiryResult as e, type PortableJournalEntry as f, type CreditCheckResult as g, type ResourceType as h, type TransactionType as i, type UsageHistoryResponse as j, calculateAvailableCredits as k, toPortableTimestamp as l, toDate as t };
311
+ export { type AIProviderType as A, type BuiltinTier as B, type CreditSource as C, type DeductCreditsResult as D, type JournalReferenceType as J, type MonthlyResetResult as M, type PortableTransaction as P, type ReservationStatus as R, type SubscriptionTier as S, type TierConfig as T, type UsageHistoryEntry as U, type WithCreditsOptions as W, type CreditOperationType as a, type PortableUserCredits as b, type PortableReservation as c, type PortableUsageLog as d, type SubscriptionExpiryResult as e, type PortableJournalEntry as f, type CreditCheckResult as g, type ResourceType as h, type TransactionType as i, type UsageHistoryResponse as j, calculateAvailableCredits as k, toPortableTimestamp as l, toDate as t };
@@ -1,4 +1,4 @@
1
- import { C as CreditSource, J as JournalReferenceType, a as CreditOperationType, P as PortableTransaction, A as AIProviderType, S as SubscriptionTier, b as PortableUserCredits, c as PortableReservation, R as ReservationStatus, d as PortableUsageLog, M as MonthlyResetResult, e as SubscriptionExpiryResult, f as PortableJournalEntry } from './types-BBxIWHK2.js';
1
+ import { C as CreditSource, J as JournalReferenceType, a as CreditOperationType, P as PortableTransaction, A as AIProviderType, S as SubscriptionTier, b as PortableUserCredits, c as PortableReservation, R as ReservationStatus, d as PortableUsageLog, M as MonthlyResetResult, e as SubscriptionExpiryResult, f as PortableJournalEntry } from './types-CjLj3GW5.js';
2
2
 
3
3
  /**
4
4
  * Input for creating a credit reservation
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nehorai/credits",
3
- "version": "1.4.0",
3
+ "version": "1.6.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -52,6 +52,14 @@
52
52
  "url": "https://github.com/NehoraiHadad/nehorai-plugins",
53
53
  "directory": "packages/credits"
54
54
  },
55
+ "scripts": {
56
+ "build": "tsup",
57
+ "dev": "tsup --watch",
58
+ "typecheck": "tsc --noEmit",
59
+ "test": "vitest run",
60
+ "test:watch": "vitest",
61
+ "prepublishOnly": "pnpm build"
62
+ },
55
63
  "dependencies": {
56
64
  "zod": "^3.24.2"
57
65
  },
@@ -59,12 +67,5 @@
59
67
  "typescript": "^5.7.3",
60
68
  "vitest": "^3.0.5",
61
69
  "@types/node": "^22.13.4"
62
- },
63
- "scripts": {
64
- "build": "tsup",
65
- "dev": "tsup --watch",
66
- "typecheck": "tsc --noEmit",
67
- "test": "vitest run",
68
- "test:watch": "vitest"
69
70
  }
70
- }
71
+ }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 Nehorai Hadad
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.