@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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { c as PortableReservation, W as WithCreditsOptions } from '../types-CjLj3GW5.cjs';
|
|
2
2
|
import { a as CreditsUser, I as ICreditsAuthProvider } from '../types-OZU3I5OM.cjs';
|
|
3
|
-
import { I as ICreditRepository } from '../types-
|
|
3
|
+
import { I as ICreditRepository } from '../types-DFe5BoJY.cjs';
|
|
4
4
|
import { D as DeferredExecutor } from '../deferred-C7rrRfkm.cjs';
|
|
5
5
|
|
|
6
6
|
/**
|
package/dist/adapters/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { c as PortableReservation, W as WithCreditsOptions } from '../types-CjLj3GW5.js';
|
|
2
2
|
import { a as CreditsUser, I as ICreditsAuthProvider } from '../types-OZU3I5OM.js';
|
|
3
|
-
import { I as ICreditRepository } from '../types-
|
|
3
|
+
import { I as ICreditRepository } from '../types-_bSybQnb.js';
|
|
4
4
|
import { D as DeferredExecutor } from '../deferred-C7rrRfkm.js';
|
|
5
5
|
|
|
6
6
|
/**
|
package/dist/core/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { c as PortableReservation } from '../types-CjLj3GW5.cjs';
|
|
2
2
|
export { A as AIProviderType, B as BuiltinTier, g as CreditCheckResult, a as CreditOperationType, C as CreditSource, D as DeductCreditsResult, J as JournalReferenceType, M as MonthlyResetResult, f as PortableJournalEntry, P as PortableTransaction, d as PortableUsageLog, b as PortableUserCredits, R as ReservationStatus, h as ResourceType, e as SubscriptionExpiryResult, S as SubscriptionTier, T as TierConfig, i as TransactionType, U as UsageHistoryEntry, j as UsageHistoryResponse, W as WithCreditsOptions, k as calculateAvailableCredits, t as toDate, l as toPortableTimestamp } from '../types-CjLj3GW5.cjs';
|
|
3
3
|
export { D as DeferredExecutor, c as createDeferredExecutor, g as genericDeferred, n as noopDeferred, s as synchronousDeferred } from '../deferred-C7rrRfkm.cjs';
|
|
4
|
-
import { I as ICreditRepository } from '../types-
|
|
4
|
+
import { I as ICreditRepository } from '../types-DFe5BoJY.cjs';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Credit system error classes - framework agnostic
|
package/dist/core/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { c as PortableReservation } from '../types-CjLj3GW5.js';
|
|
2
2
|
export { A as AIProviderType, B as BuiltinTier, g as CreditCheckResult, a as CreditOperationType, C as CreditSource, D as DeductCreditsResult, J as JournalReferenceType, M as MonthlyResetResult, f as PortableJournalEntry, P as PortableTransaction, d as PortableUsageLog, b as PortableUserCredits, R as ReservationStatus, h as ResourceType, e as SubscriptionExpiryResult, S as SubscriptionTier, T as TierConfig, i as TransactionType, U as UsageHistoryEntry, j as UsageHistoryResponse, W as WithCreditsOptions, k as calculateAvailableCredits, t as toDate, l as toPortableTimestamp } from '../types-CjLj3GW5.js';
|
|
3
3
|
export { D as DeferredExecutor, c as createDeferredExecutor, g as genericDeferred, n as noopDeferred, s as synchronousDeferred } from '../deferred-C7rrRfkm.js';
|
|
4
|
-
import { I as ICreditRepository } from '../types-
|
|
4
|
+
import { I as ICreditRepository } from '../types-_bSybQnb.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Credit system error classes - framework agnostic
|
package/dist/index.cjs
CHANGED
|
@@ -912,23 +912,39 @@ var InMemoryCreditRepository = class {
|
|
|
912
912
|
this.users.set(userId, credits);
|
|
913
913
|
await this.updateReservationStatus(userId, reservationId, "released", /* @__PURE__ */ new Date());
|
|
914
914
|
}
|
|
915
|
-
async addCreditsAtomic(userId, amount, description, paymentRef) {
|
|
915
|
+
async addCreditsAtomic(userId, amount, description, paymentRef, options) {
|
|
916
916
|
const credits = this.users.get(userId);
|
|
917
917
|
if (!credits) {
|
|
918
918
|
throw new Error(`User ${userId} not found`);
|
|
919
919
|
}
|
|
920
|
-
const
|
|
920
|
+
const previousTotal = credits.balance + credits.bonusCredits;
|
|
921
921
|
credits.bonusCredits += amount;
|
|
922
922
|
credits.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
923
923
|
this.users.set(userId, credits);
|
|
924
|
-
|
|
924
|
+
const newTotal = credits.balance + credits.bonusCredits;
|
|
925
|
+
const transaction = await this.createTransaction({
|
|
925
926
|
userId,
|
|
926
927
|
type: "purchase",
|
|
927
928
|
amount,
|
|
928
929
|
description,
|
|
929
930
|
paymentRef,
|
|
930
|
-
previousBalance,
|
|
931
|
-
newBalance:
|
|
931
|
+
previousBalance: previousTotal,
|
|
932
|
+
newBalance: newTotal
|
|
933
|
+
});
|
|
934
|
+
const journalMetadata = {
|
|
935
|
+
...paymentRef ? { paymentRef } : {},
|
|
936
|
+
...options?.metadata ?? {}
|
|
937
|
+
};
|
|
938
|
+
await this.createJournalEntry({
|
|
939
|
+
userId,
|
|
940
|
+
entryType: "credit",
|
|
941
|
+
amount,
|
|
942
|
+
balanceAfter: newTotal,
|
|
943
|
+
source: options?.source ?? "purchase",
|
|
944
|
+
referenceId: transaction.id ?? paymentRef ?? "unknown",
|
|
945
|
+
referenceType: options?.referenceType ?? "transaction",
|
|
946
|
+
description,
|
|
947
|
+
metadata: Object.keys(journalMetadata).length > 0 ? journalMetadata : void 0
|
|
932
948
|
});
|
|
933
949
|
}
|
|
934
950
|
async deductCreditsAtomic(userId, amount) {
|
|
@@ -1510,9 +1526,11 @@ var CreditsService = class {
|
|
|
1510
1526
|
* @param amount - Credits to add
|
|
1511
1527
|
* @param description - Transaction description
|
|
1512
1528
|
* @param paymentRef - Optional payment reference
|
|
1529
|
+
* @param options - Optional journal source / reference type / extra metadata
|
|
1530
|
+
* for the credit journal entry (e.g. revenue-attribution fields).
|
|
1513
1531
|
*/
|
|
1514
|
-
async addCredits(userId, amount, description, paymentRef) {
|
|
1515
|
-
return this.repository.addCreditsAtomic(userId, amount, description, paymentRef);
|
|
1532
|
+
async addCredits(userId, amount, description, paymentRef, options) {
|
|
1533
|
+
return this.repository.addCreditsAtomic(userId, amount, description, paymentRef, options);
|
|
1516
1534
|
}
|
|
1517
1535
|
/**
|
|
1518
1536
|
* One-shot atomic "deduct-if-sufficient" credit charge.
|