@nehorai/credits 1.6.0 → 1.7.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/dist/adapters/index.d.cts +1 -1
- package/dist/adapters/index.d.ts +1 -1
- package/dist/core/index.d.cts +1 -1
- package/dist/core/index.d.ts +1 -1
- package/dist/index.cjs +25 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +25 -7
- package/dist/index.js.map +1 -1
- package/dist/repository/index.cjs +21 -5
- package/dist/repository/index.cjs.map +1 -1
- package/dist/repository/index.d.cts +4 -4
- package/dist/repository/index.d.ts +4 -4
- package/dist/repository/index.js +21 -5
- package/dist/repository/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 +4 -2
- package/dist/service/index.d.ts +4 -2
- package/dist/service/index.js +4 -2
- package/dist/service/index.js.map +1 -1
- package/dist/{types-CZNv1U3e.d.cts → types-DFe5BoJY.d.cts} +26 -2
- package/dist/{types-G4WK0rM-.d.ts → types-_bSybQnb.d.ts} +26 -2
- package/package.json +9 -10
package/dist/index.d.cts
CHANGED
|
@@ -3,7 +3,7 @@ export { A as AIProviderType, B as BuiltinTier, g as CreditCheckResult, a as Cre
|
|
|
3
3
|
export { CreditError, CreditErrorCode, CreditErrorCodeType, commitReservationWithJournal, createInsufficientCreditsError, createInvalidOperationTypeError, createReservationAlreadyProcessedError, createReservationExpiredError, createReservationNotFoundError, createUserNotFoundError, isCreditError, isInsufficientCreditsError, releaseReservationWithJournal, reserveCreditsForOperation } from './core/index.cjs';
|
|
4
4
|
export { D as DeferredExecutor, c as createDeferredExecutor, g as genericDeferred, n as noopDeferred, s as synchronousDeferred } from './deferred-C7rrRfkm.cjs';
|
|
5
5
|
import { z } from 'zod';
|
|
6
|
-
export { C as CreateJournalEntryInput, a as CreateReservationInput, b as CreateTransactionInput, c as CreateUsageLogInput, d as CreditBalanceUpdate, e as CreditRepositoryFactory, I as ICreditRepository, J as JournalEntryQuery, T as TierUpdateInput, U as UsageLogQuery, t as toClientUserCredits } from './types-
|
|
6
|
+
export { A as AddCreditsAtomicOptions, C as CreateJournalEntryInput, a as CreateReservationInput, b as CreateTransactionInput, c as CreateUsageLogInput, d as CreditBalanceUpdate, e as CreditRepositoryFactory, I as ICreditRepository, J as JournalEntryQuery, T as TierUpdateInput, U as UsageLogQuery, t as toClientUserCredits } from './types-DFe5BoJY.cjs';
|
|
7
7
|
export { InMemoryCreditRepository, createInMemoryCreditRepository, generateId, getNextMonthlyReset } 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';
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export { A as AIProviderType, B as BuiltinTier, g as CreditCheckResult, a as Cre
|
|
|
3
3
|
export { CreditError, CreditErrorCode, CreditErrorCodeType, commitReservationWithJournal, createInsufficientCreditsError, createInvalidOperationTypeError, createReservationAlreadyProcessedError, createReservationExpiredError, createReservationNotFoundError, createUserNotFoundError, isCreditError, isInsufficientCreditsError, releaseReservationWithJournal, reserveCreditsForOperation } from './core/index.js';
|
|
4
4
|
export { D as DeferredExecutor, c as createDeferredExecutor, g as genericDeferred, n as noopDeferred, s as synchronousDeferred } from './deferred-C7rrRfkm.js';
|
|
5
5
|
import { z } from 'zod';
|
|
6
|
-
export { C as CreateJournalEntryInput, a as CreateReservationInput, b as CreateTransactionInput, c as CreateUsageLogInput, d as CreditBalanceUpdate, e as CreditRepositoryFactory, I as ICreditRepository, J as JournalEntryQuery, T as TierUpdateInput, U as UsageLogQuery, t as toClientUserCredits } from './types-
|
|
6
|
+
export { A as AddCreditsAtomicOptions, C as CreateJournalEntryInput, a as CreateReservationInput, b as CreateTransactionInput, c as CreateUsageLogInput, d as CreditBalanceUpdate, e as CreditRepositoryFactory, I as ICreditRepository, J as JournalEntryQuery, T as TierUpdateInput, U as UsageLogQuery, t as toClientUserCredits } from './types-_bSybQnb.js';
|
|
7
7
|
export { InMemoryCreditRepository, createInMemoryCreditRepository, generateId, getNextMonthlyReset } 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';
|
package/dist/index.js
CHANGED
|
@@ -798,23 +798,39 @@ var InMemoryCreditRepository = class {
|
|
|
798
798
|
this.users.set(userId, credits);
|
|
799
799
|
await this.updateReservationStatus(userId, reservationId, "released", /* @__PURE__ */ new Date());
|
|
800
800
|
}
|
|
801
|
-
async addCreditsAtomic(userId, amount, description, paymentRef) {
|
|
801
|
+
async addCreditsAtomic(userId, amount, description, paymentRef, options) {
|
|
802
802
|
const credits = this.users.get(userId);
|
|
803
803
|
if (!credits) {
|
|
804
804
|
throw new Error(`User ${userId} not found`);
|
|
805
805
|
}
|
|
806
|
-
const
|
|
806
|
+
const previousTotal = credits.balance + credits.bonusCredits;
|
|
807
807
|
credits.bonusCredits += amount;
|
|
808
808
|
credits.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
809
809
|
this.users.set(userId, credits);
|
|
810
|
-
|
|
810
|
+
const newTotal = credits.balance + credits.bonusCredits;
|
|
811
|
+
const transaction = await this.createTransaction({
|
|
811
812
|
userId,
|
|
812
813
|
type: "purchase",
|
|
813
814
|
amount,
|
|
814
815
|
description,
|
|
815
816
|
paymentRef,
|
|
816
|
-
previousBalance,
|
|
817
|
-
newBalance:
|
|
817
|
+
previousBalance: previousTotal,
|
|
818
|
+
newBalance: newTotal
|
|
819
|
+
});
|
|
820
|
+
const journalMetadata = {
|
|
821
|
+
...paymentRef ? { paymentRef } : {},
|
|
822
|
+
...options?.metadata ?? {}
|
|
823
|
+
};
|
|
824
|
+
await this.createJournalEntry({
|
|
825
|
+
userId,
|
|
826
|
+
entryType: "credit",
|
|
827
|
+
amount,
|
|
828
|
+
balanceAfter: newTotal,
|
|
829
|
+
source: options?.source ?? "purchase",
|
|
830
|
+
referenceId: transaction.id ?? paymentRef ?? "unknown",
|
|
831
|
+
referenceType: options?.referenceType ?? "transaction",
|
|
832
|
+
description,
|
|
833
|
+
metadata: Object.keys(journalMetadata).length > 0 ? journalMetadata : void 0
|
|
818
834
|
});
|
|
819
835
|
}
|
|
820
836
|
async deductCreditsAtomic(userId, amount) {
|
|
@@ -1396,9 +1412,11 @@ var CreditsService = class {
|
|
|
1396
1412
|
* @param amount - Credits to add
|
|
1397
1413
|
* @param description - Transaction description
|
|
1398
1414
|
* @param paymentRef - Optional payment reference
|
|
1415
|
+
* @param options - Optional journal source / reference type / extra metadata
|
|
1416
|
+
* for the credit journal entry (e.g. revenue-attribution fields).
|
|
1399
1417
|
*/
|
|
1400
|
-
async addCredits(userId, amount, description, paymentRef) {
|
|
1401
|
-
return this.repository.addCreditsAtomic(userId, amount, description, paymentRef);
|
|
1418
|
+
async addCredits(userId, amount, description, paymentRef, options) {
|
|
1419
|
+
return this.repository.addCreditsAtomic(userId, amount, description, paymentRef, options);
|
|
1402
1420
|
}
|
|
1403
1421
|
/**
|
|
1404
1422
|
* One-shot atomic "deduct-if-sufficient" credit charge.
|