go-go-try 7.4.0 → 8.0.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 +45 -4
- package/dist/index.cjs +95 -7
- package/dist/index.d.cts +118 -18
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +118 -18
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +92 -8
- package/package.json +1 -1
- package/src/ensure.test.ts +116 -0
- package/src/ensure.ts +137 -0
- package/src/go.test.ts +107 -0
- package/src/go.ts +37 -0
- package/src/goElse.ts +74 -0
- package/src/goTryAll.ts +55 -11
- package/src/index.test.ts +87 -2
- package/src/index.ts +5 -0
- package/src/types.ts +10 -0
package/README.md
CHANGED
|
@@ -408,15 +408,50 @@ const [errors, results] = await goTryAll([
|
|
|
408
408
|
// - Limit expensive computation resources
|
|
409
409
|
```
|
|
410
410
|
|
|
411
|
-
### `goTryAllRaw<T>(items, options?)`
|
|
411
|
+
### `goTryAllRaw<T, E>(items, options?)`
|
|
412
412
|
|
|
413
413
|
Like `goTryAll`, but returns raw Error objects instead of error messages.
|
|
414
|
+
Non-tagged errors are wrapped in `UnknownError` by default (consistent with `goTryRaw`).
|
|
415
|
+
Tagged errors pass through unchanged.
|
|
416
|
+
|
|
417
|
+
Supports the same `errorClass` and `systemErrorClass` options as `goTryRaw`, plus `concurrency` control.
|
|
414
418
|
|
|
415
419
|
```ts
|
|
416
|
-
|
|
420
|
+
interface GoTryAllRawOptions<E> {
|
|
421
|
+
concurrency?: number
|
|
422
|
+
// errorClass and systemErrorClass are mutually exclusive
|
|
423
|
+
errorClass?: ErrorConstructor<E> // Wrap ALL errors
|
|
424
|
+
systemErrorClass?: ErrorConstructor<E> // Only wrap non-tagged errors
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function goTryAllRaw<T extends readonly unknown[], E = UnknownError>(
|
|
417
428
|
items: { [K in keyof T]: Promise<T[K]> | (() => Promise<T[K]>) },
|
|
418
|
-
options?:
|
|
419
|
-
): Promise<[{ [K in keyof T]:
|
|
429
|
+
options?: GoTryAllRawOptions<E>
|
|
430
|
+
): Promise<[{ [K in keyof T]: E | undefined }, { [K in keyof T]: T[K] | undefined }]>
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
**Example:**
|
|
434
|
+
```ts
|
|
435
|
+
const DatabaseError = taggedError('DatabaseError')
|
|
436
|
+
|
|
437
|
+
// Default behavior - non-tagged errors wrapped in UnknownError
|
|
438
|
+
const [errors1] = await goTryAllRaw([
|
|
439
|
+
fetchUser(1),
|
|
440
|
+
fetchUser(2),
|
|
441
|
+
])
|
|
442
|
+
|
|
443
|
+
// Wrap all errors in DatabaseError
|
|
444
|
+
const [errors2] = await goTryAllRaw([
|
|
445
|
+
fetchUser(1),
|
|
446
|
+
fetchUser(2),
|
|
447
|
+
], { errorClass: DatabaseError })
|
|
448
|
+
|
|
449
|
+
// Combine concurrency control with errorClass
|
|
450
|
+
const [errors3] = await goTryAllRaw([
|
|
451
|
+
() => fetchUser(1),
|
|
452
|
+
() => fetchUser(2),
|
|
453
|
+
() => fetchUser(3),
|
|
454
|
+
], { concurrency: 2, errorClass: DatabaseError })
|
|
420
455
|
```
|
|
421
456
|
|
|
422
457
|
### `goTryOr<T>(value, defaultValue)`
|
|
@@ -591,6 +626,12 @@ type GoTryRawOptions<E> =
|
|
|
591
626
|
| { errorClass: ErrorConstructor<E>; systemErrorClass?: never }
|
|
592
627
|
| { errorClass?: never; systemErrorClass: ErrorConstructor<E> }
|
|
593
628
|
| { errorClass?: never; systemErrorClass?: never }
|
|
629
|
+
|
|
630
|
+
// Options for goTryAllRaw (includes concurrency + error class options)
|
|
631
|
+
type GoTryAllRawOptions<E> =
|
|
632
|
+
| { concurrency?: number; errorClass: ErrorConstructor<E>; systemErrorClass?: never }
|
|
633
|
+
| { concurrency?: number; errorClass?: never; systemErrorClass: ErrorConstructor<E> }
|
|
634
|
+
| { concurrency?: number; errorClass?: never; systemErrorClass?: never }
|
|
594
635
|
```
|
|
595
636
|
|
|
596
637
|
## License
|
package/dist/index.cjs
CHANGED
|
@@ -28,7 +28,7 @@ function getErrorMessage(error) {
|
|
|
28
28
|
return String(error);
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
|
-
function isPromise(value) {
|
|
31
|
+
function isPromise$1(value) {
|
|
32
32
|
return typeof value === "object" && value !== null && "then" in value && typeof value.then === "function";
|
|
33
33
|
}
|
|
34
34
|
function isError(value) {
|
|
@@ -41,7 +41,7 @@ function resolveDefault(defaultValue) {
|
|
|
41
41
|
function goTry(value) {
|
|
42
42
|
try {
|
|
43
43
|
const result = typeof value === "function" ? value() : value;
|
|
44
|
-
if (isPromise(result)) {
|
|
44
|
+
if (isPromise$1(result)) {
|
|
45
45
|
return result.then((resolvedValue) => success(resolvedValue)).catch((err) => failure(getErrorMessage(err)));
|
|
46
46
|
}
|
|
47
47
|
return success(result);
|
|
@@ -63,7 +63,7 @@ function taggedError(tag) {
|
|
|
63
63
|
|
|
64
64
|
const UnknownError = taggedError("UnknownError");
|
|
65
65
|
|
|
66
|
-
function isTaggedError(err) {
|
|
66
|
+
function isTaggedError$1(err) {
|
|
67
67
|
return isError(err) && "_tag" in err && typeof err._tag === "string";
|
|
68
68
|
}
|
|
69
69
|
function goTryRaw(value, options) {
|
|
@@ -80,7 +80,7 @@ function goTryRaw(value, options) {
|
|
|
80
80
|
return new errorClass(String(err));
|
|
81
81
|
}
|
|
82
82
|
if (actualSystemErrorClass) {
|
|
83
|
-
if (isTaggedError(err)) {
|
|
83
|
+
if (isTaggedError$1(err)) {
|
|
84
84
|
return err;
|
|
85
85
|
}
|
|
86
86
|
if (err === void 0) {
|
|
@@ -98,7 +98,7 @@ function goTryRaw(value, options) {
|
|
|
98
98
|
};
|
|
99
99
|
try {
|
|
100
100
|
const result = typeof value === "function" ? value() : value;
|
|
101
|
-
if (isPromise(result)) {
|
|
101
|
+
if (isPromise$1(result)) {
|
|
102
102
|
return result.then((resolvedValue) => success(resolvedValue)).catch((err) => failure(wrapError(err)));
|
|
103
103
|
}
|
|
104
104
|
return success(result);
|
|
@@ -110,7 +110,7 @@ function goTryRaw(value, options) {
|
|
|
110
110
|
function goTryOr(value, defaultValue) {
|
|
111
111
|
try {
|
|
112
112
|
const result = typeof value === "function" ? value() : value;
|
|
113
|
-
if (isPromise(result)) {
|
|
113
|
+
if (isPromise$1(result)) {
|
|
114
114
|
return result.then((resolvedValue) => success(resolvedValue)).catch((err) => [getErrorMessage(err), resolveDefault(defaultValue)]);
|
|
115
115
|
}
|
|
116
116
|
return success(result);
|
|
@@ -119,6 +119,31 @@ function goTryOr(value, defaultValue) {
|
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
+
function isTaggedError(err) {
|
|
123
|
+
return isError(err) && "_tag" in err && typeof err._tag === "string";
|
|
124
|
+
}
|
|
125
|
+
function wrapError(err, errorClass, systemErrorClass) {
|
|
126
|
+
if (errorClass) {
|
|
127
|
+
if (err === void 0) {
|
|
128
|
+
return new errorClass("undefined");
|
|
129
|
+
}
|
|
130
|
+
if (isError(err)) {
|
|
131
|
+
return new errorClass(err.message, { cause: err });
|
|
132
|
+
}
|
|
133
|
+
return new errorClass(String(err));
|
|
134
|
+
}
|
|
135
|
+
const actualSystemErrorClass = systemErrorClass ?? UnknownError;
|
|
136
|
+
if (isTaggedError(err)) {
|
|
137
|
+
return err;
|
|
138
|
+
}
|
|
139
|
+
if (err === void 0) {
|
|
140
|
+
return new actualSystemErrorClass("undefined");
|
|
141
|
+
}
|
|
142
|
+
if (isError(err)) {
|
|
143
|
+
return new actualSystemErrorClass(err.message, { cause: err });
|
|
144
|
+
}
|
|
145
|
+
return new actualSystemErrorClass(String(err));
|
|
146
|
+
}
|
|
122
147
|
async function runWithConcurrency(items, concurrency) {
|
|
123
148
|
if (items.length === 0) {
|
|
124
149
|
return [];
|
|
@@ -166,6 +191,7 @@ async function goTryAll(items, options) {
|
|
|
166
191
|
return [errors, results];
|
|
167
192
|
}
|
|
168
193
|
async function goTryAllRaw(items, options) {
|
|
194
|
+
const { errorClass, systemErrorClass } = options || {};
|
|
169
195
|
const settled = await runWithConcurrency(items, options?.concurrency ?? 0);
|
|
170
196
|
const errors = [];
|
|
171
197
|
const results = [];
|
|
@@ -176,7 +202,7 @@ async function goTryAllRaw(items, options) {
|
|
|
176
202
|
results[i] = item.value;
|
|
177
203
|
} else {
|
|
178
204
|
const reason = item.reason;
|
|
179
|
-
errors[i] =
|
|
205
|
+
errors[i] = wrapError(reason, errorClass, systemErrorClass);
|
|
180
206
|
results[i] = void 0;
|
|
181
207
|
}
|
|
182
208
|
}
|
|
@@ -195,10 +221,72 @@ function assert(condition, errorOrClass, message) {
|
|
|
195
221
|
}
|
|
196
222
|
}
|
|
197
223
|
|
|
224
|
+
function isPromise(value) {
|
|
225
|
+
return value instanceof Promise;
|
|
226
|
+
}
|
|
227
|
+
function isErrorConstructor(error) {
|
|
228
|
+
return typeof error === "function" && error.prototype !== void 0 && error.prototype instanceof Error;
|
|
229
|
+
}
|
|
230
|
+
function throwError(value, error) {
|
|
231
|
+
if (isErrorConstructor(error)) {
|
|
232
|
+
throw new error(String(value), { cause: value });
|
|
233
|
+
}
|
|
234
|
+
throw error(value);
|
|
235
|
+
}
|
|
236
|
+
function ensure(value, predicate, error = UnknownError) {
|
|
237
|
+
if (typeof value === "function") {
|
|
238
|
+
const result = value();
|
|
239
|
+
if (isPromise(result)) {
|
|
240
|
+
return result.then((resolved) => {
|
|
241
|
+
if (!predicate(resolved)) {
|
|
242
|
+
throwError(resolved, error);
|
|
243
|
+
}
|
|
244
|
+
return resolved;
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
if (!predicate(result)) {
|
|
248
|
+
throwError(result, error);
|
|
249
|
+
}
|
|
250
|
+
return result;
|
|
251
|
+
}
|
|
252
|
+
if (isPromise(value)) {
|
|
253
|
+
return value.then((resolved) => {
|
|
254
|
+
if (!predicate(resolved)) {
|
|
255
|
+
throwError(resolved, error);
|
|
256
|
+
}
|
|
257
|
+
return resolved;
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
if (!predicate(value)) {
|
|
261
|
+
throwError(value, error);
|
|
262
|
+
}
|
|
263
|
+
return value;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function goElse(value, defaultValue) {
|
|
267
|
+
try {
|
|
268
|
+
const result = typeof value === "function" ? value() : value;
|
|
269
|
+
if (isPromise$1(result)) {
|
|
270
|
+
return result.then((resolvedValue) => [void 0, resolvedValue]).catch((err) => {
|
|
271
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
272
|
+
return [error, resolveDefault(defaultValue)];
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
return [void 0, result];
|
|
276
|
+
} catch (err) {
|
|
277
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
278
|
+
return [error, resolveDefault(defaultValue)];
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
198
282
|
exports.UnknownError = UnknownError;
|
|
199
283
|
exports.assert = assert;
|
|
200
284
|
exports.assertNever = assertNever;
|
|
285
|
+
exports.ensure = ensure;
|
|
201
286
|
exports.failure = failure;
|
|
287
|
+
exports.go = goTryRaw;
|
|
288
|
+
exports.goAll = goTryAllRaw;
|
|
289
|
+
exports.goElse = goElse;
|
|
202
290
|
exports.goTry = goTry;
|
|
203
291
|
exports.goTryAll = goTryAll;
|
|
204
292
|
exports.goTryAllRaw = goTryAllRaw;
|
package/dist/index.d.cts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
type Success<T> = readonly [undefined, T];
|
|
5
5
|
type Failure<E> = readonly [E, undefined];
|
|
6
6
|
type Result<E, T> = Success<T> | Failure<E>;
|
|
7
|
-
type ResultWithDefault<E, T> = readonly [E | undefined, T];
|
|
7
|
+
type ResultWithDefault$1<E, T> = readonly [E | undefined, T];
|
|
8
8
|
type MaybePromise<T> = T | Promise<T>;
|
|
9
9
|
/**
|
|
10
10
|
* Base interface for tagged errors.
|
|
@@ -25,7 +25,7 @@ interface GoTryAllOptions {
|
|
|
25
25
|
/**
|
|
26
26
|
* Type for error constructors that can be used with goTryRaw.
|
|
27
27
|
*/
|
|
28
|
-
type ErrorConstructor<E> = new (message: string, options?: {
|
|
28
|
+
type ErrorConstructor$1<E> = new (message: string, options?: {
|
|
29
29
|
cause?: unknown;
|
|
30
30
|
}) => E;
|
|
31
31
|
/**
|
|
@@ -33,15 +33,33 @@ type ErrorConstructor<E> = new (message: string, options?: {
|
|
|
33
33
|
* errorClass and systemErrorClass are mutually exclusive - you can only provide one.
|
|
34
34
|
*/
|
|
35
35
|
type GoTryRawOptions<E = Error> = {
|
|
36
|
-
errorClass: ErrorConstructor<E>;
|
|
36
|
+
errorClass: ErrorConstructor$1<E>;
|
|
37
37
|
systemErrorClass?: never;
|
|
38
38
|
} | {
|
|
39
39
|
errorClass?: never;
|
|
40
|
-
systemErrorClass: ErrorConstructor<E>;
|
|
40
|
+
systemErrorClass: ErrorConstructor$1<E>;
|
|
41
41
|
} | {
|
|
42
42
|
errorClass?: never;
|
|
43
43
|
systemErrorClass?: never;
|
|
44
44
|
};
|
|
45
|
+
/**
|
|
46
|
+
* Options for goTryAllRaw function.
|
|
47
|
+
* Includes concurrency control and error class options.
|
|
48
|
+
* errorClass and systemErrorClass are mutually exclusive.
|
|
49
|
+
*/
|
|
50
|
+
type GoTryAllRawOptions<E = Error> = {
|
|
51
|
+
concurrency?: number;
|
|
52
|
+
errorClass: ErrorConstructor$1<E>;
|
|
53
|
+
systemErrorClass?: never;
|
|
54
|
+
} | {
|
|
55
|
+
concurrency?: number;
|
|
56
|
+
errorClass?: never;
|
|
57
|
+
systemErrorClass: ErrorConstructor$1<E>;
|
|
58
|
+
} | {
|
|
59
|
+
concurrency?: number;
|
|
60
|
+
errorClass?: never;
|
|
61
|
+
systemErrorClass?: never;
|
|
62
|
+
};
|
|
45
63
|
/**
|
|
46
64
|
* Creates a union type from multiple tagged error classes.
|
|
47
65
|
*
|
|
@@ -55,8 +73,8 @@ type GoTryRawOptions<E = Error> = {
|
|
|
55
73
|
* type AppError = TaggedUnion<[typeof DatabaseError, typeof NetworkError]>
|
|
56
74
|
* // Equivalent to: DatabaseError | NetworkError
|
|
57
75
|
*/
|
|
58
|
-
type TaggedUnion<T extends readonly ErrorConstructor<unknown>[]> = {
|
|
59
|
-
[K in keyof T]: T[K] extends ErrorConstructor<infer E> ? E : never;
|
|
76
|
+
type TaggedUnion<T extends readonly ErrorConstructor$1<unknown>[]> = {
|
|
77
|
+
[K in keyof T]: T[K] extends ErrorConstructor$1<infer E> ? E : never;
|
|
60
78
|
}[number];
|
|
61
79
|
|
|
62
80
|
/**
|
|
@@ -180,11 +198,11 @@ declare function goTryRaw<T, E = InstanceType<typeof UnknownError>>(value: T, op
|
|
|
180
198
|
* name: 'Guest'
|
|
181
199
|
* }))
|
|
182
200
|
*/
|
|
183
|
-
declare function goTryOr<T>(fn: () => never, defaultValue: T | (() => T)): ResultWithDefault<string, T>;
|
|
184
|
-
declare function goTryOr<T>(fn: () => Promise<T>, defaultValue: T | (() => T)): Promise<ResultWithDefault<string, T>>;
|
|
185
|
-
declare function goTryOr<T>(promise: Promise<T>, defaultValue: T | (() => T)): Promise<ResultWithDefault<string, T>>;
|
|
186
|
-
declare function goTryOr<T>(fn: () => T, defaultValue: T | (() => T)): ResultWithDefault<string, T>;
|
|
187
|
-
declare function goTryOr<T>(value: T, defaultValue: T | (() => T)): ResultWithDefault<string, T>;
|
|
201
|
+
declare function goTryOr<T>(fn: () => never, defaultValue: T | (() => T)): ResultWithDefault$1<string, T>;
|
|
202
|
+
declare function goTryOr<T>(fn: () => Promise<T>, defaultValue: T | (() => T)): Promise<ResultWithDefault$1<string, T>>;
|
|
203
|
+
declare function goTryOr<T>(promise: Promise<T>, defaultValue: T | (() => T)): Promise<ResultWithDefault$1<string, T>>;
|
|
204
|
+
declare function goTryOr<T>(fn: () => T, defaultValue: T | (() => T)): ResultWithDefault$1<string, T>;
|
|
205
|
+
declare function goTryOr<T>(value: T, defaultValue: T | (() => T)): ResultWithDefault$1<string, T>;
|
|
188
206
|
|
|
189
207
|
/**
|
|
190
208
|
* Executes multiple promises or factory functions in parallel (or with limited concurrency)
|
|
@@ -227,18 +245,25 @@ declare function goTryAll<T extends readonly unknown[]>(items: {
|
|
|
227
245
|
}]>;
|
|
228
246
|
/**
|
|
229
247
|
* Like `goTryAll`, but returns raw Error objects instead of error messages.
|
|
248
|
+
* Non-tagged errors are wrapped in `UnknownError` by default (consistent with `goTryRaw`).
|
|
249
|
+
* Tagged errors pass through unchanged.
|
|
250
|
+
*
|
|
251
|
+
* Supports `errorClass` and `systemErrorClass` options (mutually exclusive):
|
|
252
|
+
* - `errorClass`: Wrap ALL errors in the specified class
|
|
253
|
+
* - `systemErrorClass`: Only wrap non-tagged errors (defaults to UnknownError)
|
|
230
254
|
*
|
|
231
255
|
* @template T The tuple type of all promise results
|
|
256
|
+
* @template E The type of the error
|
|
232
257
|
* @param {readonly [...{ [K in keyof T]: Promise<T[K]> | (() => Promise<T[K]>) }]} items - Array of promises or factories
|
|
233
|
-
* @param {
|
|
234
|
-
* @returns {Promise<[{ [K in keyof T]:
|
|
258
|
+
* @param {GoTryAllRawOptions<E>} options - Optional configuration
|
|
259
|
+
* @returns {Promise<[{ [K in keyof T]: E | undefined }, { [K in keyof T]: T[K] | undefined }]>}
|
|
235
260
|
* A tuple where the first element is a tuple of Error objects (or undefined) and
|
|
236
261
|
* the second element is a tuple of results (or undefined), preserving input order
|
|
237
262
|
*/
|
|
238
|
-
declare function goTryAllRaw<T extends readonly unknown[]
|
|
263
|
+
declare function goTryAllRaw<T extends readonly unknown[], E = InstanceType<typeof UnknownError>>(items: {
|
|
239
264
|
[K in keyof T]: Promise<T[K]> | (() => Promise<T[K]>);
|
|
240
|
-
}, options?:
|
|
241
|
-
[K in keyof T]:
|
|
265
|
+
}, options?: GoTryAllRawOptions<E>): Promise<[{
|
|
266
|
+
[K in keyof T]: E | undefined;
|
|
242
267
|
}, {
|
|
243
268
|
[K in keyof T]: T[K] | undefined;
|
|
244
269
|
}]>;
|
|
@@ -319,6 +344,47 @@ declare function assert(condition: unknown, error: Error | string): asserts cond
|
|
|
319
344
|
*/
|
|
320
345
|
declare function assert<T extends Error>(condition: unknown, ErrorClass: new (message: string) => T, message: string): asserts condition;
|
|
321
346
|
|
|
347
|
+
/**
|
|
348
|
+
* Error class constructor type.
|
|
349
|
+
*/
|
|
350
|
+
type ErrorConstructor<E extends Error = Error> = new (message: string, options?: {
|
|
351
|
+
cause?: unknown;
|
|
352
|
+
}) => E;
|
|
353
|
+
/**
|
|
354
|
+
* Ensures a value satisfies a predicate, throwing an error if not.
|
|
355
|
+
* Returns the value if the predicate passes.
|
|
356
|
+
*
|
|
357
|
+
* Accepts sync values, promises, or functions - just like `go`.
|
|
358
|
+
*
|
|
359
|
+
* The error can be either:
|
|
360
|
+
* - An Error class constructor (instantiated with the value as cause)
|
|
361
|
+
* - A function that creates and returns an Error
|
|
362
|
+
* - If omitted, defaults to UnknownError
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
* ```typescript
|
|
366
|
+
* // With sync value (uses UnknownError by default)
|
|
367
|
+
* ensure(42, n => n > 0)
|
|
368
|
+
*
|
|
369
|
+
* // With promise (awaited internally)
|
|
370
|
+
* const res = await ensure(fetch('/api'), r => r.ok, RequestFailedError)
|
|
371
|
+
*
|
|
372
|
+
* // With function
|
|
373
|
+
* const res = ensure(() => parseInt('42'), n => !isNaN(n), Error)
|
|
374
|
+
*
|
|
375
|
+
* // With error factory function
|
|
376
|
+
* const res = ensure(
|
|
377
|
+
* await fetch('/api'),
|
|
378
|
+
* r => r.ok,
|
|
379
|
+
* r => new Error(`HTTP ${r.status}`)
|
|
380
|
+
* )
|
|
381
|
+
* ```
|
|
382
|
+
*/
|
|
383
|
+
declare function ensure<T>(value: Promise<T>, predicate: (value: T) => boolean, error?: ErrorConstructor<Error> | ((value: T) => Error)): Promise<T>;
|
|
384
|
+
declare function ensure<T>(fn: () => Promise<T>, predicate: (value: T) => boolean, error?: ErrorConstructor<Error> | ((value: T) => Error)): Promise<T>;
|
|
385
|
+
declare function ensure<T>(fn: () => T, predicate: (value: T) => boolean, error?: ErrorConstructor<Error> | ((value: T) => Error)): T;
|
|
386
|
+
declare function ensure<T>(value: T, predicate: (value: T) => boolean, error?: ErrorConstructor<Error> | ((value: T) => Error)): T;
|
|
387
|
+
|
|
322
388
|
declare function isSuccess<E, T>(result: Result<E, T>): result is Success<T>;
|
|
323
389
|
declare function isFailure<E, T>(result: Result<E, T>): result is Failure<E>;
|
|
324
390
|
declare function success<T>(value: T): Success<T>;
|
|
@@ -350,6 +416,40 @@ declare function failure<E>(error: E): Failure<E>;
|
|
|
350
416
|
*/
|
|
351
417
|
declare function assertNever(value: never): never;
|
|
352
418
|
|
|
353
|
-
|
|
354
|
-
|
|
419
|
+
/**
|
|
420
|
+
* Result type with Error and a default value.
|
|
421
|
+
* On error, returns [Error, DefaultT]
|
|
422
|
+
* On success, returns [undefined, T]
|
|
423
|
+
*/
|
|
424
|
+
type ResultWithDefault<E, T, D = T> = readonly [E, D] | readonly [undefined, T];
|
|
425
|
+
/**
|
|
426
|
+
* Executes a function, promise, or value and returns a Result type with a fallback default.
|
|
427
|
+
* If an error occurs, it returns the actual Error object and the default value.
|
|
428
|
+
*
|
|
429
|
+
* @template T The type of the successful result
|
|
430
|
+
* @template D The type of the default value (defaults to T)
|
|
431
|
+
* @param {T | Promise<T> | (() => T | Promise<T>)} value - The value, promise, or function to execute
|
|
432
|
+
* @param {D | (() => D)} defaultValue - The default value or a function to compute it (only called on failure)
|
|
433
|
+
* @returns {ResultWithDefault<Error, T, D> | Promise<ResultWithDefault<Error, T, D>>} A tuple of [error, value] or Promise thereof
|
|
434
|
+
*
|
|
435
|
+
* @example
|
|
436
|
+
* // With a static default
|
|
437
|
+
* const [err, config] = goElse(() => JSON.parse('invalid'), { port: 3000 })
|
|
438
|
+
* // err is the Error object, config is { port: 3000 }
|
|
439
|
+
*
|
|
440
|
+
* @example
|
|
441
|
+
* // With a computed default (lazy evaluation)
|
|
442
|
+
* const [err, user] = await goElse(fetchUser(id), () => ({
|
|
443
|
+
* id: 'anonymous',
|
|
444
|
+
* name: 'Guest'
|
|
445
|
+
* }))
|
|
446
|
+
*/
|
|
447
|
+
declare function goElse<T, D = T>(fn: () => never, defaultValue: D | (() => D)): ResultWithDefault<Error, never, D>;
|
|
448
|
+
declare function goElse<T, D = T>(fn: () => Promise<T>, defaultValue: D | (() => D)): Promise<ResultWithDefault<Error, T, D>>;
|
|
449
|
+
declare function goElse<T, D = T>(promise: Promise<T>, defaultValue: D | (() => D)): Promise<ResultWithDefault<Error, T, D>>;
|
|
450
|
+
declare function goElse<T, D = T>(fn: () => T, defaultValue: D | (() => D)): ResultWithDefault<Error, T, D>;
|
|
451
|
+
declare function goElse<T, D = T>(value: T, defaultValue: D | (() => D)): ResultWithDefault<Error, T, D>;
|
|
452
|
+
|
|
453
|
+
export { UnknownError, assert, assertNever, ensure, failure, goTryRaw as go, goTryAllRaw as goAll, goElse, goTry, goTryAll, goTryAllRaw, goTryOr, goTryRaw, isFailure, isSuccess, success, taggedError };
|
|
454
|
+
export type { ErrorConstructor$1 as ErrorConstructor, Failure, GoTryAllOptions, GoTryAllRawOptions, GoTryRawOptions, MaybePromise, Result, ResultWithDefault$1 as ResultWithDefault, Success, TaggedError, TaggedUnion };
|
|
355
455
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","sources":["../src/types.ts","../src/goTry.ts","../src/unknown-error.ts","../src/goTryRaw.ts","../src/goTryOr.ts","../src/goTryAll.ts","../src/tagged-error.ts","../src/assert.ts","../src/result-helpers.ts"],"mappings":"AAAA;;;AAIM,KAAM,OAAO;AACb,KAAM,OAAO;AACb,KAAM,MAAM,SAAS,OAAO,MAAM,OAAO;AAEzC,
|
|
1
|
+
{"version":3,"file":"index.d.cts","sources":["../src/types.ts","../src/goTry.ts","../src/unknown-error.ts","../src/goTryRaw.ts","../src/goTryOr.ts","../src/goTryAll.ts","../src/tagged-error.ts","../src/assert.ts","../src/ensure.ts","../src/result-helpers.ts","../src/goElse.ts"],"mappings":"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","names":["ResultWithDefault","ErrorConstructor"]}
|
package/dist/index.d.mts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
type Success<T> = readonly [undefined, T];
|
|
5
5
|
type Failure<E> = readonly [E, undefined];
|
|
6
6
|
type Result<E, T> = Success<T> | Failure<E>;
|
|
7
|
-
type ResultWithDefault<E, T> = readonly [E | undefined, T];
|
|
7
|
+
type ResultWithDefault$1<E, T> = readonly [E | undefined, T];
|
|
8
8
|
type MaybePromise<T> = T | Promise<T>;
|
|
9
9
|
/**
|
|
10
10
|
* Base interface for tagged errors.
|
|
@@ -25,7 +25,7 @@ interface GoTryAllOptions {
|
|
|
25
25
|
/**
|
|
26
26
|
* Type for error constructors that can be used with goTryRaw.
|
|
27
27
|
*/
|
|
28
|
-
type ErrorConstructor<E> = new (message: string, options?: {
|
|
28
|
+
type ErrorConstructor$1<E> = new (message: string, options?: {
|
|
29
29
|
cause?: unknown;
|
|
30
30
|
}) => E;
|
|
31
31
|
/**
|
|
@@ -33,15 +33,33 @@ type ErrorConstructor<E> = new (message: string, options?: {
|
|
|
33
33
|
* errorClass and systemErrorClass are mutually exclusive - you can only provide one.
|
|
34
34
|
*/
|
|
35
35
|
type GoTryRawOptions<E = Error> = {
|
|
36
|
-
errorClass: ErrorConstructor<E>;
|
|
36
|
+
errorClass: ErrorConstructor$1<E>;
|
|
37
37
|
systemErrorClass?: never;
|
|
38
38
|
} | {
|
|
39
39
|
errorClass?: never;
|
|
40
|
-
systemErrorClass: ErrorConstructor<E>;
|
|
40
|
+
systemErrorClass: ErrorConstructor$1<E>;
|
|
41
41
|
} | {
|
|
42
42
|
errorClass?: never;
|
|
43
43
|
systemErrorClass?: never;
|
|
44
44
|
};
|
|
45
|
+
/**
|
|
46
|
+
* Options for goTryAllRaw function.
|
|
47
|
+
* Includes concurrency control and error class options.
|
|
48
|
+
* errorClass and systemErrorClass are mutually exclusive.
|
|
49
|
+
*/
|
|
50
|
+
type GoTryAllRawOptions<E = Error> = {
|
|
51
|
+
concurrency?: number;
|
|
52
|
+
errorClass: ErrorConstructor$1<E>;
|
|
53
|
+
systemErrorClass?: never;
|
|
54
|
+
} | {
|
|
55
|
+
concurrency?: number;
|
|
56
|
+
errorClass?: never;
|
|
57
|
+
systemErrorClass: ErrorConstructor$1<E>;
|
|
58
|
+
} | {
|
|
59
|
+
concurrency?: number;
|
|
60
|
+
errorClass?: never;
|
|
61
|
+
systemErrorClass?: never;
|
|
62
|
+
};
|
|
45
63
|
/**
|
|
46
64
|
* Creates a union type from multiple tagged error classes.
|
|
47
65
|
*
|
|
@@ -55,8 +73,8 @@ type GoTryRawOptions<E = Error> = {
|
|
|
55
73
|
* type AppError = TaggedUnion<[typeof DatabaseError, typeof NetworkError]>
|
|
56
74
|
* // Equivalent to: DatabaseError | NetworkError
|
|
57
75
|
*/
|
|
58
|
-
type TaggedUnion<T extends readonly ErrorConstructor<unknown>[]> = {
|
|
59
|
-
[K in keyof T]: T[K] extends ErrorConstructor<infer E> ? E : never;
|
|
76
|
+
type TaggedUnion<T extends readonly ErrorConstructor$1<unknown>[]> = {
|
|
77
|
+
[K in keyof T]: T[K] extends ErrorConstructor$1<infer E> ? E : never;
|
|
60
78
|
}[number];
|
|
61
79
|
|
|
62
80
|
/**
|
|
@@ -180,11 +198,11 @@ declare function goTryRaw<T, E = InstanceType<typeof UnknownError>>(value: T, op
|
|
|
180
198
|
* name: 'Guest'
|
|
181
199
|
* }))
|
|
182
200
|
*/
|
|
183
|
-
declare function goTryOr<T>(fn: () => never, defaultValue: T | (() => T)): ResultWithDefault<string, T>;
|
|
184
|
-
declare function goTryOr<T>(fn: () => Promise<T>, defaultValue: T | (() => T)): Promise<ResultWithDefault<string, T>>;
|
|
185
|
-
declare function goTryOr<T>(promise: Promise<T>, defaultValue: T | (() => T)): Promise<ResultWithDefault<string, T>>;
|
|
186
|
-
declare function goTryOr<T>(fn: () => T, defaultValue: T | (() => T)): ResultWithDefault<string, T>;
|
|
187
|
-
declare function goTryOr<T>(value: T, defaultValue: T | (() => T)): ResultWithDefault<string, T>;
|
|
201
|
+
declare function goTryOr<T>(fn: () => never, defaultValue: T | (() => T)): ResultWithDefault$1<string, T>;
|
|
202
|
+
declare function goTryOr<T>(fn: () => Promise<T>, defaultValue: T | (() => T)): Promise<ResultWithDefault$1<string, T>>;
|
|
203
|
+
declare function goTryOr<T>(promise: Promise<T>, defaultValue: T | (() => T)): Promise<ResultWithDefault$1<string, T>>;
|
|
204
|
+
declare function goTryOr<T>(fn: () => T, defaultValue: T | (() => T)): ResultWithDefault$1<string, T>;
|
|
205
|
+
declare function goTryOr<T>(value: T, defaultValue: T | (() => T)): ResultWithDefault$1<string, T>;
|
|
188
206
|
|
|
189
207
|
/**
|
|
190
208
|
* Executes multiple promises or factory functions in parallel (or with limited concurrency)
|
|
@@ -227,18 +245,25 @@ declare function goTryAll<T extends readonly unknown[]>(items: {
|
|
|
227
245
|
}]>;
|
|
228
246
|
/**
|
|
229
247
|
* Like `goTryAll`, but returns raw Error objects instead of error messages.
|
|
248
|
+
* Non-tagged errors are wrapped in `UnknownError` by default (consistent with `goTryRaw`).
|
|
249
|
+
* Tagged errors pass through unchanged.
|
|
250
|
+
*
|
|
251
|
+
* Supports `errorClass` and `systemErrorClass` options (mutually exclusive):
|
|
252
|
+
* - `errorClass`: Wrap ALL errors in the specified class
|
|
253
|
+
* - `systemErrorClass`: Only wrap non-tagged errors (defaults to UnknownError)
|
|
230
254
|
*
|
|
231
255
|
* @template T The tuple type of all promise results
|
|
256
|
+
* @template E The type of the error
|
|
232
257
|
* @param {readonly [...{ [K in keyof T]: Promise<T[K]> | (() => Promise<T[K]>) }]} items - Array of promises or factories
|
|
233
|
-
* @param {
|
|
234
|
-
* @returns {Promise<[{ [K in keyof T]:
|
|
258
|
+
* @param {GoTryAllRawOptions<E>} options - Optional configuration
|
|
259
|
+
* @returns {Promise<[{ [K in keyof T]: E | undefined }, { [K in keyof T]: T[K] | undefined }]>}
|
|
235
260
|
* A tuple where the first element is a tuple of Error objects (or undefined) and
|
|
236
261
|
* the second element is a tuple of results (or undefined), preserving input order
|
|
237
262
|
*/
|
|
238
|
-
declare function goTryAllRaw<T extends readonly unknown[]
|
|
263
|
+
declare function goTryAllRaw<T extends readonly unknown[], E = InstanceType<typeof UnknownError>>(items: {
|
|
239
264
|
[K in keyof T]: Promise<T[K]> | (() => Promise<T[K]>);
|
|
240
|
-
}, options?:
|
|
241
|
-
[K in keyof T]:
|
|
265
|
+
}, options?: GoTryAllRawOptions<E>): Promise<[{
|
|
266
|
+
[K in keyof T]: E | undefined;
|
|
242
267
|
}, {
|
|
243
268
|
[K in keyof T]: T[K] | undefined;
|
|
244
269
|
}]>;
|
|
@@ -319,6 +344,47 @@ declare function assert(condition: unknown, error: Error | string): asserts cond
|
|
|
319
344
|
*/
|
|
320
345
|
declare function assert<T extends Error>(condition: unknown, ErrorClass: new (message: string) => T, message: string): asserts condition;
|
|
321
346
|
|
|
347
|
+
/**
|
|
348
|
+
* Error class constructor type.
|
|
349
|
+
*/
|
|
350
|
+
type ErrorConstructor<E extends Error = Error> = new (message: string, options?: {
|
|
351
|
+
cause?: unknown;
|
|
352
|
+
}) => E;
|
|
353
|
+
/**
|
|
354
|
+
* Ensures a value satisfies a predicate, throwing an error if not.
|
|
355
|
+
* Returns the value if the predicate passes.
|
|
356
|
+
*
|
|
357
|
+
* Accepts sync values, promises, or functions - just like `go`.
|
|
358
|
+
*
|
|
359
|
+
* The error can be either:
|
|
360
|
+
* - An Error class constructor (instantiated with the value as cause)
|
|
361
|
+
* - A function that creates and returns an Error
|
|
362
|
+
* - If omitted, defaults to UnknownError
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
* ```typescript
|
|
366
|
+
* // With sync value (uses UnknownError by default)
|
|
367
|
+
* ensure(42, n => n > 0)
|
|
368
|
+
*
|
|
369
|
+
* // With promise (awaited internally)
|
|
370
|
+
* const res = await ensure(fetch('/api'), r => r.ok, RequestFailedError)
|
|
371
|
+
*
|
|
372
|
+
* // With function
|
|
373
|
+
* const res = ensure(() => parseInt('42'), n => !isNaN(n), Error)
|
|
374
|
+
*
|
|
375
|
+
* // With error factory function
|
|
376
|
+
* const res = ensure(
|
|
377
|
+
* await fetch('/api'),
|
|
378
|
+
* r => r.ok,
|
|
379
|
+
* r => new Error(`HTTP ${r.status}`)
|
|
380
|
+
* )
|
|
381
|
+
* ```
|
|
382
|
+
*/
|
|
383
|
+
declare function ensure<T>(value: Promise<T>, predicate: (value: T) => boolean, error?: ErrorConstructor<Error> | ((value: T) => Error)): Promise<T>;
|
|
384
|
+
declare function ensure<T>(fn: () => Promise<T>, predicate: (value: T) => boolean, error?: ErrorConstructor<Error> | ((value: T) => Error)): Promise<T>;
|
|
385
|
+
declare function ensure<T>(fn: () => T, predicate: (value: T) => boolean, error?: ErrorConstructor<Error> | ((value: T) => Error)): T;
|
|
386
|
+
declare function ensure<T>(value: T, predicate: (value: T) => boolean, error?: ErrorConstructor<Error> | ((value: T) => Error)): T;
|
|
387
|
+
|
|
322
388
|
declare function isSuccess<E, T>(result: Result<E, T>): result is Success<T>;
|
|
323
389
|
declare function isFailure<E, T>(result: Result<E, T>): result is Failure<E>;
|
|
324
390
|
declare function success<T>(value: T): Success<T>;
|
|
@@ -350,6 +416,40 @@ declare function failure<E>(error: E): Failure<E>;
|
|
|
350
416
|
*/
|
|
351
417
|
declare function assertNever(value: never): never;
|
|
352
418
|
|
|
353
|
-
|
|
354
|
-
|
|
419
|
+
/**
|
|
420
|
+
* Result type with Error and a default value.
|
|
421
|
+
* On error, returns [Error, DefaultT]
|
|
422
|
+
* On success, returns [undefined, T]
|
|
423
|
+
*/
|
|
424
|
+
type ResultWithDefault<E, T, D = T> = readonly [E, D] | readonly [undefined, T];
|
|
425
|
+
/**
|
|
426
|
+
* Executes a function, promise, or value and returns a Result type with a fallback default.
|
|
427
|
+
* If an error occurs, it returns the actual Error object and the default value.
|
|
428
|
+
*
|
|
429
|
+
* @template T The type of the successful result
|
|
430
|
+
* @template D The type of the default value (defaults to T)
|
|
431
|
+
* @param {T | Promise<T> | (() => T | Promise<T>)} value - The value, promise, or function to execute
|
|
432
|
+
* @param {D | (() => D)} defaultValue - The default value or a function to compute it (only called on failure)
|
|
433
|
+
* @returns {ResultWithDefault<Error, T, D> | Promise<ResultWithDefault<Error, T, D>>} A tuple of [error, value] or Promise thereof
|
|
434
|
+
*
|
|
435
|
+
* @example
|
|
436
|
+
* // With a static default
|
|
437
|
+
* const [err, config] = goElse(() => JSON.parse('invalid'), { port: 3000 })
|
|
438
|
+
* // err is the Error object, config is { port: 3000 }
|
|
439
|
+
*
|
|
440
|
+
* @example
|
|
441
|
+
* // With a computed default (lazy evaluation)
|
|
442
|
+
* const [err, user] = await goElse(fetchUser(id), () => ({
|
|
443
|
+
* id: 'anonymous',
|
|
444
|
+
* name: 'Guest'
|
|
445
|
+
* }))
|
|
446
|
+
*/
|
|
447
|
+
declare function goElse<T, D = T>(fn: () => never, defaultValue: D | (() => D)): ResultWithDefault<Error, never, D>;
|
|
448
|
+
declare function goElse<T, D = T>(fn: () => Promise<T>, defaultValue: D | (() => D)): Promise<ResultWithDefault<Error, T, D>>;
|
|
449
|
+
declare function goElse<T, D = T>(promise: Promise<T>, defaultValue: D | (() => D)): Promise<ResultWithDefault<Error, T, D>>;
|
|
450
|
+
declare function goElse<T, D = T>(fn: () => T, defaultValue: D | (() => D)): ResultWithDefault<Error, T, D>;
|
|
451
|
+
declare function goElse<T, D = T>(value: T, defaultValue: D | (() => D)): ResultWithDefault<Error, T, D>;
|
|
452
|
+
|
|
453
|
+
export { UnknownError, assert, assertNever, ensure, failure, goTryRaw as go, goTryAllRaw as goAll, goElse, goTry, goTryAll, goTryAllRaw, goTryOr, goTryRaw, isFailure, isSuccess, success, taggedError };
|
|
454
|
+
export type { ErrorConstructor$1 as ErrorConstructor, Failure, GoTryAllOptions, GoTryAllRawOptions, GoTryRawOptions, MaybePromise, Result, ResultWithDefault$1 as ResultWithDefault, Success, TaggedError, TaggedUnion };
|
|
355
455
|
//# sourceMappingURL=index.d.mts.map
|