@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 CHANGED
@@ -830,6 +830,9 @@ var InMemoryCreditRepository = class {
830
830
  if (!reservation) {
831
831
  throw new Error(`Reservation ${reservationId} not found`);
832
832
  }
833
+ if (reservation.status === "committed") {
834
+ return;
835
+ }
833
836
  if (reservation.status !== "reserved") {
834
837
  throw new Error(
835
838
  `Cannot commit reservation in ${reservation.status} state`
@@ -1372,11 +1375,13 @@ var CreditsService = class {
1372
1375
  * @param userId - User ID
1373
1376
  * @param amount - Credits to reserve
1374
1377
  * @param operationType - Operation type for tracking
1378
+ * @param options - Optional settings (e.g. a custom `ttlMs` for long-running async jobs)
1375
1379
  * @returns Reservation object
1376
1380
  * @throws Error if insufficient credits
1377
1381
  */
1378
- async reserveCredits(userId, amount, operationType) {
1379
- const expiresAt = new Date(Date.now() + getConfig().reservationExpiryMs);
1382
+ async reserveCredits(userId, amount, operationType, options) {
1383
+ const ttlMs = options?.ttlMs ?? getConfig().reservationExpiryMs;
1384
+ const expiresAt = new Date(Date.now() + ttlMs);
1380
1385
  return this.repository.reserveCreditsAtomic(userId, amount, operationType, expiresAt);
1381
1386
  }
1382
1387
  /**
@@ -1389,6 +1394,9 @@ var CreditsService = class {
1389
1394
  if (!reservation) {
1390
1395
  throw new Error(`Reservation ${reservationId} not found`);
1391
1396
  }
1397
+ if (reservation.status === "committed") {
1398
+ return;
1399
+ }
1392
1400
  await this.repository.commitReservationAtomic(userId, reservationId);
1393
1401
  const credits = await this.repository.getUserCredits(userId);
1394
1402
  if (credits) {