@nehorai/credits 2.0.0 → 2.1.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/README.md +384 -377
- package/dist/adapters/index.cjs +10 -6
- package/dist/adapters/index.cjs.map +1 -1
- package/dist/adapters/index.d.cts +3 -2
- package/dist/adapters/index.d.ts +3 -2
- package/dist/adapters/index.js +10 -6
- 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 +4 -2
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.cts +4 -4
- package/dist/core/index.d.ts +4 -4
- package/dist/core/index.js +4 -2
- package/dist/core/index.js.map +1 -1
- package/dist/index.cjs +10 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -4
- package/dist/index.d.ts +6 -4
- package/dist/index.js +10 -6
- package/dist/index.js.map +1 -1
- package/dist/repository/index.cjs +4 -2
- package/dist/repository/index.cjs.map +1 -1
- package/dist/repository/index.d.cts +3 -3
- package/dist/repository/index.d.ts +3 -3
- package/dist/repository/index.js +4 -2
- 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 +4 -2
- 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 +4 -2
- package/dist/service/index.js.map +1 -1
- package/dist/{types-BmG5Omzj.d.ts → types-BhCdep_t.d.ts} +1 -1
- package/dist/{types-DJ8yqaVr.d.ts → types-Dzc443vy.d.cts} +5 -1
- package/dist/{types-DJ8yqaVr.d.cts → types-Dzc443vy.d.ts} +5 -1
- package/dist/{types-C8hZMe8a.d.cts → types-wtksMa4n.d.cts} +1 -1
- package/package.json +71 -70
- package/LICENSE +0 -21
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { S as SubscriptionTier, T as TierConfig, a as PortableReservation } from './types-
|
|
2
|
-
export { A as AIProviderType, B as BuiltinTier, g as CreditCheckResult, C as CreditOperationType, b as CreditSource, D as DeductCreditsResult, J as JournalReferenceType, M as MonthlyResetResult, f as PortableJournalEntry, P as PortableTransaction, d as PortableUsageLog, c as PortableUserCredits, R as ReservationStatus, h as ResourceType, g as SDKCreditCheckResult, e as SubscriptionExpiryResult, i as TransactionType, U as UsageHistoryEntry, j as UsageHistoryResponse, c as UserCredits, W as WithCreditsOptions, k as calculateAvailableCredits, t as toDate, l as toPortableTimestamp } from './types-
|
|
3
|
-
export { A as AddCreditsAtomicOptions, a as AddCreditsOutcome, b as AddCreditsV2Input, C as CommitOutcome, c as CreateJournalEntryInput, d as CreateReservationInput, e as CreateTransactionInput, f as CreateUsageLogInput, g as CreditBalanceUpdate, h as CreditRepositoryFactory, E as ExpireOutcome, i as ExpireReservationV2Options, I as ICreditRepository, j as ICreditRepositoryCreditsV2, k as ICreditRepositoryV2, J as JournalEntryQuery, R as ReleaseOutcome, l as ReservationTransitionOptions, m as ReserveCreditsV2Input, n as ReserveOutcome, T as TerminalReservationStatus, o as TierUpdateInput, U as UsageLogQuery, p as isCreditedOutcome, q as isReservedOutcome, r as isWinningOutcome, s as reservationJournalKey, t as supportsCreditsV2, u as supportsIdempotentCredit, v as toClientUserCredits } from './types-
|
|
1
|
+
import { S as SubscriptionTier, T as TierConfig, a as PortableReservation } from './types-Dzc443vy.cjs';
|
|
2
|
+
export { A as AIProviderType, B as BuiltinTier, g as CreditCheckResult, C as CreditOperationType, b as CreditSource, D as DeductCreditsResult, J as JournalReferenceType, M as MonthlyResetResult, f as PortableJournalEntry, P as PortableTransaction, d as PortableUsageLog, c as PortableUserCredits, R as ReservationStatus, h as ResourceType, g as SDKCreditCheckResult, e as SubscriptionExpiryResult, i as TransactionType, U as UsageHistoryEntry, j as UsageHistoryResponse, c as UserCredits, W as WithCreditsOptions, k as calculateAvailableCredits, t as toDate, l as toPortableTimestamp } from './types-Dzc443vy.cjs';
|
|
3
|
+
export { A as AddCreditsAtomicOptions, a as AddCreditsOutcome, b as AddCreditsV2Input, C as CommitOutcome, c as CreateJournalEntryInput, d as CreateReservationInput, e as CreateTransactionInput, f as CreateUsageLogInput, g as CreditBalanceUpdate, h as CreditRepositoryFactory, E as ExpireOutcome, i as ExpireReservationV2Options, I as ICreditRepository, j as ICreditRepositoryCreditsV2, k as ICreditRepositoryV2, J as JournalEntryQuery, R as ReleaseOutcome, l as ReservationTransitionOptions, m as ReserveCreditsV2Input, n as ReserveOutcome, T as TerminalReservationStatus, o as TierUpdateInput, U as UsageLogQuery, p as isCreditedOutcome, q as isReservedOutcome, r as isWinningOutcome, s as reservationJournalKey, t as supportsCreditsV2, u as supportsIdempotentCredit, v as toClientUserCredits } from './types-wtksMa4n.cjs';
|
|
4
4
|
export { CREDIT_AMOUNT_MAX, CREDIT_AMOUNT_SCALE, CreditError, CreditErrorCode, CreditErrorCodeType, PaymentEventPayload, RESERVATION_STATUSES, RESERVED_JOURNAL_KEY_PREFIX, ReservationIntegrityView, StoredPaymentEvent, TERMINAL_RESERVATION_STATUSES, assertDirectStatusWriteAllowed, assertHoldPlaced, assertKnownReservationStatus, assertPublicJournalKey, assertRepresentableAmount, assertRepresentableFields, assertRepresentableTierAmount, assertUnkeyedDirectReservation, assertUnreferencedDirectTransaction, assertValidCreditAmount, assertValidIdempotencyKey, assertValidStoredAmount, assertValidStoredAmountRaw, backedBalanceFloor, classifyDatabaseError, commitReservationWithJournal, createIdempotencyConflictError, createInsufficientCreditsError, createInvalidAmountError, createInvalidOperationTypeError, createPaymentRefConflictError, createReservationAlreadyProcessedError, createReservationExpiredError, createReservationNotFoundError, createTransientError, createUnsupportedOperationError, createUserNotFoundError, describePaymentMismatch, getSqlState, hasPlacedHold, isCreditError, isIdempotencyConflictError, isInsufficientCreditsError, isRepresentableAmount, isReservationStatus, isReservedJournalKey, isTerminalReservationStatus, isTransientDatabaseError, isTransientError, isUniqueViolation, isValidCreditAmount, isValidIdempotencyKey, normalizePaymentRef, numericToCents, releaseReservationWithJournal, reserveCreditsForOperation, sameAmount, storedMonthlyLimit, sumAmounts, terminalStatusOf, toCents } from './core/index.cjs';
|
|
5
5
|
export { D as DeferredExecutor, c as createDeferredExecutor, g as genericDeferred, n as noopDeferred, s as synchronousDeferred } from './deferred-C7rrRfkm.cjs';
|
|
6
6
|
import { z } from 'zod';
|
|
@@ -18,7 +18,9 @@ declare const creditSystemConfigSchema: z.ZodObject<{
|
|
|
18
18
|
/**
|
|
19
19
|
* Operation costs in credits
|
|
20
20
|
* Keys are operation type names (any non-empty string)
|
|
21
|
-
* Values are
|
|
21
|
+
* Values are non-negative numbers representing credit cost. 0 marks a free
|
|
22
|
+
* operation: `withCredits` runs it without touching the ledger. Amounts that
|
|
23
|
+
* move credits stay strictly positive, so `reserveCredits` refuses a 0.
|
|
22
24
|
*/
|
|
23
25
|
operationCosts: z.ZodRecord<z.ZodString, z.ZodNumber>;
|
|
24
26
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { S as SubscriptionTier, T as TierConfig, a as PortableReservation } from './types-
|
|
2
|
-
export { A as AIProviderType, B as BuiltinTier, g as CreditCheckResult, C as CreditOperationType, b as CreditSource, D as DeductCreditsResult, J as JournalReferenceType, M as MonthlyResetResult, f as PortableJournalEntry, P as PortableTransaction, d as PortableUsageLog, c as PortableUserCredits, R as ReservationStatus, h as ResourceType, g as SDKCreditCheckResult, e as SubscriptionExpiryResult, i as TransactionType, U as UsageHistoryEntry, j as UsageHistoryResponse, c as UserCredits, W as WithCreditsOptions, k as calculateAvailableCredits, t as toDate, l as toPortableTimestamp } from './types-
|
|
3
|
-
export { A as AddCreditsAtomicOptions, a as AddCreditsOutcome, b as AddCreditsV2Input, C as CommitOutcome, c as CreateJournalEntryInput, d as CreateReservationInput, e as CreateTransactionInput, f as CreateUsageLogInput, g as CreditBalanceUpdate, h as CreditRepositoryFactory, E as ExpireOutcome, i as ExpireReservationV2Options, I as ICreditRepository, j as ICreditRepositoryCreditsV2, k as ICreditRepositoryV2, J as JournalEntryQuery, R as ReleaseOutcome, l as ReservationTransitionOptions, m as ReserveCreditsV2Input, n as ReserveOutcome, T as TerminalReservationStatus, o as TierUpdateInput, U as UsageLogQuery, p as isCreditedOutcome, q as isReservedOutcome, r as isWinningOutcome, s as reservationJournalKey, t as supportsCreditsV2, u as supportsIdempotentCredit, v as toClientUserCredits } from './types-
|
|
1
|
+
import { S as SubscriptionTier, T as TierConfig, a as PortableReservation } from './types-Dzc443vy.js';
|
|
2
|
+
export { A as AIProviderType, B as BuiltinTier, g as CreditCheckResult, C as CreditOperationType, b as CreditSource, D as DeductCreditsResult, J as JournalReferenceType, M as MonthlyResetResult, f as PortableJournalEntry, P as PortableTransaction, d as PortableUsageLog, c as PortableUserCredits, R as ReservationStatus, h as ResourceType, g as SDKCreditCheckResult, e as SubscriptionExpiryResult, i as TransactionType, U as UsageHistoryEntry, j as UsageHistoryResponse, c as UserCredits, W as WithCreditsOptions, k as calculateAvailableCredits, t as toDate, l as toPortableTimestamp } from './types-Dzc443vy.js';
|
|
3
|
+
export { A as AddCreditsAtomicOptions, a as AddCreditsOutcome, b as AddCreditsV2Input, C as CommitOutcome, c as CreateJournalEntryInput, d as CreateReservationInput, e as CreateTransactionInput, f as CreateUsageLogInput, g as CreditBalanceUpdate, h as CreditRepositoryFactory, E as ExpireOutcome, i as ExpireReservationV2Options, I as ICreditRepository, j as ICreditRepositoryCreditsV2, k as ICreditRepositoryV2, J as JournalEntryQuery, R as ReleaseOutcome, l as ReservationTransitionOptions, m as ReserveCreditsV2Input, n as ReserveOutcome, T as TerminalReservationStatus, o as TierUpdateInput, U as UsageLogQuery, p as isCreditedOutcome, q as isReservedOutcome, r as isWinningOutcome, s as reservationJournalKey, t as supportsCreditsV2, u as supportsIdempotentCredit, v as toClientUserCredits } from './types-BhCdep_t.js';
|
|
4
4
|
export { CREDIT_AMOUNT_MAX, CREDIT_AMOUNT_SCALE, CreditError, CreditErrorCode, CreditErrorCodeType, PaymentEventPayload, RESERVATION_STATUSES, RESERVED_JOURNAL_KEY_PREFIX, ReservationIntegrityView, StoredPaymentEvent, TERMINAL_RESERVATION_STATUSES, assertDirectStatusWriteAllowed, assertHoldPlaced, assertKnownReservationStatus, assertPublicJournalKey, assertRepresentableAmount, assertRepresentableFields, assertRepresentableTierAmount, assertUnkeyedDirectReservation, assertUnreferencedDirectTransaction, assertValidCreditAmount, assertValidIdempotencyKey, assertValidStoredAmount, assertValidStoredAmountRaw, backedBalanceFloor, classifyDatabaseError, commitReservationWithJournal, createIdempotencyConflictError, createInsufficientCreditsError, createInvalidAmountError, createInvalidOperationTypeError, createPaymentRefConflictError, createReservationAlreadyProcessedError, createReservationExpiredError, createReservationNotFoundError, createTransientError, createUnsupportedOperationError, createUserNotFoundError, describePaymentMismatch, getSqlState, hasPlacedHold, isCreditError, isIdempotencyConflictError, isInsufficientCreditsError, isRepresentableAmount, isReservationStatus, isReservedJournalKey, isTerminalReservationStatus, isTransientDatabaseError, isTransientError, isUniqueViolation, isValidCreditAmount, isValidIdempotencyKey, normalizePaymentRef, numericToCents, releaseReservationWithJournal, reserveCreditsForOperation, sameAmount, storedMonthlyLimit, sumAmounts, terminalStatusOf, toCents } from './core/index.js';
|
|
5
5
|
export { D as DeferredExecutor, c as createDeferredExecutor, g as genericDeferred, n as noopDeferred, s as synchronousDeferred } from './deferred-C7rrRfkm.js';
|
|
6
6
|
import { z } from 'zod';
|
|
@@ -18,7 +18,9 @@ declare const creditSystemConfigSchema: z.ZodObject<{
|
|
|
18
18
|
/**
|
|
19
19
|
* Operation costs in credits
|
|
20
20
|
* Keys are operation type names (any non-empty string)
|
|
21
|
-
* Values are
|
|
21
|
+
* Values are non-negative numbers representing credit cost. 0 marks a free
|
|
22
|
+
* operation: `withCredits` runs it without touching the ledger. Amounts that
|
|
23
|
+
* move credits stay strictly positive, so `reserveCredits` refuses a 0.
|
|
22
24
|
*/
|
|
23
25
|
operationCosts: z.ZodRecord<z.ZodString, z.ZodNumber>;
|
|
24
26
|
/**
|
package/dist/index.js
CHANGED
|
@@ -599,11 +599,13 @@ var creditSystemConfigSchema = z.object({
|
|
|
599
599
|
/**
|
|
600
600
|
* Operation costs in credits
|
|
601
601
|
* Keys are operation type names (any non-empty string)
|
|
602
|
-
* Values are
|
|
602
|
+
* Values are non-negative numbers representing credit cost. 0 marks a free
|
|
603
|
+
* operation: `withCredits` runs it without touching the ledger. Amounts that
|
|
604
|
+
* move credits stay strictly positive, so `reserveCredits` refuses a 0.
|
|
603
605
|
*/
|
|
604
606
|
operationCosts: z.record(
|
|
605
607
|
z.string().min(1),
|
|
606
|
-
z.number().
|
|
608
|
+
z.number().nonnegative()
|
|
607
609
|
),
|
|
608
610
|
/**
|
|
609
611
|
* Display labels for operation types (English canonical labels).
|
|
@@ -2936,9 +2938,11 @@ function createGenericAdapter(config) {
|
|
|
2936
2938
|
return handler(user, data, dummyReservation);
|
|
2937
2939
|
}
|
|
2938
2940
|
let reservation;
|
|
2941
|
+
let isFree = false;
|
|
2939
2942
|
try {
|
|
2940
2943
|
const cost = options.customCost ?? getOperationCost2(options.operationType);
|
|
2941
|
-
|
|
2944
|
+
isFree = options.customCost === void 0 && cost === 0;
|
|
2945
|
+
reservation = isFree ? { ...createDummyReservation(user.id, options.operationType), id: "free-operation" } : await reserveCreditsForOperation(
|
|
2942
2946
|
repository,
|
|
2943
2947
|
user.id,
|
|
2944
2948
|
cost,
|
|
@@ -2961,7 +2965,7 @@ function createGenericAdapter(config) {
|
|
|
2961
2965
|
try {
|
|
2962
2966
|
const result = await handler(user, data, reservation);
|
|
2963
2967
|
if (result.success) {
|
|
2964
|
-
await commitReservationWithJournal(repository, user.id, reservation.id);
|
|
2968
|
+
if (!isFree) await commitReservationWithJournal(repository, user.id, reservation.id);
|
|
2965
2969
|
logUsage({
|
|
2966
2970
|
userId: user.id,
|
|
2967
2971
|
operationType: options.operationType,
|
|
@@ -2972,7 +2976,7 @@ function createGenericAdapter(config) {
|
|
|
2972
2976
|
requestId
|
|
2973
2977
|
});
|
|
2974
2978
|
} else {
|
|
2975
|
-
await releaseReservationWithJournal(repository, user.id, reservation.id);
|
|
2979
|
+
if (!isFree) await releaseReservationWithJournal(repository, user.id, reservation.id);
|
|
2976
2980
|
logUsage({
|
|
2977
2981
|
userId: user.id,
|
|
2978
2982
|
operationType: options.operationType,
|
|
@@ -2987,7 +2991,7 @@ function createGenericAdapter(config) {
|
|
|
2987
2991
|
return result;
|
|
2988
2992
|
} catch (error) {
|
|
2989
2993
|
const message = error instanceof Error ? error.message : "An unexpected error occurred";
|
|
2990
|
-
await releaseReservationWithJournal(repository, user.id, reservation.id);
|
|
2994
|
+
if (!isFree) await releaseReservationWithJournal(repository, user.id, reservation.id);
|
|
2991
2995
|
logUsage({
|
|
2992
2996
|
userId: user.id,
|
|
2993
2997
|
operationType: options.operationType,
|