@nehorai/credits 3.0.0 → 3.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/dist/adapters/index.cjs +169 -156
- package/dist/adapters/index.cjs.map +1 -1
- package/dist/adapters/index.d.cts +1 -1
- package/dist/adapters/index.d.ts +1 -1
- package/dist/adapters/index.js +169 -156
- package/dist/adapters/index.js.map +1 -1
- package/dist/core/index.cjs +9 -1
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.cts +2 -2
- package/dist/core/index.d.ts +2 -2
- package/dist/core/index.js +9 -1
- package/dist/core/index.js.map +1 -1
- package/dist/index-BrogG0Gc.d.cts +277 -0
- package/dist/index-DOvJES2m.d.ts +277 -0
- package/dist/index.cjs +78 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +76 -10
- package/dist/index.js.map +1 -1
- package/dist/repository/index.cjs +71 -8
- package/dist/repository/index.cjs.map +1 -1
- package/dist/repository/index.d.cts +3 -255
- package/dist/repository/index.d.ts +3 -255
- package/dist/repository/index.js +71 -8
- package/dist/repository/index.js.map +1 -1
- package/dist/service/index.cjs +17 -3
- package/dist/service/index.cjs.map +1 -1
- package/dist/service/index.d.cts +1 -1
- package/dist/service/index.d.ts +1 -1
- package/dist/service/index.js +17 -3
- package/dist/service/index.js.map +1 -1
- package/dist/{types-CY-oHIMA.d.ts → types-BL3YqxUD.d.ts} +9 -3
- package/dist/{types-DOjguhEX.d.cts → types-YLAB8bFk.d.cts} +9 -3
- package/package.json +1 -1
package/dist/adapters/index.js
CHANGED
|
@@ -1,6 +1,166 @@
|
|
|
1
1
|
// src/config/index.ts
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
|
|
4
|
+
// src/core/errors.ts
|
|
5
|
+
var CreditErrorCode = {
|
|
6
|
+
INSUFFICIENT_CREDITS: "INSUFFICIENT_CREDITS",
|
|
7
|
+
RESERVATION_NOT_FOUND: "RESERVATION_NOT_FOUND",
|
|
8
|
+
RESERVATION_EXPIRED: "RESERVATION_EXPIRED",
|
|
9
|
+
RESERVATION_ALREADY_PROCESSED: "RESERVATION_ALREADY_PROCESSED",
|
|
10
|
+
USER_NOT_FOUND: "USER_NOT_FOUND",
|
|
11
|
+
INVALID_OPERATION_TYPE: "INVALID_OPERATION_TYPE",
|
|
12
|
+
CONFIGURATION_ERROR: "CONFIGURATION_ERROR",
|
|
13
|
+
DATABASE_ERROR: "DATABASE_ERROR",
|
|
14
|
+
/** Caller reused an idempotency key with a different immutable payload. */
|
|
15
|
+
IDEMPOTENCY_CONFLICT: "IDEMPOTENCY_CONFLICT",
|
|
16
|
+
/**
|
|
17
|
+
* Retryable infrastructure failure — serialization/deadlock, lock timeout,
|
|
18
|
+
* connection loss, pool exhaustion. Distinct from `DATABASE_ERROR`, which is
|
|
19
|
+
* a deterministic failure that will fail again identically on retry.
|
|
20
|
+
*/
|
|
21
|
+
TRANSIENT_ERROR: "TRANSIENT_ERROR",
|
|
22
|
+
/** The configured repository adapter does not implement the requested V2 method. */
|
|
23
|
+
UNSUPPORTED_OPERATION: "UNSUPPORTED_OPERATION",
|
|
24
|
+
/** Amount was not a finite positive number. */
|
|
25
|
+
INVALID_AMOUNT: "INVALID_AMOUNT",
|
|
26
|
+
/** Idempotency key was supplied but empty or whitespace-only. */
|
|
27
|
+
INVALID_IDEMPOTENCY_KEY: "INVALID_IDEMPOTENCY_KEY",
|
|
28
|
+
/**
|
|
29
|
+
* A persisted reservation holds a status this library does not define, so no
|
|
30
|
+
* transition can reason about it. Quarantined rather than cast; nothing moved.
|
|
31
|
+
*/
|
|
32
|
+
CORRUPT_RESERVATION_STATUS: "CORRUPT_RESERVATION_STATUS",
|
|
33
|
+
/**
|
|
34
|
+
* A persisted reservation does not record that its hold was placed
|
|
35
|
+
* atomically, so the credits it claims are not known to be reserved. Moving
|
|
36
|
+
* it would spend another hold's coverage; nothing moved.
|
|
37
|
+
*/
|
|
38
|
+
UNBACKED_RESERVATION: "UNBACKED_RESERVATION"
|
|
39
|
+
};
|
|
40
|
+
var CreditError = class _CreditError extends Error {
|
|
41
|
+
code;
|
|
42
|
+
details;
|
|
43
|
+
constructor(message, code, details) {
|
|
44
|
+
super(message);
|
|
45
|
+
this.name = "CreditError";
|
|
46
|
+
this.code = code;
|
|
47
|
+
this.details = details;
|
|
48
|
+
if (Error.captureStackTrace) {
|
|
49
|
+
Error.captureStackTrace(this, _CreditError);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Convert to JSON-serializable object
|
|
54
|
+
*/
|
|
55
|
+
toJSON() {
|
|
56
|
+
return {
|
|
57
|
+
name: this.name,
|
|
58
|
+
code: this.code,
|
|
59
|
+
message: this.message,
|
|
60
|
+
...this.details && { details: this.details }
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
function isCreditError(error) {
|
|
65
|
+
return error instanceof CreditError;
|
|
66
|
+
}
|
|
67
|
+
function isInsufficientCreditsError(error) {
|
|
68
|
+
if (isCreditError(error)) {
|
|
69
|
+
return error.code === CreditErrorCode.INSUFFICIENT_CREDITS;
|
|
70
|
+
}
|
|
71
|
+
if (error instanceof Error) {
|
|
72
|
+
return error.message.toLowerCase().includes("insufficient credits");
|
|
73
|
+
}
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
function createInsufficientCreditsError(required, available) {
|
|
77
|
+
return new CreditError(
|
|
78
|
+
`Insufficient credits: available ${available}, required ${required}`,
|
|
79
|
+
CreditErrorCode.INSUFFICIENT_CREDITS,
|
|
80
|
+
{ required, available, shortfall: sumAmounts(required, -available) }
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
function createReservationNotFoundError(reservationId) {
|
|
84
|
+
return new CreditError(
|
|
85
|
+
`Reservation ${reservationId} not found`,
|
|
86
|
+
CreditErrorCode.RESERVATION_NOT_FOUND,
|
|
87
|
+
{ reservationId }
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
function createReservationAlreadyProcessedError(reservationId, status) {
|
|
91
|
+
return new CreditError(
|
|
92
|
+
`Reservation ${reservationId} has already been ${status}`,
|
|
93
|
+
CreditErrorCode.RESERVATION_ALREADY_PROCESSED,
|
|
94
|
+
{ reservationId, status }
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
function createUnsupportedOperationError(operation, hint) {
|
|
98
|
+
return new CreditError(
|
|
99
|
+
hint ? `Repository does not support ${operation}. ${hint}` : `Repository does not support ${operation}`,
|
|
100
|
+
CreditErrorCode.UNSUPPORTED_OPERATION,
|
|
101
|
+
{ operation, ...hint ? { hint } : {} }
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/core/amount.ts
|
|
106
|
+
var CREDIT_AMOUNT_SCALE = 2;
|
|
107
|
+
var CREDIT_AMOUNT_MAX = 999999999999e-2;
|
|
108
|
+
var MAX_CENTS = 999999999999;
|
|
109
|
+
function toCents(value) {
|
|
110
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return null;
|
|
111
|
+
const cents = Math.round(value * 100);
|
|
112
|
+
if (!Number.isSafeInteger(cents)) return null;
|
|
113
|
+
return value === cents / 100 ? cents : null;
|
|
114
|
+
}
|
|
115
|
+
function sumAmounts(...values) {
|
|
116
|
+
let cents = 0;
|
|
117
|
+
for (const value of values) {
|
|
118
|
+
const valueCents = toCents(value);
|
|
119
|
+
if (valueCents === null) return floatSum(values);
|
|
120
|
+
cents += valueCents;
|
|
121
|
+
}
|
|
122
|
+
return Number.isSafeInteger(cents) ? cents / 100 : floatSum(values);
|
|
123
|
+
}
|
|
124
|
+
function floatSum(values) {
|
|
125
|
+
let total = 0;
|
|
126
|
+
for (const value of values) total += value;
|
|
127
|
+
return total;
|
|
128
|
+
}
|
|
129
|
+
var FLOAT_NOISE_RELATIVE = 64 * Number.EPSILON;
|
|
130
|
+
function isValidCreditAmount(value) {
|
|
131
|
+
if (typeof value !== "number") return false;
|
|
132
|
+
const cents = toCents(value);
|
|
133
|
+
return cents !== null && cents > 0 && cents <= MAX_CENTS;
|
|
134
|
+
}
|
|
135
|
+
function assertValidCreditAmount(value, context) {
|
|
136
|
+
if (isValidCreditAmount(value)) return;
|
|
137
|
+
throw new CreditError(
|
|
138
|
+
creditAmountReason(value),
|
|
139
|
+
CreditErrorCode.INVALID_AMOUNT,
|
|
140
|
+
{ amount: value, max: CREDIT_AMOUNT_MAX, scale: CREDIT_AMOUNT_SCALE, ...context }
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
function creditAmountReason(value) {
|
|
144
|
+
if (typeof value !== "number") {
|
|
145
|
+
return `Credit amount must be a number (got ${typeof value})`;
|
|
146
|
+
}
|
|
147
|
+
if (!Number.isFinite(value)) {
|
|
148
|
+
return `Credit amount must be finite (got ${String(value)})`;
|
|
149
|
+
}
|
|
150
|
+
if (value <= 0) {
|
|
151
|
+
return `Credit amount must be greater than zero (got ${value})`;
|
|
152
|
+
}
|
|
153
|
+
if (value > CREDIT_AMOUNT_MAX) {
|
|
154
|
+
return `Credit amount ${value} exceeds the maximum of ${CREDIT_AMOUNT_MAX}`;
|
|
155
|
+
}
|
|
156
|
+
return `Credit amount ${value} is not exact to ${CREDIT_AMOUNT_SCALE} decimal places`;
|
|
157
|
+
}
|
|
158
|
+
function isRepresentableAmount(value) {
|
|
159
|
+
if (typeof value !== "number") return false;
|
|
160
|
+
const cents = toCents(value);
|
|
161
|
+
return cents !== null && Math.abs(cents) <= MAX_CENTS;
|
|
162
|
+
}
|
|
163
|
+
|
|
4
164
|
// src/config/provider.ts
|
|
5
165
|
var configProvider = null;
|
|
6
166
|
function getConfigProvider() {
|
|
@@ -157,17 +317,25 @@ var DEFAULT_CONFIG = {
|
|
|
157
317
|
}
|
|
158
318
|
};
|
|
159
319
|
var currentConfig = DEFAULT_CONFIG;
|
|
320
|
+
function isOperationCostMap(value) {
|
|
321
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
|
|
322
|
+
return Object.values(value).every((cost) => isRepresentableAmount(cost) && cost >= 0);
|
|
323
|
+
}
|
|
160
324
|
function loadConfigFromEnv() {
|
|
161
325
|
const envConfig = {};
|
|
162
326
|
const operationCostsEnv = process.env.CREDITS_OPERATION_COSTS;
|
|
163
327
|
if (operationCostsEnv) {
|
|
164
328
|
try {
|
|
165
329
|
const parsed = JSON.parse(operationCostsEnv);
|
|
166
|
-
if (
|
|
330
|
+
if (isOperationCostMap(parsed)) {
|
|
167
331
|
envConfig.operationCosts = {
|
|
168
332
|
...DEFAULT_CONFIG.operationCosts,
|
|
169
333
|
...parsed
|
|
170
334
|
};
|
|
335
|
+
} else {
|
|
336
|
+
console.warn(
|
|
337
|
+
"[Credits Config] Ignoring CREDITS_OPERATION_COSTS: it must be a JSON object whose costs are numbers >= 0, exact to the cent"
|
|
338
|
+
);
|
|
171
339
|
}
|
|
172
340
|
} catch {
|
|
173
341
|
console.warn("[Credits Config] Failed to parse CREDITS_OPERATION_COSTS env var as JSON");
|
|
@@ -261,161 +429,6 @@ function getOperationLabel(operationType) {
|
|
|
261
429
|
}
|
|
262
430
|
initializeConfig();
|
|
263
431
|
|
|
264
|
-
// src/core/errors.ts
|
|
265
|
-
var CreditErrorCode = {
|
|
266
|
-
INSUFFICIENT_CREDITS: "INSUFFICIENT_CREDITS",
|
|
267
|
-
RESERVATION_NOT_FOUND: "RESERVATION_NOT_FOUND",
|
|
268
|
-
RESERVATION_EXPIRED: "RESERVATION_EXPIRED",
|
|
269
|
-
RESERVATION_ALREADY_PROCESSED: "RESERVATION_ALREADY_PROCESSED",
|
|
270
|
-
USER_NOT_FOUND: "USER_NOT_FOUND",
|
|
271
|
-
INVALID_OPERATION_TYPE: "INVALID_OPERATION_TYPE",
|
|
272
|
-
CONFIGURATION_ERROR: "CONFIGURATION_ERROR",
|
|
273
|
-
DATABASE_ERROR: "DATABASE_ERROR",
|
|
274
|
-
/** Caller reused an idempotency key with a different immutable payload. */
|
|
275
|
-
IDEMPOTENCY_CONFLICT: "IDEMPOTENCY_CONFLICT",
|
|
276
|
-
/**
|
|
277
|
-
* Retryable infrastructure failure — serialization/deadlock, lock timeout,
|
|
278
|
-
* connection loss, pool exhaustion. Distinct from `DATABASE_ERROR`, which is
|
|
279
|
-
* a deterministic failure that will fail again identically on retry.
|
|
280
|
-
*/
|
|
281
|
-
TRANSIENT_ERROR: "TRANSIENT_ERROR",
|
|
282
|
-
/** The configured repository adapter does not implement the requested V2 method. */
|
|
283
|
-
UNSUPPORTED_OPERATION: "UNSUPPORTED_OPERATION",
|
|
284
|
-
/** Amount was not a finite positive number. */
|
|
285
|
-
INVALID_AMOUNT: "INVALID_AMOUNT",
|
|
286
|
-
/** Idempotency key was supplied but empty or whitespace-only. */
|
|
287
|
-
INVALID_IDEMPOTENCY_KEY: "INVALID_IDEMPOTENCY_KEY",
|
|
288
|
-
/**
|
|
289
|
-
* A persisted reservation holds a status this library does not define, so no
|
|
290
|
-
* transition can reason about it. Quarantined rather than cast; nothing moved.
|
|
291
|
-
*/
|
|
292
|
-
CORRUPT_RESERVATION_STATUS: "CORRUPT_RESERVATION_STATUS",
|
|
293
|
-
/**
|
|
294
|
-
* A persisted reservation does not record that its hold was placed
|
|
295
|
-
* atomically, so the credits it claims are not known to be reserved. Moving
|
|
296
|
-
* it would spend another hold's coverage; nothing moved.
|
|
297
|
-
*/
|
|
298
|
-
UNBACKED_RESERVATION: "UNBACKED_RESERVATION"
|
|
299
|
-
};
|
|
300
|
-
var CreditError = class _CreditError extends Error {
|
|
301
|
-
code;
|
|
302
|
-
details;
|
|
303
|
-
constructor(message, code, details) {
|
|
304
|
-
super(message);
|
|
305
|
-
this.name = "CreditError";
|
|
306
|
-
this.code = code;
|
|
307
|
-
this.details = details;
|
|
308
|
-
if (Error.captureStackTrace) {
|
|
309
|
-
Error.captureStackTrace(this, _CreditError);
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
/**
|
|
313
|
-
* Convert to JSON-serializable object
|
|
314
|
-
*/
|
|
315
|
-
toJSON() {
|
|
316
|
-
return {
|
|
317
|
-
name: this.name,
|
|
318
|
-
code: this.code,
|
|
319
|
-
message: this.message,
|
|
320
|
-
...this.details && { details: this.details }
|
|
321
|
-
};
|
|
322
|
-
}
|
|
323
|
-
};
|
|
324
|
-
function isCreditError(error) {
|
|
325
|
-
return error instanceof CreditError;
|
|
326
|
-
}
|
|
327
|
-
function isInsufficientCreditsError(error) {
|
|
328
|
-
if (isCreditError(error)) {
|
|
329
|
-
return error.code === CreditErrorCode.INSUFFICIENT_CREDITS;
|
|
330
|
-
}
|
|
331
|
-
if (error instanceof Error) {
|
|
332
|
-
return error.message.toLowerCase().includes("insufficient credits");
|
|
333
|
-
}
|
|
334
|
-
return false;
|
|
335
|
-
}
|
|
336
|
-
function createInsufficientCreditsError(required, available) {
|
|
337
|
-
return new CreditError(
|
|
338
|
-
`Insufficient credits: available ${available}, required ${required}`,
|
|
339
|
-
CreditErrorCode.INSUFFICIENT_CREDITS,
|
|
340
|
-
{ required, available, shortfall: sumAmounts(required, -available) }
|
|
341
|
-
);
|
|
342
|
-
}
|
|
343
|
-
function createReservationNotFoundError(reservationId) {
|
|
344
|
-
return new CreditError(
|
|
345
|
-
`Reservation ${reservationId} not found`,
|
|
346
|
-
CreditErrorCode.RESERVATION_NOT_FOUND,
|
|
347
|
-
{ reservationId }
|
|
348
|
-
);
|
|
349
|
-
}
|
|
350
|
-
function createReservationAlreadyProcessedError(reservationId, status) {
|
|
351
|
-
return new CreditError(
|
|
352
|
-
`Reservation ${reservationId} has already been ${status}`,
|
|
353
|
-
CreditErrorCode.RESERVATION_ALREADY_PROCESSED,
|
|
354
|
-
{ reservationId, status }
|
|
355
|
-
);
|
|
356
|
-
}
|
|
357
|
-
function createUnsupportedOperationError(operation, hint) {
|
|
358
|
-
return new CreditError(
|
|
359
|
-
hint ? `Repository does not support ${operation}. ${hint}` : `Repository does not support ${operation}`,
|
|
360
|
-
CreditErrorCode.UNSUPPORTED_OPERATION,
|
|
361
|
-
{ operation, ...hint ? { hint } : {} }
|
|
362
|
-
);
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
// src/core/amount.ts
|
|
366
|
-
var CREDIT_AMOUNT_SCALE = 2;
|
|
367
|
-
var CREDIT_AMOUNT_MAX = 999999999999e-2;
|
|
368
|
-
var MAX_CENTS = 999999999999;
|
|
369
|
-
function toCents(value) {
|
|
370
|
-
if (typeof value !== "number" || !Number.isFinite(value)) return null;
|
|
371
|
-
const cents = Math.round(value * 100);
|
|
372
|
-
if (!Number.isSafeInteger(cents)) return null;
|
|
373
|
-
return value === cents / 100 ? cents : null;
|
|
374
|
-
}
|
|
375
|
-
function sumAmounts(...values) {
|
|
376
|
-
let cents = 0;
|
|
377
|
-
for (const value of values) {
|
|
378
|
-
const valueCents = toCents(value);
|
|
379
|
-
if (valueCents === null) return floatSum(values);
|
|
380
|
-
cents += valueCents;
|
|
381
|
-
}
|
|
382
|
-
return Number.isSafeInteger(cents) ? cents / 100 : floatSum(values);
|
|
383
|
-
}
|
|
384
|
-
function floatSum(values) {
|
|
385
|
-
let total = 0;
|
|
386
|
-
for (const value of values) total += value;
|
|
387
|
-
return total;
|
|
388
|
-
}
|
|
389
|
-
var FLOAT_NOISE_RELATIVE = 64 * Number.EPSILON;
|
|
390
|
-
function isValidCreditAmount(value) {
|
|
391
|
-
if (typeof value !== "number") return false;
|
|
392
|
-
const cents = toCents(value);
|
|
393
|
-
return cents !== null && cents > 0 && cents <= MAX_CENTS;
|
|
394
|
-
}
|
|
395
|
-
function assertValidCreditAmount(value, context) {
|
|
396
|
-
if (isValidCreditAmount(value)) return;
|
|
397
|
-
throw new CreditError(
|
|
398
|
-
creditAmountReason(value),
|
|
399
|
-
CreditErrorCode.INVALID_AMOUNT,
|
|
400
|
-
{ amount: value, max: CREDIT_AMOUNT_MAX, scale: CREDIT_AMOUNT_SCALE, ...context }
|
|
401
|
-
);
|
|
402
|
-
}
|
|
403
|
-
function creditAmountReason(value) {
|
|
404
|
-
if (typeof value !== "number") {
|
|
405
|
-
return `Credit amount must be a number (got ${typeof value})`;
|
|
406
|
-
}
|
|
407
|
-
if (!Number.isFinite(value)) {
|
|
408
|
-
return `Credit amount must be finite (got ${String(value)})`;
|
|
409
|
-
}
|
|
410
|
-
if (value <= 0) {
|
|
411
|
-
return `Credit amount must be greater than zero (got ${value})`;
|
|
412
|
-
}
|
|
413
|
-
if (value > CREDIT_AMOUNT_MAX) {
|
|
414
|
-
return `Credit amount ${value} exceeds the maximum of ${CREDIT_AMOUNT_MAX}`;
|
|
415
|
-
}
|
|
416
|
-
return `Credit amount ${value} is not exact to ${CREDIT_AMOUNT_SCALE} decimal places`;
|
|
417
|
-
}
|
|
418
|
-
|
|
419
432
|
// src/repository/types.ts
|
|
420
433
|
function supportsCreditsV2(repository) {
|
|
421
434
|
return typeof repository.reserveCreditsV2 === "function" && typeof repository.commitReservationV2 === "function" && typeof repository.releaseReservationV2 === "function" && typeof repository.expireReservationV2 === "function";
|