resultar 3.3.1 → 3.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/README.md +30 -1
- package/dist/index.d.ts +898 -112
- package/dist/index.js +267 -10
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
//#region src/abort-error.ts
|
|
2
|
+
/**
|
|
3
|
+
* Standard cancellation error used by ResultAsync retry, race, timeout, and resource helpers.
|
|
4
|
+
*/
|
|
2
5
|
var AbortError = class extends Error {
|
|
6
|
+
/**
|
|
7
|
+
* Creates an AbortError.
|
|
8
|
+
*/
|
|
3
9
|
constructor(message = "Operation aborted", options) {
|
|
4
10
|
super(message, options);
|
|
5
11
|
this.name = "AbortError";
|
|
@@ -7,6 +13,9 @@ var AbortError = class extends Error {
|
|
|
7
13
|
};
|
|
8
14
|
const hasAbortName = (value) => typeof value === "object" && value !== null && "name" in value && value.name === "AbortError";
|
|
9
15
|
const hasAbortCode = (value) => typeof value === "object" && value !== null && "code" in value && value.code === "ABORT_ERR";
|
|
16
|
+
/**
|
|
17
|
+
* Checks whether a value is an AbortError or abort-shaped platform error.
|
|
18
|
+
*/
|
|
10
19
|
const isAbortError = (value) => value instanceof AbortError || hasAbortName(value) || hasAbortCode(value);
|
|
11
20
|
//#endregion
|
|
12
21
|
//#region src/pipe.ts
|
|
@@ -64,8 +73,9 @@ const callTaggedHandler = (value, handlers) => {
|
|
|
64
73
|
return { value: handler(value) };
|
|
65
74
|
};
|
|
66
75
|
const callErrorHandler = (error, handlers) => {
|
|
67
|
-
|
|
68
|
-
|
|
76
|
+
const handler = handlers.Error;
|
|
77
|
+
if (handler === void 0) return noTaggedHandlerMatch;
|
|
78
|
+
return { value: handler(error) };
|
|
69
79
|
};
|
|
70
80
|
const callTaggedOrErrorHandler = (error, handlers) => {
|
|
71
81
|
const tagged = callTaggedHandler(error, handlers);
|
|
@@ -537,32 +547,64 @@ var Err = class extends ResultVariant {
|
|
|
537
547
|
var DisposableResult = class {
|
|
538
548
|
disposed = false;
|
|
539
549
|
finalizer;
|
|
550
|
+
/**
|
|
551
|
+
* Wrapped Result value.
|
|
552
|
+
*/
|
|
540
553
|
result;
|
|
554
|
+
/**
|
|
555
|
+
* Creates a disposable wrapper around a Result.
|
|
556
|
+
*/
|
|
541
557
|
constructor(result, finalizer) {
|
|
542
558
|
this.result = result;
|
|
543
559
|
this.finalizer = finalizer;
|
|
544
560
|
}
|
|
561
|
+
/**
|
|
562
|
+
* Ok value when present, otherwise undefined cast to `T` for compatibility.
|
|
563
|
+
*/
|
|
545
564
|
get value() {
|
|
546
565
|
return this.result.isOk() ? this.result.value : void 0;
|
|
547
566
|
}
|
|
567
|
+
/**
|
|
568
|
+
* Err value when present, otherwise undefined cast to `E` for compatibility.
|
|
569
|
+
*/
|
|
548
570
|
get error() {
|
|
549
571
|
return this.result.isErr() ? this.result.error : void 0;
|
|
550
572
|
}
|
|
573
|
+
/**
|
|
574
|
+
* Delegates to the wrapped Result `_unsafeUnwrap`.
|
|
575
|
+
*/
|
|
551
576
|
_unsafeUnwrap(config) {
|
|
552
577
|
return this.result._unsafeUnwrap(config);
|
|
553
578
|
}
|
|
579
|
+
/**
|
|
580
|
+
* Delegates to the wrapped Result `_unsafeUnwrapErr`.
|
|
581
|
+
*/
|
|
554
582
|
_unsafeUnwrapErr(config) {
|
|
555
583
|
return this.result._unsafeUnwrapErr(config);
|
|
556
584
|
}
|
|
585
|
+
/**
|
|
586
|
+
* Returns whether the wrapped Result is Ok.
|
|
587
|
+
*/
|
|
557
588
|
isOk() {
|
|
558
589
|
return this.result.isOk();
|
|
559
590
|
}
|
|
591
|
+
/**
|
|
592
|
+
* Returns whether the wrapped Result is Err.
|
|
593
|
+
*/
|
|
560
594
|
isErr() {
|
|
561
595
|
return this.result.isErr();
|
|
562
596
|
}
|
|
597
|
+
/**
|
|
598
|
+
* Returns the wrapped Ok value, or `defaultValue` when Err.
|
|
599
|
+
*/
|
|
563
600
|
unwrapOr(defaultValue) {
|
|
564
601
|
return this.result.unwrapOr(defaultValue);
|
|
565
602
|
}
|
|
603
|
+
/**
|
|
604
|
+
* Runs the finalizer once.
|
|
605
|
+
*
|
|
606
|
+
* Finalizer exceptions are intentionally swallowed.
|
|
607
|
+
*/
|
|
566
608
|
[Symbol.dispose]() {
|
|
567
609
|
if (this.disposed) return;
|
|
568
610
|
this.disposed = true;
|
|
@@ -593,6 +635,11 @@ function ok(value) {
|
|
|
593
635
|
function err(error) {
|
|
594
636
|
return new Err(error);
|
|
595
637
|
}
|
|
638
|
+
/**
|
|
639
|
+
* Wraps a throwing synchronous function so every later call returns a Result.
|
|
640
|
+
*
|
|
641
|
+
* Prefer `tryResult` when you want to run the operation immediately.
|
|
642
|
+
*/
|
|
596
643
|
const fromThrowable = Result.fromThrowable;
|
|
597
644
|
function tryResult(input, errorFn) {
|
|
598
645
|
const fn = typeof input === "function" ? input : input.try;
|
|
@@ -604,9 +651,19 @@ function tryResult(input, errorFn) {
|
|
|
604
651
|
return err(error);
|
|
605
652
|
}
|
|
606
653
|
}
|
|
654
|
+
/**
|
|
655
|
+
* Creates an Ok result with `undefined`.
|
|
656
|
+
*/
|
|
607
657
|
function unit() {
|
|
608
658
|
return ok(void 0);
|
|
609
659
|
}
|
|
660
|
+
/**
|
|
661
|
+
* Runs a `Result` at an application boundary and returns the success value.
|
|
662
|
+
*
|
|
663
|
+
* If the result is `Err`, this throws the error. Use it at final framework, CLI, test, or bootstrap
|
|
664
|
+
* boundaries after the error channel has already been modeled with Resultar.
|
|
665
|
+
*/
|
|
666
|
+
const runSync = (result) => result.unwrapOrThrow();
|
|
610
667
|
/** Compatibility alias. Prefer `tryResult` in new code. */
|
|
611
668
|
const tryCatch = tryResult;
|
|
612
669
|
//#endregion
|
|
@@ -1060,13 +1117,24 @@ var DisposableResultAsync = class {
|
|
|
1060
1117
|
innerPromise;
|
|
1061
1118
|
finalizer;
|
|
1062
1119
|
disposed = false;
|
|
1120
|
+
/**
|
|
1121
|
+
* Creates an async disposable wrapper around a ResultAsync promise.
|
|
1122
|
+
*/
|
|
1063
1123
|
constructor(res, finalizer) {
|
|
1064
1124
|
this.innerPromise = res;
|
|
1065
1125
|
this.finalizer = finalizer;
|
|
1066
1126
|
}
|
|
1127
|
+
/**
|
|
1128
|
+
* PromiseLike integration. Resolves to the wrapped Result.
|
|
1129
|
+
*/
|
|
1067
1130
|
then(successCallback, failureCallback) {
|
|
1068
1131
|
return this.innerPromise.then(successCallback, failureCallback);
|
|
1069
1132
|
}
|
|
1133
|
+
/**
|
|
1134
|
+
* Runs the finalizer once after the wrapped Result resolves.
|
|
1135
|
+
*
|
|
1136
|
+
* Finalizer exceptions and rejections are intentionally swallowed.
|
|
1137
|
+
*/
|
|
1070
1138
|
async [Symbol.asyncDispose]() {
|
|
1071
1139
|
if (this.disposed) return;
|
|
1072
1140
|
this.disposed = true;
|
|
@@ -1155,24 +1223,52 @@ var ResultAsync = class ResultAsync extends Pipeable {
|
|
|
1155
1223
|
if (f === void 0) return combineResultAsyncListWithAllErrors(asyncResultListOrItems);
|
|
1156
1224
|
return validateAllResultAsyncItems(asyncResultListOrItems, f, options);
|
|
1157
1225
|
}
|
|
1226
|
+
/**
|
|
1227
|
+
* Combines exactly two ResultAsync values into an Ok tuple or the first Err.
|
|
1228
|
+
*/
|
|
1158
1229
|
static zip(left, right) {
|
|
1159
1230
|
return combineResultAsyncList([left, right]);
|
|
1160
1231
|
}
|
|
1232
|
+
/**
|
|
1233
|
+
* Retries a ResultAsync task according to a typed retry policy.
|
|
1234
|
+
*
|
|
1235
|
+
* The returned error channel includes the task error and `AbortError`.
|
|
1236
|
+
*/
|
|
1161
1237
|
static retry(task, options) {
|
|
1162
1238
|
return retryResultAsyncTask(task, options);
|
|
1163
1239
|
}
|
|
1240
|
+
/**
|
|
1241
|
+
* Retries a ResultAsync task and falls back to another Resultar operation after retry exhaustion.
|
|
1242
|
+
*/
|
|
1164
1243
|
static retryOrElse(task, options) {
|
|
1165
1244
|
return retryOrElseResultAsyncTask(task, options);
|
|
1166
1245
|
}
|
|
1246
|
+
/**
|
|
1247
|
+
* Acquires, uses, and releases a resource with cleanup on every path.
|
|
1248
|
+
*
|
|
1249
|
+
* Release errors are intentionally not part of the success/error channel; model cleanup failures
|
|
1250
|
+
* in `use` when they should affect control flow.
|
|
1251
|
+
*/
|
|
1167
1252
|
static withResource(options) {
|
|
1168
1253
|
return withResultAsyncResource(options);
|
|
1169
1254
|
}
|
|
1255
|
+
/**
|
|
1256
|
+
* Races two cooperative ResultAsync tasks and returns the first settled Result.
|
|
1257
|
+
*/
|
|
1170
1258
|
static race(left, right) {
|
|
1171
1259
|
return raceResultAsyncTasks([left, right]);
|
|
1172
1260
|
}
|
|
1261
|
+
/**
|
|
1262
|
+
* Races multiple cooperative ResultAsync tasks and returns the first settled Result.
|
|
1263
|
+
*/
|
|
1173
1264
|
static raceAll(tasks) {
|
|
1174
1265
|
return raceResultAsyncTasks(tasks);
|
|
1175
1266
|
}
|
|
1267
|
+
/**
|
|
1268
|
+
* Races two cooperative ResultAsync tasks and returns the first successful Ok.
|
|
1269
|
+
*
|
|
1270
|
+
* If both fail, the returned error is the final observed Err.
|
|
1271
|
+
*/
|
|
1176
1272
|
static raceFirst(left, right) {
|
|
1177
1273
|
return observeRaceResultAsyncTasks([left, right], (resolve, startedTasks) => ({
|
|
1178
1274
|
resolve,
|
|
@@ -1180,6 +1276,12 @@ var ResultAsync = class ResultAsync extends Pipeable {
|
|
|
1180
1276
|
state: { settled: false }
|
|
1181
1277
|
}), observeRaceFirstTask);
|
|
1182
1278
|
}
|
|
1279
|
+
/**
|
|
1280
|
+
* Races two cooperative ResultAsync tasks and lets handlers decide what to do when either side
|
|
1281
|
+
* settles first.
|
|
1282
|
+
*
|
|
1283
|
+
* Handlers receive the settled Result and a handle to the still-running opposite task.
|
|
1284
|
+
*/
|
|
1183
1285
|
static raceWith(left, right, handlers) {
|
|
1184
1286
|
return new ResultAsync(new Promise((resolve) => {
|
|
1185
1287
|
const leftTask = startRaceTask(left, 0);
|
|
@@ -1205,6 +1307,9 @@ var ResultAsync = class ResultAsync extends Pipeable {
|
|
|
1205
1307
|
});
|
|
1206
1308
|
}));
|
|
1207
1309
|
}
|
|
1310
|
+
/**
|
|
1311
|
+
* Runs a cooperative ResultAsync task with a typed timeout error.
|
|
1312
|
+
*/
|
|
1208
1313
|
static timeout(task, options) {
|
|
1209
1314
|
const timeoutTask = (signal) => {
|
|
1210
1315
|
return new ResultAsync(new Promise((resolve) => {
|
|
@@ -1227,6 +1332,9 @@ var ResultAsync = class ResultAsync extends Pipeable {
|
|
|
1227
1332
|
}
|
|
1228
1333
|
});
|
|
1229
1334
|
}
|
|
1335
|
+
/**
|
|
1336
|
+
* Runs async candidates until the first Ok and returns the last Err if every candidate fails.
|
|
1337
|
+
*/
|
|
1230
1338
|
static firstSuccessOf(candidates) {
|
|
1231
1339
|
return firstSuccessOfAsyncCandidates(candidates);
|
|
1232
1340
|
}
|
|
@@ -1234,15 +1342,27 @@ var ResultAsync = class ResultAsync extends Pipeable {
|
|
|
1234
1342
|
if (typeof condition === "boolean" || typeof condition === "function") return ifResultAsync(condition, options);
|
|
1235
1343
|
return ifResultAsyncWithCondition(condition, options);
|
|
1236
1344
|
}
|
|
1345
|
+
/**
|
|
1346
|
+
* Runs `body` only when the condition is true; otherwise returns Ok(undefined).
|
|
1347
|
+
*/
|
|
1237
1348
|
static when(condition, body) {
|
|
1238
1349
|
return whenResultAsync(condition, body);
|
|
1239
1350
|
}
|
|
1351
|
+
/**
|
|
1352
|
+
* Runs `body` only when a Resultar boolean condition is Ok(true).
|
|
1353
|
+
*/
|
|
1240
1354
|
static whenResult(condition, body) {
|
|
1241
1355
|
return whenResultAsyncWithCondition(condition, body);
|
|
1242
1356
|
}
|
|
1357
|
+
/**
|
|
1358
|
+
* Runs `body` only when the condition is false; otherwise returns Ok(undefined).
|
|
1359
|
+
*/
|
|
1243
1360
|
static unless(condition, body) {
|
|
1244
1361
|
return unlessResultAsync(condition, body);
|
|
1245
1362
|
}
|
|
1363
|
+
/**
|
|
1364
|
+
* Runs `body` only when a Resultar boolean condition is Ok(false).
|
|
1365
|
+
*/
|
|
1246
1366
|
static unlessResult(condition, body) {
|
|
1247
1367
|
return unlessResultAsyncWithCondition(condition, body);
|
|
1248
1368
|
}
|
|
@@ -1256,12 +1376,9 @@ var ResultAsync = class ResultAsync extends Pipeable {
|
|
|
1256
1376
|
return forEachResultAsync(items, f, options);
|
|
1257
1377
|
}
|
|
1258
1378
|
/**
|
|
1259
|
-
* Wraps
|
|
1260
|
-
* arguments but returning `Ok` if successful, `Err` if the function throws
|
|
1379
|
+
* Wraps an async function so every later call returns a ResultAsync.
|
|
1261
1380
|
*
|
|
1262
|
-
*
|
|
1263
|
-
* @param errorFn when an error is thrown, this will wrap the error result if provided
|
|
1264
|
-
* @returns a new function that returns a `ResultAsync`
|
|
1381
|
+
* Prefer `tryResultAsync` when you want to run the operation immediately.
|
|
1265
1382
|
*/
|
|
1266
1383
|
static fromThrowable(fn, errorFn) {
|
|
1267
1384
|
return (...args) => new ResultAsync((async () => {
|
|
@@ -1273,25 +1390,45 @@ var ResultAsync = class ResultAsync extends Pipeable {
|
|
|
1273
1390
|
})());
|
|
1274
1391
|
}
|
|
1275
1392
|
innerPromise;
|
|
1393
|
+
/**
|
|
1394
|
+
* Creates a ResultAsync from a Promise that resolves to a Result.
|
|
1395
|
+
*
|
|
1396
|
+
* Most application code should prefer `tryResultAsync`, `fromPromise`, `okAsync`, or `errAsync`.
|
|
1397
|
+
*/
|
|
1276
1398
|
constructor(res) {
|
|
1277
1399
|
super();
|
|
1278
1400
|
this.innerPromise = res;
|
|
1279
1401
|
}
|
|
1402
|
+
/**
|
|
1403
|
+
* PromiseLike integration. Resolves to the underlying `Result<T, E>`.
|
|
1404
|
+
*
|
|
1405
|
+
* Prefer ResultAsync combinators for application flow; this exists so ResultAsync can be awaited
|
|
1406
|
+
* and used by Promise APIs.
|
|
1407
|
+
*/
|
|
1280
1408
|
then(successCallback, failureCallback) {
|
|
1281
1409
|
return this.innerPromise.then(successCallback, failureCallback);
|
|
1282
1410
|
}
|
|
1411
|
+
/**
|
|
1412
|
+
* Maps the Err value while preserving the Ok value.
|
|
1413
|
+
*/
|
|
1283
1414
|
mapErr(f) {
|
|
1284
1415
|
return new ResultAsync(this.innerPromise.then(async (res) => {
|
|
1285
1416
|
if (res.isOk()) return ok(res.value);
|
|
1286
1417
|
return err(await f(res.error));
|
|
1287
1418
|
}));
|
|
1288
1419
|
}
|
|
1420
|
+
/**
|
|
1421
|
+
* Maps the Ok value while preserving the error channel.
|
|
1422
|
+
*/
|
|
1289
1423
|
map(f) {
|
|
1290
1424
|
return new ResultAsync(this.innerPromise.then(async (res) => {
|
|
1291
1425
|
if (res.isErr()) return err(res.error);
|
|
1292
1426
|
return ok(await f(res.value));
|
|
1293
1427
|
}));
|
|
1294
1428
|
}
|
|
1429
|
+
/**
|
|
1430
|
+
* Replaces the Ok value with a constant while preserving the error channel.
|
|
1431
|
+
*/
|
|
1295
1432
|
as(value) {
|
|
1296
1433
|
return new ResultAsync(this.innerPromise.then((res) => {
|
|
1297
1434
|
if (res.isErr()) return err(res.error);
|
|
@@ -1312,6 +1449,9 @@ var ResultAsync = class ResultAsync extends Pipeable {
|
|
|
1312
1449
|
return next instanceof ResultAsync ? next.innerPromise : next;
|
|
1313
1450
|
}));
|
|
1314
1451
|
}
|
|
1452
|
+
/**
|
|
1453
|
+
* Branches from the Ok value into one of two async fallible Result branches.
|
|
1454
|
+
*/
|
|
1315
1455
|
if(fCondition) {
|
|
1316
1456
|
return { true: (fTrue) => ({ false: (fFalse) => new ResultAsync(this.innerPromise.then(async (res) => {
|
|
1317
1457
|
if (res.isOk()) return fCondition(res.value) ? fTrue(res.value) : fFalse(res.value);
|
|
@@ -1337,6 +1477,9 @@ var ResultAsync = class ResultAsync extends Pipeable {
|
|
|
1337
1477
|
return Result.err(res.error);
|
|
1338
1478
|
}));
|
|
1339
1479
|
}
|
|
1480
|
+
/**
|
|
1481
|
+
* Recovers from multiple tagged errors by `_tag`.
|
|
1482
|
+
*/
|
|
1340
1483
|
catchTags(handlers) {
|
|
1341
1484
|
return new ResultAsync(this.innerPromise.then((res) => {
|
|
1342
1485
|
if (res.isOk()) return Result.ok(res.value);
|
|
@@ -1362,6 +1505,9 @@ var ResultAsync = class ResultAsync extends Pipeable {
|
|
|
1362
1505
|
return Result.err(res.error);
|
|
1363
1506
|
}));
|
|
1364
1507
|
}
|
|
1508
|
+
/**
|
|
1509
|
+
* Recovers from multiple nested tagged `reason` variants on a specific tagged error.
|
|
1510
|
+
*/
|
|
1365
1511
|
catchReasons(errorTag, handlers) {
|
|
1366
1512
|
return new ResultAsync(this.innerPromise.then((res) => {
|
|
1367
1513
|
if (res.isOk()) return Result.ok(res.value);
|
|
@@ -1378,6 +1524,9 @@ var ResultAsync = class ResultAsync extends Pipeable {
|
|
|
1378
1524
|
return Result.err(res.error);
|
|
1379
1525
|
}));
|
|
1380
1526
|
}
|
|
1527
|
+
/**
|
|
1528
|
+
* Moves the nested `reason` of a tagged error into the ResultAsync error channel.
|
|
1529
|
+
*/
|
|
1381
1530
|
unwrapReason(errorTag) {
|
|
1382
1531
|
return new ResultAsync(this.innerPromise.then((res) => {
|
|
1383
1532
|
if (res.isOk()) return Result.ok(res.value);
|
|
@@ -1388,6 +1537,9 @@ var ResultAsync = class ResultAsync extends Pipeable {
|
|
|
1388
1537
|
async match(input, fnErr) {
|
|
1389
1538
|
return this.innerPromise.then((res) => typeof input === "function" ? res.match(input, getMatchErrorHandler(input, fnErr)) : res.match(input));
|
|
1390
1539
|
}
|
|
1540
|
+
/**
|
|
1541
|
+
* Converts this ResultAsync into a Promise of a plain value with exhaustive tagged-error handlers.
|
|
1542
|
+
*/
|
|
1391
1543
|
async matchTags(ok, handlers) {
|
|
1392
1544
|
return this.innerPromise.then((res) => matchResolvedResultTags({
|
|
1393
1545
|
result: res,
|
|
@@ -1398,6 +1550,10 @@ var ResultAsync = class ResultAsync extends Pipeable {
|
|
|
1398
1550
|
}
|
|
1399
1551
|
}));
|
|
1400
1552
|
}
|
|
1553
|
+
/**
|
|
1554
|
+
* Converts this ResultAsync into a Promise of a plain value with partial tagged-error handlers and
|
|
1555
|
+
* a fallback.
|
|
1556
|
+
*/
|
|
1401
1557
|
async matchTagsPartial(ok, handlers, fallback) {
|
|
1402
1558
|
return this.innerPromise.then((res) => matchResolvedResultTags({
|
|
1403
1559
|
result: res,
|
|
@@ -1406,9 +1562,17 @@ var ResultAsync = class ResultAsync extends Pipeable {
|
|
|
1406
1562
|
fallback
|
|
1407
1563
|
}));
|
|
1408
1564
|
}
|
|
1565
|
+
/**
|
|
1566
|
+
* Resolves to the Ok value, or `t` when this ResultAsync is Err.
|
|
1567
|
+
*/
|
|
1409
1568
|
async unwrapOr(t) {
|
|
1410
1569
|
return this.innerPromise.then((res) => res.unwrapOr(t));
|
|
1411
1570
|
}
|
|
1571
|
+
/**
|
|
1572
|
+
* Resolves to the Ok value or rejects with the Err value.
|
|
1573
|
+
*
|
|
1574
|
+
* Use this at final application boundaries after the error channel has been modeled.
|
|
1575
|
+
*/
|
|
1412
1576
|
async unwrapOrThrow() {
|
|
1413
1577
|
return this.innerPromise.then((res) => res.unwrapOrThrow());
|
|
1414
1578
|
}
|
|
@@ -1460,9 +1624,15 @@ var ResultAsync = class ResultAsync extends Pipeable {
|
|
|
1460
1624
|
return okAsync(res.value);
|
|
1461
1625
|
}));
|
|
1462
1626
|
}
|
|
1627
|
+
/**
|
|
1628
|
+
* Attaches an async disposal finalizer to this ResultAsync.
|
|
1629
|
+
*/
|
|
1463
1630
|
toAsyncDisposable(f) {
|
|
1464
1631
|
return new DisposableResultAsync(this.innerPromise, f);
|
|
1465
1632
|
}
|
|
1633
|
+
/**
|
|
1634
|
+
* Enables `yield* resultAsync` inside async `safeTry` generators.
|
|
1635
|
+
*/
|
|
1466
1636
|
async *[Symbol.asyncIterator]() {
|
|
1467
1637
|
const result = await this.innerPromise;
|
|
1468
1638
|
if (result.isOk()) return result.value;
|
|
@@ -1471,11 +1641,31 @@ var ResultAsync = class ResultAsync extends Pipeable {
|
|
|
1471
1641
|
}
|
|
1472
1642
|
};
|
|
1473
1643
|
registerResultAsyncFactory((promise) => new ResultAsync(promise));
|
|
1644
|
+
/**
|
|
1645
|
+
* Creates an Ok ResultAsync.
|
|
1646
|
+
*/
|
|
1474
1647
|
const okAsync = ResultAsync.okAsync;
|
|
1648
|
+
/**
|
|
1649
|
+
* Creates an Err ResultAsync.
|
|
1650
|
+
*/
|
|
1475
1651
|
const errAsync = ResultAsync.errAsync;
|
|
1652
|
+
/**
|
|
1653
|
+
* Converts an existing promise into a ResultAsync and maps rejection into a typed Err.
|
|
1654
|
+
*/
|
|
1476
1655
|
const fromPromise = ResultAsync.fromPromise;
|
|
1656
|
+
/**
|
|
1657
|
+
* Converts an existing promise that is expected not to reject into a ResultAsync.
|
|
1658
|
+
*
|
|
1659
|
+
* Use `fromPromise` or `tryResultAsync` when rejection should become a typed Err.
|
|
1660
|
+
*/
|
|
1477
1661
|
const fromSafePromise = ResultAsync.fromSafePromise;
|
|
1662
|
+
/**
|
|
1663
|
+
* Creates an Ok ResultAsync with `undefined`.
|
|
1664
|
+
*/
|
|
1478
1665
|
const unitAsync = ResultAsync.unitAsync;
|
|
1666
|
+
/**
|
|
1667
|
+
* Wraps an async function so every later call returns a ResultAsync.
|
|
1668
|
+
*/
|
|
1479
1669
|
const fromThrowableAsync = ResultAsync.fromThrowable;
|
|
1480
1670
|
function tryResultAsync(input, errorFn) {
|
|
1481
1671
|
const promiseOrFn = isTryResultAsyncOptions(input) ? input.try : input;
|
|
@@ -1485,6 +1675,13 @@ function tryResultAsync(input, errorFn) {
|
|
|
1485
1675
|
return Result.err(error);
|
|
1486
1676
|
}));
|
|
1487
1677
|
}
|
|
1678
|
+
/**
|
|
1679
|
+
* Runs a `ResultAsync` at an application boundary and returns the success value.
|
|
1680
|
+
*
|
|
1681
|
+
* If the result is `Err`, this rejects with the error. Use it at final framework, CLI, test, or
|
|
1682
|
+
* bootstrap boundaries after the error channel has already been modeled with Resultar.
|
|
1683
|
+
*/
|
|
1684
|
+
const runPromise = (result) => result.unwrapOrThrow();
|
|
1488
1685
|
/** Compatibility alias. Prefer `tryResultAsync` in new code. */
|
|
1489
1686
|
const tryCatchAsync = tryResultAsync;
|
|
1490
1687
|
//#endregion
|
|
@@ -1494,27 +1691,48 @@ const formatRedacted = (label) => label === void 0 ? "<redacted>" : `<redacted:$
|
|
|
1494
1691
|
var RedactedValue = class {
|
|
1495
1692
|
__resultarRedacted = true;
|
|
1496
1693
|
label;
|
|
1694
|
+
/**
|
|
1695
|
+
* Stores a sensitive value behind a redacted string wrapper.
|
|
1696
|
+
*/
|
|
1497
1697
|
constructor(value, label) {
|
|
1498
1698
|
this.label = label;
|
|
1499
1699
|
redactedValues.set(this, value);
|
|
1500
1700
|
}
|
|
1701
|
+
/**
|
|
1702
|
+
* Serializes to the redacted placeholder.
|
|
1703
|
+
*/
|
|
1501
1704
|
toJSON() {
|
|
1502
1705
|
return this.toString();
|
|
1503
1706
|
}
|
|
1707
|
+
/**
|
|
1708
|
+
* Converts to the redacted placeholder.
|
|
1709
|
+
*/
|
|
1504
1710
|
toString() {
|
|
1505
1711
|
return formatRedacted(this.label);
|
|
1506
1712
|
}
|
|
1507
1713
|
};
|
|
1714
|
+
/**
|
|
1715
|
+
* Wraps a sensitive value so tagged-error messages and JSON output show only a placeholder.
|
|
1716
|
+
*/
|
|
1508
1717
|
const redact = (value, label) => new RedactedValue(value, label);
|
|
1718
|
+
/**
|
|
1719
|
+
* Checks whether a value was created by `redact`.
|
|
1720
|
+
*/
|
|
1509
1721
|
const isRedacted = (value) => typeof value === "object" && value !== null && redactedValues.has(value);
|
|
1722
|
+
/**
|
|
1723
|
+
* Returns the original sensitive value from a Redacted wrapper.
|
|
1724
|
+
*/
|
|
1510
1725
|
const revealRedacted = (value) => redactedValues.get(value);
|
|
1726
|
+
/**
|
|
1727
|
+
* Converts a Redacted wrapper to its placeholder string.
|
|
1728
|
+
*/
|
|
1511
1729
|
const stringifyRedacted = (value) => value.toString();
|
|
1512
1730
|
//#endregion
|
|
1513
1731
|
//#region src/tagged-error.ts
|
|
1514
1732
|
const defaultBaseError = Error;
|
|
1515
1733
|
const defaultMessageTemplate = "$message";
|
|
1516
1734
|
const nativeError = Error;
|
|
1517
|
-
const templateVariableMatcher = /\$([a-zA-Z_][a-zA-Z0-9_]*)/gu;
|
|
1735
|
+
const templateVariableMatcher = /\$(?<name>[a-zA-Z_][a-zA-Z0-9_]*)/gu;
|
|
1518
1736
|
const reservedTemplateVariables = new Set([
|
|
1519
1737
|
"_tag",
|
|
1520
1738
|
"cause",
|
|
@@ -1550,7 +1768,7 @@ const compileMessageInterpolator = (template) => (values) => template.replaceAll
|
|
|
1550
1768
|
});
|
|
1551
1769
|
const getTemplateVariableNames = (template) => {
|
|
1552
1770
|
templateVariableMatcher.lastIndex = 0;
|
|
1553
|
-
return [...template.matchAll(templateVariableMatcher)].map((match) => String(match[
|
|
1771
|
+
return [...template.matchAll(templateVariableMatcher)].map((match) => String(match.groups?.["name"]));
|
|
1554
1772
|
};
|
|
1555
1773
|
const assertNoReservedTemplateVariables = (tag, template) => {
|
|
1556
1774
|
if (template === defaultMessageTemplate) return;
|
|
@@ -1584,6 +1802,9 @@ const serializeCause = (cause, seen = /* @__PURE__ */ new Set()) => {
|
|
|
1584
1802
|
if (cause.cause !== void 0) json["cause"] = serializeCause(cause.cause, seen);
|
|
1585
1803
|
return json;
|
|
1586
1804
|
};
|
|
1805
|
+
/**
|
|
1806
|
+
* Finds the first cause in an Error cause chain that is an instance of `ErrorClass`.
|
|
1807
|
+
*/
|
|
1587
1808
|
const findCause = (error, ErrorClass) => {
|
|
1588
1809
|
const seen = /* @__PURE__ */ new Set();
|
|
1589
1810
|
let current = error;
|
|
@@ -1594,6 +1815,19 @@ const findCause = (error, ErrorClass) => {
|
|
|
1594
1815
|
current = current.cause;
|
|
1595
1816
|
}
|
|
1596
1817
|
};
|
|
1818
|
+
/**
|
|
1819
|
+
* Creates an Error class with a stable `_tag`, typed template props, cause support, and JSON output.
|
|
1820
|
+
*
|
|
1821
|
+
* Prefer tagged errors for domain failures that travel through Resultar error channels.
|
|
1822
|
+
*
|
|
1823
|
+
* @example
|
|
1824
|
+
* ```ts
|
|
1825
|
+
* class UserNotFoundError extends createTaggedError({
|
|
1826
|
+
* name: "UserNotFoundError",
|
|
1827
|
+
* message: "User $id was not found",
|
|
1828
|
+
* }) {}
|
|
1829
|
+
* ```
|
|
1830
|
+
*/
|
|
1597
1831
|
function createTaggedError(options) {
|
|
1598
1832
|
const BaseError = options.extends ?? defaultBaseError;
|
|
1599
1833
|
const messageTemplate = options.message ?? defaultMessageTemplate;
|
|
@@ -1612,15 +1846,27 @@ function createTaggedError(options) {
|
|
|
1612
1846
|
for (const [key, value] of Object.entries(props)) if (!reservedTemplateVariables.has(key)) assignReadonly(this, key, value);
|
|
1613
1847
|
}
|
|
1614
1848
|
}
|
|
1849
|
+
/**
|
|
1850
|
+
* Narrows a value to this tagged error class.
|
|
1851
|
+
*/
|
|
1615
1852
|
static is(value) {
|
|
1616
1853
|
return value instanceof this;
|
|
1617
1854
|
}
|
|
1855
|
+
/**
|
|
1856
|
+
* Creates an Err containing this tagged error.
|
|
1857
|
+
*/
|
|
1618
1858
|
static err(...args) {
|
|
1619
1859
|
return err(new this(...args));
|
|
1620
1860
|
}
|
|
1861
|
+
/**
|
|
1862
|
+
* Finds a matching cause in this error's cause chain.
|
|
1863
|
+
*/
|
|
1621
1864
|
findCause(ErrorClass) {
|
|
1622
1865
|
return findCause(this, ErrorClass);
|
|
1623
1866
|
}
|
|
1867
|
+
/**
|
|
1868
|
+
* Serializes the tagged error, metadata, and cause chain.
|
|
1869
|
+
*/
|
|
1624
1870
|
toJSON() {
|
|
1625
1871
|
const json = {
|
|
1626
1872
|
_tag: this._tag,
|
|
@@ -1636,6 +1882,9 @@ function createTaggedError(options) {
|
|
|
1636
1882
|
}
|
|
1637
1883
|
return GeneratedTaggedError;
|
|
1638
1884
|
}
|
|
1885
|
+
/**
|
|
1886
|
+
* Creates a small tagged-union factory with member constructors, `$is`, and `$match`.
|
|
1887
|
+
*/
|
|
1639
1888
|
const taggedEnum = () => {
|
|
1640
1889
|
return new Proxy({
|
|
1641
1890
|
$is(tag, value) {
|
|
@@ -1658,6 +1907,11 @@ const taggedEnum = () => {
|
|
|
1658
1907
|
};
|
|
1659
1908
|
} });
|
|
1660
1909
|
};
|
|
1910
|
+
/**
|
|
1911
|
+
* Matches an Error by tagged `_tag`, with optional native `Error` fallback.
|
|
1912
|
+
*
|
|
1913
|
+
* Throws the original error when no handler matches.
|
|
1914
|
+
*/
|
|
1661
1915
|
const matchError = (error, handlers) => {
|
|
1662
1916
|
if ("_tag" in error && typeof error._tag === "string" && error._tag in handlers) {
|
|
1663
1917
|
const handler = handlers[error._tag];
|
|
@@ -1666,6 +1920,9 @@ const matchError = (error, handlers) => {
|
|
|
1666
1920
|
if (handlers.Error) return handlers.Error(error);
|
|
1667
1921
|
throw error;
|
|
1668
1922
|
};
|
|
1923
|
+
/**
|
|
1924
|
+
* Matches an Error by tagged `_tag`, then native `Error`, then the provided fallback.
|
|
1925
|
+
*/
|
|
1669
1926
|
const matchErrorPartial = (error, handlers, fallback) => {
|
|
1670
1927
|
if ("_tag" in error && typeof error._tag === "string" && error._tag in handlers) {
|
|
1671
1928
|
const handler = handlers[error._tag];
|
|
@@ -1675,6 +1932,6 @@ const matchErrorPartial = (error, handlers, fallback) => {
|
|
|
1675
1932
|
return fallback(error);
|
|
1676
1933
|
};
|
|
1677
1934
|
//#endregion
|
|
1678
|
-
export { AbortError, DisposableResult, DisposableResultAsync, Result, ResultAsync, createTaggedError, tryResult as default, tryResult as try, tryResult, err, errAsync, findCause, fromPromise, fromSafePromise, fromThrowable, fromThrowableAsync, isAbortError, isError, isRedacted, matchError, matchErrorPartial, ok, okAsync, redact, revealRedacted, safeTry, taggedEnum, tryResultAsync as tryAsync, tryResultAsync, tryCatch, tryCatchAsync, unit, unitAsync };
|
|
1935
|
+
export { AbortError, DisposableResult, DisposableResultAsync, Result, ResultAsync, createTaggedError, tryResult as default, tryResult as try, tryResult, err, errAsync, findCause, fromPromise, fromSafePromise, fromThrowable, fromThrowableAsync, isAbortError, isError, isRedacted, matchError, matchErrorPartial, ok, okAsync, redact, revealRedacted, runPromise, runSync, safeTry, taggedEnum, tryResultAsync as tryAsync, tryResultAsync, tryCatch, tryCatchAsync, unit, unitAsync };
|
|
1679
1936
|
|
|
1680
1937
|
//# sourceMappingURL=index.js.map
|