@onrails/result 0.1.4 → 0.3.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.
Files changed (72) hide show
  1. package/DESIGN.md +11 -19
  2. package/README.md +14 -62
  3. package/RECIPES.md +12 -25
  4. package/dist/async-BR3AX0ck.d.cts +326 -0
  5. package/dist/async-SZ7BEl0g.d.ts +326 -0
  6. package/dist/compat/neverthrow.cjs +266 -94
  7. package/dist/compat/neverthrow.cjs.map +1 -1
  8. package/dist/compat/neverthrow.d.cts +4 -4
  9. package/dist/compat/neverthrow.d.ts +4 -4
  10. package/dist/compat/neverthrow.js +263 -91
  11. package/dist/compat/neverthrow.js.map +1 -1
  12. package/dist/extra.cjs +2 -25
  13. package/dist/extra.cjs.map +1 -1
  14. package/dist/extra.d.cts +7 -19
  15. package/dist/extra.d.ts +7 -19
  16. package/dist/extra.js +3 -25
  17. package/dist/extra.js.map +1 -1
  18. package/dist/fluent.cjs +55 -29
  19. package/dist/fluent.cjs.map +1 -1
  20. package/dist/fluent.d.cts +38 -11
  21. package/dist/fluent.d.ts +38 -11
  22. package/dist/fluent.js +55 -29
  23. package/dist/fluent.js.map +1 -1
  24. package/dist/index.cjs +290 -131
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.d.cts +294 -69
  27. package/dist/index.d.ts +294 -69
  28. package/dist/index.js +287 -127
  29. package/dist/index.js.map +1 -1
  30. package/dist/pipe.cjs.map +1 -1
  31. package/dist/pipe.d.cts +7 -5
  32. package/dist/pipe.d.ts +7 -5
  33. package/dist/pipe.js.map +1 -1
  34. package/dist/railway.cjs +293 -188
  35. package/dist/railway.cjs.map +1 -1
  36. package/dist/railway.d.cts +26 -180
  37. package/dist/railway.d.ts +26 -180
  38. package/dist/railway.js +294 -178
  39. package/dist/railway.js.map +1 -1
  40. package/dist/try-gen.cjs +1 -1
  41. package/dist/try-gen.cjs.map +1 -1
  42. package/dist/try-gen.d.cts +29 -3
  43. package/dist/try-gen.d.ts +29 -3
  44. package/dist/try-gen.js +1 -1
  45. package/dist/try-gen.js.map +1 -1
  46. package/dist/types-BQ9vv0nD.d.cts +55 -0
  47. package/dist/types-BQ9vv0nD.d.ts +55 -0
  48. package/package.json +5 -18
  49. package/skills/railway-do-notation/SKILL.md +397 -0
  50. package/skills/result-composition/SKILL.md +323 -0
  51. package/dist/$.cjs +0 -40
  52. package/dist/$.cjs.map +0 -1
  53. package/dist/$.js +0 -36
  54. package/dist/$.js.map +0 -1
  55. package/dist/_.d.cts +0 -2
  56. package/dist/_.d.ts +0 -2
  57. package/dist/async-CH8Cs8KE.d.cts +0 -161
  58. package/dist/async-DKISqIBc.d.ts +0 -161
  59. package/dist/interop.cjs +0 -253
  60. package/dist/interop.cjs.map +0 -1
  61. package/dist/interop.d.cts +0 -35
  62. package/dist/interop.d.ts +0 -35
  63. package/dist/interop.js +0 -249
  64. package/dist/interop.js.map +0 -1
  65. package/dist/types-C2Dp1d5J.d.cts +0 -21
  66. package/dist/types-C2Dp1d5J.d.ts +0 -21
  67. package/dist/validation.cjs +0 -53
  68. package/dist/validation.cjs.map +0 -1
  69. package/dist/validation.d.cts +0 -72
  70. package/dist/validation.d.ts +0 -72
  71. package/dist/validation.js +0 -48
  72. package/dist/validation.js.map +0 -1
package/dist/railway.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { R as ResultAsync } from './async-DKISqIBc.js';
2
- import { R as Result } from './types-C2Dp1d5J.js';
1
+ import { R as ResultAsync } from './async-SZ7BEl0g.js';
2
+ import { R as Result } from './types-BQ9vv0nD.js';
3
3
 
4
4
  /**
5
5
  * Tracks whether a {@link Railway} workflow has crossed an async boundary.
@@ -11,20 +11,10 @@ type RailwayMode = "sync" | "async";
11
11
  * `Result<T, E>`, async mode → `ResultAsync<T, E>`.
12
12
  */
13
13
  type RailwayOutput<T, E, M extends RailwayMode> = M extends "async" ? ResultAsync<T, E> : Result<T, E>;
14
- /**
15
- * Initial context shape for a `railway` functional pipeline.
16
- * Wraps the raw input under the `input` key so subsequent named steps
17
- * can reference it via `ctx.input`.
18
- */
19
- type RailwayInput<I> = {
20
- readonly input: I;
21
- };
22
14
  type BranchFn<C extends object> = (ctx: C) => ResultAsync<unknown, unknown>;
23
15
  type BranchRecord = Record<string, (ctx: never) => ResultAsync<unknown, unknown>>;
24
16
  type BranchOk<R> = R extends (ctx: never) => ResultAsync<infer T, infer _E> ? T : never;
25
17
  type BranchErr<R> = R extends (ctx: never) => ResultAsync<infer _T, infer E> ? E : never;
26
- type BranchInput<R> = R extends (ctx: infer C) => ResultAsync<unknown, unknown> ? C : never;
27
- type ParallelInput<R extends BranchRecord> = BranchInput<R[keyof R]> & object;
28
18
  type ParallelOutput<R extends BranchRecord> = {
29
19
  [K in keyof R]: BranchOk<R[K]>;
30
20
  };
@@ -33,27 +23,6 @@ type ParallelError<R extends BranchRecord> = BranchErr<R[keyof R]>;
33
23
  * Named-context workflow builder. Each step appends a typed field to the
34
24
  * accumulating context object; the workflow tracks sync/async mode so the
35
25
  * final output type ({@link RailwayOutput}) is correct.
36
- *
37
- * Use `Railway` when a service workflow has 4+ named steps, mixed sync and
38
- * async boundaries, or independent async branches that should run in
39
- * parallel. For 1–2 step flows, prefer `flatMap` / `asyncAfter` directly.
40
- *
41
- * @example
42
- * ```ts
43
- * const summary = Railway
44
- * .fromSync("id", () => IdSchema.parse(raw), toError)
45
- * .fromPromise("row", ({ id }) => db.profiles.findFirst({ where: eq(profiles.id, id) }), toError)
46
- * .require("profile", "row", ({ id }) => ({ kind: "not_found" as const, id }))
47
- * .derive("normalized", ({ profile }) => normalizeProfile(profile))
48
- * .parallel({
49
- * artifacts: ({ normalized }) => loadArtifacts(normalized.id),
50
- * metrics: ({ normalized }) => loadMetrics(normalized.id),
51
- * })
52
- * .select(({ normalized, artifacts, metrics }) =>
53
- * toProfileSummary({ profile: normalized, artifacts, metrics }),
54
- * );
55
- * // ResultAsync<ProfileSummary, ParseError | DbError | NotFound>
56
- * ```
57
26
  */
58
27
  declare class Railway<C extends object, E, M extends RailwayMode> {
59
28
  private readonly state;
@@ -62,10 +31,7 @@ declare class Railway<C extends object, E, M extends RailwayMode> {
62
31
  static empty(): Railway<Record<never, never>, never, "sync">;
63
32
  /** Start a sync workflow with the given context as the initial state. */
64
33
  static context<C extends object>(context: C): Railway<C, never, "sync">;
65
- /**
66
- * Start a sync workflow with a throwing function — `onThrow` maps any
67
- * exception to a typed error.
68
- */
34
+ /** Start a sync workflow with a throwing function. */
69
35
  static fromSync<K extends string, T, E>(key: K, fn: () => T, onThrow: (error: unknown) => E): Railway<Record<K, T>, E, "sync">;
70
36
  /** Start a sync workflow with a `Result`-returning function. */
71
37
  static fromResult<K extends string, T, E>(key: K, fn: () => Result<T, E>): Railway<Record<K, T>, E, "sync">;
@@ -74,159 +40,39 @@ declare class Railway<C extends object, E, M extends RailwayMode> {
74
40
  /** Start an async workflow with a `ResultAsync`-returning function. */
75
41
  static fromAsync<K extends string, T, E>(key: K, fn: () => ResultAsync<T, E>): Railway<Record<K, T>, E, "async">;
76
42
  /**
77
- * Pure sync derivation — `fn` must not throw. Use {@link fromSync} for
78
- * throwing transforms.
43
+ * Rebuild the workflow in its current mode after transforming the carried
44
+ * result. Single re-link point for the state/phantom invariant: the mode
45
+ * tag is preserved verbatim, so `M` still describes the new state — TS
46
+ * cannot narrow the phantom `M` from the runtime tag, hence the cast.
79
47
  */
80
- derive<K extends string, T>(key: K, fn: (ctx: C) => T): Railway<C & Record<K, T>, E, M>;
48
+ private step;
81
49
  /**
82
- * Throwing sync transform. Adds `{ [key]: T }` to context; converts any
83
- * exception to `Err<F>` via `onThrow`.
50
+ * Project the carried result into the mode-aware output type. Counterpart
51
+ * of {@link Railway.step} for terminal steps: the runtime branch matches
52
+ * the branch `RailwayOutput` picks for `M` (state/phantom invariant), but
53
+ * TS cannot resolve the conditional on an unresolved `M`, hence the cast.
84
54
  */
55
+ private out;
56
+ /** Lift the carried result into `ResultAsync` for async-upgrading steps. */
57
+ private toAsync;
58
+ /** Pure sync derivation. */
59
+ derive<K extends string, T>(key: K, fn: (ctx: C) => T): Railway<C & Record<K, T>, E, M>;
60
+ /** Throwing sync transform. */
85
61
  fromSync<K extends string, T, F>(key: K, fn: (ctx: C) => T, onThrow: (error: unknown) => F): Railway<C & Record<K, T>, E | F, M>;
86
- /**
87
- * Sync `Result`-returning step. Adds `{ [key]: T }` to context on `Ok`;
88
- * short-circuits the workflow on `Err`. Error union widens to `E | F`.
89
- */
62
+ /** Sync `Result`-returning step. */
90
63
  fromResult<K extends string, T, F>(key: K, fn: (ctx: C) => Result<T, F>): Railway<C & Record<K, T>, E | F, M>;
91
- /**
92
- * Promise-returning step — upgrades the workflow to async mode. Reject
93
- * reasons go through `onReject` to become typed `Err<F>`.
94
- */
64
+ /** Promise-returning step — upgrades the workflow to async mode. */
95
65
  fromPromise<K extends string, T, F>(key: K, fn: (ctx: C) => PromiseLike<T>, onReject: (error: unknown) => F): Railway<C & Record<K, T>, E | F, "async">;
96
- /**
97
- * `ResultAsync`-returning step — upgrades the workflow to async mode.
98
- * Already-typed error: no mapper needed.
99
- */
66
+ /** `ResultAsync`-returning step — upgrades the workflow to async mode. */
100
67
  fromAsync<K extends string, T, F>(key: K, fn: (ctx: C) => ResultAsync<T, F>): Railway<C & Record<K, T>, E | F, "async">;
101
- /**
102
- * Narrow a nullable context field into a required non-null field. If the
103
- * source field is `null` / `undefined`, the workflow short-circuits with
104
- * `onMissing(ctx)`.
105
- *
106
- * @example
107
- * ```ts
108
- * .fromPromise("row", ({ id }) => db.users.findFirst({ where: eq(users.id, id) }))
109
- * .require("user", "row", ({ id }) => ({ kind: "not_found" as const, id }))
110
- * // user is now User (non-null), not User | null
111
- * ```
112
- */
68
+ /** Narrow a nullable context field into a required non-null field. */
113
69
  require<K extends string, S extends keyof C, F>(key: K, source: S, onMissing: (ctx: C) => F): Railway<C & Record<K, NonNullable<C[S]>>, E | F, M>;
114
- /**
115
- * Run independent `ResultAsync` branches concurrently and merge their
116
- * named outputs back into context. Upgrades the workflow to async mode.
117
- * On multiple failures, the first `Err` in record-iteration order wins.
118
- *
119
- * @example
120
- * ```ts
121
- * .parallel({
122
- * recent: ({ userId }) => loadRecent(userId),
123
- * metrics: ({ userId }) => loadMetrics(userId),
124
- * })
125
- * // ctx now has { ..., recent, metrics }
126
- * ```
127
- */
70
+ /** Run independent `ResultAsync` branches concurrently and merge outputs. */
128
71
  parallel<R extends Record<string, BranchFn<C>>>(branches: R): Railway<C & ParallelOutput<R>, E | ParallelError<R>, "async">;
129
- /**
130
- * Project the final context into the workflow's output type. Hides the
131
- * internal context shape from callers.
132
- */
72
+ /** Project the final context into the workflow's output type. */
133
73
  select<T>(fn: (ctx: C) => T): RailwayOutput<T, E, M>;
134
- /**
135
- * Return the accumulated context as-is. Use when downstream code needs
136
- * every named field; prefer {@link select} when you can project to a DTO.
137
- */
74
+ /** Return the accumulated context as-is. */
138
75
  done(): RailwayOutput<C, E, M>;
139
76
  }
140
77
 
141
- type ParserLike<I, T> = {
142
- readonly parse: (input: I) => T;
143
- } | ((input: I) => T);
144
- type UnaryStep<I, O> = (input: I) => O;
145
- /**
146
- * Functional companion to {@link Railway} — point-free composition of
147
- * reusable workflow steps. Starts from `Railway.context({ input })` and
148
- * applies each step in order. Step factories live below: {@link parseWith},
149
- * {@link fromSyncNamed}, {@link fromResultNamed}, {@link fromPromiseNamed},
150
- * {@link fromAsyncNamed}, {@link deriveNamed}, {@link requireNamed},
151
- * {@link parallelNamed}, {@link select}.
152
- *
153
- * @example
154
- * ```ts
155
- * const summary = railway(
156
- * rawId,
157
- * parseWith(IdSchema, toError).as("id"),
158
- * fromPromiseNamed("row", ({ id }) => db.profiles.findFirst({ where: eq(profiles.id, id) }), toError),
159
- * requireNamed("profile", "row", ({ id }) => ({ kind: "not_found" as const, id })),
160
- * deriveNamed("normalized", ({ profile }) => normalizeProfile(profile)),
161
- * select(({ normalized }) => toProfileSummary(normalized)),
162
- * );
163
- * ```
164
- */
165
- declare function railway<I, A>(input: I, step1: UnaryStep<Railway<RailwayInput<I>, never, "sync">, A>): A;
166
- declare function railway<I, A, B>(input: I, step1: UnaryStep<Railway<RailwayInput<I>, never, "sync">, A>, step2: UnaryStep<A, B>): B;
167
- declare function railway<I, A, B, C>(input: I, step1: UnaryStep<Railway<RailwayInput<I>, never, "sync">, A>, step2: UnaryStep<A, B>, step3: UnaryStep<B, C>): C;
168
- declare function railway<I, A, B, C, D>(input: I, step1: UnaryStep<Railway<RailwayInput<I>, never, "sync">, A>, step2: UnaryStep<A, B>, step3: UnaryStep<B, C>, step4: UnaryStep<C, D>): D;
169
- declare function railway<I, A, B, C, D, E>(input: I, step1: UnaryStep<Railway<RailwayInput<I>, never, "sync">, A>, step2: UnaryStep<A, B>, step3: UnaryStep<B, C>, step4: UnaryStep<C, D>, step5: UnaryStep<D, E>): E;
170
- declare function railway<I, A, B, C, D, E, F>(input: I, step1: UnaryStep<Railway<RailwayInput<I>, never, "sync">, A>, step2: UnaryStep<A, B>, step3: UnaryStep<B, C>, step4: UnaryStep<C, D>, step5: UnaryStep<D, E>, step6: UnaryStep<E, F>): F;
171
- declare function railway<I, A, B, C, D, E, F, G>(input: I, step1: UnaryStep<Railway<RailwayInput<I>, never, "sync">, A>, step2: UnaryStep<A, B>, step3: UnaryStep<B, C>, step4: UnaryStep<C, D>, step5: UnaryStep<D, E>, step6: UnaryStep<E, F>, step7: UnaryStep<F, G>): G;
172
- declare function railway<I, A, B, C, D, E, F, G, H>(input: I, step1: UnaryStep<Railway<RailwayInput<I>, never, "sync">, A>, step2: UnaryStep<A, B>, step3: UnaryStep<B, C>, step4: UnaryStep<C, D>, step5: UnaryStep<D, E>, step6: UnaryStep<E, F>, step7: UnaryStep<F, G>, step8: UnaryStep<G, H>): H;
173
- /**
174
- * Step factory: parse the workflow input with a Zod-like schema (or a
175
- * unary parse function). Call `.as(key)` to name the output field.
176
- *
177
- * @example
178
- * ```ts
179
- * parseWith(IdSchema, toError).as("id");
180
- * // Used as the first step in `railway(rawId, parseId, ...)`
181
- * ```
182
- */
183
- declare const parseWith: <I, T, E>(parser: ParserLike<I, T>, onThrow: (error: unknown) => E) => {
184
- as: <K extends string>(key: K) => <F, M extends RailwayMode>(workflow: Railway<RailwayInput<I>, F, M>) => Railway<RailwayInput<I> & Record<K, T>, F | E, M>;
185
- };
186
- /**
187
- * Name-first companion to {@link parseWith}: parse the workflow input with a
188
- * Zod-like schema (or unary parse fn), naming the output field in the same
189
- * `(key, ...)` shape as {@link fromSyncNamed}, {@link deriveNamed}, et al.
190
- * Prefer this in `railway(...)` pipelines so every step's name aligns at the
191
- * left edge.
192
- *
193
- * @example
194
- * ```ts
195
- * railway(
196
- * rawId,
197
- * parseNamed("id", IdSchema, toError),
198
- * fromPromiseNamed("profile", ({ id }) => fetchProfile(id), toError),
199
- * );
200
- * ```
201
- */
202
- declare const parseNamed: <K extends string, I, T, E>(key: K, parser: ParserLike<I, T>, onThrow: (error: unknown) => E) => <F, M extends RailwayMode>(workflow: Railway<RailwayInput<I>, F, M>) => Railway<RailwayInput<I> & Record<K, T>, F | E, M>;
203
- /**
204
- * Reusable wrapper around {@link Railway.fromSync}. Captures key + fn +
205
- * onThrow once; applies to any compatible workflow.
206
- */
207
- declare const fromSyncNamed: <K extends string, C extends object, T, E>(key: K, fn: (ctx: C) => T, onThrow: (error: unknown) => E) => <I extends C, F, M extends RailwayMode>(workflow: Railway<I, F, M>) => Railway<I & Record<K, T>, F | E, M>;
208
- /** Reusable wrapper around {@link Railway.fromResult}. */
209
- declare const fromResultNamed: <K extends string, C extends object, T, E>(key: K, fn: (ctx: C) => Result<T, E>) => <I extends C, F, M extends RailwayMode>(workflow: Railway<I, F, M>) => Railway<I & Record<K, T>, F | E, M>;
210
- /**
211
- * Reusable wrapper around {@link Railway.fromPromise}. Upgrades the
212
- * workflow to async mode when applied.
213
- */
214
- declare const fromPromiseNamed: <K extends string, C extends object, T, E>(key: K, fn: (ctx: C) => PromiseLike<T>, onReject: (error: unknown) => E) => <I extends C, F, M extends RailwayMode>(workflow: Railway<I, F, M>) => Railway<I & Record<K, T>, F | E, "async">;
215
- /** Reusable wrapper around {@link Railway.fromAsync}. */
216
- declare const fromAsyncNamed: <K extends string, C extends object, T, E>(key: K, fn: (ctx: C) => ResultAsync<T, E>) => <I extends C, F, M extends RailwayMode>(workflow: Railway<I, F, M>) => Railway<I & Record<K, T>, F | E, "async">;
217
- /** Reusable wrapper around {@link Railway.derive} — pure sync derivation. */
218
- declare const deriveNamed: <K extends string, C extends object, T>(key: K, fn: (ctx: C) => T) => <I extends C, E, M extends RailwayMode>(workflow: Railway<I, E, M>) => Railway<I & Record<K, T>, E, M>;
219
- /** Reusable wrapper around {@link Railway.require} — narrows a nullable field. */
220
- declare const requireNamed: <K extends string, S extends string, C extends object, E>(key: K, source: S, onMissing: (ctx: C & Record<S, unknown>) => E) => <I extends C & Record<S, unknown>, F, M extends RailwayMode>(workflow: Railway<I, F, M>) => Railway<I & Record<K, NonNullable<I[S]>>, F | E, M>;
221
- /**
222
- * Reusable wrapper around {@link Railway.parallel}. Branches run
223
- * concurrently and merge their named outputs back into context.
224
- */
225
- declare const parallelNamed: <R extends BranchRecord>(branches: R) => <I extends ParallelInput<R>, E, M extends RailwayMode>(workflow: Railway<I, E, M>) => Railway<I & ParallelOutput<R>, E | ParallelError<R>, "async">;
226
- /**
227
- * Reusable wrapper around {@link Railway.select} — projects the final
228
- * context into the workflow's output type.
229
- */
230
- declare const select: <C extends object, T>(fn: (ctx: C) => T) => <E, M extends RailwayMode>(workflow: Railway<C, E, M>) => RailwayOutput<T, E, M>;
231
-
232
- export { Railway, type RailwayInput, type RailwayMode, type RailwayOutput, deriveNamed, fromAsyncNamed, fromPromiseNamed, fromResultNamed, fromSyncNamed, parallelNamed, parseNamed, parseWith, railway, requireNamed, select };
78
+ export { type BranchFn, type BranchRecord, type ParallelError, type ParallelOutput, Railway, type RailwayMode, type RailwayOutput };