@nehorai/credits 1.6.0 → 2.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/LICENSE +21 -0
- package/README.md +226 -2
- package/dist/adapters/index.cjs +298 -40
- 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 +298 -40
- package/dist/adapters/index.js.map +1 -1
- package/dist/core/index.cjs +696 -74
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.cts +583 -5
- package/dist/core/index.d.ts +583 -5
- package/dist/core/index.js +648 -74
- package/dist/core/index.js.map +1 -1
- package/dist/index.cjs +1779 -364
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +41 -6
- package/dist/index.d.ts +41 -6
- package/dist/index.js +1723 -364
- package/dist/index.js.map +1 -1
- package/dist/repository/index.cjs +1413 -181
- package/dist/repository/index.cjs.map +1 -1
- package/dist/repository/index.d.cts +142 -16
- package/dist/repository/index.d.ts +142 -16
- package/dist/repository/index.js +1406 -181
- package/dist/repository/index.js.map +1 -1
- package/dist/sdk/index.cjs +23 -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 +23 -1
- package/dist/sdk/index.js.map +1 -1
- package/dist/service/index.cjs +483 -105
- package/dist/service/index.cjs.map +1 -1
- package/dist/service/index.d.cts +70 -10
- package/dist/service/index.d.ts +70 -10
- package/dist/service/index.js +483 -105
- package/dist/service/index.js.map +1 -1
- package/dist/types-BmG5Omzj.d.ts +646 -0
- package/dist/types-C8hZMe8a.d.cts +646 -0
- package/dist/{types-CjLj3GW5.d.cts → types-DJ8yqaVr.d.cts} +39 -1
- package/dist/{types-CjLj3GW5.d.ts → types-DJ8yqaVr.d.ts} +39 -1
- package/package.json +9 -10
- package/dist/types-CZNv1U3e.d.cts +0 -305
- package/dist/types-G4WK0rM-.d.ts +0 -305
package/dist/index.d.cts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { S as SubscriptionTier, T as TierConfig,
|
|
2
|
-
export { A as AIProviderType, B as BuiltinTier, g as CreditCheckResult,
|
|
3
|
-
export {
|
|
1
|
+
import { S as SubscriptionTier, T as TierConfig, a as PortableReservation } from './types-DJ8yqaVr.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-DJ8yqaVr.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-C8hZMe8a.cjs';
|
|
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';
|
|
4
5
|
export { D as DeferredExecutor, c as createDeferredExecutor, g as genericDeferred, n as noopDeferred, s as synchronousDeferred } from './deferred-C7rrRfkm.cjs';
|
|
5
6
|
import { z } from 'zod';
|
|
6
|
-
export {
|
|
7
|
-
export { InMemoryCreditRepository, createInMemoryCreditRepository, generateId, getNextMonthlyReset } from './repository/index.cjs';
|
|
7
|
+
export { InMemoryCreditRepository, addCreditsThroughRepository, commitThroughRepository, createInMemoryCreditRepository, generateId, getNextMonthlyReset, releaseThroughRepository, reserveThroughRepository } from './repository/index.cjs';
|
|
8
8
|
export { C as CreditsAuthProviderFactory, a as CreditsUser, I as ICreditsAuthProvider } from './types-OZU3I5OM.cjs';
|
|
9
9
|
export { ApiKeyCreditsProvider, createApiKeyProvider, verifyAdminApiKey, verifyBearerToken } from './auth/index.cjs';
|
|
10
10
|
export { CreditsService, DeductCreditsOptions, LowBalanceNotificationCallback, ReserveCreditsOptions, SubscriptionExpiredNotificationCallback, createCreditsService } from './service/index.cjs';
|
|
@@ -212,6 +212,41 @@ declare const UNLIMITED_BALANCE_SENTINEL = 999999;
|
|
|
212
212
|
* Get the magic balance assigned to unlimited tiers on upgrade.
|
|
213
213
|
*/
|
|
214
214
|
declare function getUnlimitedSentinelBalance(): number;
|
|
215
|
+
/**
|
|
216
|
+
* What a monthly reset must leave in `balance`, as a storable instruction.
|
|
217
|
+
*
|
|
218
|
+
* This is the one place the unlimited contract is defined, because it is the
|
|
219
|
+
* one thing the two adapters kept disagreeing about. The canonical persisted
|
|
220
|
+
* representation of an unlimited tier is:
|
|
221
|
+
*
|
|
222
|
+
* - `monthlyLimit` = `storedMonthlyLimit(Infinity)`, i.e. the top of the
|
|
223
|
+
* representable range. Never `0`, which reads as *no* allowance.
|
|
224
|
+
* - `balance` = at least {@link UNLIMITED_BALANCE_SENTINEL}, which is what
|
|
225
|
+
* `updateTier` writes on upgrade.
|
|
226
|
+
*
|
|
227
|
+
* The reset used to leave an unlimited balance strictly alone ("`Infinity` is
|
|
228
|
+
* not storable, so change nothing"), which is correct for a healthy account and
|
|
229
|
+
* a trap for a degraded one: an unlimited user whose balance had been driven to
|
|
230
|
+
* `0` — by a spend, or by the older code that stored `0` as the limit — stayed
|
|
231
|
+
* at `0` through every subsequent reset and could never buy anything again. So
|
|
232
|
+
* the instruction is `atLeast`, not "leave alone": it restores a degraded
|
|
233
|
+
* balance to the sentinel and leaves a topped-up one (a purchase on top of an
|
|
234
|
+
* unlimited plan) untouched. It is expressed as a floor rather than as a number
|
|
235
|
+
* so the SQL adapter can apply it as `greatest(balance, n)` inside its guarded
|
|
236
|
+
* UPDATE instead of writing a literal read before the lock.
|
|
237
|
+
*/
|
|
238
|
+
type MonthlyResetBalance =
|
|
239
|
+
/** Store exactly this value. */
|
|
240
|
+
{
|
|
241
|
+
kind: "absolute";
|
|
242
|
+
value: number;
|
|
243
|
+
}
|
|
244
|
+
/** Raise the stored balance to this value; leave anything higher alone. */
|
|
245
|
+
| {
|
|
246
|
+
kind: "atLeast";
|
|
247
|
+
value: number;
|
|
248
|
+
};
|
|
249
|
+
declare function monthlyResetBalance(tier: SubscriptionTier): MonthlyResetBalance;
|
|
215
250
|
/**
|
|
216
251
|
* Get tier configuration from configuration.
|
|
217
252
|
* Falls back to the default tier (with a warning) if the tier is unknown.
|
|
@@ -493,4 +528,4 @@ declare function isPreviewMode(data: unknown): boolean;
|
|
|
493
528
|
*/
|
|
494
529
|
declare function createDummyReservation(userId: string, operationType: string): PortableReservation;
|
|
495
530
|
|
|
496
|
-
export { ConsoleNotificationHandler, type CreditErrorInfo, type CreditNotificationEvent, PortableReservation as CreditReservation, type CreditSystemConfig, type ICreditConfigProvider, type ICreditNotificationHandler, type OperationCostInfo, PortableReservation, SubscriptionTier as SDKSubscriptionTier, SubscriptionTier, TierConfig, UNLIMITED_BALANCE_SENTINEL, checkAndNotifyLowBalance, clearConfigProvider, clearCooldownState, clearNotificationHandlers, createDummyReservation, generateRequestId, getConfig, getConfigMonthlyLimit, getConfigOperationCost, getConfigProvider, getConfigTierConfig, getDefaultFreeCredits, getDefaultTier, getMonthlyLimit, getOperationCost, getOperationCosts, getOperationCostsWithLabels, getOperationLabel, getOperationLabels, getReservationExpiryMs, getTierConfig, getTotalCredits, getUnlimitedSentinelBalance, getValidOperationTypes, getValidTiers, initializeConfig, isCreditErrorMessage, isFeatureEnabled, isFreeTier, isPreviewMode, isUnlimitedTier, isValidOperationType, isValidTier, loadConfigFromEnv, notifySubscriptionExpired, notifySubscriptionExpiring, parseCreditError, parseTier, registerConfigProvider, registerNotificationHandler, resetConfig, tierSchema };
|
|
531
|
+
export { ConsoleNotificationHandler, type CreditErrorInfo, type CreditNotificationEvent, PortableReservation as CreditReservation, type CreditSystemConfig, type ICreditConfigProvider, type ICreditNotificationHandler, type MonthlyResetBalance, type OperationCostInfo, PortableReservation, SubscriptionTier as SDKSubscriptionTier, SubscriptionTier, TierConfig, UNLIMITED_BALANCE_SENTINEL, checkAndNotifyLowBalance, clearConfigProvider, clearCooldownState, clearNotificationHandlers, createDummyReservation, generateRequestId, getConfig, getConfigMonthlyLimit, getConfigOperationCost, getConfigProvider, getConfigTierConfig, getDefaultFreeCredits, getDefaultTier, getMonthlyLimit, getOperationCost, getOperationCosts, getOperationCostsWithLabels, getOperationLabel, getOperationLabels, getReservationExpiryMs, getTierConfig, getTotalCredits, getUnlimitedSentinelBalance, getValidOperationTypes, getValidTiers, initializeConfig, isCreditErrorMessage, isFeatureEnabled, isFreeTier, isPreviewMode, isUnlimitedTier, isValidOperationType, isValidTier, loadConfigFromEnv, monthlyResetBalance, notifySubscriptionExpired, notifySubscriptionExpiring, parseCreditError, parseTier, registerConfigProvider, registerNotificationHandler, resetConfig, tierSchema };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { S as SubscriptionTier, T as TierConfig,
|
|
2
|
-
export { A as AIProviderType, B as BuiltinTier, g as CreditCheckResult,
|
|
3
|
-
export {
|
|
1
|
+
import { S as SubscriptionTier, T as TierConfig, a as PortableReservation } from './types-DJ8yqaVr.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-DJ8yqaVr.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-BmG5Omzj.js';
|
|
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';
|
|
4
5
|
export { D as DeferredExecutor, c as createDeferredExecutor, g as genericDeferred, n as noopDeferred, s as synchronousDeferred } from './deferred-C7rrRfkm.js';
|
|
5
6
|
import { z } from 'zod';
|
|
6
|
-
export {
|
|
7
|
-
export { InMemoryCreditRepository, createInMemoryCreditRepository, generateId, getNextMonthlyReset } from './repository/index.js';
|
|
7
|
+
export { InMemoryCreditRepository, addCreditsThroughRepository, commitThroughRepository, createInMemoryCreditRepository, generateId, getNextMonthlyReset, releaseThroughRepository, reserveThroughRepository } from './repository/index.js';
|
|
8
8
|
export { C as CreditsAuthProviderFactory, a as CreditsUser, I as ICreditsAuthProvider } from './types-OZU3I5OM.js';
|
|
9
9
|
export { ApiKeyCreditsProvider, createApiKeyProvider, verifyAdminApiKey, verifyBearerToken } from './auth/index.js';
|
|
10
10
|
export { CreditsService, DeductCreditsOptions, LowBalanceNotificationCallback, ReserveCreditsOptions, SubscriptionExpiredNotificationCallback, createCreditsService } from './service/index.js';
|
|
@@ -212,6 +212,41 @@ declare const UNLIMITED_BALANCE_SENTINEL = 999999;
|
|
|
212
212
|
* Get the magic balance assigned to unlimited tiers on upgrade.
|
|
213
213
|
*/
|
|
214
214
|
declare function getUnlimitedSentinelBalance(): number;
|
|
215
|
+
/**
|
|
216
|
+
* What a monthly reset must leave in `balance`, as a storable instruction.
|
|
217
|
+
*
|
|
218
|
+
* This is the one place the unlimited contract is defined, because it is the
|
|
219
|
+
* one thing the two adapters kept disagreeing about. The canonical persisted
|
|
220
|
+
* representation of an unlimited tier is:
|
|
221
|
+
*
|
|
222
|
+
* - `monthlyLimit` = `storedMonthlyLimit(Infinity)`, i.e. the top of the
|
|
223
|
+
* representable range. Never `0`, which reads as *no* allowance.
|
|
224
|
+
* - `balance` = at least {@link UNLIMITED_BALANCE_SENTINEL}, which is what
|
|
225
|
+
* `updateTier` writes on upgrade.
|
|
226
|
+
*
|
|
227
|
+
* The reset used to leave an unlimited balance strictly alone ("`Infinity` is
|
|
228
|
+
* not storable, so change nothing"), which is correct for a healthy account and
|
|
229
|
+
* a trap for a degraded one: an unlimited user whose balance had been driven to
|
|
230
|
+
* `0` — by a spend, or by the older code that stored `0` as the limit — stayed
|
|
231
|
+
* at `0` through every subsequent reset and could never buy anything again. So
|
|
232
|
+
* the instruction is `atLeast`, not "leave alone": it restores a degraded
|
|
233
|
+
* balance to the sentinel and leaves a topped-up one (a purchase on top of an
|
|
234
|
+
* unlimited plan) untouched. It is expressed as a floor rather than as a number
|
|
235
|
+
* so the SQL adapter can apply it as `greatest(balance, n)` inside its guarded
|
|
236
|
+
* UPDATE instead of writing a literal read before the lock.
|
|
237
|
+
*/
|
|
238
|
+
type MonthlyResetBalance =
|
|
239
|
+
/** Store exactly this value. */
|
|
240
|
+
{
|
|
241
|
+
kind: "absolute";
|
|
242
|
+
value: number;
|
|
243
|
+
}
|
|
244
|
+
/** Raise the stored balance to this value; leave anything higher alone. */
|
|
245
|
+
| {
|
|
246
|
+
kind: "atLeast";
|
|
247
|
+
value: number;
|
|
248
|
+
};
|
|
249
|
+
declare function monthlyResetBalance(tier: SubscriptionTier): MonthlyResetBalance;
|
|
215
250
|
/**
|
|
216
251
|
* Get tier configuration from configuration.
|
|
217
252
|
* Falls back to the default tier (with a warning) if the tier is unknown.
|
|
@@ -493,4 +528,4 @@ declare function isPreviewMode(data: unknown): boolean;
|
|
|
493
528
|
*/
|
|
494
529
|
declare function createDummyReservation(userId: string, operationType: string): PortableReservation;
|
|
495
530
|
|
|
496
|
-
export { ConsoleNotificationHandler, type CreditErrorInfo, type CreditNotificationEvent, PortableReservation as CreditReservation, type CreditSystemConfig, type ICreditConfigProvider, type ICreditNotificationHandler, type OperationCostInfo, PortableReservation, SubscriptionTier as SDKSubscriptionTier, SubscriptionTier, TierConfig, UNLIMITED_BALANCE_SENTINEL, checkAndNotifyLowBalance, clearConfigProvider, clearCooldownState, clearNotificationHandlers, createDummyReservation, generateRequestId, getConfig, getConfigMonthlyLimit, getConfigOperationCost, getConfigProvider, getConfigTierConfig, getDefaultFreeCredits, getDefaultTier, getMonthlyLimit, getOperationCost, getOperationCosts, getOperationCostsWithLabels, getOperationLabel, getOperationLabels, getReservationExpiryMs, getTierConfig, getTotalCredits, getUnlimitedSentinelBalance, getValidOperationTypes, getValidTiers, initializeConfig, isCreditErrorMessage, isFeatureEnabled, isFreeTier, isPreviewMode, isUnlimitedTier, isValidOperationType, isValidTier, loadConfigFromEnv, notifySubscriptionExpired, notifySubscriptionExpiring, parseCreditError, parseTier, registerConfigProvider, registerNotificationHandler, resetConfig, tierSchema };
|
|
531
|
+
export { ConsoleNotificationHandler, type CreditErrorInfo, type CreditNotificationEvent, PortableReservation as CreditReservation, type CreditSystemConfig, type ICreditConfigProvider, type ICreditNotificationHandler, type MonthlyResetBalance, type OperationCostInfo, PortableReservation, SubscriptionTier as SDKSubscriptionTier, SubscriptionTier, TierConfig, UNLIMITED_BALANCE_SENTINEL, checkAndNotifyLowBalance, clearConfigProvider, clearCooldownState, clearNotificationHandlers, createDummyReservation, generateRequestId, getConfig, getConfigMonthlyLimit, getConfigOperationCost, getConfigProvider, getConfigTierConfig, getDefaultFreeCredits, getDefaultTier, getMonthlyLimit, getOperationCost, getOperationCosts, getOperationCostsWithLabels, getOperationLabel, getOperationLabels, getReservationExpiryMs, getTierConfig, getTotalCredits, getUnlimitedSentinelBalance, getValidOperationTypes, getValidTiers, initializeConfig, isCreditErrorMessage, isFeatureEnabled, isFreeTier, isPreviewMode, isUnlimitedTier, isValidOperationType, isValidTier, loadConfigFromEnv, monthlyResetBalance, notifySubscriptionExpired, notifySubscriptionExpiring, parseCreditError, parseTier, registerConfigProvider, registerNotificationHandler, resetConfig, tierSchema };
|