@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.cjs
CHANGED
|
@@ -769,11 +769,13 @@ var creditSystemConfigSchema = import_zod.z.object({
|
|
|
769
769
|
/**
|
|
770
770
|
* Operation costs in credits
|
|
771
771
|
* Keys are operation type names (any non-empty string)
|
|
772
|
-
* Values are
|
|
772
|
+
* Values are non-negative numbers representing credit cost. 0 marks a free
|
|
773
|
+
* operation: `withCredits` runs it without touching the ledger. Amounts that
|
|
774
|
+
* move credits stay strictly positive, so `reserveCredits` refuses a 0.
|
|
773
775
|
*/
|
|
774
776
|
operationCosts: import_zod.z.record(
|
|
775
777
|
import_zod.z.string().min(1),
|
|
776
|
-
import_zod.z.number().
|
|
778
|
+
import_zod.z.number().nonnegative()
|
|
777
779
|
),
|
|
778
780
|
/**
|
|
779
781
|
* Display labels for operation types (English canonical labels).
|
|
@@ -3106,9 +3108,11 @@ function createGenericAdapter(config) {
|
|
|
3106
3108
|
return handler(user, data, dummyReservation);
|
|
3107
3109
|
}
|
|
3108
3110
|
let reservation;
|
|
3111
|
+
let isFree = false;
|
|
3109
3112
|
try {
|
|
3110
3113
|
const cost = options.customCost ?? getOperationCost2(options.operationType);
|
|
3111
|
-
|
|
3114
|
+
isFree = options.customCost === void 0 && cost === 0;
|
|
3115
|
+
reservation = isFree ? { ...createDummyReservation(user.id, options.operationType), id: "free-operation" } : await reserveCreditsForOperation(
|
|
3112
3116
|
repository,
|
|
3113
3117
|
user.id,
|
|
3114
3118
|
cost,
|
|
@@ -3131,7 +3135,7 @@ function createGenericAdapter(config) {
|
|
|
3131
3135
|
try {
|
|
3132
3136
|
const result = await handler(user, data, reservation);
|
|
3133
3137
|
if (result.success) {
|
|
3134
|
-
await commitReservationWithJournal(repository, user.id, reservation.id);
|
|
3138
|
+
if (!isFree) await commitReservationWithJournal(repository, user.id, reservation.id);
|
|
3135
3139
|
logUsage({
|
|
3136
3140
|
userId: user.id,
|
|
3137
3141
|
operationType: options.operationType,
|
|
@@ -3142,7 +3146,7 @@ function createGenericAdapter(config) {
|
|
|
3142
3146
|
requestId
|
|
3143
3147
|
});
|
|
3144
3148
|
} else {
|
|
3145
|
-
await releaseReservationWithJournal(repository, user.id, reservation.id);
|
|
3149
|
+
if (!isFree) await releaseReservationWithJournal(repository, user.id, reservation.id);
|
|
3146
3150
|
logUsage({
|
|
3147
3151
|
userId: user.id,
|
|
3148
3152
|
operationType: options.operationType,
|
|
@@ -3157,7 +3161,7 @@ function createGenericAdapter(config) {
|
|
|
3157
3161
|
return result;
|
|
3158
3162
|
} catch (error) {
|
|
3159
3163
|
const message = error instanceof Error ? error.message : "An unexpected error occurred";
|
|
3160
|
-
await releaseReservationWithJournal(repository, user.id, reservation.id);
|
|
3164
|
+
if (!isFree) await releaseReservationWithJournal(repository, user.id, reservation.id);
|
|
3161
3165
|
logUsage({
|
|
3162
3166
|
userId: user.id,
|
|
3163
3167
|
operationType: options.operationType,
|