unthrown 0.2.0 → 1.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 +2 -2
- package/dist/index.cjs +282 -76
- package/dist/index.d.cts +201 -61
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +201 -61
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +277 -74
- package/dist/index.mjs.map +1 -1
- package/docs/index.md +270 -138
- package/package.json +3 -2
package/docs/index.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
### UnwrapError
|
|
10
10
|
|
|
11
|
-
Defined in: [packages/core/src/core.ts:
|
|
11
|
+
Defined in: [packages/core/src/core.ts:30](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/core.ts#L30)
|
|
12
12
|
|
|
13
13
|
Thrown by a [Result](#result)'s `unwrap` / `unwrapErr` when the assertion is
|
|
14
14
|
wrong on a *modeled* result — `unwrap()` on an `Err`, or `unwrapErr()` on an
|
|
@@ -37,7 +37,7 @@ re-thrown (with its original stack) instead.
|
|
|
37
37
|
new UnwrapError<E>(error): UnwrapError<E>;
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
-
Defined in: [packages/core/src/core.ts:
|
|
40
|
+
Defined in: [packages/core/src/core.ts:36](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/core.ts#L36)
|
|
41
41
|
|
|
42
42
|
###### Parameters
|
|
43
43
|
|
|
@@ -60,7 +60,7 @@ Error.constructor
|
|
|
60
60
|
| Property | Modifier | Type | Description | Inherited from | Defined in |
|
|
61
61
|
| ------ | ------ | ------ | ------ | ------ | ------ |
|
|
62
62
|
| <a id="cause"></a> `cause?` | `public` | `unknown` | - | `Error.cause` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es2022.error.d.ts:24 |
|
|
63
|
-
| <a id="error"></a> `error` | `readonly` | `E` | The offending value: the `Err` error for `unwrap()`, or the `Ok` value for `unwrapErr()`. | - | [packages/core/src/core.ts:
|
|
63
|
+
| <a id="error"></a> `error` | `readonly` | `E` | The offending value: the `Err` error for `unwrap()`, or the `Ok` value for `unwrapErr()`. | - | [packages/core/src/core.ts:35](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/core.ts#L35) |
|
|
64
64
|
| <a id="message"></a> `message` | `public` | `string` | - | `Error.message` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1075 |
|
|
65
65
|
| <a id="name"></a> `name` | `public` | `string` | - | `Error.name` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1074 |
|
|
66
66
|
| <a id="stack"></a> `stack?` | `public` | `string` | - | `Error.stack` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1076 |
|
|
@@ -174,7 +174,7 @@ Error.prepareStackTrace
|
|
|
174
174
|
type AsyncErrOf<R> = R extends AsyncResult<unknown, infer E> ? E : never;
|
|
175
175
|
```
|
|
176
176
|
|
|
177
|
-
Defined in: [packages/core/src/types.ts:
|
|
177
|
+
Defined in: [packages/core/src/types.ts:442](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L442)
|
|
178
178
|
|
|
179
179
|
Extract the error type `E` from an [AsyncResult](#asyncresult).
|
|
180
180
|
|
|
@@ -192,7 +192,7 @@ Extract the error type `E` from an [AsyncResult](#asyncresult).
|
|
|
192
192
|
type AsyncOkOf<R> = R extends AsyncResult<infer T, unknown> ? T : never;
|
|
193
193
|
```
|
|
194
194
|
|
|
195
|
-
Defined in: [packages/core/src/types.ts:
|
|
195
|
+
Defined in: [packages/core/src/types.ts:436](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L436)
|
|
196
196
|
|
|
197
197
|
Extract the success type `T` from an [AsyncResult](#asyncresult).
|
|
198
198
|
|
|
@@ -210,7 +210,7 @@ Extract the success type `T` from an [AsyncResult](#asyncresult).
|
|
|
210
210
|
type AsyncResult<T, E> = Awaitable<Result<T, E>> & object;
|
|
211
211
|
```
|
|
212
212
|
|
|
213
|
-
Defined in: [packages/core/src/types.ts:
|
|
213
|
+
Defined in: [packages/core/src/types.ts:352](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L352)
|
|
214
214
|
|
|
215
215
|
The asynchronous counterpart of [Result](#result): an awaitable wrapper with the
|
|
216
216
|
same method surface, collapsing to a `Result<T, E>` when `await`-ed.
|
|
@@ -219,23 +219,26 @@ same method surface, collapsing to a `Result<T, E>` when `await`-ed.
|
|
|
219
219
|
|
|
220
220
|
| Name | Type | Description | Defined in |
|
|
221
221
|
| ------ | ------ | ------ | ------ |
|
|
222
|
-
| `as()` | (`value`) => [`AsyncResult`](#asyncresult)<`U`, `E`> | Asynchronous `as`. | [packages/core/src/types.ts:
|
|
223
|
-
| `
|
|
224
|
-
| `
|
|
225
|
-
| `
|
|
226
|
-
| `
|
|
227
|
-
| `
|
|
228
|
-
| `
|
|
229
|
-
| `
|
|
230
|
-
| `
|
|
231
|
-
| `
|
|
232
|
-
| `
|
|
233
|
-
| `
|
|
234
|
-
| `
|
|
235
|
-
| `
|
|
236
|
-
| `
|
|
237
|
-
| `
|
|
238
|
-
| `
|
|
222
|
+
| `as()` | (`value`) => [`AsyncResult`](#asyncresult)<`U`, `E`> | Asynchronous `as`. | [packages/core/src/types.ts:381](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L381) |
|
|
223
|
+
| `bind()` | (`name`, `f`) => [`AsyncResult`](#asyncresult)<\{ \[K in string \| number \| symbol\]: (Omit\<T, K\> & \{ readonly \[P in string\]: U \})\[K\] \}, `E` \| `E2`> | Asynchronous `bind` (do-notation). `f` may return a `Result` **or** an `AsyncResult`; its value is bound under `name` in the accumulating scope. | [packages/core/src/types.ts:374](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L374) |
|
|
224
|
+
| `flatMap()` | (`f`) => [`AsyncResult`](#asyncresult)<`U`, `E` \| `E2`> | Asynchronous `flatMap`. `f` may return a `Result` **or** an `AsyncResult` (never a raw `Promise`); a throw becomes a `Defect`. | [packages/core/src/types.ts:359](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L359) |
|
|
225
|
+
| `flatTap()` | (`f`) => [`AsyncResult`](#asyncresult)<`T`, `E` \| `E2`> | Asynchronous `flatTap` — a failable tap that keeps the original value. `f` may return a `Result` **or** an `AsyncResult`; its `Ok` value is discarded, an `Err`/`Defect` short-circuits, and a throw becomes a `Defect`. | [packages/core/src/types.ts:367](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L367) |
|
|
226
|
+
| `getOrNull()` | () => `Promise`<`T` \| `null`> | Asynchronous `getOrNull`. | [packages/core/src/types.ts:414](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L414) |
|
|
227
|
+
| `getOrUndefined()` | () => `Promise`<`T` \| `undefined`> | Asynchronous `getOrUndefined`. | [packages/core/src/types.ts:416](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L416) |
|
|
228
|
+
| `let()` | (`name`, `f`) => [`AsyncResult`](#asyncresult)<\{ \[K in string \| number \| symbol\]: (Omit\<T, K\> & \{ readonly \[P in string\]: U \})\[K\] \}, `E`> | Asynchronous `let` (do-notation). `f` returns a plain value, bound under `name`. | [packages/core/src/types.ts:379](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L379) |
|
|
229
|
+
| `map()` | (`f`) => [`AsyncResult`](#asyncresult)<`U`, `E`> | Asynchronous `map`. `f` is synchronous; a throw becomes a `Defect`. | [packages/core/src/types.ts:354](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L354) |
|
|
230
|
+
| `mapErr()` | (`f`) => [`AsyncResult`](#asyncresult)<`T`, `E2`> | Asynchronous `mapErr`. `f` is synchronous; a throw becomes a `Defect`. | [packages/core/src/types.ts:384](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L384) |
|
|
231
|
+
| `match()` | (`cases`) => `Promise`<`R`> | Asynchronous `match`. Handlers are synchronous; resolves to `R`. | [packages/core/src/types.ts:400](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L400) |
|
|
232
|
+
| `orElse()` | (`f`) => [`AsyncResult`](#asyncresult)<`T` \| `U`, `E2`> | Asynchronous `orElse`. `f` may return a `Result` or an `AsyncResult`. | [packages/core/src/types.ts:386](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L386) |
|
|
233
|
+
| `recover()` | (`f`) => [`AsyncResult`](#asyncresult)<`T` \| `U`, `never`> | Asynchronous `recover`. `f` is synchronous; a throw becomes a `Defect`. | [packages/core/src/types.ts:388](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L388) |
|
|
234
|
+
| `recoverDefect()` | (`f`) => [`AsyncResult`](#asyncresult)<`T` \| `U`, `E` \| `E2`> | Asynchronous `recoverDefect`. `f` may return a `Result` or an `AsyncResult`. | [packages/core/src/types.ts:393](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L393) |
|
|
235
|
+
| `tap()` | (`f`) => [`AsyncResult`](#asyncresult)<`T`, `E`> | Asynchronous `tap`. `f` is synchronous; a throw becomes a `Defect`. | [packages/core/src/types.ts:361](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L361) |
|
|
236
|
+
| `tapDefect()` | (`f`) => [`AsyncResult`](#asyncresult)<`T`, `E`> | Asynchronous `tapDefect`. | [packages/core/src/types.ts:397](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L397) |
|
|
237
|
+
| `tapErr()` | (`f`) => [`AsyncResult`](#asyncresult)<`T`, `E`> | Asynchronous `tapErr`. `f` is synchronous; a throw becomes a `Defect`. | [packages/core/src/types.ts:390](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L390) |
|
|
238
|
+
| `unwrap()` | () => `Promise`<`T`> | Asynchronous `unwrap`. The returned promise rejects on `Err`/`Defect`. | [packages/core/src/types.ts:406](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L406) |
|
|
239
|
+
| `unwrapErr()` | () => `Promise`<`E`> | Asynchronous `unwrapErr`. | [packages/core/src/types.ts:408](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L408) |
|
|
240
|
+
| `unwrapOr()` | (`fallback`) => `Promise`<`T`> | Asynchronous `unwrapOr`. | [packages/core/src/types.ts:410](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L410) |
|
|
241
|
+
| `unwrapOrElse()` | (`f`) => `Promise`<`T`> | Asynchronous `unwrapOrElse`. | [packages/core/src/types.ts:412](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L412) |
|
|
239
242
|
|
|
240
243
|
#### Type Parameters
|
|
241
244
|
|
|
@@ -252,7 +255,8 @@ rejection would silently become a `Defect`, skipping the triage that
|
|
|
252
255
|
[fromPromise](#frompromise) forces. To do further async work, re-enter through a
|
|
253
256
|
qualified boundary and compose it: `ar.flatMap((v) => fromPromise(work(v),
|
|
254
257
|
qualify))`. The eliminators (`unwrap`, …) return promises; the binds
|
|
255
|
-
(`flatMap`, `orElse`, `recoverDefect`) additionally accept an
|
|
258
|
+
(`flatMap`, `flatTap`, `orElse`, `recoverDefect`) additionally accept an
|
|
259
|
+
`AsyncResult`.
|
|
256
260
|
|
|
257
261
|
To pattern-match an `AsyncResult`, `await` it first: `match(await ar)`.
|
|
258
262
|
|
|
@@ -264,7 +268,7 @@ To pattern-match an `AsyncResult`, `await` it first: `match(await ar)`.
|
|
|
264
268
|
type Awaitable<T> = object;
|
|
265
269
|
```
|
|
266
270
|
|
|
267
|
-
Defined in: [packages/core/src/types.ts:
|
|
271
|
+
Defined in: [packages/core/src/types.ts:329](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L329)
|
|
268
272
|
|
|
269
273
|
A success-only thenable: awaitable, but deliberately **not** a full
|
|
270
274
|
`PromiseLike`.
|
|
@@ -291,7 +295,7 @@ being treated as a raw promise (e.g. dropped into `Promise.all`).
|
|
|
291
295
|
then<R>(onfulfilled?): PromiseLike<R>;
|
|
292
296
|
```
|
|
293
297
|
|
|
294
|
-
Defined in: [packages/core/src/types.ts:
|
|
298
|
+
Defined in: [packages/core/src/types.ts:330](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L330)
|
|
295
299
|
|
|
296
300
|
###### Type Parameters
|
|
297
301
|
|
|
@@ -317,7 +321,7 @@ Defined in: [packages/core/src/types.ts:257](https://github.com/btravstack/unthr
|
|
|
317
321
|
type Defect = object;
|
|
318
322
|
```
|
|
319
323
|
|
|
320
|
-
Defined in: [packages/core/src/defect.ts:
|
|
324
|
+
Defined in: [packages/core/src/defect.ts:36](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/defect.ts#L36)
|
|
321
325
|
|
|
322
326
|
The marker a `qualify` function returns to triage a cause as **unexpected**.
|
|
323
327
|
|
|
@@ -325,7 +329,7 @@ The marker a `qualify` function returns to triage a cause as **unexpected**.
|
|
|
325
329
|
|
|
326
330
|
`qualify` (passed to [fromPromise](#frompromise) / [fromThrowable](#fromthrowable)) returns
|
|
327
331
|
`E | Defect`: either a modeled domain error, or a `Defect` produced by
|
|
328
|
-
[
|
|
332
|
+
[Defect](#defect-2) to say "this failure is not modeled". A `Defect` is opaque —
|
|
329
333
|
it carries the original cause for the boundary to convert into the third
|
|
330
334
|
runtime state of a `Result`.
|
|
331
335
|
|
|
@@ -333,8 +337,8 @@ runtime state of a `Result`.
|
|
|
333
337
|
|
|
334
338
|
| Property | Modifier | Type | Defined in |
|
|
335
339
|
| ------ | ------ | ------ | ------ |
|
|
336
|
-
| <a id="defect-1"></a> `[DEFECT]` | `readonly` | `true` | [packages/core/src/defect.ts:16](https://github.com/btravstack/unthrown/blob/
|
|
337
|
-
| <a id="cause-1"></a> `cause` | `readonly` | `unknown` | [packages/core/src/defect.ts:17](https://github.com/btravstack/unthrown/blob/
|
|
340
|
+
| <a id="defect-1"></a> `[DEFECT]` | `readonly` | `true` | [packages/core/src/defect.ts:16](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/defect.ts#L16) |
|
|
341
|
+
| <a id="cause-1"></a> `cause` | `readonly` | `unknown` | [packages/core/src/defect.ts:17](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/defect.ts#L17) |
|
|
338
342
|
|
|
339
343
|
***
|
|
340
344
|
|
|
@@ -344,7 +348,7 @@ runtime state of a `Result`.
|
|
|
344
348
|
type DefectView<T, E> = ResultMethods<T, E> & object;
|
|
345
349
|
```
|
|
346
350
|
|
|
347
|
-
Defined in: [packages/core/src/types.ts:
|
|
351
|
+
Defined in: [packages/core/src/types.ts:272](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L272)
|
|
348
352
|
|
|
349
353
|
The `Defect` variant of a [Result](#result): an unmodeled failure carrying a `cause`.
|
|
350
354
|
|
|
@@ -352,8 +356,8 @@ The `Defect` variant of a [Result](#result): an unmodeled failure carrying a `ca
|
|
|
352
356
|
|
|
353
357
|
| Name | Type | Defined in |
|
|
354
358
|
| ------ | ------ | ------ |
|
|
355
|
-
| `cause` | `unknown` | [packages/core/src/types.ts:
|
|
356
|
-
| `tag` | `"Defect"` | [packages/core/src/types.ts:
|
|
359
|
+
| `cause` | `unknown` | [packages/core/src/types.ts:274](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L274) |
|
|
360
|
+
| `tag` | `"Defect"` | [packages/core/src/types.ts:273](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L273) |
|
|
357
361
|
|
|
358
362
|
#### Type Parameters
|
|
359
363
|
|
|
@@ -370,7 +374,7 @@ The `Defect` variant of a [Result](#result): an unmodeled failure carrying a `ca
|
|
|
370
374
|
type ErrOf<R> = R extends object ? E : never;
|
|
371
375
|
```
|
|
372
376
|
|
|
373
|
-
Defined in: [packages/core/src/types.ts:
|
|
377
|
+
Defined in: [packages/core/src/types.ts:430](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L430)
|
|
374
378
|
|
|
375
379
|
Extract the error type `E` from a `Result`.
|
|
376
380
|
|
|
@@ -388,7 +392,7 @@ Extract the error type `E` from a `Result`.
|
|
|
388
392
|
type ErrView<E, T> = ResultMethods<T, E> & object;
|
|
389
393
|
```
|
|
390
394
|
|
|
391
|
-
Defined in: [packages/core/src/types.ts:
|
|
395
|
+
Defined in: [packages/core/src/types.ts:267](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L267)
|
|
392
396
|
|
|
393
397
|
The `Err` variant of a [Result](#result): a modeled failure carrying an `error`.
|
|
394
398
|
|
|
@@ -396,8 +400,8 @@ The `Err` variant of a [Result](#result): a modeled failure carrying an `error`.
|
|
|
396
400
|
|
|
397
401
|
| Name | Type | Defined in |
|
|
398
402
|
| ------ | ------ | ------ |
|
|
399
|
-
| `error` | `E` | [packages/core/src/types.ts:
|
|
400
|
-
| `tag` | `"Err"` | [packages/core/src/types.ts:
|
|
403
|
+
| `error` | `E` | [packages/core/src/types.ts:269](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L269) |
|
|
404
|
+
| `tag` | `"Err"` | [packages/core/src/types.ts:268](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L268) |
|
|
401
405
|
|
|
402
406
|
#### Type Parameters
|
|
403
407
|
|
|
@@ -414,7 +418,7 @@ The `Err` variant of a [Result](#result): a modeled failure carrying an `error`.
|
|
|
414
418
|
type OkOf<R> = R extends object ? T : never;
|
|
415
419
|
```
|
|
416
420
|
|
|
417
|
-
Defined in: [packages/core/src/types.ts:
|
|
421
|
+
Defined in: [packages/core/src/types.ts:424](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L424)
|
|
418
422
|
|
|
419
423
|
Extract the success type `T` from a `Result`.
|
|
420
424
|
|
|
@@ -432,7 +436,7 @@ Extract the success type `T` from a `Result`.
|
|
|
432
436
|
type OkView<T, E> = ResultMethods<T, E> & object;
|
|
433
437
|
```
|
|
434
438
|
|
|
435
|
-
Defined in: [packages/core/src/types.ts:
|
|
439
|
+
Defined in: [packages/core/src/types.ts:262](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L262)
|
|
436
440
|
|
|
437
441
|
The `Ok` variant of a [Result](#result): a success carrying a `value`.
|
|
438
442
|
|
|
@@ -440,8 +444,8 @@ The `Ok` variant of a [Result](#result): a success carrying a `value`.
|
|
|
440
444
|
|
|
441
445
|
| Name | Type | Defined in |
|
|
442
446
|
| ------ | ------ | ------ |
|
|
443
|
-
| `tag` | `"Ok"` | [packages/core/src/types.ts:
|
|
444
|
-
| `value` | `T` | [packages/core/src/types.ts:
|
|
447
|
+
| `tag` | `"Ok"` | [packages/core/src/types.ts:263](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L263) |
|
|
448
|
+
| `value` | `T` | [packages/core/src/types.ts:264](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L264) |
|
|
445
449
|
|
|
446
450
|
#### Type Parameters
|
|
447
451
|
|
|
@@ -458,13 +462,14 @@ The `Ok` variant of a [Result](#result): a success carrying a `value`.
|
|
|
458
462
|
type Result<T, E> = ResultType<T, E>;
|
|
459
463
|
```
|
|
460
464
|
|
|
461
|
-
Defined in: [packages/core/src/facade.ts:
|
|
465
|
+
Defined in: [packages/core/src/facade.ts:43](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/facade.ts#L43)
|
|
462
466
|
|
|
463
467
|
Companion object grouping the standalone entry points under a single,
|
|
464
|
-
discoverable namespace: [Result.
|
|
465
|
-
[Result.
|
|
468
|
+
discoverable namespace: [Result.Ok](#property-ok), [Result.Err](#property-err),
|
|
469
|
+
[Result.Defect](#property-defect), [Result.fromNullable](#property-fromnullable), [Result.fromThrowable](#property-fromthrowable),
|
|
466
470
|
[Result.fromPromise](#property-frompromise), [Result.fromSafePromise](#property-fromsafepromise), [Result.all](#property-all),
|
|
467
|
-
[Result.allAsync](#property-allasync), [Result.
|
|
471
|
+
[Result.allAsync](#property-allasync), [Result.allFromDict](#property-allfromdict),
|
|
472
|
+
[Result.allFromDictAsync](#property-allfromdictasync), [Result.isOk](#property-isok), [Result.isErr](#property-iserr),
|
|
468
473
|
[Result.isDefect](#property-isdefect).
|
|
469
474
|
|
|
470
475
|
#### Type Parameters
|
|
@@ -478,14 +483,14 @@ discoverable namespace: [Result.ok](#property-ok), [Result.err](#property-err),
|
|
|
478
483
|
|
|
479
484
|
Purely additive sugar — each member **is** the corresponding free function.
|
|
480
485
|
The free functions remain the primary, tree-shakeable API; importing only
|
|
481
|
-
`{
|
|
486
|
+
`{ Ok }` never pulls this object in. The value `Result` and the type
|
|
482
487
|
[Result](#result-1) share one name (the companion-object pattern).
|
|
483
488
|
|
|
484
489
|
#### Example
|
|
485
490
|
|
|
486
491
|
```ts
|
|
487
492
|
import { Result } from "unthrown";
|
|
488
|
-
Result.
|
|
493
|
+
Result.Ok(1).flatMap((n) => Result.Ok(n + 1)).unwrap(); // 2
|
|
489
494
|
```
|
|
490
495
|
|
|
491
496
|
***
|
|
@@ -496,7 +501,7 @@ Result.ok(1).flatMap((n) => Result.ok(n + 1)).unwrap(); // 2
|
|
|
496
501
|
type TaggedErrorConstructor<Tag> = <A>(args) => TaggedErrorInstance<Tag, A>;
|
|
497
502
|
```
|
|
498
503
|
|
|
499
|
-
Defined in: [packages/core/src/tagged.ts:28](https://github.com/btravstack/unthrown/blob/
|
|
504
|
+
Defined in: [packages/core/src/tagged.ts:28](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/tagged.ts#L28)
|
|
500
505
|
|
|
501
506
|
The class constructor returned by [TaggedError](#taggederror). Generic in its payload:
|
|
502
507
|
apply it with an instantiation expression at the `extends` site.
|
|
@@ -530,7 +535,7 @@ When the payload is empty, the constructor takes **no** arguments (the
|
|
|
530
535
|
type TaggedErrorInstance<Tag, A> = Error & Readonly<A> & object;
|
|
531
536
|
```
|
|
532
537
|
|
|
533
|
-
Defined in: [packages/core/src/tagged.ts:15](https://github.com/btravstack/unthrown/blob/
|
|
538
|
+
Defined in: [packages/core/src/tagged.ts:15](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/tagged.ts#L15)
|
|
534
539
|
|
|
535
540
|
The instance shape produced by a [TaggedError](#taggederror) class: an `Error` plus a
|
|
536
541
|
`_tag` discriminant and the (readonly) payload fields.
|
|
@@ -539,7 +544,7 @@ The instance shape produced by a [TaggedError](#taggederror) class: an `Error` p
|
|
|
539
544
|
|
|
540
545
|
| Name | Type | Defined in |
|
|
541
546
|
| ------ | ------ | ------ |
|
|
542
|
-
| `_tag` | `Tag` | [packages/core/src/tagged.ts:16](https://github.com/btravstack/unthrown/blob/
|
|
547
|
+
| `_tag` | `Tag` | [packages/core/src/tagged.ts:16](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/tagged.ts#L16) |
|
|
543
548
|
|
|
544
549
|
#### Type Parameters
|
|
545
550
|
|
|
@@ -556,7 +561,7 @@ The instance shape produced by a [TaggedError](#taggederror) class: an `Error` p
|
|
|
556
561
|
type TagHandlers<T, E, R> = object & { [K in E["_tag"]]: (error: Extract<E, { _tag: K }>) => R };
|
|
557
562
|
```
|
|
558
563
|
|
|
559
|
-
Defined in: [packages/core/src/tagged.ts:103](https://github.com/btravstack/unthrown/blob/
|
|
564
|
+
Defined in: [packages/core/src/tagged.ts:103](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/tagged.ts#L103)
|
|
560
565
|
|
|
561
566
|
The handler object [matchTags](#matchtags) requires: a branch per error tag, plus
|
|
562
567
|
`Ok` and `Defect`. Miss a tag and it will not compile — the exhaustiveness is
|
|
@@ -566,8 +571,8 @@ enforced by the type, with no `.exhaustive()` to forget.
|
|
|
566
571
|
|
|
567
572
|
| Name | Type | Defined in |
|
|
568
573
|
| ------ | ------ | ------ |
|
|
569
|
-
| `Defect()` | (`cause`) => `R` | [packages/core/src/tagged.ts:105](https://github.com/btravstack/unthrown/blob/
|
|
570
|
-
| `Ok()` | (`value`) => `R` | [packages/core/src/tagged.ts:104](https://github.com/btravstack/unthrown/blob/
|
|
574
|
+
| `Defect()` | (`cause`) => `R` | [packages/core/src/tagged.ts:105](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/tagged.ts#L105) |
|
|
575
|
+
| `Ok()` | (`value`) => `R` | [packages/core/src/tagged.ts:104](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/tagged.ts#L104) |
|
|
571
576
|
|
|
572
577
|
#### Type Parameters
|
|
573
578
|
|
|
@@ -585,44 +590,48 @@ enforced by the type, with no `.exhaustive()` to forget.
|
|
|
585
590
|
const Result: object;
|
|
586
591
|
```
|
|
587
592
|
|
|
588
|
-
Defined in: [packages/core/src/facade.ts:
|
|
593
|
+
Defined in: [packages/core/src/facade.ts:43](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/facade.ts#L43)
|
|
589
594
|
|
|
590
595
|
Companion object grouping the standalone entry points under a single,
|
|
591
|
-
discoverable namespace: [Result.
|
|
592
|
-
[Result.
|
|
596
|
+
discoverable namespace: [Result.Ok](#property-ok), [Result.Err](#property-err),
|
|
597
|
+
[Result.Defect](#property-defect), [Result.fromNullable](#property-fromnullable), [Result.fromThrowable](#property-fromthrowable),
|
|
593
598
|
[Result.fromPromise](#property-frompromise), [Result.fromSafePromise](#property-fromsafepromise), [Result.all](#property-all),
|
|
594
|
-
[Result.allAsync](#property-allasync), [Result.
|
|
599
|
+
[Result.allAsync](#property-allasync), [Result.allFromDict](#property-allfromdict),
|
|
600
|
+
[Result.allFromDictAsync](#property-allfromdictasync), [Result.isOk](#property-isok), [Result.isErr](#property-iserr),
|
|
595
601
|
[Result.isDefect](#property-isdefect).
|
|
596
602
|
|
|
597
603
|
#### Type Declaration
|
|
598
604
|
|
|
599
605
|
| Name | Type | Defined in |
|
|
600
606
|
| ------ | ------ | ------ |
|
|
601
|
-
| <a id="property-all"></a> `all()` | <`Rs`>(`results`) => `Result`<`AllOk`<`Rs`, \{ \[K in string \| number \| symbol\]: OkOf\<Rs\[K\]\> \}>, [`ErrOf`](#errof)<`Rs`\[`number`\]>> | [packages/core/src/facade.ts:
|
|
602
|
-
| <a id="property-allasync"></a> `allAsync()` | <`Rs`>(`results`) => [`AsyncResult`](#asyncresult)<`AllOk`<`Rs`, \{ \[K in string \| number \| symbol\]: AsyncOkOf\<Rs\[K\]\> \}>, [`AsyncErrOf`](#asyncerrof)<`Rs`\[`number`\]>> | [packages/core/src/facade.ts:
|
|
603
|
-
| <a id="property-
|
|
604
|
-
| <a id="property-
|
|
605
|
-
| <a id="property-
|
|
606
|
-
| <a id="property-
|
|
607
|
-
| <a id="property-
|
|
608
|
-
| <a id="property-
|
|
609
|
-
| <a id="property-
|
|
610
|
-
| <a id="property-
|
|
611
|
-
| <a id="property-
|
|
612
|
-
| <a id="property-
|
|
607
|
+
| <a id="property-all"></a> `all()` | <`Rs`>(`results`) => `Result`<`AllOk`<`Rs`, \{ \[K in string \| number \| symbol\]: OkOf\<Rs\[K\]\> \}>, [`ErrOf`](#errof)<`Rs`\[`number`\]>> | [packages/core/src/facade.ts:52](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/facade.ts#L52) |
|
|
608
|
+
| <a id="property-allasync"></a> `allAsync()` | <`Rs`>(`results`) => [`AsyncResult`](#asyncresult)<`AllOk`<`Rs`, \{ \[K in string \| number \| symbol\]: AsyncOkOf\<Rs\[K\]\> \}>, [`AsyncErrOf`](#asyncerrof)<`Rs`\[`number`\]>> | [packages/core/src/facade.ts:53](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/facade.ts#L53) |
|
|
609
|
+
| <a id="property-allfromdict"></a> `allFromDict()` | <`R`>(`results`) => `Result`<\{ \[K in string \| number \| symbol\]: OkOf\<R\[K\]\> \}, [`ErrOf`](#errof)<`R`\[keyof `R`\]>> | [packages/core/src/facade.ts:54](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/facade.ts#L54) |
|
|
610
|
+
| <a id="property-allfromdictasync"></a> `allFromDictAsync()` | <`R`>(`results`) => [`AsyncResult`](#asyncresult)<\{ \[K in string \| number \| symbol\]: AsyncOkOf\<R\[K\]\> \}, [`AsyncErrOf`](#asyncerrof)<`R`\[keyof `R`\]>> | [packages/core/src/facade.ts:55](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/facade.ts#L55) |
|
|
611
|
+
| <a id="property-defect"></a> `Defect()` | (`cause`) => [`Defect`](#defect) | [packages/core/src/facade.ts:46](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/facade.ts#L46) |
|
|
612
|
+
| <a id="property-do"></a> `Do()` | () => `Result`<\{ \}, `never`> | [packages/core/src/facade.ts:47](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/facade.ts#L47) |
|
|
613
|
+
| <a id="property-err"></a> `Err()` | <`E`>(`error`) => `Result`<`never`, `E`> | [packages/core/src/facade.ts:45](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/facade.ts#L45) |
|
|
614
|
+
| <a id="property-fromnullable"></a> `fromNullable()` | <`T`, `E`>(`value`, `onAbsent`) => `Result`<`NonNullable`<`T`>, `E`> | [packages/core/src/facade.ts:48](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/facade.ts#L48) |
|
|
615
|
+
| <a id="property-frompromise"></a> `fromPromise()` | <`T`, `R`>(`promise`, `qualify`) => [`AsyncResult`](#asyncresult)<`T`, `Exclude`<`R`, [`Defect`](#defect)>> | [packages/core/src/facade.ts:50](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/facade.ts#L50) |
|
|
616
|
+
| <a id="property-fromsafepromise"></a> `fromSafePromise()` | <`T`>(`promise`) => [`AsyncResult`](#asyncresult)<`T`, `never`> | [packages/core/src/facade.ts:51](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/facade.ts#L51) |
|
|
617
|
+
| <a id="property-fromthrowable"></a> `fromThrowable()` | <`A`, `T`, `R`>(`fn`, `qualify`) => (...`args`) => `Result`<`T`, `Exclude`<`R`, [`Defect`](#defect)>> | [packages/core/src/facade.ts:49](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/facade.ts#L49) |
|
|
618
|
+
| <a id="property-isdefect"></a> `isDefect()` | <`T`, `E`>(`r`) => `r is DefectView<T, E>` | [packages/core/src/facade.ts:58](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/facade.ts#L58) |
|
|
619
|
+
| <a id="property-iserr"></a> `isErr()` | <`T`, `E`>(`r`) => `r is ErrView<E, T>` | [packages/core/src/facade.ts:57](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/facade.ts#L57) |
|
|
620
|
+
| <a id="property-isok"></a> `isOk()` | <`T`, `E`>(`r`) => `r is OkView<T, E>` | [packages/core/src/facade.ts:56](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/facade.ts#L56) |
|
|
621
|
+
| <a id="property-ok"></a> `Ok()` | <`T`>(`value`) => `Result`<`T`, `never`> | [packages/core/src/facade.ts:44](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/facade.ts#L44) |
|
|
613
622
|
|
|
614
623
|
#### Remarks
|
|
615
624
|
|
|
616
625
|
Purely additive sugar — each member **is** the corresponding free function.
|
|
617
626
|
The free functions remain the primary, tree-shakeable API; importing only
|
|
618
|
-
`{
|
|
627
|
+
`{ Ok }` never pulls this object in. The value `Result` and the type
|
|
619
628
|
[Result](#result-1) share one name (the companion-object pattern).
|
|
620
629
|
|
|
621
630
|
#### Example
|
|
622
631
|
|
|
623
632
|
```ts
|
|
624
633
|
import { Result } from "unthrown";
|
|
625
|
-
Result.
|
|
634
|
+
Result.Ok(1).flatMap((n) => Result.Ok(n + 1)).unwrap(); // 2
|
|
626
635
|
```
|
|
627
636
|
|
|
628
637
|
## Functions
|
|
@@ -633,21 +642,22 @@ Result.ok(1).flatMap((n) => Result.ok(n + 1)).unwrap(); // 2
|
|
|
633
642
|
function all<Rs>(results): Result<AllOk<Rs, { [K in string | number | symbol]: OkOf<Rs[K]> }>, ErrOf<Rs[number]>>;
|
|
634
643
|
```
|
|
635
644
|
|
|
636
|
-
Defined in: [packages/core/src/interop.ts:
|
|
645
|
+
Defined in: [packages/core/src/interop.ts:249](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/interop.ts#L249)
|
|
637
646
|
|
|
638
|
-
Collect [Result](#result)s into a single `Result` of all their
|
|
647
|
+
Collect a tuple/array of [Result](#result)s into a single `Result` of all their
|
|
648
|
+
success values.
|
|
639
649
|
|
|
640
650
|
#### Type Parameters
|
|
641
651
|
|
|
642
|
-
| Type Parameter |
|
|
643
|
-
| ------ |
|
|
644
|
-
| `Rs` *extends* readonly `Result`<`unknown`, `unknown`>[] |
|
|
652
|
+
| Type Parameter |
|
|
653
|
+
| ------ |
|
|
654
|
+
| `Rs` *extends* readonly `Result`<`unknown`, `unknown`>[] |
|
|
645
655
|
|
|
646
656
|
#### Parameters
|
|
647
657
|
|
|
648
|
-
| Parameter | Type |
|
|
649
|
-
| ------ | ------ |
|
|
650
|
-
| `results` | readonly \[`Rs`\] |
|
|
658
|
+
| Parameter | Type |
|
|
659
|
+
| ------ | ------ |
|
|
660
|
+
| `results` | readonly \[`Rs`\] |
|
|
651
661
|
|
|
652
662
|
#### Returns
|
|
653
663
|
|
|
@@ -657,16 +667,17 @@ Collect [Result](#result)s into a single `Result` of all their success values.
|
|
|
657
667
|
|
|
658
668
|
Short-circuits on the **first** `Err` (later entries are not inspected for
|
|
659
669
|
their error); any `Defect` present **dominates**, winning even over an earlier
|
|
660
|
-
`Err`. A **fixed tuple** keeps its positional types — `all([
|
|
670
|
+
`Err`. A **fixed tuple** keeps its positional types — `all([Ok(1), Ok("a")])`
|
|
661
671
|
is `Result<[number, string], …>` — while a **dynamic array** `Result<T, E>[]`
|
|
662
|
-
collapses to `Result<T[], E>` with no cast.
|
|
672
|
+
collapses to `Result<T[], E>` with no cast. For a **record** keyed by name,
|
|
673
|
+
use [allFromDict](#allfromdict).
|
|
663
674
|
|
|
664
675
|
#### Example
|
|
665
676
|
|
|
666
677
|
```ts
|
|
667
|
-
import { all,
|
|
668
|
-
all([
|
|
669
|
-
all([
|
|
678
|
+
import { all, Ok } from "unthrown";
|
|
679
|
+
all([Ok(1), Ok("a"), Ok(true)]).unwrap(); // [1, "a", true] (typed [number, string, boolean])
|
|
680
|
+
all([Ok(1), Ok(2)] as Result<number, never>[]).unwrap(); // number[]
|
|
670
681
|
```
|
|
671
682
|
|
|
672
683
|
***
|
|
@@ -677,22 +688,22 @@ all([ok(1), ok(2)] as Result<number, never>[]).unwrap(); // number[]
|
|
|
677
688
|
function allAsync<Rs>(results): AsyncResult<AllOk<Rs, { [K in string | number | symbol]: AsyncOkOf<Rs[K]> }>, AsyncErrOf<Rs[number]>>;
|
|
678
689
|
```
|
|
679
690
|
|
|
680
|
-
Defined in: [packages/core/src/interop.ts:
|
|
691
|
+
Defined in: [packages/core/src/interop.ts:299](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/interop.ts#L299)
|
|
681
692
|
|
|
682
|
-
The asynchronous counterpart of [all](#all): combine
|
|
683
|
-
one `AsyncResult` of all their success values.
|
|
693
|
+
The asynchronous counterpart of [all](#all): combine a tuple/array of
|
|
694
|
+
[AsyncResult](#asyncresult)s into one `AsyncResult` of all their success values.
|
|
684
695
|
|
|
685
696
|
#### Type Parameters
|
|
686
697
|
|
|
687
|
-
| Type Parameter |
|
|
688
|
-
| ------ |
|
|
689
|
-
| `Rs` *extends* readonly [`AsyncResult`](#asyncresult)<`unknown`, `unknown`>[] |
|
|
698
|
+
| Type Parameter |
|
|
699
|
+
| ------ |
|
|
700
|
+
| `Rs` *extends* readonly [`AsyncResult`](#asyncresult)<`unknown`, `unknown`>[] |
|
|
690
701
|
|
|
691
702
|
#### Parameters
|
|
692
703
|
|
|
693
|
-
| Parameter | Type |
|
|
694
|
-
| ------ | ------ |
|
|
695
|
-
| `results` | readonly \[`Rs`\] |
|
|
704
|
+
| Parameter | Type |
|
|
705
|
+
| ------ | ------ |
|
|
706
|
+
| `results` | readonly \[`Rs`\] |
|
|
696
707
|
|
|
697
708
|
#### Returns
|
|
698
709
|
|
|
@@ -700,31 +711,113 @@ one `AsyncResult` of all their success values.
|
|
|
700
711
|
|
|
701
712
|
#### Remarks
|
|
702
713
|
|
|
703
|
-
The inputs are resolved **concurrently** (
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
positional types; a **dynamic array** `AsyncResult<T, E>[]` collapses to
|
|
708
|
-
`AsyncResult<T[], E>`.
|
|
714
|
+
The inputs are resolved **concurrently** (order preserved); the resolved
|
|
715
|
+
`Result`s are then folded with the same rules as [all](#all) — first `Err`
|
|
716
|
+
short-circuits, any `Defect` dominates. As ever, the returned `AsyncResult`'s
|
|
717
|
+
internal promise never rejects. For a **record**, use [allFromDictAsync](#allfromdictasync).
|
|
709
718
|
|
|
710
719
|
#### Example
|
|
711
720
|
|
|
712
721
|
```ts
|
|
713
722
|
import { allAsync, fromSafePromise } from "unthrown";
|
|
714
|
-
|
|
723
|
+
await allAsync([fromSafePromise(a()), fromSafePromise(b())]);
|
|
715
724
|
```
|
|
716
725
|
|
|
717
726
|
***
|
|
718
727
|
|
|
719
|
-
###
|
|
728
|
+
### allFromDict()
|
|
720
729
|
|
|
721
730
|
```ts
|
|
722
|
-
function
|
|
731
|
+
function allFromDict<R>(results): Result<{ [K in string | number | symbol]: OkOf<R[K]> }, ErrOf<R[keyof R]>>;
|
|
723
732
|
```
|
|
724
733
|
|
|
725
|
-
Defined in: [packages/core/src/
|
|
734
|
+
Defined in: [packages/core/src/interop.ts:274](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/interop.ts#L274)
|
|
735
|
+
|
|
736
|
+
Collect a **record** of [Result](#result)s into a single `Result` of a record of
|
|
737
|
+
their success values — `allFromDict({ a: Result<A, E>, b: Result<B, E> })` is
|
|
738
|
+
`Result<{ a: A; b: B }, E>`. The named counterpart of [all](#all), for
|
|
739
|
+
parallel work you'd rather not tuple.
|
|
740
|
+
|
|
741
|
+
#### Type Parameters
|
|
742
|
+
|
|
743
|
+
| Type Parameter |
|
|
744
|
+
| ------ |
|
|
745
|
+
| `R` *extends* `ResultRecord` |
|
|
746
|
+
|
|
747
|
+
#### Parameters
|
|
726
748
|
|
|
727
|
-
|
|
749
|
+
| Parameter | Type |
|
|
750
|
+
| ------ | ------ |
|
|
751
|
+
| `results` | `R` |
|
|
752
|
+
|
|
753
|
+
#### Returns
|
|
754
|
+
|
|
755
|
+
`Result`<\{ \[K in string \| number \| symbol\]: OkOf\<R\[K\]\> \}, [`ErrOf`](#errof)<`R`\[keyof `R`\]>>
|
|
756
|
+
|
|
757
|
+
#### Remarks
|
|
758
|
+
|
|
759
|
+
Same folding rules as [all](#all): first `Err` short-circuits, any `Defect`
|
|
760
|
+
dominates. This is **not** error accumulation.
|
|
761
|
+
|
|
762
|
+
#### Example
|
|
763
|
+
|
|
764
|
+
```ts
|
|
765
|
+
import { allFromDict, Ok } from "unthrown";
|
|
766
|
+
allFromDict({ id: Ok(1), name: Ok("ada") }).unwrap(); // { id: 1, name: "ada" }
|
|
767
|
+
```
|
|
768
|
+
|
|
769
|
+
***
|
|
770
|
+
|
|
771
|
+
### allFromDictAsync()
|
|
772
|
+
|
|
773
|
+
```ts
|
|
774
|
+
function allFromDictAsync<R>(results): AsyncResult<{ [K in string | number | symbol]: AsyncOkOf<R[K]> }, AsyncErrOf<R[keyof R]>>;
|
|
775
|
+
```
|
|
776
|
+
|
|
777
|
+
Defined in: [packages/core/src/interop.ts:327](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/interop.ts#L327)
|
|
778
|
+
|
|
779
|
+
The asynchronous counterpart of [allFromDict](#allfromdict): combine a record of
|
|
780
|
+
[AsyncResult](#asyncresult)s into one `AsyncResult` of a record of their values.
|
|
781
|
+
|
|
782
|
+
#### Type Parameters
|
|
783
|
+
|
|
784
|
+
| Type Parameter |
|
|
785
|
+
| ------ |
|
|
786
|
+
| `R` *extends* `AsyncResultRecord` |
|
|
787
|
+
|
|
788
|
+
#### Parameters
|
|
789
|
+
|
|
790
|
+
| Parameter | Type |
|
|
791
|
+
| ------ | ------ |
|
|
792
|
+
| `results` | `R` |
|
|
793
|
+
|
|
794
|
+
#### Returns
|
|
795
|
+
|
|
796
|
+
[`AsyncResult`](#asyncresult)<\{ \[K in string \| number \| symbol\]: AsyncOkOf\<R\[K\]\> \}, [`AsyncErrOf`](#asyncerrof)<`R`\[keyof `R`\]>>
|
|
797
|
+
|
|
798
|
+
#### Remarks
|
|
799
|
+
|
|
800
|
+
Resolved concurrently (order preserved), folded with the [all](#all) rules,
|
|
801
|
+
and the internal promise never rejects.
|
|
802
|
+
|
|
803
|
+
#### Example
|
|
804
|
+
|
|
805
|
+
```ts
|
|
806
|
+
import { allFromDictAsync, fromSafePromise } from "unthrown";
|
|
807
|
+
await allFromDictAsync({ a: fromSafePromise(a()), b: fromSafePromise(b()) });
|
|
808
|
+
```
|
|
809
|
+
|
|
810
|
+
***
|
|
811
|
+
|
|
812
|
+
### Defect()
|
|
813
|
+
|
|
814
|
+
```ts
|
|
815
|
+
function Defect(cause): Defect;
|
|
816
|
+
```
|
|
817
|
+
|
|
818
|
+
Defined in: [packages/core/src/defect.ts:36](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/defect.ts#L36)
|
|
819
|
+
|
|
820
|
+
Wrap a cause as a [Defect](#defect-2) — the value you return from a `qualify`
|
|
728
821
|
function when a failure is **not** a modeled domain error.
|
|
729
822
|
|
|
730
823
|
#### Parameters
|
|
@@ -737,27 +830,66 @@ function when a failure is **not** a modeled domain error.
|
|
|
737
830
|
|
|
738
831
|
[`Defect`](#defect)
|
|
739
832
|
|
|
740
|
-
an opaque
|
|
833
|
+
an opaque Defect marker carrying `cause`.
|
|
741
834
|
|
|
742
835
|
#### Example
|
|
743
836
|
|
|
744
837
|
```ts
|
|
745
|
-
import { fromPromise,
|
|
838
|
+
import { fromPromise, Defect } from "unthrown";
|
|
746
839
|
|
|
747
840
|
const user = fromPromise(fetchUser(id), (cause) =>
|
|
748
|
-
cause instanceof NotFoundError ? cause :
|
|
841
|
+
cause instanceof NotFoundError ? cause : Defect(cause),
|
|
749
842
|
);
|
|
750
843
|
```
|
|
751
844
|
|
|
752
845
|
***
|
|
753
846
|
|
|
754
|
-
###
|
|
847
|
+
### Do()
|
|
848
|
+
|
|
849
|
+
```ts
|
|
850
|
+
function Do(): Result<{
|
|
851
|
+
}, never>;
|
|
852
|
+
```
|
|
853
|
+
|
|
854
|
+
Defined in: [packages/core/src/do.ts:30](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/do.ts#L30)
|
|
855
|
+
|
|
856
|
+
Start a do-notation chain with an empty object scope, grown step by step with
|
|
857
|
+
`bind` (for `Result`-returning steps) and `let` (for pure values).
|
|
858
|
+
|
|
859
|
+
#### Returns
|
|
860
|
+
|
|
861
|
+
`Result`<\{
|
|
862
|
+
\}, `never`>
|
|
863
|
+
|
|
864
|
+
#### Remarks
|
|
865
|
+
|
|
866
|
+
Capitalised because `do` is a reserved word. Each step receives the scope
|
|
867
|
+
accumulated so far; the error types union across `bind`s, and a throw in any
|
|
868
|
+
step becomes a `Defect`. To go asynchronous, lift the chain with `toAsync()`
|
|
869
|
+
(then a `bind` may return an `AsyncResult`).
|
|
870
|
+
|
|
871
|
+
#### Example
|
|
872
|
+
|
|
873
|
+
```ts
|
|
874
|
+
import { Do, Ok } from "unthrown";
|
|
875
|
+
|
|
876
|
+
const result = Do()
|
|
877
|
+
.bind("user", () => findUser(id)) // Result<User, NotFound>
|
|
878
|
+
.bind("org", ({ user }) => findOrg(user.orgId)) // Result<Org, NotFound>
|
|
879
|
+
.let("label", ({ user, org }) => `${user.name} @ ${org.name}`)
|
|
880
|
+
.map(({ user, org, label }) => render(user, org, label));
|
|
881
|
+
// Result<View, NotFound>
|
|
882
|
+
```
|
|
883
|
+
|
|
884
|
+
***
|
|
885
|
+
|
|
886
|
+
### Err()
|
|
755
887
|
|
|
756
888
|
```ts
|
|
757
|
-
function
|
|
889
|
+
function Err<E>(error): Result<never, E>;
|
|
758
890
|
```
|
|
759
891
|
|
|
760
|
-
Defined in: [packages/core/src/constructors.ts:34](https://github.com/btravstack/unthrown/blob/
|
|
892
|
+
Defined in: [packages/core/src/constructors.ts:34](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/constructors.ts#L34)
|
|
761
893
|
|
|
762
894
|
Construct a failed [Result](#result) carrying a **modeled** error.
|
|
763
895
|
|
|
@@ -780,8 +912,8 @@ Construct a failed [Result](#result) carrying a **modeled** error.
|
|
|
780
912
|
#### Example
|
|
781
913
|
|
|
782
914
|
```ts
|
|
783
|
-
import {
|
|
784
|
-
|
|
915
|
+
import { Err } from "unthrown";
|
|
916
|
+
Err("not_found").unwrapErr(); // "not_found"
|
|
785
917
|
```
|
|
786
918
|
|
|
787
919
|
***
|
|
@@ -792,7 +924,7 @@ err("not_found").unwrapErr(); // "not_found"
|
|
|
792
924
|
function fromNullable<T, E>(value, onAbsent): Result<NonNullable<T>, E>;
|
|
793
925
|
```
|
|
794
926
|
|
|
795
|
-
Defined in: [packages/core/src/interop.ts:29](https://github.com/btravstack/unthrown/blob/
|
|
927
|
+
Defined in: [packages/core/src/interop.ts:29](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/interop.ts#L29)
|
|
796
928
|
|
|
797
929
|
Bridge a nullable value into a [Result](#result): absence becomes a **modeled**
|
|
798
930
|
`Err`. The sanctioned alternative to an `Option` type.
|
|
@@ -817,7 +949,7 @@ Bridge a nullable value into a [Result](#result): absence becomes a **modeled**
|
|
|
817
949
|
|
|
818
950
|
#### Remarks
|
|
819
951
|
|
|
820
|
-
`null` and `undefined` map to `
|
|
952
|
+
`null` and `undefined` map to `Err(onAbsent())`; any other value (including
|
|
821
953
|
falsy ones like `0`, `""`, `false`) maps to `Ok`.
|
|
822
954
|
|
|
823
955
|
#### Example
|
|
@@ -835,7 +967,7 @@ fromNullable(map.get(key), () => "missing").unwrap();
|
|
|
835
967
|
function fromPromise<T, R>(promise, qualify): AsyncResult<T, Exclude<R, Defect>>;
|
|
836
968
|
```
|
|
837
969
|
|
|
838
|
-
Defined in: [packages/core/src/interop.ts:110](https://github.com/btravstack/unthrown/blob/
|
|
970
|
+
Defined in: [packages/core/src/interop.ts:110](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/interop.ts#L110)
|
|
839
971
|
|
|
840
972
|
Wrap a `Promise` (or a thunk producing one) as an [AsyncResult](#asyncresult), forcing
|
|
841
973
|
every rejection to be triaged.
|
|
@@ -861,21 +993,21 @@ every rejection to be triaged.
|
|
|
861
993
|
#### Remarks
|
|
862
994
|
|
|
863
995
|
`qualify` **must** map each rejection cause into a modeled error `E` or a
|
|
864
|
-
[Defect](#defect). The returned `AsyncResult`'s internal promise never rejects;
|
|
996
|
+
[Defect](#defect-2). The returned `AsyncResult`'s internal promise never rejects;
|
|
865
997
|
`await`-ing it always yields a `Result`. A throw inside `qualify` is itself a
|
|
866
998
|
`Defect`.
|
|
867
999
|
|
|
868
1000
|
The modeled error type is `Exclude<R, Defect>` — the `Defect` arm of
|
|
869
1001
|
`qualify`'s return is **subtracted** from `E`, never inferred into it. So a
|
|
870
|
-
`qualify` that returns *only* `
|
|
871
|
-
rejection is a
|
|
1002
|
+
`qualify` that returns *only* `Defect(cause)` yields `E = never`; when every
|
|
1003
|
+
rejection is a Defect, prefer [fromSafePromise](#fromsafepromise).
|
|
872
1004
|
|
|
873
1005
|
#### Example
|
|
874
1006
|
|
|
875
1007
|
```ts
|
|
876
|
-
import { fromPromise,
|
|
1008
|
+
import { fromPromise, Defect } from "unthrown";
|
|
877
1009
|
const user = await fromPromise(fetchUser(id), (cause) =>
|
|
878
|
-
cause instanceof NotFoundError ? ("not_found" as const) :
|
|
1010
|
+
cause instanceof NotFoundError ? ("not_found" as const) : Defect(cause),
|
|
879
1011
|
);
|
|
880
1012
|
```
|
|
881
1013
|
|
|
@@ -887,7 +1019,7 @@ const user = await fromPromise(fetchUser(id), (cause) =>
|
|
|
887
1019
|
function fromSafePromise<T>(promise): AsyncResult<T, never>;
|
|
888
1020
|
```
|
|
889
1021
|
|
|
890
|
-
Defined in: [packages/core/src/interop.ts:136](https://github.com/btravstack/unthrown/blob/
|
|
1022
|
+
Defined in: [packages/core/src/interop.ts:136](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/interop.ts#L136)
|
|
891
1023
|
|
|
892
1024
|
Wrap a `Promise` asserted **not** to fail in any modeled way: any rejection
|
|
893
1025
|
becomes a `Defect`.
|
|
@@ -922,7 +1054,7 @@ triage. (`await`-ing still yields a `Result`; it never throws.)
|
|
|
922
1054
|
function fromThrowable<A, T, R>(fn, qualify): (...args) => Result<T, Exclude<R, Defect>>;
|
|
923
1055
|
```
|
|
924
1056
|
|
|
925
|
-
Defined in: [packages/core/src/interop.ts:66](https://github.com/btravstack/unthrown/blob/
|
|
1057
|
+
Defined in: [packages/core/src/interop.ts:66](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/interop.ts#L66)
|
|
926
1058
|
|
|
927
1059
|
Wrap a throwing synchronous function so it returns a [Result](#result) instead of
|
|
928
1060
|
throwing.
|
|
@@ -951,20 +1083,20 @@ a function with the same arguments returning `Result<T, E>`.
|
|
|
951
1083
|
#### Remarks
|
|
952
1084
|
|
|
953
1085
|
`qualify` **must** triage every thrown cause into a modeled error `E` or a
|
|
954
|
-
[Defect](#defect) (via [
|
|
1086
|
+
[Defect](#defect-2) (via [Defect](#defect-2)) — there is no path that leaves `unknown`
|
|
955
1087
|
in `E`. A throw inside `qualify` itself is treated as a `Defect`.
|
|
956
1088
|
|
|
957
1089
|
The modeled error type is `Exclude<R, Defect>` — the `Defect` arm of
|
|
958
1090
|
`qualify`'s return is **subtracted** from `E`, never inferred into it. So a
|
|
959
|
-
`qualify` that returns *only* `
|
|
1091
|
+
`qualify` that returns *only* `Defect(cause)` yields `E = never` (a Defect is
|
|
960
1092
|
out-of-band and must not pollute the error channel); reach for
|
|
961
|
-
[fromSafePromise](#fromsafepromise) when every failure is a
|
|
1093
|
+
[fromSafePromise](#fromsafepromise) when every failure is a Defect.
|
|
962
1094
|
|
|
963
1095
|
#### Example
|
|
964
1096
|
|
|
965
1097
|
```ts
|
|
966
|
-
import { fromThrowable,
|
|
967
|
-
const parse = fromThrowable(JSON.parse, (cause) =>
|
|
1098
|
+
import { fromThrowable, Defect } from "unthrown";
|
|
1099
|
+
const parse = fromThrowable(JSON.parse, (cause) => Defect(cause));
|
|
968
1100
|
parse("{}").unwrap();
|
|
969
1101
|
```
|
|
970
1102
|
|
|
@@ -976,7 +1108,7 @@ parse("{}").unwrap();
|
|
|
976
1108
|
function isDefect<T, E>(r): r is DefectView<T, E>;
|
|
977
1109
|
```
|
|
978
1110
|
|
|
979
|
-
Defined in: [packages/core/src/constructors.ts:66](https://github.com/btravstack/unthrown/blob/
|
|
1111
|
+
Defined in: [packages/core/src/constructors.ts:66](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/constructors.ts#L66)
|
|
980
1112
|
|
|
981
1113
|
Type guard: narrow a [Result](#result) to its `Defect` variant, exposing `.cause`.
|
|
982
1114
|
|
|
@@ -1007,7 +1139,7 @@ Type guard: narrow a [Result](#result) to its `Defect` variant, exposing `.cause
|
|
|
1007
1139
|
function isErr<T, E>(r): r is ErrView<E, T>;
|
|
1008
1140
|
```
|
|
1009
1141
|
|
|
1010
|
-
Defined in: [packages/core/src/constructors.ts:58](https://github.com/btravstack/unthrown/blob/
|
|
1142
|
+
Defined in: [packages/core/src/constructors.ts:58](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/constructors.ts#L58)
|
|
1011
1143
|
|
|
1012
1144
|
Type guard: narrow a [Result](#result) to its `Err` variant, exposing `.error`.
|
|
1013
1145
|
|
|
@@ -1038,7 +1170,7 @@ Type guard: narrow a [Result](#result) to its `Err` variant, exposing `.error`.
|
|
|
1038
1170
|
function isOk<T, E>(r): r is OkView<T, E>;
|
|
1039
1171
|
```
|
|
1040
1172
|
|
|
1041
|
-
Defined in: [packages/core/src/constructors.ts:50](https://github.com/btravstack/unthrown/blob/
|
|
1173
|
+
Defined in: [packages/core/src/constructors.ts:50](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/constructors.ts#L50)
|
|
1042
1174
|
|
|
1043
1175
|
Type guard: narrow a [Result](#result) to its `Ok` variant, exposing `.value`.
|
|
1044
1176
|
|
|
@@ -1079,7 +1211,7 @@ if (isOk(r)) r.value; // number, narrowed
|
|
|
1079
1211
|
function matchTags<T, E, R>(result, handlers): R;
|
|
1080
1212
|
```
|
|
1081
1213
|
|
|
1082
|
-
Defined in: [packages/core/src/tagged.ts:138](https://github.com/btravstack/unthrown/blob/
|
|
1214
|
+
Defined in: [packages/core/src/tagged.ts:138](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/tagged.ts#L138)
|
|
1083
1215
|
|
|
1084
1216
|
Exhaustively fold a [Result](#result) (or [AsyncResult](#asyncresult)) whose error type is
|
|
1085
1217
|
a tagged union, dispatching each error to the handler matching its `_tag`.
|
|
@@ -1131,7 +1263,7 @@ matchTags(r, {
|
|
|
1131
1263
|
function matchTags<T, E, R>(result, handlers): Promise<R>;
|
|
1132
1264
|
```
|
|
1133
1265
|
|
|
1134
|
-
Defined in: [packages/core/src/tagged.ts:142](https://github.com/btravstack/unthrown/blob/
|
|
1266
|
+
Defined in: [packages/core/src/tagged.ts:142](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/tagged.ts#L142)
|
|
1135
1267
|
|
|
1136
1268
|
Exhaustively fold a [Result](#result) (or [AsyncResult](#asyncresult)) whose error type is
|
|
1137
1269
|
a tagged union, dispatching each error to the handler matching its `_tag`.
|
|
@@ -1179,13 +1311,13 @@ matchTags(r, {
|
|
|
1179
1311
|
|
|
1180
1312
|
***
|
|
1181
1313
|
|
|
1182
|
-
###
|
|
1314
|
+
### Ok()
|
|
1183
1315
|
|
|
1184
1316
|
```ts
|
|
1185
|
-
function
|
|
1317
|
+
function Ok<T>(value): Result<T, never>;
|
|
1186
1318
|
```
|
|
1187
1319
|
|
|
1188
|
-
Defined in: [packages/core/src/constructors.ts:18](https://github.com/btravstack/unthrown/blob/
|
|
1320
|
+
Defined in: [packages/core/src/constructors.ts:18](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/constructors.ts#L18)
|
|
1189
1321
|
|
|
1190
1322
|
Construct a successful [Result](#result).
|
|
1191
1323
|
|
|
@@ -1208,8 +1340,8 @@ Construct a successful [Result](#result).
|
|
|
1208
1340
|
#### Example
|
|
1209
1341
|
|
|
1210
1342
|
```ts
|
|
1211
|
-
import {
|
|
1212
|
-
|
|
1343
|
+
import { Ok } from "unthrown";
|
|
1344
|
+
Ok(42).unwrap(); // 42
|
|
1213
1345
|
```
|
|
1214
1346
|
|
|
1215
1347
|
***
|
|
@@ -1220,7 +1352,7 @@ ok(42).unwrap(); // 42
|
|
|
1220
1352
|
function TaggedError<Tag>(tag, options?): TaggedErrorConstructor<Tag>;
|
|
1221
1353
|
```
|
|
1222
1354
|
|
|
1223
|
-
Defined in: [packages/core/src/tagged.ts:72](https://github.com/btravstack/unthrown/blob/
|
|
1355
|
+
Defined in: [packages/core/src/tagged.ts:72](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/tagged.ts#L72)
|
|
1224
1356
|
|
|
1225
1357
|
Build a base class for a tagged error — a class extending `Error` with a
|
|
1226
1358
|
`_tag` string discriminant, in the style of Effect's `Data.TaggedError`.
|