@nehorai/credits 2.2.0 → 3.0.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/dist/adapters/index.cjs +1 -0
- 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 +1 -0
- package/dist/adapters/index.js.map +1 -1
- package/dist/core/index.cjs +139 -23
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.cts +113 -22
- package/dist/core/index.d.ts +113 -22
- package/dist/core/index.js +128 -21
- package/dist/core/index.js.map +1 -1
- package/dist/index.cjs +246 -61
- 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 +235 -59
- package/dist/index.js.map +1 -1
- package/dist/repository/index.cjs +183 -41
- package/dist/repository/index.cjs.map +1 -1
- package/dist/repository/index.d.cts +6 -5
- package/dist/repository/index.d.ts +6 -5
- package/dist/repository/index.js +183 -41
- package/dist/repository/index.js.map +1 -1
- package/dist/sdk/index.cjs +1 -0
- 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 +1 -0
- package/dist/sdk/index.js.map +1 -1
- package/dist/service/index.cjs +25 -16
- package/dist/service/index.cjs.map +1 -1
- package/dist/service/index.d.cts +2 -2
- package/dist/service/index.d.ts +2 -2
- package/dist/service/index.js +25 -16
- package/dist/service/index.js.map +1 -1
- package/dist/{types-MaViK5tD.d.cts → types-C3sxDvNb.d.cts} +29 -6
- package/dist/{types-MaViK5tD.d.ts → types-C3sxDvNb.d.ts} +29 -6
- package/dist/{types-3c-IAwZI.d.ts → types-CY-oHIMA.d.ts} +19 -3
- package/dist/{types-XSjNXO5l.d.cts → types-DOjguhEX.d.cts} +19 -3
- package/package.json +1 -1
|
@@ -99,6 +99,11 @@ interface PortableUserCredits {
|
|
|
99
99
|
* Absent means 0. See `planBalanceCap`.
|
|
100
100
|
*/
|
|
101
101
|
expiredHoldBacking?: number;
|
|
102
|
+
/**
|
|
103
|
+
* Where those credits end among the open holds, so a hold's share needs no write
|
|
104
|
+
* to the hold. Internal to the repositories: `toClientUserCredits` drops it.
|
|
105
|
+
*/
|
|
106
|
+
expiredBackingFrontier?: ExpiredBackingFrontier | null;
|
|
102
107
|
/**
|
|
103
108
|
* Set when the account was created with a signup-grant claim: the amount
|
|
104
109
|
* granted, 0 when the claim was already taken. Absent on any other account.
|
|
@@ -109,6 +114,21 @@ interface PortableUserCredits {
|
|
|
109
114
|
/** Last update timestamp - ISO 8601 */
|
|
110
115
|
updatedAt: string;
|
|
111
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* The newest open hold carrying marked credits (`expiredHoldBacking`).
|
|
119
|
+
*
|
|
120
|
+
* Credits are marked oldest hold first, so a single point describes every share:
|
|
121
|
+
* a hold older than this carries its full amount, this hold carries `share`, and a
|
|
122
|
+
* newer one carries nothing. A marked credit leaves the balance when its hold closes,
|
|
123
|
+
* however it closes. See `allocateExpiredBacking` and `holdExpiredBacking`.
|
|
124
|
+
*/
|
|
125
|
+
interface ExpiredBackingFrontier {
|
|
126
|
+
/** The hold's `holdPlacedAt`. */
|
|
127
|
+
holdPlacedAt: string;
|
|
128
|
+
/** The hold's id, which orders holds placed in the same millisecond. */
|
|
129
|
+
id: string;
|
|
130
|
+
share: number;
|
|
131
|
+
}
|
|
112
132
|
/**
|
|
113
133
|
* Credit reservation status for two-phase commit
|
|
114
134
|
*/
|
|
@@ -148,10 +168,8 @@ interface PortableReservation {
|
|
|
148
168
|
*/
|
|
149
169
|
holdPlacedAt?: string;
|
|
150
170
|
/**
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
* hold placed after the expiry. Open holds' shares sum to the account's
|
|
154
|
-
* `expiredHoldBacking`.
|
|
171
|
+
* This hold's share of the marked credits, stored by 2.2.0. A hold placed since carries
|
|
172
|
+
* none and takes its share from the account's frontier. See `holdExpiredBacking`.
|
|
155
173
|
*/
|
|
156
174
|
expiredBacking?: number;
|
|
157
175
|
}
|
|
@@ -268,8 +286,13 @@ interface PortableJournalEntry {
|
|
|
268
286
|
entryType: "debit" | "credit";
|
|
269
287
|
/** Amount of credits involved */
|
|
270
288
|
amount: number;
|
|
271
|
-
/**
|
|
289
|
+
/** `balance + bonusCredits` after this entry */
|
|
272
290
|
balanceAfter: number;
|
|
291
|
+
/**
|
|
292
|
+
* `balance + bonusCredits` before this entry, read in the same atomic step.
|
|
293
|
+
* Absent on entries written before 3.0.0.
|
|
294
|
+
*/
|
|
295
|
+
balanceBefore?: number;
|
|
273
296
|
/** Source of the credit change */
|
|
274
297
|
source: CreditSource;
|
|
275
298
|
/** Reference ID (e.g., reservationId, transactionId) */
|
|
@@ -368,4 +391,4 @@ interface WithCreditsOptions {
|
|
|
368
391
|
resourceType?: ResourceType;
|
|
369
392
|
}
|
|
370
393
|
|
|
371
|
-
export { type AIProviderType as A, type BuiltinTier as B, type CreditOperationType 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 PortableReservation as a, type CreditSource as b, type PortableUserCredits 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 };
|
|
394
|
+
export { type AIProviderType as A, type BuiltinTier as B, type CreditOperationType as C, type DeductCreditsResult as D, type ExpiredBackingFrontier as E, 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 PortableReservation as a, type CreditSource as b, type PortableUserCredits 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 };
|
|
@@ -99,6 +99,11 @@ interface PortableUserCredits {
|
|
|
99
99
|
* Absent means 0. See `planBalanceCap`.
|
|
100
100
|
*/
|
|
101
101
|
expiredHoldBacking?: number;
|
|
102
|
+
/**
|
|
103
|
+
* Where those credits end among the open holds, so a hold's share needs no write
|
|
104
|
+
* to the hold. Internal to the repositories: `toClientUserCredits` drops it.
|
|
105
|
+
*/
|
|
106
|
+
expiredBackingFrontier?: ExpiredBackingFrontier | null;
|
|
102
107
|
/**
|
|
103
108
|
* Set when the account was created with a signup-grant claim: the amount
|
|
104
109
|
* granted, 0 when the claim was already taken. Absent on any other account.
|
|
@@ -109,6 +114,21 @@ interface PortableUserCredits {
|
|
|
109
114
|
/** Last update timestamp - ISO 8601 */
|
|
110
115
|
updatedAt: string;
|
|
111
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* The newest open hold carrying marked credits (`expiredHoldBacking`).
|
|
119
|
+
*
|
|
120
|
+
* Credits are marked oldest hold first, so a single point describes every share:
|
|
121
|
+
* a hold older than this carries its full amount, this hold carries `share`, and a
|
|
122
|
+
* newer one carries nothing. A marked credit leaves the balance when its hold closes,
|
|
123
|
+
* however it closes. See `allocateExpiredBacking` and `holdExpiredBacking`.
|
|
124
|
+
*/
|
|
125
|
+
interface ExpiredBackingFrontier {
|
|
126
|
+
/** The hold's `holdPlacedAt`. */
|
|
127
|
+
holdPlacedAt: string;
|
|
128
|
+
/** The hold's id, which orders holds placed in the same millisecond. */
|
|
129
|
+
id: string;
|
|
130
|
+
share: number;
|
|
131
|
+
}
|
|
112
132
|
/**
|
|
113
133
|
* Credit reservation status for two-phase commit
|
|
114
134
|
*/
|
|
@@ -148,10 +168,8 @@ interface PortableReservation {
|
|
|
148
168
|
*/
|
|
149
169
|
holdPlacedAt?: string;
|
|
150
170
|
/**
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
* hold placed after the expiry. Open holds' shares sum to the account's
|
|
154
|
-
* `expiredHoldBacking`.
|
|
171
|
+
* This hold's share of the marked credits, stored by 2.2.0. A hold placed since carries
|
|
172
|
+
* none and takes its share from the account's frontier. See `holdExpiredBacking`.
|
|
155
173
|
*/
|
|
156
174
|
expiredBacking?: number;
|
|
157
175
|
}
|
|
@@ -268,8 +286,13 @@ interface PortableJournalEntry {
|
|
|
268
286
|
entryType: "debit" | "credit";
|
|
269
287
|
/** Amount of credits involved */
|
|
270
288
|
amount: number;
|
|
271
|
-
/**
|
|
289
|
+
/** `balance + bonusCredits` after this entry */
|
|
272
290
|
balanceAfter: number;
|
|
291
|
+
/**
|
|
292
|
+
* `balance + bonusCredits` before this entry, read in the same atomic step.
|
|
293
|
+
* Absent on entries written before 3.0.0.
|
|
294
|
+
*/
|
|
295
|
+
balanceBefore?: number;
|
|
273
296
|
/** Source of the credit change */
|
|
274
297
|
source: CreditSource;
|
|
275
298
|
/** Reference ID (e.g., reservationId, transactionId) */
|
|
@@ -368,4 +391,4 @@ interface WithCreditsOptions {
|
|
|
368
391
|
resourceType?: ResourceType;
|
|
369
392
|
}
|
|
370
393
|
|
|
371
|
-
export { type AIProviderType as A, type BuiltinTier as B, type CreditOperationType 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 PortableReservation as a, type CreditSource as b, type PortableUserCredits 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 };
|
|
394
|
+
export { type AIProviderType as A, type BuiltinTier as B, type CreditOperationType as C, type DeductCreditsResult as D, type ExpiredBackingFrontier as E, 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 PortableReservation as a, type CreditSource as b, type PortableUserCredits 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 { P as PortableTransaction, a as PortableReservation, R as ReservationStatus, C as CreditOperationType, b as CreditSource, J as JournalReferenceType, A as AIProviderType, S as SubscriptionTier, c as PortableUserCredits, d as PortableUsageLog, M as MonthlyResetResult, e as SubscriptionExpiryResult, f as PortableJournalEntry } from './types-
|
|
1
|
+
import { P as PortableTransaction, a as PortableReservation, R as ReservationStatus, C as CreditOperationType, b as CreditSource, J as JournalReferenceType, A as AIProviderType, S as SubscriptionTier, c as PortableUserCredits, d as PortableUsageLog, M as MonthlyResetResult, e as SubscriptionExpiryResult, f as PortableJournalEntry } from './types-C3sxDvNb.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Typed outcomes for the V2 reservation boundary.
|
|
@@ -326,7 +326,10 @@ interface CreateJournalEntryInput {
|
|
|
326
326
|
userId: string;
|
|
327
327
|
entryType: "debit" | "credit";
|
|
328
328
|
amount: number;
|
|
329
|
+
/** `balance + bonusCredits` after this entry. */
|
|
329
330
|
balanceAfter: number;
|
|
331
|
+
/** `balance + bonusCredits` before this entry. */
|
|
332
|
+
balanceBefore?: number;
|
|
330
333
|
source: CreditSource;
|
|
331
334
|
referenceId: string;
|
|
332
335
|
referenceType: JournalReferenceType;
|
|
@@ -335,6 +338,18 @@ interface CreateJournalEntryInput {
|
|
|
335
338
|
/** Deterministic key making this entry unique per user (V2, optional). */
|
|
336
339
|
idempotencyKey?: string;
|
|
337
340
|
}
|
|
341
|
+
/**
|
|
342
|
+
* The journal entry of a one-shot deduct, written in the deduct's own atomic step.
|
|
343
|
+
*/
|
|
344
|
+
interface DeductJournalInput {
|
|
345
|
+
referenceId: string;
|
|
346
|
+
description: string;
|
|
347
|
+
/** Defaults to `"operation_commit"`. */
|
|
348
|
+
source?: CreditSource;
|
|
349
|
+
/** Defaults to `"adjustment"`. */
|
|
350
|
+
referenceType?: JournalReferenceType;
|
|
351
|
+
metadata?: Record<string, unknown>;
|
|
352
|
+
}
|
|
338
353
|
/**
|
|
339
354
|
* Query options for journal entries
|
|
340
355
|
*/
|
|
@@ -556,9 +571,10 @@ interface ICreditRepository extends Partial<ICreditRepositoryV2>, Partial<ICredi
|
|
|
556
571
|
* @returns Combined totals (balance + bonusCredits) before and after.
|
|
557
572
|
* @throws If user has no credits doc or insufficient credits
|
|
558
573
|
*/
|
|
559
|
-
deductCreditsAtomic(userId: string, amount: number): Promise<{
|
|
574
|
+
deductCreditsAtomic(userId: string, amount: number, journal?: DeductJournalInput): Promise<{
|
|
560
575
|
previousBalance: number;
|
|
561
576
|
newBalance: number;
|
|
577
|
+
journalEntryId?: string;
|
|
562
578
|
}>;
|
|
563
579
|
/**
|
|
564
580
|
* Create a credit transaction *record* — no balance moves.
|
|
@@ -678,4 +694,4 @@ declare function supportsIdempotentCredit(repository: ICreditRepository): reposi
|
|
|
678
694
|
*/
|
|
679
695
|
declare function toClientUserCredits(credits: PortableUserCredits): PortableUserCredits;
|
|
680
696
|
|
|
681
|
-
export { type AddCreditsAtomicOptions as A, type CommitOutcome as C, type ExpireOutcome as E, type ICreditRepository as I, type JournalEntryQuery as J, type ReleaseOutcome as R, type TerminalReservationStatus as T, type UsageLogQuery as U, type AddCreditsOutcome as a, type AddCreditsV2Input as b, type CreateJournalEntryInput as c, type CreateReservationInput as d, type CreateTransactionInput as e, type CreateUsageLogInput as f, type CreditBalanceUpdate as g, type CreditPolicyOptions as h, type CreditRepositoryFactory as i, type ExpireReservationV2Options as j, type ICreditRepositoryCreditsV2 as k, type ICreditRepositoryV2 as l, type ReservationTransitionOptions as m, type ReserveCreditsV2Input as n, type ReserveOutcome as o, type TierUpdateInput as p, isCreditedOutcome as q, isReservedOutcome as r, isWinningOutcome as s, reservationJournalKey as t, supportsCreditsV2 as u, supportsIdempotentCredit as v, toClientUserCredits as w };
|
|
697
|
+
export { type AddCreditsAtomicOptions as A, type CommitOutcome as C, type DeductJournalInput as D, type ExpireOutcome as E, type ICreditRepository as I, type JournalEntryQuery as J, type ReleaseOutcome as R, type TerminalReservationStatus as T, type UsageLogQuery as U, type AddCreditsOutcome as a, type AddCreditsV2Input as b, type CreateJournalEntryInput as c, type CreateReservationInput as d, type CreateTransactionInput as e, type CreateUsageLogInput as f, type CreditBalanceUpdate as g, type CreditPolicyOptions as h, type CreditRepositoryFactory as i, type ExpireReservationV2Options as j, type ICreditRepositoryCreditsV2 as k, type ICreditRepositoryV2 as l, type ReservationTransitionOptions as m, type ReserveCreditsV2Input as n, type ReserveOutcome as o, type TierUpdateInput as p, isCreditedOutcome as q, isReservedOutcome as r, isWinningOutcome as s, reservationJournalKey as t, supportsCreditsV2 as u, supportsIdempotentCredit as v, toClientUserCredits as w };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { P as PortableTransaction, a as PortableReservation, R as ReservationStatus, C as CreditOperationType, b as CreditSource, J as JournalReferenceType, A as AIProviderType, S as SubscriptionTier, c as PortableUserCredits, d as PortableUsageLog, M as MonthlyResetResult, e as SubscriptionExpiryResult, f as PortableJournalEntry } from './types-
|
|
1
|
+
import { P as PortableTransaction, a as PortableReservation, R as ReservationStatus, C as CreditOperationType, b as CreditSource, J as JournalReferenceType, A as AIProviderType, S as SubscriptionTier, c as PortableUserCredits, d as PortableUsageLog, M as MonthlyResetResult, e as SubscriptionExpiryResult, f as PortableJournalEntry } from './types-C3sxDvNb.cjs';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Typed outcomes for the V2 reservation boundary.
|
|
@@ -326,7 +326,10 @@ interface CreateJournalEntryInput {
|
|
|
326
326
|
userId: string;
|
|
327
327
|
entryType: "debit" | "credit";
|
|
328
328
|
amount: number;
|
|
329
|
+
/** `balance + bonusCredits` after this entry. */
|
|
329
330
|
balanceAfter: number;
|
|
331
|
+
/** `balance + bonusCredits` before this entry. */
|
|
332
|
+
balanceBefore?: number;
|
|
330
333
|
source: CreditSource;
|
|
331
334
|
referenceId: string;
|
|
332
335
|
referenceType: JournalReferenceType;
|
|
@@ -335,6 +338,18 @@ interface CreateJournalEntryInput {
|
|
|
335
338
|
/** Deterministic key making this entry unique per user (V2, optional). */
|
|
336
339
|
idempotencyKey?: string;
|
|
337
340
|
}
|
|
341
|
+
/**
|
|
342
|
+
* The journal entry of a one-shot deduct, written in the deduct's own atomic step.
|
|
343
|
+
*/
|
|
344
|
+
interface DeductJournalInput {
|
|
345
|
+
referenceId: string;
|
|
346
|
+
description: string;
|
|
347
|
+
/** Defaults to `"operation_commit"`. */
|
|
348
|
+
source?: CreditSource;
|
|
349
|
+
/** Defaults to `"adjustment"`. */
|
|
350
|
+
referenceType?: JournalReferenceType;
|
|
351
|
+
metadata?: Record<string, unknown>;
|
|
352
|
+
}
|
|
338
353
|
/**
|
|
339
354
|
* Query options for journal entries
|
|
340
355
|
*/
|
|
@@ -556,9 +571,10 @@ interface ICreditRepository extends Partial<ICreditRepositoryV2>, Partial<ICredi
|
|
|
556
571
|
* @returns Combined totals (balance + bonusCredits) before and after.
|
|
557
572
|
* @throws If user has no credits doc or insufficient credits
|
|
558
573
|
*/
|
|
559
|
-
deductCreditsAtomic(userId: string, amount: number): Promise<{
|
|
574
|
+
deductCreditsAtomic(userId: string, amount: number, journal?: DeductJournalInput): Promise<{
|
|
560
575
|
previousBalance: number;
|
|
561
576
|
newBalance: number;
|
|
577
|
+
journalEntryId?: string;
|
|
562
578
|
}>;
|
|
563
579
|
/**
|
|
564
580
|
* Create a credit transaction *record* — no balance moves.
|
|
@@ -678,4 +694,4 @@ declare function supportsIdempotentCredit(repository: ICreditRepository): reposi
|
|
|
678
694
|
*/
|
|
679
695
|
declare function toClientUserCredits(credits: PortableUserCredits): PortableUserCredits;
|
|
680
696
|
|
|
681
|
-
export { type AddCreditsAtomicOptions as A, type CommitOutcome as C, type ExpireOutcome as E, type ICreditRepository as I, type JournalEntryQuery as J, type ReleaseOutcome as R, type TerminalReservationStatus as T, type UsageLogQuery as U, type AddCreditsOutcome as a, type AddCreditsV2Input as b, type CreateJournalEntryInput as c, type CreateReservationInput as d, type CreateTransactionInput as e, type CreateUsageLogInput as f, type CreditBalanceUpdate as g, type CreditPolicyOptions as h, type CreditRepositoryFactory as i, type ExpireReservationV2Options as j, type ICreditRepositoryCreditsV2 as k, type ICreditRepositoryV2 as l, type ReservationTransitionOptions as m, type ReserveCreditsV2Input as n, type ReserveOutcome as o, type TierUpdateInput as p, isCreditedOutcome as q, isReservedOutcome as r, isWinningOutcome as s, reservationJournalKey as t, supportsCreditsV2 as u, supportsIdempotentCredit as v, toClientUserCredits as w };
|
|
697
|
+
export { type AddCreditsAtomicOptions as A, type CommitOutcome as C, type DeductJournalInput as D, type ExpireOutcome as E, type ICreditRepository as I, type JournalEntryQuery as J, type ReleaseOutcome as R, type TerminalReservationStatus as T, type UsageLogQuery as U, type AddCreditsOutcome as a, type AddCreditsV2Input as b, type CreateJournalEntryInput as c, type CreateReservationInput as d, type CreateTransactionInput as e, type CreateUsageLogInput as f, type CreditBalanceUpdate as g, type CreditPolicyOptions as h, type CreditRepositoryFactory as i, type ExpireReservationV2Options as j, type ICreditRepositoryCreditsV2 as k, type ICreditRepositoryV2 as l, type ReservationTransitionOptions as m, type ReserveCreditsV2Input as n, type ReserveOutcome as o, type TierUpdateInput as p, isCreditedOutcome as q, isReservedOutcome as r, isWinningOutcome as s, reservationJournalKey as t, supportsCreditsV2 as u, supportsIdempotentCredit as v, toClientUserCredits as w };
|