@nehorai/credits 1.3.0 → 1.4.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/dist/index.cjs +10 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +10 -2
- package/dist/index.js.map +1 -1
- package/dist/repository/index.cjs +3 -0
- package/dist/repository/index.cjs.map +1 -1
- package/dist/repository/index.js +3 -0
- package/dist/repository/index.js.map +1 -1
- package/dist/service/index.cjs +7 -2
- package/dist/service/index.cjs.map +1 -1
- package/dist/service/index.d.cts +16 -1
- package/dist/service/index.d.ts +16 -1
- package/dist/service/index.js +7 -2
- package/dist/service/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -725,6 +725,9 @@ var InMemoryCreditRepository = class {
|
|
|
725
725
|
if (!reservation) {
|
|
726
726
|
throw new Error(`Reservation ${reservationId} not found`);
|
|
727
727
|
}
|
|
728
|
+
if (reservation.status === "committed") {
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
728
731
|
if (reservation.status !== "reserved") {
|
|
729
732
|
throw new Error(
|
|
730
733
|
`Cannot commit reservation in ${reservation.status} state`
|
|
@@ -1267,11 +1270,13 @@ var CreditsService = class {
|
|
|
1267
1270
|
* @param userId - User ID
|
|
1268
1271
|
* @param amount - Credits to reserve
|
|
1269
1272
|
* @param operationType - Operation type for tracking
|
|
1273
|
+
* @param options - Optional settings (e.g. a custom `ttlMs` for long-running async jobs)
|
|
1270
1274
|
* @returns Reservation object
|
|
1271
1275
|
* @throws Error if insufficient credits
|
|
1272
1276
|
*/
|
|
1273
|
-
async reserveCredits(userId, amount, operationType) {
|
|
1274
|
-
const
|
|
1277
|
+
async reserveCredits(userId, amount, operationType, options) {
|
|
1278
|
+
const ttlMs = options?.ttlMs ?? getConfig().reservationExpiryMs;
|
|
1279
|
+
const expiresAt = new Date(Date.now() + ttlMs);
|
|
1275
1280
|
return this.repository.reserveCreditsAtomic(userId, amount, operationType, expiresAt);
|
|
1276
1281
|
}
|
|
1277
1282
|
/**
|
|
@@ -1284,6 +1289,9 @@ var CreditsService = class {
|
|
|
1284
1289
|
if (!reservation) {
|
|
1285
1290
|
throw new Error(`Reservation ${reservationId} not found`);
|
|
1286
1291
|
}
|
|
1292
|
+
if (reservation.status === "committed") {
|
|
1293
|
+
return;
|
|
1294
|
+
}
|
|
1287
1295
|
await this.repository.commitReservationAtomic(userId, reservationId);
|
|
1288
1296
|
const credits = await this.repository.getUserCredits(userId);
|
|
1289
1297
|
if (credits) {
|