unthrown 1.1.0 → 3.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 +3 -3
- package/dist/index.cjs +92 -65
- package/dist/index.d.cts +106 -86
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +106 -86
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +92 -65
- package/dist/index.mjs.map +1 -1
- package/docs/index.md +164 -200
- package/package.json +1 -1
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:35](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/core.ts#L35)
|
|
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
|
|
@@ -16,6 +16,11 @@ wrong on a *modeled* result — `unwrap()` on an `Err`, or `unwrapErr()` on an
|
|
|
16
16
|
|
|
17
17
|
#### Remarks
|
|
18
18
|
|
|
19
|
+
The offending value is exposed two ways: the typed [UnwrapError.error](#error)
|
|
20
|
+
property for programmatic access, and the standard `Error.cause` for the
|
|
21
|
+
runtime and devtools to chain — when `E` is an `Error` (e.g. a `TaggedError`)
|
|
22
|
+
its original stack is printed under "caused by".
|
|
23
|
+
|
|
19
24
|
A `Defect` is never wrapped in an `UnwrapError`: its original cause is
|
|
20
25
|
re-thrown (with its original stack) instead.
|
|
21
26
|
|
|
@@ -37,7 +42,7 @@ re-thrown (with its original stack) instead.
|
|
|
37
42
|
new UnwrapError<E>(error): UnwrapError<E>;
|
|
38
43
|
```
|
|
39
44
|
|
|
40
|
-
Defined in: [packages/core/src/core.ts:
|
|
45
|
+
Defined in: [packages/core/src/core.ts:41](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/core.ts#L41)
|
|
41
46
|
|
|
42
47
|
###### Parameters
|
|
43
48
|
|
|
@@ -60,7 +65,7 @@ Error.constructor
|
|
|
60
65
|
| Property | Modifier | Type | Description | Inherited from | Defined in |
|
|
61
66
|
| ------ | ------ | ------ | ------ | ------ | ------ |
|
|
62
67
|
| <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:
|
|
68
|
+
| <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:40](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/core.ts#L40) |
|
|
64
69
|
| <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
70
|
| <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
71
|
| <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 +179,7 @@ Error.prepareStackTrace
|
|
|
174
179
|
type AsyncErrOf<R> = R extends AsyncResult<unknown, infer E> ? E : never;
|
|
175
180
|
```
|
|
176
181
|
|
|
177
|
-
Defined in: [packages/core/src/types.ts:468](https://github.com/btravstack/unthrown/blob/
|
|
182
|
+
Defined in: [packages/core/src/types.ts:468](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L468)
|
|
178
183
|
|
|
179
184
|
Extract the error type `E` from an [AsyncResult](#asyncresult).
|
|
180
185
|
|
|
@@ -192,7 +197,7 @@ Extract the error type `E` from an [AsyncResult](#asyncresult).
|
|
|
192
197
|
type AsyncOkOf<R> = R extends AsyncResult<infer T, unknown> ? T : never;
|
|
193
198
|
```
|
|
194
199
|
|
|
195
|
-
Defined in: [packages/core/src/types.ts:462](https://github.com/btravstack/unthrown/blob/
|
|
200
|
+
Defined in: [packages/core/src/types.ts:462](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L462)
|
|
196
201
|
|
|
197
202
|
Extract the success type `T` from an [AsyncResult](#asyncresult).
|
|
198
203
|
|
|
@@ -207,59 +212,39 @@ Extract the success type `T` from an [AsyncResult](#asyncresult).
|
|
|
207
212
|
### AsyncResult
|
|
208
213
|
|
|
209
214
|
```ts
|
|
210
|
-
type AsyncResult<T, E> =
|
|
215
|
+
type AsyncResult<T, E> = AsyncResultType<T, E>;
|
|
211
216
|
```
|
|
212
217
|
|
|
213
|
-
Defined in: [packages/core/src/
|
|
214
|
-
|
|
215
|
-
The asynchronous counterpart of [Result](#result): an awaitable wrapper with the
|
|
216
|
-
same method surface, collapsing to a `Result<T, E>` when `await`-ed.
|
|
218
|
+
Defined in: [packages/core/src/facade.ts:85](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L85)
|
|
217
219
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
| `as()` | (`value`) => [`AsyncResult`](#asyncresult)<`U`, `E`> | Asynchronous `as`. | [packages/core/src/types.ts:398](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L398) |
|
|
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:391](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L391) |
|
|
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:376](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L376) |
|
|
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:384](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L384) |
|
|
226
|
-
| `flatTapErr()` | (`f`) => [`AsyncResult`](#asyncresult)<`T`, `E` \| `E2`> | Asynchronous `flatTapErr` — a failable tap on the error that keeps the original error. `f` may return a `Result` **or** an `AsyncResult`; its `Ok` value is discarded, an `Err`/`Defect` from `f` threads through, and a throw becomes a `Defect`. | [packages/core/src/types.ts:414](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L414) |
|
|
227
|
-
| `getOrNull()` | () => `Promise`<`T` \| `null`> | Asynchronous `getOrNull`. | [packages/core/src/types.ts:440](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L440) |
|
|
228
|
-
| `getOrUndefined()` | () => `Promise`<`T` \| `undefined`> | Asynchronous `getOrUndefined`. | [packages/core/src/types.ts:442](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L442) |
|
|
229
|
-
| `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:396](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L396) |
|
|
230
|
-
| `map()` | (`f`) => [`AsyncResult`](#asyncresult)<`U`, `E`> | Asynchronous `map`. `f` is synchronous; a throw becomes a `Defect`. | [packages/core/src/types.ts:371](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L371) |
|
|
231
|
-
| `mapErr()` | (`f`) => [`AsyncResult`](#asyncresult)<`T`, `E2`> | Asynchronous `mapErr`. `f` is synchronous; a throw becomes a `Defect`. | [packages/core/src/types.ts:401](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L401) |
|
|
232
|
-
| `match()` | (`cases`) => `Promise`<`R`> | Asynchronous `match`. Handlers are synchronous; resolves to `R`. | [packages/core/src/types.ts:426](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L426) |
|
|
233
|
-
| `orElse()` | (`f`) => [`AsyncResult`](#asyncresult)<`T` \| `U`, `E2`> | Asynchronous `orElse`. `f` may return a `Result` or an `AsyncResult`. | [packages/core/src/types.ts:403](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L403) |
|
|
234
|
-
| `recover()` | (`f`) => [`AsyncResult`](#asyncresult)<`T` \| `U`, `never`> | Asynchronous `recover`. `f` is synchronous; a throw becomes a `Defect`. | [packages/core/src/types.ts:405](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L405) |
|
|
235
|
-
| `recoverDefect()` | (`f`) => [`AsyncResult`](#asyncresult)<`T` \| `U`, `E` \| `E2`> | Asynchronous `recoverDefect`. `f` may return a `Result` or an `AsyncResult`. | [packages/core/src/types.ts:419](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L419) |
|
|
236
|
-
| `tap()` | (`f`) => [`AsyncResult`](#asyncresult)<`T`, `E`> | Asynchronous `tap`. `f` is synchronous; a throw becomes a `Defect`. | [packages/core/src/types.ts:378](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L378) |
|
|
237
|
-
| `tapDefect()` | (`f`) => [`AsyncResult`](#asyncresult)<`T`, `E`> | Asynchronous `tapDefect`. | [packages/core/src/types.ts:423](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L423) |
|
|
238
|
-
| `tapErr()` | (`f`) => [`AsyncResult`](#asyncresult)<`T`, `E`> | Asynchronous `tapErr`. `f` is synchronous; a throw becomes a `Defect`. | [packages/core/src/types.ts:407](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L407) |
|
|
239
|
-
| `unwrap()` | () => `Promise`<`T`> | Asynchronous `unwrap`. The returned promise rejects on `Err`/`Defect`. | [packages/core/src/types.ts:432](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L432) |
|
|
240
|
-
| `unwrapErr()` | () => `Promise`<`E`> | Asynchronous `unwrapErr`. | [packages/core/src/types.ts:434](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L434) |
|
|
241
|
-
| `unwrapOr()` | (`fallback`) => `Promise`<`T`> | Asynchronous `unwrapOr`. | [packages/core/src/types.ts:436](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L436) |
|
|
242
|
-
| `unwrapOrElse()` | (`f`) => `Promise`<`T`> | Asynchronous `unwrapOrElse`. | [packages/core/src/types.ts:438](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/types.ts#L438) |
|
|
220
|
+
Companion object grouping the **`AsyncResult`-producing** entry points under
|
|
221
|
+
the matching namespace: [AsyncResult.fromPromise](#property-frompromise),
|
|
222
|
+
[AsyncResult.fromSafePromise](#property-fromsafepromise), [AsyncResult.all](#property-all),
|
|
223
|
+
[AsyncResult.allFromDict](#property-allfromdict).
|
|
243
224
|
|
|
244
225
|
#### Type Parameters
|
|
245
226
|
|
|
246
|
-
| Type Parameter |
|
|
247
|
-
| ------ |
|
|
248
|
-
| `T` |
|
|
249
|
-
| `E` |
|
|
227
|
+
| Type Parameter |
|
|
228
|
+
| ------ |
|
|
229
|
+
| `T` |
|
|
230
|
+
| `E` |
|
|
250
231
|
|
|
251
232
|
#### Remarks
|
|
252
233
|
|
|
253
|
-
|
|
254
|
-
`
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
234
|
+
The async sibling of [Result](#result-1). Statics are grouped by what they
|
|
235
|
+
**return**, so `fromPromise`/`fromSafePromise` and the async aggregates sit
|
|
236
|
+
here rather than on [Result](#result-1); the namespace already conveys "async", so
|
|
237
|
+
the aggregates drop the `Async` suffix (`AsyncResult.all` is the free function
|
|
238
|
+
`allAsync`; `AsyncResult.allFromDict` is `allFromDictAsync`). Like
|
|
239
|
+
[Result](#result-1), the free functions remain the primary, tree-shakeable API; the
|
|
240
|
+
value `AsyncResult` and the type [AsyncResult](#asyncresult-1) share one name.
|
|
241
|
+
|
|
242
|
+
#### Example
|
|
261
243
|
|
|
262
|
-
|
|
244
|
+
```ts
|
|
245
|
+
import { AsyncResult } from "unthrown";
|
|
246
|
+
const user = await AsyncResult.fromPromise(fetchUser(id), (c, defect) => defect(c));
|
|
247
|
+
```
|
|
263
248
|
|
|
264
249
|
***
|
|
265
250
|
|
|
@@ -269,7 +254,7 @@ To pattern-match an `AsyncResult`, `await` it first: `match(await ar)`.
|
|
|
269
254
|
type Awaitable<T> = object;
|
|
270
255
|
```
|
|
271
256
|
|
|
272
|
-
Defined in: [packages/core/src/types.ts:346](https://github.com/btravstack/unthrown/blob/
|
|
257
|
+
Defined in: [packages/core/src/types.ts:346](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L346)
|
|
273
258
|
|
|
274
259
|
A success-only thenable: awaitable, but deliberately **not** a full
|
|
275
260
|
`PromiseLike`.
|
|
@@ -296,7 +281,7 @@ being treated as a raw promise (e.g. dropped into `Promise.all`).
|
|
|
296
281
|
then<R>(onfulfilled?): PromiseLike<R>;
|
|
297
282
|
```
|
|
298
283
|
|
|
299
|
-
Defined in: [packages/core/src/types.ts:347](https://github.com/btravstack/unthrown/blob/
|
|
284
|
+
Defined in: [packages/core/src/types.ts:347](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L347)
|
|
300
285
|
|
|
301
286
|
###### Type Parameters
|
|
302
287
|
|
|
@@ -316,40 +301,13 @@ Defined in: [packages/core/src/types.ts:347](https://github.com/btravstack/unthr
|
|
|
316
301
|
|
|
317
302
|
***
|
|
318
303
|
|
|
319
|
-
### Defect
|
|
320
|
-
|
|
321
|
-
```ts
|
|
322
|
-
type Defect = object;
|
|
323
|
-
```
|
|
324
|
-
|
|
325
|
-
Defined in: [packages/core/src/defect.ts:36](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/defect.ts#L36)
|
|
326
|
-
|
|
327
|
-
The marker a `qualify` function returns to triage a cause as **unexpected**.
|
|
328
|
-
|
|
329
|
-
#### Remarks
|
|
330
|
-
|
|
331
|
-
`qualify` (passed to [fromPromise](#frompromise) / [fromThrowable](#fromthrowable)) returns
|
|
332
|
-
`E | Defect`: either a modeled domain error, or a `Defect` produced by
|
|
333
|
-
[Defect](#defect-2) to say "this failure is not modeled". A `Defect` is opaque —
|
|
334
|
-
it carries the original cause for the boundary to convert into the third
|
|
335
|
-
runtime state of a `Result`.
|
|
336
|
-
|
|
337
|
-
#### Properties
|
|
338
|
-
|
|
339
|
-
| Property | Modifier | Type | Defined in |
|
|
340
|
-
| ------ | ------ | ------ | ------ |
|
|
341
|
-
| <a id="defect-1"></a> `[DEFECT]` | `readonly` | `true` | [packages/core/src/defect.ts:16](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/defect.ts#L16) |
|
|
342
|
-
| <a id="cause-1"></a> `cause` | `readonly` | `unknown` | [packages/core/src/defect.ts:17](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/defect.ts#L17) |
|
|
343
|
-
|
|
344
|
-
***
|
|
345
|
-
|
|
346
304
|
### DefectView
|
|
347
305
|
|
|
348
306
|
```ts
|
|
349
307
|
type DefectView<T, E> = ResultMethods<T, E> & object;
|
|
350
308
|
```
|
|
351
309
|
|
|
352
|
-
Defined in: [packages/core/src/types.ts:289](https://github.com/btravstack/unthrown/blob/
|
|
310
|
+
Defined in: [packages/core/src/types.ts:289](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L289)
|
|
353
311
|
|
|
354
312
|
The `Defect` variant of a [Result](#result): an unmodeled failure carrying a `cause`.
|
|
355
313
|
|
|
@@ -357,8 +315,8 @@ The `Defect` variant of a [Result](#result): an unmodeled failure carrying a `ca
|
|
|
357
315
|
|
|
358
316
|
| Name | Type | Defined in |
|
|
359
317
|
| ------ | ------ | ------ |
|
|
360
|
-
| `cause` | `unknown` | [packages/core/src/types.ts:291](https://github.com/btravstack/unthrown/blob/
|
|
361
|
-
| `tag` | `"Defect"` | [packages/core/src/types.ts:290](https://github.com/btravstack/unthrown/blob/
|
|
318
|
+
| `cause` | `unknown` | [packages/core/src/types.ts:291](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L291) |
|
|
319
|
+
| `tag` | `"Defect"` | [packages/core/src/types.ts:290](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L290) |
|
|
362
320
|
|
|
363
321
|
#### Type Parameters
|
|
364
322
|
|
|
@@ -375,7 +333,7 @@ The `Defect` variant of a [Result](#result): an unmodeled failure carrying a `ca
|
|
|
375
333
|
type ErrOf<R> = R extends object ? E : never;
|
|
376
334
|
```
|
|
377
335
|
|
|
378
|
-
Defined in: [packages/core/src/types.ts:456](https://github.com/btravstack/unthrown/blob/
|
|
336
|
+
Defined in: [packages/core/src/types.ts:456](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L456)
|
|
379
337
|
|
|
380
338
|
Extract the error type `E` from a `Result`.
|
|
381
339
|
|
|
@@ -393,7 +351,7 @@ Extract the error type `E` from a `Result`.
|
|
|
393
351
|
type ErrView<E, T> = ResultMethods<T, E> & object;
|
|
394
352
|
```
|
|
395
353
|
|
|
396
|
-
Defined in: [packages/core/src/types.ts:284](https://github.com/btravstack/unthrown/blob/
|
|
354
|
+
Defined in: [packages/core/src/types.ts:284](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L284)
|
|
397
355
|
|
|
398
356
|
The `Err` variant of a [Result](#result): a modeled failure carrying an `error`.
|
|
399
357
|
|
|
@@ -401,8 +359,8 @@ The `Err` variant of a [Result](#result): a modeled failure carrying an `error`.
|
|
|
401
359
|
|
|
402
360
|
| Name | Type | Defined in |
|
|
403
361
|
| ------ | ------ | ------ |
|
|
404
|
-
| `error` | `E` | [packages/core/src/types.ts:286](https://github.com/btravstack/unthrown/blob/
|
|
405
|
-
| `tag` | `"Err"` | [packages/core/src/types.ts:285](https://github.com/btravstack/unthrown/blob/
|
|
362
|
+
| `error` | `E` | [packages/core/src/types.ts:286](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L286) |
|
|
363
|
+
| `tag` | `"Err"` | [packages/core/src/types.ts:285](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L285) |
|
|
406
364
|
|
|
407
365
|
#### Type Parameters
|
|
408
366
|
|
|
@@ -419,7 +377,7 @@ The `Err` variant of a [Result](#result): a modeled failure carrying an `error`.
|
|
|
419
377
|
type OkOf<R> = R extends object ? T : never;
|
|
420
378
|
```
|
|
421
379
|
|
|
422
|
-
Defined in: [packages/core/src/types.ts:450](https://github.com/btravstack/unthrown/blob/
|
|
380
|
+
Defined in: [packages/core/src/types.ts:450](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L450)
|
|
423
381
|
|
|
424
382
|
Extract the success type `T` from a `Result`.
|
|
425
383
|
|
|
@@ -437,7 +395,7 @@ Extract the success type `T` from a `Result`.
|
|
|
437
395
|
type OkView<T, E> = ResultMethods<T, E> & object;
|
|
438
396
|
```
|
|
439
397
|
|
|
440
|
-
Defined in: [packages/core/src/types.ts:279](https://github.com/btravstack/unthrown/blob/
|
|
398
|
+
Defined in: [packages/core/src/types.ts:279](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L279)
|
|
441
399
|
|
|
442
400
|
The `Ok` variant of a [Result](#result): a success carrying a `value`.
|
|
443
401
|
|
|
@@ -445,8 +403,8 @@ The `Ok` variant of a [Result](#result): a success carrying a `value`.
|
|
|
445
403
|
|
|
446
404
|
| Name | Type | Defined in |
|
|
447
405
|
| ------ | ------ | ------ |
|
|
448
|
-
| `tag` | `"Ok"` | [packages/core/src/types.ts:280](https://github.com/btravstack/unthrown/blob/
|
|
449
|
-
| `value` | `T` | [packages/core/src/types.ts:281](https://github.com/btravstack/unthrown/blob/
|
|
406
|
+
| `tag` | `"Ok"` | [packages/core/src/types.ts:280](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L280) |
|
|
407
|
+
| `value` | `T` | [packages/core/src/types.ts:281](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L281) |
|
|
450
408
|
|
|
451
409
|
#### Type Parameters
|
|
452
410
|
|
|
@@ -463,15 +421,13 @@ The `Ok` variant of a [Result](#result): a success carrying a `value`.
|
|
|
463
421
|
type Result<T, E> = ResultType<T, E>;
|
|
464
422
|
```
|
|
465
423
|
|
|
466
|
-
Defined in: [packages/core/src/facade.ts:
|
|
424
|
+
Defined in: [packages/core/src/facade.ts:45](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L45)
|
|
467
425
|
|
|
468
|
-
Companion object grouping the
|
|
469
|
-
discoverable namespace: [Result.Ok](#property-ok), [Result.Err](#property-err),
|
|
470
|
-
[Result.
|
|
471
|
-
[Result.
|
|
472
|
-
[Result.
|
|
473
|
-
[Result.allFromDictAsync](#property-allfromdictasync), [Result.isOk](#property-isok), [Result.isErr](#property-iserr),
|
|
474
|
-
[Result.isDefect](#property-isdefect), [Result.isResult](#property-isresult).
|
|
426
|
+
Companion object grouping the **`Result`-producing** entry points under a
|
|
427
|
+
single, discoverable namespace: [Result.Ok](#property-ok), [Result.Err](#property-err),
|
|
428
|
+
[Result.Do](#property-do), [Result.fromNullable](#property-fromnullable), [Result.fromThrowable](#property-fromthrowable),
|
|
429
|
+
[Result.all](#property-all-1), [Result.allFromDict](#property-allfromdict-1), [Result.isOk](#property-isok),
|
|
430
|
+
[Result.isErr](#property-iserr), [Result.isDefect](#property-isdefect), [Result.isResult](#property-isresult).
|
|
475
431
|
|
|
476
432
|
#### Type Parameters
|
|
477
433
|
|
|
@@ -487,6 +443,10 @@ The free functions remain the primary, tree-shakeable API; importing only
|
|
|
487
443
|
`{ Ok }` never pulls this object in. The value `Result` and the type
|
|
488
444
|
[Result](#result-1) share one name (the companion-object pattern).
|
|
489
445
|
|
|
446
|
+
The **async** entry points live on the sibling [AsyncResult](#asyncresult-1) companion
|
|
447
|
+
(`AsyncResult.fromPromise`, `AsyncResult.all`, …), grouped by what they
|
|
448
|
+
return — a static lives in exactly one namespace.
|
|
449
|
+
|
|
490
450
|
#### Example
|
|
491
451
|
|
|
492
452
|
```ts
|
|
@@ -502,7 +462,7 @@ Result.Ok(1).flatMap((n) => Result.Ok(n + 1)).unwrap(); // 2
|
|
|
502
462
|
type TaggedErrorConstructor<Tag> = <A>(args) => TaggedErrorInstance<Tag, A>;
|
|
503
463
|
```
|
|
504
464
|
|
|
505
|
-
Defined in: [packages/core/src/tagged.ts:28](https://github.com/btravstack/unthrown/blob/
|
|
465
|
+
Defined in: [packages/core/src/tagged.ts:28](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/tagged.ts#L28)
|
|
506
466
|
|
|
507
467
|
The class constructor returned by [TaggedError](#taggederror). Generic in its payload:
|
|
508
468
|
apply it with an instantiation expression at the `extends` site.
|
|
@@ -536,7 +496,7 @@ When the payload is empty, the constructor takes **no** arguments (the
|
|
|
536
496
|
type TaggedErrorInstance<Tag, A> = Error & Readonly<A> & object;
|
|
537
497
|
```
|
|
538
498
|
|
|
539
|
-
Defined in: [packages/core/src/tagged.ts:15](https://github.com/btravstack/unthrown/blob/
|
|
499
|
+
Defined in: [packages/core/src/tagged.ts:15](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/tagged.ts#L15)
|
|
540
500
|
|
|
541
501
|
The instance shape produced by a [TaggedError](#taggederror) class: an `Error` plus a
|
|
542
502
|
`_tag` discriminant and the (readonly) payload fields.
|
|
@@ -545,7 +505,7 @@ The instance shape produced by a [TaggedError](#taggederror) class: an `Error` p
|
|
|
545
505
|
|
|
546
506
|
| Name | Type | Defined in |
|
|
547
507
|
| ------ | ------ | ------ |
|
|
548
|
-
| `_tag` | `Tag` | [packages/core/src/tagged.ts:16](https://github.com/btravstack/unthrown/blob/
|
|
508
|
+
| `_tag` | `Tag` | [packages/core/src/tagged.ts:16](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/tagged.ts#L16) |
|
|
549
509
|
|
|
550
510
|
#### Type Parameters
|
|
551
511
|
|
|
@@ -562,7 +522,7 @@ The instance shape produced by a [TaggedError](#taggederror) class: an `Error` p
|
|
|
562
522
|
type TagHandlers<T, E, R> = object & { [K in E["_tag"]]: (error: Extract<E, { _tag: K }>) => R };
|
|
563
523
|
```
|
|
564
524
|
|
|
565
|
-
Defined in: [packages/core/src/tagged.ts:103](https://github.com/btravstack/unthrown/blob/
|
|
525
|
+
Defined in: [packages/core/src/tagged.ts:103](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/tagged.ts#L103)
|
|
566
526
|
|
|
567
527
|
The handler object [matchTags](#matchtags) requires: a branch per error tag, plus
|
|
568
528
|
`Ok` and `Defect`. Miss a tag and it will not compile — the exhaustiveness is
|
|
@@ -572,8 +532,8 @@ enforced by the type, with no `.exhaustive()` to forget.
|
|
|
572
532
|
|
|
573
533
|
| Name | Type | Defined in |
|
|
574
534
|
| ------ | ------ | ------ |
|
|
575
|
-
| `Defect()` | (`cause`) => `R` | [packages/core/src/tagged.ts:105](https://github.com/btravstack/unthrown/blob/
|
|
576
|
-
| `Ok()` | (`value`) => `R` | [packages/core/src/tagged.ts:104](https://github.com/btravstack/unthrown/blob/
|
|
535
|
+
| `Defect()` | (`cause`) => `R` | [packages/core/src/tagged.ts:105](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/tagged.ts#L105) |
|
|
536
|
+
| `Ok()` | (`value`) => `R` | [packages/core/src/tagged.ts:104](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/tagged.ts#L104) |
|
|
577
537
|
|
|
578
538
|
#### Type Parameters
|
|
579
539
|
|
|
@@ -585,42 +545,76 @@ enforced by the type, with no `.exhaustive()` to forget.
|
|
|
585
545
|
|
|
586
546
|
## Variables
|
|
587
547
|
|
|
548
|
+
### AsyncResult
|
|
549
|
+
|
|
550
|
+
```ts
|
|
551
|
+
const AsyncResult: object;
|
|
552
|
+
```
|
|
553
|
+
|
|
554
|
+
Defined in: [packages/core/src/facade.ts:85](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L85)
|
|
555
|
+
|
|
556
|
+
Companion object grouping the **`AsyncResult`-producing** entry points under
|
|
557
|
+
the matching namespace: [AsyncResult.fromPromise](#property-frompromise),
|
|
558
|
+
[AsyncResult.fromSafePromise](#property-fromsafepromise), [AsyncResult.all](#property-all),
|
|
559
|
+
[AsyncResult.allFromDict](#property-allfromdict).
|
|
560
|
+
|
|
561
|
+
#### Type Declaration
|
|
562
|
+
|
|
563
|
+
| Name | Type | Default value | Defined in |
|
|
564
|
+
| ------ | ------ | ------ | ------ |
|
|
565
|
+
| <a id="property-all"></a> `all()` | <`Rs`>(`results`) => `AsyncResult`<`AllOk`<`Rs`, \{ \[K in string \| number \| symbol\]: AsyncOkOf\<Rs\[K\]\> \}>, [`AsyncErrOf`](#asyncerrof)<`Rs`\[`number`\]>> | `allAsync` | [packages/core/src/facade.ts:88](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L88) |
|
|
566
|
+
| <a id="property-allfromdict"></a> `allFromDict()` | <`R`>(`results`) => `AsyncResult`<\{ \[K in string \| number \| symbol\]: AsyncOkOf\<R\[K\]\> \}, [`AsyncErrOf`](#asyncerrof)<`R`\[keyof `R`\]>> | `allFromDictAsync` | [packages/core/src/facade.ts:89](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L89) |
|
|
567
|
+
| <a id="property-frompromise"></a> `fromPromise()` | <`T`, `R`>(`promise`, `qualify`) => `AsyncResult`<`T`, `Exclude`<`R`, `Defect`>> | - | [packages/core/src/facade.ts:86](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L86) |
|
|
568
|
+
| <a id="property-fromsafepromise"></a> `fromSafePromise()` | <`T`>(`promise`) => `AsyncResult`<`T`, `never`> | - | [packages/core/src/facade.ts:87](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L87) |
|
|
569
|
+
|
|
570
|
+
#### Remarks
|
|
571
|
+
|
|
572
|
+
The async sibling of [Result](#result-1). Statics are grouped by what they
|
|
573
|
+
**return**, so `fromPromise`/`fromSafePromise` and the async aggregates sit
|
|
574
|
+
here rather than on [Result](#result-1); the namespace already conveys "async", so
|
|
575
|
+
the aggregates drop the `Async` suffix (`AsyncResult.all` is the free function
|
|
576
|
+
`allAsync`; `AsyncResult.allFromDict` is `allFromDictAsync`). Like
|
|
577
|
+
[Result](#result-1), the free functions remain the primary, tree-shakeable API; the
|
|
578
|
+
value `AsyncResult` and the type [AsyncResult](#asyncresult-1) share one name.
|
|
579
|
+
|
|
580
|
+
#### Example
|
|
581
|
+
|
|
582
|
+
```ts
|
|
583
|
+
import { AsyncResult } from "unthrown";
|
|
584
|
+
const user = await AsyncResult.fromPromise(fetchUser(id), (c, defect) => defect(c));
|
|
585
|
+
```
|
|
586
|
+
|
|
587
|
+
***
|
|
588
|
+
|
|
588
589
|
### Result
|
|
589
590
|
|
|
590
591
|
```ts
|
|
591
592
|
const Result: object;
|
|
592
593
|
```
|
|
593
594
|
|
|
594
|
-
Defined in: [packages/core/src/facade.ts:
|
|
595
|
+
Defined in: [packages/core/src/facade.ts:45](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L45)
|
|
595
596
|
|
|
596
|
-
Companion object grouping the
|
|
597
|
-
discoverable namespace: [Result.Ok](#property-ok), [Result.Err](#property-err),
|
|
598
|
-
[Result.
|
|
599
|
-
[Result.
|
|
600
|
-
[Result.
|
|
601
|
-
[Result.allFromDictAsync](#property-allfromdictasync), [Result.isOk](#property-isok), [Result.isErr](#property-iserr),
|
|
602
|
-
[Result.isDefect](#property-isdefect), [Result.isResult](#property-isresult).
|
|
597
|
+
Companion object grouping the **`Result`-producing** entry points under a
|
|
598
|
+
single, discoverable namespace: [Result.Ok](#property-ok), [Result.Err](#property-err),
|
|
599
|
+
[Result.Do](#property-do), [Result.fromNullable](#property-fromnullable), [Result.fromThrowable](#property-fromthrowable),
|
|
600
|
+
[Result.all](#property-all-1), [Result.allFromDict](#property-allfromdict-1), [Result.isOk](#property-isok),
|
|
601
|
+
[Result.isErr](#property-iserr), [Result.isDefect](#property-isdefect), [Result.isResult](#property-isresult).
|
|
603
602
|
|
|
604
603
|
#### Type Declaration
|
|
605
604
|
|
|
606
605
|
| Name | Type | Defined in |
|
|
607
606
|
| ------ | ------ | ------ |
|
|
608
|
-
| <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:
|
|
609
|
-
| <a id="property-
|
|
610
|
-
| <a id="property-
|
|
611
|
-
| <a id="property-
|
|
612
|
-
| <a id="property-
|
|
613
|
-
| <a id="property-
|
|
614
|
-
| <a id="property-
|
|
615
|
-
| <a id="property-
|
|
616
|
-
| <a id="property-
|
|
617
|
-
| <a id="property-
|
|
618
|
-
| <a id="property-
|
|
619
|
-
| <a id="property-isdefect"></a> `isDefect()` | <`T`, `E`>(`r`) => `r is DefectView<T, E>` | [packages/core/src/facade.ts:59](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/facade.ts#L59) |
|
|
620
|
-
| <a id="property-iserr"></a> `isErr()` | <`T`, `E`>(`r`) => `r is ErrView<E, T>` | [packages/core/src/facade.ts:58](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/facade.ts#L58) |
|
|
621
|
-
| <a id="property-isok"></a> `isOk()` | <`T`, `E`>(`r`) => `r is OkView<T, E>` | [packages/core/src/facade.ts:57](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/facade.ts#L57) |
|
|
622
|
-
| <a id="property-isresult"></a> `isResult()` | (`x`) => `x is Result<unknown, unknown>` | [packages/core/src/facade.ts:60](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/facade.ts#L60) |
|
|
623
|
-
| <a id="property-ok"></a> `Ok()` | <`T`>(`value`) => `Result`<`T`, `never`> | [packages/core/src/facade.ts:45](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/facade.ts#L45) |
|
|
607
|
+
| <a id="property-all-1"></a> `all()` | <`Rs`>(`results`) => `Result`<`AllOk`<`Rs`, \{ \[K in string \| number \| symbol\]: OkOf\<Rs\[K\]\> \}>, [`ErrOf`](#errof)<`Rs`\[`number`\]>> | [packages/core/src/facade.ts:51](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L51) |
|
|
608
|
+
| <a id="property-allfromdict-1"></a> `allFromDict()` | <`R`>(`results`) => `Result`<\{ \[K in string \| number \| symbol\]: OkOf\<R\[K\]\> \}, [`ErrOf`](#errof)<`R`\[keyof `R`\]>> | [packages/core/src/facade.ts:52](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L52) |
|
|
609
|
+
| <a id="property-do"></a> `Do()` | () => `Result`<\{ \}, `never`> | [packages/core/src/facade.ts:48](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L48) |
|
|
610
|
+
| <a id="property-err"></a> `Err()` | <`E`>(`error`) => `Result`<`never`, `E`> | [packages/core/src/facade.ts:47](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L47) |
|
|
611
|
+
| <a id="property-fromnullable"></a> `fromNullable()` | <`T`, `E`>(`value`, `onAbsent`) => `Result`<`NonNullable`<`T`>, `E`> | [packages/core/src/facade.ts:49](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L49) |
|
|
612
|
+
| <a id="property-fromthrowable"></a> `fromThrowable()` | <`A`, `T`, `R`>(`fn`, `qualify`) => (...`args`) => `Result`<`T`, `Exclude`<`R`, `Defect`>> | [packages/core/src/facade.ts:50](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L50) |
|
|
613
|
+
| <a id="property-isdefect"></a> `isDefect()` | <`T`, `E`>(`r`) => `r is DefectView<T, E>` | [packages/core/src/facade.ts:55](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L55) |
|
|
614
|
+
| <a id="property-iserr"></a> `isErr()` | <`T`, `E`>(`r`) => `r is ErrView<E, T>` | [packages/core/src/facade.ts:54](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L54) |
|
|
615
|
+
| <a id="property-isok"></a> `isOk()` | <`T`, `E`>(`r`) => `r is OkView<T, E>` | [packages/core/src/facade.ts:53](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L53) |
|
|
616
|
+
| <a id="property-isresult"></a> `isResult()` | (`x`) => `x is Result<unknown, unknown>` | [packages/core/src/facade.ts:56](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L56) |
|
|
617
|
+
| <a id="property-ok"></a> `Ok()` | <`T`>(`value`) => `Result`<`T`, `never`> | [packages/core/src/facade.ts:46](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L46) |
|
|
624
618
|
|
|
625
619
|
#### Remarks
|
|
626
620
|
|
|
@@ -629,6 +623,10 @@ The free functions remain the primary, tree-shakeable API; importing only
|
|
|
629
623
|
`{ Ok }` never pulls this object in. The value `Result` and the type
|
|
630
624
|
[Result](#result-1) share one name (the companion-object pattern).
|
|
631
625
|
|
|
626
|
+
The **async** entry points live on the sibling [AsyncResult](#asyncresult-1) companion
|
|
627
|
+
(`AsyncResult.fromPromise`, `AsyncResult.all`, …), grouped by what they
|
|
628
|
+
return — a static lives in exactly one namespace.
|
|
629
|
+
|
|
632
630
|
#### Example
|
|
633
631
|
|
|
634
632
|
```ts
|
|
@@ -644,7 +642,7 @@ Result.Ok(1).flatMap((n) => Result.Ok(n + 1)).unwrap(); // 2
|
|
|
644
642
|
function all<Rs>(results): Result<AllOk<Rs, { [K in string | number | symbol]: OkOf<Rs[K]> }>, ErrOf<Rs[number]>>;
|
|
645
643
|
```
|
|
646
644
|
|
|
647
|
-
Defined in: [packages/core/src/interop.ts:
|
|
645
|
+
Defined in: [packages/core/src/interop.ts:252](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/interop.ts#L252)
|
|
648
646
|
|
|
649
647
|
Collect a tuple/array of [Result](#result)s into a single `Result` of all their
|
|
650
648
|
success values.
|
|
@@ -690,7 +688,7 @@ all([Ok(1), Ok(2)] as Result<number, never>[]).unwrap(); // number[]
|
|
|
690
688
|
function allAsync<Rs>(results): AsyncResult<AllOk<Rs, { [K in string | number | symbol]: AsyncOkOf<Rs[K]> }>, AsyncErrOf<Rs[number]>>;
|
|
691
689
|
```
|
|
692
690
|
|
|
693
|
-
Defined in: [packages/core/src/interop.ts:
|
|
691
|
+
Defined in: [packages/core/src/interop.ts:302](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/interop.ts#L302)
|
|
694
692
|
|
|
695
693
|
The asynchronous counterpart of [all](#all): combine a tuple/array of
|
|
696
694
|
[AsyncResult](#asyncresult)s into one `AsyncResult` of all their success values.
|
|
@@ -699,7 +697,7 @@ The asynchronous counterpart of [all](#all): combine a tuple/array of
|
|
|
699
697
|
|
|
700
698
|
| Type Parameter |
|
|
701
699
|
| ------ |
|
|
702
|
-
| `Rs` *extends* readonly
|
|
700
|
+
| `Rs` *extends* readonly `AsyncResult`<`unknown`, `unknown`>[] |
|
|
703
701
|
|
|
704
702
|
#### Parameters
|
|
705
703
|
|
|
@@ -709,7 +707,7 @@ The asynchronous counterpart of [all](#all): combine a tuple/array of
|
|
|
709
707
|
|
|
710
708
|
#### Returns
|
|
711
709
|
|
|
712
|
-
|
|
710
|
+
`AsyncResult`<`AllOk`<`Rs`, \{ \[K in string \| number \| symbol\]: AsyncOkOf\<Rs\[K\]\> \}>, [`AsyncErrOf`](#asyncerrof)<`Rs`\[`number`\]>>
|
|
713
711
|
|
|
714
712
|
#### Remarks
|
|
715
713
|
|
|
@@ -733,7 +731,7 @@ await allAsync([fromSafePromise(a()), fromSafePromise(b())]);
|
|
|
733
731
|
function allFromDict<R>(results): Result<{ [K in string | number | symbol]: OkOf<R[K]> }, ErrOf<R[keyof R]>>;
|
|
734
732
|
```
|
|
735
733
|
|
|
736
|
-
Defined in: [packages/core/src/interop.ts:
|
|
734
|
+
Defined in: [packages/core/src/interop.ts:277](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/interop.ts#L277)
|
|
737
735
|
|
|
738
736
|
Collect a **record** of [Result](#result)s into a single `Result` of a record of
|
|
739
737
|
their success values — `allFromDict({ a: Result<A, E>, b: Result<B, E> })` is
|
|
@@ -776,7 +774,7 @@ allFromDict({ id: Ok(1), name: Ok("ada") }).unwrap(); // { id: 1, name: "ada" }
|
|
|
776
774
|
function allFromDictAsync<R>(results): AsyncResult<{ [K in string | number | symbol]: AsyncOkOf<R[K]> }, AsyncErrOf<R[keyof R]>>;
|
|
777
775
|
```
|
|
778
776
|
|
|
779
|
-
Defined in: [packages/core/src/interop.ts:
|
|
777
|
+
Defined in: [packages/core/src/interop.ts:330](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/interop.ts#L330)
|
|
780
778
|
|
|
781
779
|
The asynchronous counterpart of [allFromDict](#allfromdict): combine a record of
|
|
782
780
|
[AsyncResult](#asyncresult)s into one `AsyncResult` of a record of their values.
|
|
@@ -795,7 +793,7 @@ The asynchronous counterpart of [allFromDict](#allfromdict): combine a record of
|
|
|
795
793
|
|
|
796
794
|
#### Returns
|
|
797
795
|
|
|
798
|
-
|
|
796
|
+
`AsyncResult`<\{ \[K in string \| number \| symbol\]: AsyncOkOf\<R\[K\]\> \}, [`AsyncErrOf`](#asyncerrof)<`R`\[keyof `R`\]>>
|
|
799
797
|
|
|
800
798
|
#### Remarks
|
|
801
799
|
|
|
@@ -811,41 +809,6 @@ await allFromDictAsync({ a: fromSafePromise(a()), b: fromSafePromise(b()) });
|
|
|
811
809
|
|
|
812
810
|
***
|
|
813
811
|
|
|
814
|
-
### Defect()
|
|
815
|
-
|
|
816
|
-
```ts
|
|
817
|
-
function Defect(cause): Defect;
|
|
818
|
-
```
|
|
819
|
-
|
|
820
|
-
Defined in: [packages/core/src/defect.ts:36](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/core/src/defect.ts#L36)
|
|
821
|
-
|
|
822
|
-
Wrap a cause as a [Defect](#defect-2) — the value you return from a `qualify`
|
|
823
|
-
function when a failure is **not** a modeled domain error.
|
|
824
|
-
|
|
825
|
-
#### Parameters
|
|
826
|
-
|
|
827
|
-
| Parameter | Type | Description |
|
|
828
|
-
| ------ | ------ | ------ |
|
|
829
|
-
| `cause` | `unknown` | the original thrown/rejected value. |
|
|
830
|
-
|
|
831
|
-
#### Returns
|
|
832
|
-
|
|
833
|
-
[`Defect`](#defect)
|
|
834
|
-
|
|
835
|
-
an opaque Defect marker carrying `cause`.
|
|
836
|
-
|
|
837
|
-
#### Example
|
|
838
|
-
|
|
839
|
-
```ts
|
|
840
|
-
import { fromPromise, Defect } from "unthrown";
|
|
841
|
-
|
|
842
|
-
const user = fromPromise(fetchUser(id), (cause) =>
|
|
843
|
-
cause instanceof NotFoundError ? cause : Defect(cause),
|
|
844
|
-
);
|
|
845
|
-
```
|
|
846
|
-
|
|
847
|
-
***
|
|
848
|
-
|
|
849
812
|
### Do()
|
|
850
813
|
|
|
851
814
|
```ts
|
|
@@ -853,7 +816,7 @@ function Do(): Result<{
|
|
|
853
816
|
}, never>;
|
|
854
817
|
```
|
|
855
818
|
|
|
856
|
-
Defined in: [packages/core/src/do.ts:30](https://github.com/btravstack/unthrown/blob/
|
|
819
|
+
Defined in: [packages/core/src/do.ts:30](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/do.ts#L30)
|
|
857
820
|
|
|
858
821
|
Start a do-notation chain with an empty object scope, grown step by step with
|
|
859
822
|
`bind` (for `Result`-returning steps) and `let` (for pure values).
|
|
@@ -891,7 +854,7 @@ const result = Do()
|
|
|
891
854
|
function Err<E>(error): Result<never, E>;
|
|
892
855
|
```
|
|
893
856
|
|
|
894
|
-
Defined in: [packages/core/src/constructors.ts:34](https://github.com/btravstack/unthrown/blob/
|
|
857
|
+
Defined in: [packages/core/src/constructors.ts:34](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/constructors.ts#L34)
|
|
895
858
|
|
|
896
859
|
Construct a failed [Result](#result) carrying a **modeled** error.
|
|
897
860
|
|
|
@@ -926,7 +889,7 @@ Err("not_found").unwrapErr(); // "not_found"
|
|
|
926
889
|
function fromNullable<T, E>(value, onAbsent): Result<NonNullable<T>, E>;
|
|
927
890
|
```
|
|
928
891
|
|
|
929
|
-
Defined in: [packages/core/src/interop.ts:29](https://github.com/btravstack/unthrown/blob/
|
|
892
|
+
Defined in: [packages/core/src/interop.ts:29](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/interop.ts#L29)
|
|
930
893
|
|
|
931
894
|
Bridge a nullable value into a [Result](#result): absence becomes a **modeled**
|
|
932
895
|
`Err`. The sanctioned alternative to an `Option` type.
|
|
@@ -969,7 +932,7 @@ fromNullable(map.get(key), () => "missing").unwrap();
|
|
|
969
932
|
function fromPromise<T, R>(promise, qualify): AsyncResult<T, Exclude<R, Defect>>;
|
|
970
933
|
```
|
|
971
934
|
|
|
972
|
-
Defined in: [packages/core/src/interop.ts:
|
|
935
|
+
Defined in: [packages/core/src/interop.ts:113](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/interop.ts#L113)
|
|
973
936
|
|
|
974
937
|
Wrap a `Promise` (or a thunk producing one) as an [AsyncResult](#asyncresult), forcing
|
|
975
938
|
every rejection to be triaged.
|
|
@@ -986,30 +949,30 @@ every rejection to be triaged.
|
|
|
986
949
|
| Parameter | Type | Description |
|
|
987
950
|
| ------ | ------ | ------ |
|
|
988
951
|
| `promise` | `Promise`<`T`> \| (() => `Promise`<`T`>) | the promise, or a thunk returning one. |
|
|
989
|
-
| `qualify` | (`cause`) => `R` | triages a rejection cause into `E
|
|
952
|
+
| `qualify` | (`cause`, `defect`) => `R` | triages a rejection `cause` into a modeled `E`, or marks it unmodeled by returning `defect(cause)` (the helper passed as its second arg). |
|
|
990
953
|
|
|
991
954
|
#### Returns
|
|
992
955
|
|
|
993
|
-
|
|
956
|
+
`AsyncResult`<`T`, `Exclude`<`R`, `Defect`>>
|
|
994
957
|
|
|
995
958
|
#### Remarks
|
|
996
959
|
|
|
997
960
|
`qualify` **must** map each rejection cause into a modeled error `E` or a
|
|
998
|
-
|
|
999
|
-
`await`-ing it always yields a
|
|
1000
|
-
`Defect`.
|
|
961
|
+
`Defect` (via the injected `defect` helper, its second argument). The returned
|
|
962
|
+
`AsyncResult`'s internal promise never rejects; `await`-ing it always yields a
|
|
963
|
+
`Result`. A throw inside `qualify` is itself a `Defect`.
|
|
1001
964
|
|
|
1002
965
|
The modeled error type is `Exclude<R, Defect>` — the `Defect` arm of
|
|
1003
966
|
`qualify`'s return is **subtracted** from `E`, never inferred into it. So a
|
|
1004
|
-
`qualify` that returns *only* `
|
|
967
|
+
`qualify` that returns *only* `defect(cause)` yields `E = never`; when every
|
|
1005
968
|
rejection is a Defect, prefer [fromSafePromise](#fromsafepromise).
|
|
1006
969
|
|
|
1007
970
|
#### Example
|
|
1008
971
|
|
|
1009
972
|
```ts
|
|
1010
|
-
import { fromPromise
|
|
1011
|
-
const user = await fromPromise(fetchUser(id), (cause) =>
|
|
1012
|
-
cause instanceof NotFoundError ? ("not_found" as const) :
|
|
973
|
+
import { fromPromise } from "unthrown";
|
|
974
|
+
const user = await fromPromise(fetchUser(id), (cause, defect) =>
|
|
975
|
+
cause instanceof NotFoundError ? ("not_found" as const) : defect(cause),
|
|
1013
976
|
);
|
|
1014
977
|
```
|
|
1015
978
|
|
|
@@ -1021,7 +984,7 @@ const user = await fromPromise(fetchUser(id), (cause) =>
|
|
|
1021
984
|
function fromSafePromise<T>(promise): AsyncResult<T, never>;
|
|
1022
985
|
```
|
|
1023
986
|
|
|
1024
|
-
Defined in: [packages/core/src/interop.ts:
|
|
987
|
+
Defined in: [packages/core/src/interop.ts:139](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/interop.ts#L139)
|
|
1025
988
|
|
|
1026
989
|
Wrap a `Promise` asserted **not** to fail in any modeled way: any rejection
|
|
1027
990
|
becomes a `Defect`.
|
|
@@ -1040,7 +1003,7 @@ becomes a `Defect`.
|
|
|
1040
1003
|
|
|
1041
1004
|
#### Returns
|
|
1042
1005
|
|
|
1043
|
-
|
|
1006
|
+
`AsyncResult`<`T`, `never`>
|
|
1044
1007
|
|
|
1045
1008
|
#### Remarks
|
|
1046
1009
|
|
|
@@ -1056,7 +1019,7 @@ triage. (`await`-ing still yields a `Result`; it never throws.)
|
|
|
1056
1019
|
function fromThrowable<A, T, R>(fn, qualify): (...args) => Result<T, Exclude<R, Defect>>;
|
|
1057
1020
|
```
|
|
1058
1021
|
|
|
1059
|
-
Defined in: [packages/core/src/interop.ts:
|
|
1022
|
+
Defined in: [packages/core/src/interop.ts:68](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/interop.ts#L68)
|
|
1060
1023
|
|
|
1061
1024
|
Wrap a throwing synchronous function so it returns a [Result](#result) instead of
|
|
1062
1025
|
throwing.
|
|
@@ -1074,31 +1037,32 @@ throwing.
|
|
|
1074
1037
|
| Parameter | Type | Description |
|
|
1075
1038
|
| ------ | ------ | ------ |
|
|
1076
1039
|
| `fn` | (...`args`) => `T` | the throwing function to wrap. |
|
|
1077
|
-
| `qualify` | (`cause`) => `R` | triages a thrown cause into `E
|
|
1040
|
+
| `qualify` | (`cause`, `defect`) => `R` | triages a thrown `cause` into a modeled `E`, or marks it unmodeled by returning `defect(cause)` (the helper passed as its second arg). |
|
|
1078
1041
|
|
|
1079
1042
|
#### Returns
|
|
1080
1043
|
|
|
1081
1044
|
a function with the same arguments returning `Result<T, E>`.
|
|
1082
1045
|
|
|
1083
|
-
(...`args`) => `Result`<`T`, `Exclude`<`R`,
|
|
1046
|
+
(...`args`) => `Result`<`T`, `Exclude`<`R`, `Defect`>>
|
|
1084
1047
|
|
|
1085
1048
|
#### Remarks
|
|
1086
1049
|
|
|
1087
1050
|
`qualify` **must** triage every thrown cause into a modeled error `E` or a
|
|
1088
|
-
|
|
1089
|
-
in `E`. A throw inside `qualify` itself is treated
|
|
1051
|
+
`Defect` (via the injected `defect` helper, its second argument) — there is no
|
|
1052
|
+
path that leaves `unknown` in `E`. A throw inside `qualify` itself is treated
|
|
1053
|
+
as a `Defect`.
|
|
1090
1054
|
|
|
1091
1055
|
The modeled error type is `Exclude<R, Defect>` — the `Defect` arm of
|
|
1092
1056
|
`qualify`'s return is **subtracted** from `E`, never inferred into it. So a
|
|
1093
|
-
`qualify` that returns *only* `
|
|
1057
|
+
`qualify` that returns *only* `defect(cause)` yields `E = never` (a Defect is
|
|
1094
1058
|
out-of-band and must not pollute the error channel); reach for
|
|
1095
1059
|
[fromSafePromise](#fromsafepromise) when every failure is a Defect.
|
|
1096
1060
|
|
|
1097
1061
|
#### Example
|
|
1098
1062
|
|
|
1099
1063
|
```ts
|
|
1100
|
-
import { fromThrowable
|
|
1101
|
-
const parse = fromThrowable(JSON.parse, (cause) =>
|
|
1064
|
+
import { fromThrowable } from "unthrown";
|
|
1065
|
+
const parse = fromThrowable(JSON.parse, (cause, defect) => defect(cause));
|
|
1102
1066
|
parse("{}").unwrap();
|
|
1103
1067
|
```
|
|
1104
1068
|
|
|
@@ -1110,7 +1074,7 @@ parse("{}").unwrap();
|
|
|
1110
1074
|
function isDefect<T, E>(r): r is DefectView<T, E>;
|
|
1111
1075
|
```
|
|
1112
1076
|
|
|
1113
|
-
Defined in: [packages/core/src/constructors.ts:66](https://github.com/btravstack/unthrown/blob/
|
|
1077
|
+
Defined in: [packages/core/src/constructors.ts:66](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/constructors.ts#L66)
|
|
1114
1078
|
|
|
1115
1079
|
Type guard: narrow a [Result](#result) to its `Defect` variant, exposing `.cause`.
|
|
1116
1080
|
|
|
@@ -1141,7 +1105,7 @@ Type guard: narrow a [Result](#result) to its `Defect` variant, exposing `.cause
|
|
|
1141
1105
|
function isErr<T, E>(r): r is ErrView<E, T>;
|
|
1142
1106
|
```
|
|
1143
1107
|
|
|
1144
|
-
Defined in: [packages/core/src/constructors.ts:58](https://github.com/btravstack/unthrown/blob/
|
|
1108
|
+
Defined in: [packages/core/src/constructors.ts:58](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/constructors.ts#L58)
|
|
1145
1109
|
|
|
1146
1110
|
Type guard: narrow a [Result](#result) to its `Err` variant, exposing `.error`.
|
|
1147
1111
|
|
|
@@ -1172,7 +1136,7 @@ Type guard: narrow a [Result](#result) to its `Err` variant, exposing `.error`.
|
|
|
1172
1136
|
function isOk<T, E>(r): r is OkView<T, E>;
|
|
1173
1137
|
```
|
|
1174
1138
|
|
|
1175
|
-
Defined in: [packages/core/src/constructors.ts:50](https://github.com/btravstack/unthrown/blob/
|
|
1139
|
+
Defined in: [packages/core/src/constructors.ts:50](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/constructors.ts#L50)
|
|
1176
1140
|
|
|
1177
1141
|
Type guard: narrow a [Result](#result) to its `Ok` variant, exposing `.value`.
|
|
1178
1142
|
|
|
@@ -1211,7 +1175,7 @@ if (isOk(r)) r.value; // number, narrowed
|
|
|
1211
1175
|
function isResult(x): x is Result<unknown, unknown>;
|
|
1212
1176
|
```
|
|
1213
1177
|
|
|
1214
|
-
Defined in: [packages/core/src/core.ts:
|
|
1178
|
+
Defined in: [packages/core/src/core.ts:338](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/core.ts#L338)
|
|
1215
1179
|
|
|
1216
1180
|
Type guard: is `x` a [Result](#result) (any of `Ok` / `Err` / `Defect`)?
|
|
1217
1181
|
|
|
@@ -1245,7 +1209,7 @@ is not a `Result` and returns `false`.
|
|
|
1245
1209
|
function matchTags<T, E, R>(result, handlers): R;
|
|
1246
1210
|
```
|
|
1247
1211
|
|
|
1248
|
-
Defined in: [packages/core/src/tagged.ts:138](https://github.com/btravstack/unthrown/blob/
|
|
1212
|
+
Defined in: [packages/core/src/tagged.ts:138](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/tagged.ts#L138)
|
|
1249
1213
|
|
|
1250
1214
|
Exhaustively fold a [Result](#result) (or [AsyncResult](#asyncresult)) whose error type is
|
|
1251
1215
|
a tagged union, dispatching each error to the handler matching its `_tag`.
|
|
@@ -1297,7 +1261,7 @@ matchTags(r, {
|
|
|
1297
1261
|
function matchTags<T, E, R>(result, handlers): Promise<R>;
|
|
1298
1262
|
```
|
|
1299
1263
|
|
|
1300
|
-
Defined in: [packages/core/src/tagged.ts:142](https://github.com/btravstack/unthrown/blob/
|
|
1264
|
+
Defined in: [packages/core/src/tagged.ts:142](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/tagged.ts#L142)
|
|
1301
1265
|
|
|
1302
1266
|
Exhaustively fold a [Result](#result) (or [AsyncResult](#asyncresult)) whose error type is
|
|
1303
1267
|
a tagged union, dispatching each error to the handler matching its `_tag`.
|
|
@@ -1314,7 +1278,7 @@ a tagged union, dispatching each error to the handler matching its `_tag`.
|
|
|
1314
1278
|
|
|
1315
1279
|
| Parameter | Type | Description |
|
|
1316
1280
|
| ------ | ------ | ------ |
|
|
1317
|
-
| `result` |
|
|
1281
|
+
| `result` | `AsyncResult`<`T`, `E`> | the result to fold. |
|
|
1318
1282
|
| `handlers` | [`TagHandlers`](#taghandlers)<`T`, `E`, `R`> | one branch per channel/tag. |
|
|
1319
1283
|
|
|
1320
1284
|
##### Returns
|
|
@@ -1351,7 +1315,7 @@ matchTags(r, {
|
|
|
1351
1315
|
function Ok<T>(value): Result<T, never>;
|
|
1352
1316
|
```
|
|
1353
1317
|
|
|
1354
|
-
Defined in: [packages/core/src/constructors.ts:18](https://github.com/btravstack/unthrown/blob/
|
|
1318
|
+
Defined in: [packages/core/src/constructors.ts:18](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/constructors.ts#L18)
|
|
1355
1319
|
|
|
1356
1320
|
Construct a successful [Result](#result).
|
|
1357
1321
|
|
|
@@ -1386,7 +1350,7 @@ Ok(42).unwrap(); // 42
|
|
|
1386
1350
|
function TaggedError<Tag>(tag, options?): TaggedErrorConstructor<Tag>;
|
|
1387
1351
|
```
|
|
1388
1352
|
|
|
1389
|
-
Defined in: [packages/core/src/tagged.ts:72](https://github.com/btravstack/unthrown/blob/
|
|
1353
|
+
Defined in: [packages/core/src/tagged.ts:72](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/tagged.ts#L72)
|
|
1390
1354
|
|
|
1391
1355
|
Build a base class for a tagged error — a class extending `Error` with a
|
|
1392
1356
|
`_tag` string discriminant, in the style of Effect's `Data.TaggedError`.
|