@onrails/result 0.4.0 → 0.4.1
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/DESIGN.md +1 -1
- package/README.md +165 -12
- package/RECIPES.md +6 -4
- package/dist/async-fns.cjs +52 -0
- package/dist/async-fns.cjs.map +1 -0
- package/dist/async-fns.d.cts +143 -0
- package/dist/async-fns.d.ts +143 -0
- package/dist/async-fns.js +42 -0
- package/dist/async-fns.js.map +1 -0
- package/dist/async-lift.d.cts +109 -0
- package/dist/async-lift.d.ts +109 -0
- package/dist/{async-Ci6WSL5-.d.ts → async.d.cts} +42 -58
- package/dist/{async-DcXR7k9K.d.cts → async.d.ts} +42 -58
- package/dist/collections.d.cts +105 -0
- package/dist/collections.d.ts +105 -0
- package/dist/compat/neverthrow.cjs +57 -45
- package/dist/compat/neverthrow.cjs.map +1 -1
- package/dist/compat/neverthrow.d.cts +21 -24
- package/dist/compat/neverthrow.d.ts +21 -24
- package/dist/compat/neverthrow.js +56 -44
- package/dist/compat/neverthrow.js.map +1 -1
- package/dist/extra.cjs +3 -1
- package/dist/extra.cjs.map +1 -1
- package/dist/extra.d.cts +11 -14
- package/dist/extra.d.ts +11 -14
- package/dist/extra.js +3 -1
- package/dist/extra.js.map +1 -1
- package/dist/fluent.cjs +15 -13
- package/dist/fluent.cjs.map +1 -1
- package/dist/fluent.d.cts +4 -7
- package/dist/fluent.d.ts +4 -7
- package/dist/fluent.js +15 -13
- package/dist/fluent.js.map +1 -1
- package/dist/index.cjs +77 -52
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +28 -490
- package/dist/index.d.ts +28 -490
- package/dist/index.js +77 -53
- package/dist/index.js.map +1 -1
- package/dist/internal/ctor.d.cts +12 -0
- package/dist/internal/ctor.d.ts +12 -0
- package/dist/internal/dual.d.cts +42 -0
- package/dist/internal/dual.d.ts +42 -0
- package/dist/internal/infer.d.cts +40 -0
- package/dist/internal/infer.d.ts +40 -0
- package/dist/internal/lock.d.cts +16 -0
- package/dist/internal/lock.d.ts +16 -0
- package/dist/internal/to-error.d.cts +2 -0
- package/dist/internal/to-error.d.ts +2 -0
- package/dist/pipe.cjs +8 -0
- package/dist/pipe.cjs.map +1 -1
- package/dist/pipe.d.cts +58 -11
- package/dist/pipe.d.ts +58 -11
- package/dist/pipe.js +8 -1
- package/dist/pipe.js.map +1 -1
- package/dist/railway.cjs +87 -63
- package/dist/railway.cjs.map +1 -1
- package/dist/railway.d.cts +49 -19
- package/dist/railway.d.ts +49 -19
- package/dist/railway.js +87 -63
- package/dist/railway.js.map +1 -1
- package/dist/result.d.cts +315 -0
- package/dist/result.d.ts +315 -0
- package/dist/try-gen.cjs +3 -3
- package/dist/try-gen.cjs.map +1 -1
- package/dist/try-gen.d.cts +4 -7
- package/dist/try-gen.d.ts +4 -7
- package/dist/try-gen.js +3 -3
- package/dist/try-gen.js.map +1 -1
- package/dist/{types-BQ9vv0nD.d.cts → types.d.cts} +6 -8
- package/dist/{types-BQ9vv0nD.d.ts → types.d.ts} +6 -8
- package/package.json +69 -24
- package/skills/railway-do-notation/SKILL.md +9 -9
package/dist/index.d.ts
CHANGED
|
@@ -1,491 +1,29 @@
|
|
|
1
|
-
import { R as ResultAsync, I as InferErr, a as InferOk } from './async-Ci6WSL5-.js';
|
|
2
|
-
import { R as Result, U as UnexpectedError, E as Err, O as Ok } from './types-BQ9vv0nD.js';
|
|
3
|
-
export { flow } from './pipe.js';
|
|
4
|
-
export { $, tryGen, yieldResult } from './try-gen.js';
|
|
5
|
-
|
|
6
1
|
/**
|
|
7
|
-
* @onrails/result —
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
* @example
|
|
36
|
-
* ```ts
|
|
37
|
-
* const body = fromPromise(
|
|
38
|
-
* fetch(url).then((r) => r.text()),
|
|
39
|
-
* (e): NetworkError => ({ kind: "network", cause: String(e) }),
|
|
40
|
-
* );
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
declare const fromPromise: typeof ResultAsync.fromPromise;
|
|
44
|
-
/**
|
|
45
|
-
* Wraps a `PromiseLike<T>` that **never rejects** into {@link ResultAsync}.
|
|
46
|
-
* Skips the `onReject` mapper. Use only when the promise is provably safe.
|
|
47
|
-
*/
|
|
48
|
-
declare const fromSafePromise: typeof ResultAsync.fromSafePromise;
|
|
49
|
-
/**
|
|
50
|
-
* Lifts an already-settled sync {@link Result} into a {@link ResultAsync}, so
|
|
51
|
-
* it can be chained alongside async steps in a railway.
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* ```ts
|
|
55
|
-
* const ra = fromResult(ok(1)); // ResultAsync<number, never>
|
|
56
|
-
* await fromResult(err(error)).resolve(); // Err(error)
|
|
57
|
-
* ```
|
|
58
|
-
*/
|
|
59
|
-
declare const fromResult: typeof ResultAsync.fromResult;
|
|
60
|
-
/**
|
|
61
|
-
* Bind a sync {@link Result} into an async step without widening defects.
|
|
62
|
-
* Dual-form: data-first for one-shots, data-last (curried) for `pipe`/`flow`.
|
|
63
|
-
*
|
|
64
|
-
* @example
|
|
65
|
-
* ```ts
|
|
66
|
-
* asyncAfter(result, (u) => tryAsync(db.insert(u), toErr)); // data-first
|
|
67
|
-
* asyncAfter((u) => tryAsync(db.insert(u), toErr)); // data-last
|
|
68
|
-
* ```
|
|
69
|
-
*/
|
|
70
|
-
declare const asyncAfter: {
|
|
71
|
-
<T, U, E, F>(result: Result<T, E>, fn: (value: T) => ResultAsync<U, F>): ResultAsync<U, E | F>;
|
|
72
|
-
<T, U, F>(fn: (value: T) => ResultAsync<U, F>): <E>(result: Result<T, E>) => ResultAsync<U, E | F>;
|
|
73
|
-
};
|
|
74
|
-
type AnyResult = Result<unknown, unknown>;
|
|
75
|
-
/**
|
|
76
|
-
* Lifts a `(...args) => Promise<Result<T, E>>` function (typical of interop or
|
|
77
|
-
* boundary code) into one returning `(...args) => ResultAsync<T, E>`. Expected
|
|
78
|
-
* `Err`s pass through; an unexpected promise rejection (a defect) is routed via
|
|
79
|
-
* `onDefect`, defaulting to {@link UnexpectedError} and widening the error union.
|
|
80
|
-
*
|
|
81
|
-
* @param fn - a function returning a promise that already yields a `Result`
|
|
82
|
-
* @param onDefect - maps an unexpected rejection to the `Err` channel
|
|
83
|
-
*
|
|
84
|
-
* @example
|
|
85
|
-
* ```ts
|
|
86
|
-
* const loadUser = fromAsync(
|
|
87
|
-
* (id: string): Promise<Result<User, NotFound>> => api.getUser(id),
|
|
88
|
-
* );
|
|
89
|
-
* const ra = loadUser("u1"); // ResultAsync<User, NotFound | UnexpectedError>
|
|
90
|
-
* ```
|
|
91
|
-
*/
|
|
92
|
-
declare const fromAsync: <A extends readonly unknown[], R extends AnyResult>(fn: (...args: A) => Promise<R>, onDefect?: (error: unknown) => InferErr<R> | UnexpectedError) => ((...args: A) => ResultAsync<InferOk<R>, InferErr<R> | UnexpectedError>);
|
|
93
|
-
/**
|
|
94
|
-
* Convenience wrapper over {@link fromPromise} with default `Error`
|
|
95
|
-
* normalization. Call without `onReject` to get `ResultAsync<T, Error>`,
|
|
96
|
-
* or pass a custom mapper for a typed error.
|
|
97
|
-
*
|
|
98
|
-
* @example
|
|
99
|
-
* ```ts
|
|
100
|
-
* // Default: rejection → Err(Error)
|
|
101
|
-
* const a = tryAsync(db.users.insert(row));
|
|
102
|
-
*
|
|
103
|
-
* // Custom: typed error
|
|
104
|
-
* const b = tryAsync(db.users.insert(row), (e): DbError => ({
|
|
105
|
-
* kind: "db",
|
|
106
|
-
* cause: e,
|
|
107
|
-
* }));
|
|
108
|
-
* ```
|
|
109
|
-
*/
|
|
110
|
-
declare function tryAsync<T>(promise: PromiseLike<T>): ResultAsync<T, Error>;
|
|
111
|
-
declare function tryAsync<T, E>(promise: PromiseLike<T>, onReject: (error: unknown) => E): ResultAsync<T, E>;
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* @onrails/result — sync aggregation, one module, two axes:
|
|
115
|
-
*
|
|
116
|
-
* short-circuit (first `Err` wins) — {@link combine} / {@link combineTuple}
|
|
117
|
-
* accumulate expected failures — {@link validateAll} / {@link validateTuple}
|
|
118
|
-
*
|
|
119
|
-
* The accumulate pair collects every failure into a readonly array by
|
|
120
|
-
* default, or folds them with an explicit `combineErrors` when given one.
|
|
121
|
-
*/
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Combines a homogeneous array of results into a single result holding an
|
|
125
|
-
* array of the `Ok` values, in input order. Short-circuits on the first `Err`
|
|
126
|
-
* (first failure wins). For heterogeneous tuples that preserve per-index
|
|
127
|
-
* types, use {@link combineTuple}.
|
|
128
|
-
*
|
|
129
|
-
* @example
|
|
130
|
-
* ```ts
|
|
131
|
-
* combine([ok(1), ok(2)]); // Ok([1, 2])
|
|
132
|
-
* combine([ok(1), err("e")]); // Err("e")
|
|
133
|
-
* ```
|
|
134
|
-
*/
|
|
135
|
-
declare const combine: <T, E>(results: readonly Result<T, E>[]) => Result<T[], E>;
|
|
136
|
-
type CombineTuple<R extends readonly Result<unknown, unknown>[]> = Result<{
|
|
137
|
-
[K in keyof R]: InferOk<R[K]>;
|
|
138
|
-
}, {
|
|
139
|
-
[K in keyof R]: InferErr<R[K]>;
|
|
140
|
-
}[number]>;
|
|
141
|
-
/**
|
|
142
|
-
* Heterogeneous tuple combine — like {@link combine} but preserves each
|
|
143
|
-
* branch's `Ok` type by position, so the result destructures type-safely.
|
|
144
|
-
* Short-circuits on the first `Err` in input order (neverthrow-style).
|
|
145
|
-
*
|
|
146
|
-
* @example
|
|
147
|
-
* ```ts
|
|
148
|
-
* const r = combineTuple([ok(1), ok("x")] as const);
|
|
149
|
-
* // Result<readonly [number, string], never>
|
|
150
|
-
* if (isOk(r)) {
|
|
151
|
-
* const [n, s] = r.value; // typed per position
|
|
152
|
-
* }
|
|
153
|
-
* ```
|
|
154
|
-
*/
|
|
155
|
-
declare const combineTuple: <const R extends readonly Result<unknown, unknown>[]>(results: R) => CombineTuple<R>;
|
|
156
|
-
/**
|
|
157
|
-
* Accumulate independent validation failures. Returns `Ok<T[]>` only when
|
|
158
|
-
* every input is `Ok`. Without a combiner, failures are collected into a
|
|
159
|
-
* readonly array; with `combineErrors` they are folded into a single `E`.
|
|
160
|
-
*
|
|
161
|
-
* Unlike {@link combine}, this does **not** short-circuit on first failure —
|
|
162
|
-
* use for independent checks where you want to report all problems at once.
|
|
163
|
-
*
|
|
164
|
-
* @example
|
|
165
|
-
* ```ts
|
|
166
|
-
* const checks: Result<string, string[]>[] = [ok("Ada"), err(["age required"])];
|
|
167
|
-
*
|
|
168
|
-
* validateAll(checks);
|
|
169
|
-
* // Result<string[], readonly string[][]>
|
|
170
|
-
*
|
|
171
|
-
* validateAll(checks, (left, right) => [...left, ...right]);
|
|
172
|
-
* // Result<string[], string[]>
|
|
173
|
-
* ```
|
|
174
|
-
*/
|
|
175
|
-
declare function validateAll<T, E>(results: readonly Result<T, E>[]): Result<T[], readonly E[]>;
|
|
176
|
-
declare function validateAll<T, E>(results: readonly Result<T, E>[], combineErrors: (left: E, right: E) => E): Result<T[], E>;
|
|
177
|
-
/**
|
|
178
|
-
* Tuple-preserving variant of {@link validateAll}. Heterogeneous input tuple
|
|
179
|
-
* → preserved `Ok` tuple shape. Without a combiner, the error union is
|
|
180
|
-
* collected into a readonly array; with `combineErrors`, all inputs must
|
|
181
|
-
* share the error type `E` and failures fold into a single `E`.
|
|
182
|
-
*
|
|
183
|
-
* @example
|
|
184
|
-
* ```ts
|
|
185
|
-
* const name: Result<string, string[]> = ok("Ada");
|
|
186
|
-
* const age: Result<number, string[]> = ok(36);
|
|
187
|
-
*
|
|
188
|
-
* validateTuple([name, age] as const);
|
|
189
|
-
* // Result<readonly [string, number], readonly string[][]>
|
|
190
|
-
*
|
|
191
|
-
* validateTuple([name, age] as const, (l, r) => [...l, ...r]);
|
|
192
|
-
* // Result<readonly [string, number], string[]>
|
|
193
|
-
* ```
|
|
194
|
-
*/
|
|
195
|
-
declare function validateTuple<const R extends readonly Result<unknown, unknown>[]>(results: R): Result<{
|
|
196
|
-
[K in keyof R]: InferOk<R[K]>;
|
|
197
|
-
}, readonly InferErr<R[number]>[]>;
|
|
198
|
-
declare function validateTuple<const R extends readonly Result<unknown, unknown>[], E = InferErr<R[number]>>(results: R & readonly Result<unknown, E>[], combineErrors: (left: E, right: E) => E): Result<{
|
|
199
|
-
[K in keyof R]: InferOk<R[K]>;
|
|
200
|
-
}, E>;
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Lifts a value into the success track.
|
|
204
|
-
*
|
|
205
|
-
* @example
|
|
206
|
-
* ```ts
|
|
207
|
-
* const r = ok(42); // Result<number, never>
|
|
208
|
-
* const typed: Result<number, "parse"> = ok(1);
|
|
209
|
-
* ```
|
|
210
|
-
*/
|
|
211
|
-
declare const ok: <T, E = never>(value: T) => Result<T, E>;
|
|
212
|
-
/**
|
|
213
|
-
* Fantasy Land `pure` — alias of {@link ok}. One lift name shared across the
|
|
214
|
-
* trio (`of` / `Maybe.of` / `ResultAsync.of`) for generic and FL-style code.
|
|
215
|
-
*
|
|
216
|
-
* @example
|
|
217
|
-
* ```ts
|
|
218
|
-
* const r = of(42); // Result<number, never> — identical to ok(42)
|
|
219
|
-
* ```
|
|
220
|
-
*/
|
|
221
|
-
declare const of: <T, E = never>(value: T) => Result<T, E>;
|
|
222
|
-
/**
|
|
223
|
-
* Lifts a value into the error track.
|
|
224
|
-
*
|
|
225
|
-
* @example
|
|
226
|
-
* ```ts
|
|
227
|
-
* const r = err({ kind: "parse", message: "bad json" });
|
|
228
|
-
* // Result<never, { kind: "parse"; message: string }>
|
|
229
|
-
* ```
|
|
230
|
-
*/
|
|
231
|
-
declare const err: <T = never, E = unknown>(error: E) => Result<T, E>;
|
|
232
|
-
/**
|
|
233
|
-
* Type-narrowing predicate: returns `true` when the result is `Ok`.
|
|
234
|
-
*
|
|
235
|
-
* @example
|
|
236
|
-
* ```ts
|
|
237
|
-
* if (isOk(r)) {
|
|
238
|
-
* console.log(r.value); // narrowed to Ok branch
|
|
239
|
-
* }
|
|
240
|
-
* ```
|
|
241
|
-
*/
|
|
242
|
-
declare const isOk: <T, E>(result: Result<T, E>) => result is Ok<T, E>;
|
|
243
|
-
/**
|
|
244
|
-
* Type-narrowing predicate: returns `true` when the result is `Err`.
|
|
245
|
-
*
|
|
246
|
-
* @example
|
|
247
|
-
* ```ts
|
|
248
|
-
* if (isErr(r)) {
|
|
249
|
-
* metrics.inc("error", { kind: r.error.kind });
|
|
250
|
-
* }
|
|
251
|
-
* ```
|
|
252
|
-
*/
|
|
253
|
-
declare const isErr: <T, E>(result: Result<T, E>) => result is Err<T, E>;
|
|
254
|
-
/**
|
|
255
|
-
* Transform the `Ok` value, passing `Err` through unchanged. Dual-form:
|
|
256
|
-
* call data-first or curried (for use with {@link pipe}).
|
|
257
|
-
*
|
|
258
|
-
* @example
|
|
259
|
-
* ```ts
|
|
260
|
-
* map(ok(2), (n) => n * 3); // Ok 6 — data-first
|
|
261
|
-
* pipe(ok("x"), map((s) => s.length));// Ok 1 — curried
|
|
262
|
-
* ```
|
|
263
|
-
*/
|
|
264
|
-
declare const map: {
|
|
265
|
-
<T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E>;
|
|
266
|
-
<T, U>(fn: (value: T) => U): <E>(result: Result<T, E>) => Result<U, E>;
|
|
267
|
-
};
|
|
268
|
-
/**
|
|
269
|
-
* Transform the `Err` value, passing `Ok` through unchanged. Useful for
|
|
270
|
-
* unifying heterogeneous failure types into one app-level union.
|
|
271
|
-
*
|
|
272
|
-
* @example
|
|
273
|
-
* ```ts
|
|
274
|
-
* type AppError = { kind: "http"; status: number } | { kind: "parse" };
|
|
275
|
-
* pipe(
|
|
276
|
-
* fetchSync(url), // Result<Body, { status: number }>
|
|
277
|
-
* mapErr((e): AppError => ({ kind: "http", status: e.status })),
|
|
278
|
-
* );
|
|
279
|
-
* ```
|
|
280
|
-
*/
|
|
281
|
-
declare const mapErr: {
|
|
282
|
-
<T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F>;
|
|
283
|
-
<E, F>(fn: (error: E) => F): <T>(result: Result<T, E>) => Result<T, F>;
|
|
284
|
-
};
|
|
285
|
-
/**
|
|
286
|
-
* Transform both tracks at once — `Ok` via `onOk`, `Err` via `onErr`.
|
|
287
|
-
* Equivalent to `mapErr(onErr)(map(onOk)(result))` but in one pass.
|
|
288
|
-
*
|
|
289
|
-
* @example
|
|
290
|
-
* ```ts
|
|
291
|
-
* bimap(parsed, (cfg) => cfg.name, (e) => ({ kind: "input", cause: e }));
|
|
292
|
-
* ```
|
|
293
|
-
*/
|
|
294
|
-
declare const bimap: {
|
|
295
|
-
<T, U, E, F>(result: Result<T, E>, onOk: (value: T) => U, onErr: (error: E) => F): Result<U, F>;
|
|
296
|
-
<T, U, E, F>(onOk: (value: T) => U, onErr: (error: E) => F): (result: Result<T, E>) => Result<U, F>;
|
|
297
|
-
};
|
|
298
|
-
/**
|
|
299
|
-
* Canonical bind (Fantasy Land `chain`). Chains a Result-returning step,
|
|
300
|
-
* widening the error union to `E | F`. Short-circuits on `Err`.
|
|
301
|
-
*
|
|
302
|
-
* @example
|
|
303
|
-
* ```ts
|
|
304
|
-
* flatMap(parseInput(raw), (data) =>
|
|
305
|
-
* data.id != null ? ok(data) : err({ kind: "missing_id" as const }),
|
|
306
|
-
* );
|
|
307
|
-
* // Result<Data, ParseError | { kind: "missing_id" }>
|
|
308
|
-
* ```
|
|
309
|
-
*/
|
|
310
|
-
declare const flatMap: {
|
|
311
|
-
<T, U, E, F>(result: Result<T, E>, fn: (value: T) => Result<U, F>): Result<U, E | F>;
|
|
312
|
-
<T, U, F>(fn: (value: T) => Result<U, F>): <E>(result: Result<T, E>) => Result<U, E | F>;
|
|
313
|
-
};
|
|
314
|
-
/**
|
|
315
|
-
* Error-track bind — runs `fn` only when the result is `Err`, allowing
|
|
316
|
-
* a failed workflow to recover to `Ok` or remap the failure. Mirror of
|
|
317
|
-
* {@link flatMap} on the error channel.
|
|
318
|
-
*
|
|
319
|
-
* @example
|
|
320
|
-
* ```ts
|
|
321
|
-
* recover(networkResult, (e) =>
|
|
322
|
-
* e.kind === "rate_limit" ? ok(cachedBody) : err(e),
|
|
323
|
-
* );
|
|
324
|
-
* ```
|
|
325
|
-
*/
|
|
326
|
-
declare const recover: {
|
|
327
|
-
<T, E, F>(result: Result<T, E>, fn: (error: E) => Result<T, F>): Result<T, F>;
|
|
328
|
-
<T, E, F>(fn: (error: E) => Result<T, F>): (result: Result<T, E>) => Result<T, F>;
|
|
329
|
-
};
|
|
330
|
-
/**
|
|
331
|
-
* Observe the `Ok` value for side effects (logging, metrics) without
|
|
332
|
-
* changing the carried value. Passes `Err` through untouched.
|
|
333
|
-
*
|
|
334
|
-
* @example
|
|
335
|
-
* ```ts
|
|
336
|
-
* pipe(
|
|
337
|
-
* parseConfig(raw),
|
|
338
|
-
* tap((cfg) => log.info({ msg: "parsed", name: cfg.name })),
|
|
339
|
-
* flatMap(validate),
|
|
340
|
-
* );
|
|
341
|
-
* ```
|
|
342
|
-
*/
|
|
343
|
-
declare const tap: {
|
|
344
|
-
<T, E>(result: Result<T, E>, fn: (value: T) => void): Result<T, E>;
|
|
345
|
-
<T>(fn: (value: T) => void): <E>(result: Result<T, E>) => Result<T, E>;
|
|
346
|
-
};
|
|
347
|
-
/**
|
|
348
|
-
* Observe the `Err` value for side effects (logging, metrics) without
|
|
349
|
-
* changing the carried error. Passes `Ok` through untouched.
|
|
350
|
-
*
|
|
351
|
-
* @example
|
|
352
|
-
* ```ts
|
|
353
|
-
* pipe(
|
|
354
|
-
* loadUser(id),
|
|
355
|
-
* tapErr((e) => metrics.inc("user.load.fail", { kind: e.kind })),
|
|
356
|
-
* );
|
|
357
|
-
* ```
|
|
358
|
-
*/
|
|
359
|
-
declare const tapErr: {
|
|
360
|
-
<T, E>(result: Result<T, E>, fn: (error: E) => void): Result<T, E>;
|
|
361
|
-
<E>(fn: (error: E) => void): <T>(result: Result<T, E>) => Result<T, E>;
|
|
362
|
-
};
|
|
363
|
-
/**
|
|
364
|
-
* Terminal collapse — fold both tracks into a single value. Dual-form:
|
|
365
|
-
* 3-args data-first, 2-args curried for {@link pipe}. Returns whatever
|
|
366
|
-
* the handlers return.
|
|
367
|
-
*
|
|
368
|
-
* For files that also import `match` from `ts-pattern`, use a namespace
|
|
369
|
-
* import (`import * as R from "@onrails/result"` → `R.match`) to dissolve
|
|
370
|
-
* the collision.
|
|
371
|
-
*
|
|
372
|
-
* @example
|
|
373
|
-
* ```ts
|
|
374
|
-
* const html = match(parsed, (cfg) => render(cfg), (e) => renderError(e));
|
|
375
|
-
* ```
|
|
376
|
-
*/
|
|
377
|
-
declare const match: {
|
|
378
|
-
<T, E, U>(result: Result<T, E>, onOk: (value: T) => U, onErr: (error: E) => U): U;
|
|
379
|
-
<T, E, U>(onOk: (value: T) => U, onErr: (error: E) => U): (result: Result<T, E>) => U;
|
|
380
|
-
};
|
|
381
|
-
/**
|
|
382
|
-
* Returns the `Ok` value, or `defaultValue` when the result is `Err`.
|
|
383
|
-
*
|
|
384
|
-
* @example
|
|
385
|
-
* ```ts
|
|
386
|
-
* unwrapOr(parsedSetting, "default-value");
|
|
387
|
-
* ```
|
|
388
|
-
*/
|
|
389
|
-
declare const unwrapOr: {
|
|
390
|
-
<T, E>(result: Result<T, E>, defaultValue: T): T;
|
|
391
|
-
<T>(defaultValue: T): <E>(result: Result<T, E>) => T;
|
|
392
|
-
};
|
|
393
|
-
/**
|
|
394
|
-
* Test/assert helper — returns the `Ok` value, or **throws the original `Err`
|
|
395
|
-
* value** when called on an `Err`. This is the assertion tier (RFC 0001 §4):
|
|
396
|
-
* intended for `*.spec.ts` / `*.test.ts`, where throwing fails the test loudly.
|
|
397
|
-
* In business logic prefer {@link match} or {@link unwrapOr}; the lint plugins
|
|
398
|
-
* flag `unwrapOk` outside test files.
|
|
399
|
-
*
|
|
400
|
-
* @returns the unwrapped `Ok` value
|
|
401
|
-
* @throws the carried `Err` value when the result is `Err`
|
|
402
|
-
*
|
|
403
|
-
* @example
|
|
404
|
-
* ```ts
|
|
405
|
-
* // in a *.spec.ts
|
|
406
|
-
* expect(unwrapOk(ok(5))).toBe(5);
|
|
407
|
-
* expect(() => unwrapOk(err(error))).toThrow(error);
|
|
408
|
-
* ```
|
|
409
|
-
*/
|
|
410
|
-
declare function unwrapOk<T, E>(result: Result<T, E>): T;
|
|
411
|
-
/** Alias of {@link unwrapOk} for syntax cohesion with `@onrails/maybe`. */
|
|
412
|
-
declare const unwrap: typeof unwrapOk;
|
|
413
|
-
/**
|
|
414
|
-
* Test/assert helper — returns the `Err` value, or **throws a `TypeError`**
|
|
415
|
-
* when called on an `Ok`. Mirror of {@link unwrapOk} on the error track and
|
|
416
|
-
* part of the same assertion tier (RFC 0001 §4): intended for `*.spec.ts` /
|
|
417
|
-
* `*.test.ts`. Prefer {@link match} / {@link unwrapOr} in business logic; the
|
|
418
|
-
* lint plugins flag `unwrapErr` outside test files.
|
|
419
|
-
*
|
|
420
|
-
* @returns the unwrapped `Err` value
|
|
421
|
-
* @throws `TypeError` when the result is `Ok`
|
|
422
|
-
*
|
|
423
|
-
* @example
|
|
424
|
-
* ```ts
|
|
425
|
-
* // in a *.spec.ts
|
|
426
|
-
* expect(unwrapErr(err("x"))).toBe("x");
|
|
427
|
-
* expect(() => unwrapErr(ok(5))).toThrow(TypeError);
|
|
428
|
-
* ```
|
|
429
|
-
*/
|
|
430
|
-
declare function unwrapErr<T, E>(result: Result<T, E>): E;
|
|
431
|
-
/**
|
|
432
|
-
* Wraps a throwing sync function, returning a function that produces a
|
|
433
|
-
* {@link Result} instead of throwing. Thrown errors pass through `onThrow` to
|
|
434
|
-
* become a typed `Err`; a normal return becomes `Ok`. The neverthrow analogue
|
|
435
|
-
* is `Result.fromThrowable`.
|
|
436
|
-
*
|
|
437
|
-
* @param fn - the throwing function to wrap
|
|
438
|
-
* @param onThrow - maps a thrown value to the `Err` channel
|
|
439
|
-
* @returns a function with `fn`'s parameters that returns `Result<ReturnType, E>`
|
|
440
|
-
*
|
|
441
|
-
* @example
|
|
442
|
-
* ```ts
|
|
443
|
-
* type ParseError = { kind: "parse"; message: string };
|
|
444
|
-
* const parse = trySync(
|
|
445
|
-
* JSON.parse,
|
|
446
|
-
* (e): ParseError => ({ kind: "parse", message: String(e) }),
|
|
447
|
-
* );
|
|
448
|
-
* parse("{}"); // Ok({})
|
|
449
|
-
* parse("nope"); // Err({ kind: "parse", … })
|
|
450
|
-
* ```
|
|
451
|
-
*/
|
|
452
|
-
declare function trySync<A extends readonly unknown[], T, E>(fn: (...args: A) => T, onThrow: (error: unknown) => E): (...args: A) => Result<T, E>;
|
|
453
|
-
declare function trySync<F extends (...args: never) => unknown, E>(fn: F, onThrow: (error: unknown) => E): (...args: Parameters<F>) => Result<ReturnType<F>, E>;
|
|
454
|
-
/**
|
|
455
|
-
* Variadic value-first pipe — threads `value` through up to nine unary fns,
|
|
456
|
-
* left-to-right. Use {@link pipe} when you already have a starting value;
|
|
457
|
-
* use {@link flow} to define a reusable composed function with no value yet.
|
|
458
|
-
*
|
|
459
|
-
* @example
|
|
460
|
-
* ```ts
|
|
461
|
-
* pipe(
|
|
462
|
-
* parseConfig(raw),
|
|
463
|
-
* map((cfg) => cfg.name),
|
|
464
|
-
* flatMap((name) => (name ? ok(name) : err({ kind: "empty" as const }))),
|
|
465
|
-
* tap(log),
|
|
466
|
-
* );
|
|
467
|
-
* ```
|
|
468
|
-
*/
|
|
469
|
-
declare function pipe<A>(value: A): A;
|
|
470
|
-
declare function pipe<A, B>(value: A, ab: (a: A) => B): B;
|
|
471
|
-
declare function pipe<A, B, C>(value: A, ab: (a: A) => B, bc: (b: B) => C): C;
|
|
472
|
-
declare function pipe<A, B, C, D>(value: A, ab: (a: A) => B, bc: (b: B) => C, cd: (c: C) => D): D;
|
|
473
|
-
declare function pipe<A, B, C, D, E>(value: A, ab: (a: A) => B, bc: (b: B) => C, cd: (c: C) => D, de: (d: D) => E): E;
|
|
474
|
-
declare function pipe<A, B, C, D, E, F>(value: A, ab: (a: A) => B, bc: (b: B) => C, cd: (c: C) => D, de: (d: D) => E, ef: (e: E) => F): F;
|
|
475
|
-
declare function pipe<A, B, C, D, E, F, G>(value: A, ab: (a: A) => B, bc: (b: B) => C, cd: (c: C) => D, de: (d: D) => E, ef: (e: E) => F, fg: (f: F) => G): G;
|
|
476
|
-
declare function pipe<A, B, C, D, E, F, G, H>(value: A, ab: (a: A) => B, bc: (b: B) => C, cd: (c: C) => D, de: (d: D) => E, ef: (e: E) => F, fg: (f: F) => G, gh: (g: G) => H): H;
|
|
477
|
-
declare function pipe<A, B, C, D, E, F, G, H, I>(value: A, ab: (a: A) => B, bc: (b: B) => C, cd: (c: C) => D, de: (d: D) => E, ef: (e: E) => F, fg: (f: F) => G, gh: (g: G) => H, hi: (h: H) => I): I;
|
|
478
|
-
/**
|
|
479
|
-
* Debug printer — renders a result as `Ok(…)` / `Err(…)` for logs. Payloads
|
|
480
|
-
* print as JSON (values are plain data by design); non-JSON payloads fall
|
|
481
|
-
* back to `String(...)`.
|
|
482
|
-
*
|
|
483
|
-
* @example
|
|
484
|
-
* ```ts
|
|
485
|
-
* show(ok(1)); // 'Ok(1)'
|
|
486
|
-
* show(err({ kind: "e" })); // 'Err({"kind":"e"})'
|
|
487
|
-
* ```
|
|
488
|
-
*/
|
|
489
|
-
declare const show: <T, E>(result: Result<T, E>) => string;
|
|
490
|
-
|
|
491
|
-
export { Err, InferErr, InferOk, Ok, Result, ResultAsync, UnexpectedError, asyncAfter, bimap, combine, combineTuple, err, errAsync, flatMap, fromAsync, fromPromise, fromResult, fromSafePromise, isErr, isOk, map, mapErr, match, of, ok, okAsync, pipe, recover, show, tap, tapErr, tryAsync, trySync, unwrap, unwrapErr, unwrapOk, unwrapOr, validateAll, validateTuple };
|
|
2
|
+
* @onrails/result — public surface.
|
|
3
|
+
*
|
|
4
|
+
* Layout:
|
|
5
|
+
* • core railway — ok/err, map/flatMap, match, recover, tap, trySync/fromThrowable, pipe
|
|
6
|
+
* • collection (sync) — combine, combineTuple, validateAll, validateTuple
|
|
7
|
+
* • async — ResultAsync, fromPromise, tryAsync
|
|
8
|
+
* • async collection — ResultAsync.combineTuple / combineTupleParallel
|
|
9
|
+
* • sync ↔ async lift — fromResult, fromAsync, asyncAfter
|
|
10
|
+
* • generator sugar — tryGen, yieldResult, $
|
|
11
|
+
* • types — Result, Ok, Err, InferOk, InferErr, UnexpectedError
|
|
12
|
+
*
|
|
13
|
+
* Decision tree:
|
|
14
|
+
* single sync value → flatMap / match
|
|
15
|
+
* single async value → ResultAsync.flatMap / asyncAfter
|
|
16
|
+
* async chain via pipe/flow → @onrails/result/async (data-last twins)
|
|
17
|
+
* named multi-step workflow → @onrails/result/railway
|
|
18
|
+
* generator-style sync sugar → @onrails/result/try-gen
|
|
19
|
+
* independent validations → validateAll / validateTuple
|
|
20
|
+
*/
|
|
21
|
+
export { ResultAsync } from "./async.js";
|
|
22
|
+
export { asyncAfter, errAsync, fromAsync, fromPromise, fromResult, fromSafePromise, okAsync, tryAsync, } from "./async-lift.js";
|
|
23
|
+
export { combine, combineTuple, validateAll, validateTuple } from "./collections.js";
|
|
24
|
+
export type { InferErr, InferOk } from "./internal/infer.js";
|
|
25
|
+
export { flow, pipe } from "./pipe.js";
|
|
26
|
+
export { bimap, err, flatMap, fromThrowable, isErr, isOk, map, mapErr, match, of, ok, recover, show, tap, tapErr, trySync, unwrap, unwrapErr, unwrapOk, unwrapOr, } from "./result.js";
|
|
27
|
+
export { $, tryGen, yieldResult } from "./try-gen.js";
|
|
28
|
+
export type { Err, Ok, Result } from "./types.js";
|
|
29
|
+
export { UnexpectedError } from "./types.js";
|