@onrails/result 0.4.0 → 0.4.2

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 (73) hide show
  1. package/DESIGN.md +1 -1
  2. package/README.md +168 -12
  3. package/RECIPES.md +11 -9
  4. package/dist/async-fns.cjs +52 -0
  5. package/dist/async-fns.cjs.map +1 -0
  6. package/dist/async-fns.d.cts +143 -0
  7. package/dist/async-fns.d.ts +143 -0
  8. package/dist/async-fns.js +42 -0
  9. package/dist/async-fns.js.map +1 -0
  10. package/dist/async-lift.d.cts +109 -0
  11. package/dist/async-lift.d.ts +109 -0
  12. package/dist/{async-Ci6WSL5-.d.ts → async.d.cts} +45 -58
  13. package/dist/{async-DcXR7k9K.d.cts → async.d.ts} +45 -58
  14. package/dist/collections.d.cts +105 -0
  15. package/dist/collections.d.ts +105 -0
  16. package/dist/compat/neverthrow.cjs +57 -45
  17. package/dist/compat/neverthrow.cjs.map +1 -1
  18. package/dist/compat/neverthrow.d.cts +21 -24
  19. package/dist/compat/neverthrow.d.ts +21 -24
  20. package/dist/compat/neverthrow.js +56 -44
  21. package/dist/compat/neverthrow.js.map +1 -1
  22. package/dist/extra.cjs +3 -1
  23. package/dist/extra.cjs.map +1 -1
  24. package/dist/extra.d.cts +11 -14
  25. package/dist/extra.d.ts +11 -14
  26. package/dist/extra.js +3 -1
  27. package/dist/extra.js.map +1 -1
  28. package/dist/fluent.cjs +15 -13
  29. package/dist/fluent.cjs.map +1 -1
  30. package/dist/fluent.d.cts +4 -7
  31. package/dist/fluent.d.ts +4 -7
  32. package/dist/fluent.js +15 -13
  33. package/dist/fluent.js.map +1 -1
  34. package/dist/index.cjs +77 -52
  35. package/dist/index.cjs.map +1 -1
  36. package/dist/index.d.cts +28 -490
  37. package/dist/index.d.ts +28 -490
  38. package/dist/index.js +77 -53
  39. package/dist/index.js.map +1 -1
  40. package/dist/internal/ctor.d.cts +11 -0
  41. package/dist/internal/ctor.d.ts +11 -0
  42. package/dist/internal/dual.d.cts +42 -0
  43. package/dist/internal/dual.d.ts +42 -0
  44. package/dist/internal/infer.d.cts +40 -0
  45. package/dist/internal/infer.d.ts +40 -0
  46. package/dist/internal/payload.d.cts +17 -0
  47. package/dist/internal/payload.d.ts +17 -0
  48. package/dist/internal/to-error.d.cts +2 -0
  49. package/dist/internal/to-error.d.ts +2 -0
  50. package/dist/pipe.cjs +8 -0
  51. package/dist/pipe.cjs.map +1 -1
  52. package/dist/pipe.d.cts +58 -11
  53. package/dist/pipe.d.ts +58 -11
  54. package/dist/pipe.js +8 -1
  55. package/dist/pipe.js.map +1 -1
  56. package/dist/railway.cjs +87 -63
  57. package/dist/railway.cjs.map +1 -1
  58. package/dist/railway.d.cts +49 -19
  59. package/dist/railway.d.ts +49 -19
  60. package/dist/railway.js +87 -63
  61. package/dist/railway.js.map +1 -1
  62. package/dist/result.d.cts +317 -0
  63. package/dist/result.d.ts +317 -0
  64. package/dist/try-gen.cjs +3 -3
  65. package/dist/try-gen.cjs.map +1 -1
  66. package/dist/try-gen.d.cts +4 -7
  67. package/dist/try-gen.d.ts +4 -7
  68. package/dist/try-gen.js +3 -3
  69. package/dist/try-gen.js.map +1 -1
  70. package/dist/{types-BQ9vv0nD.d.cts → types.d.cts} +6 -8
  71. package/dist/{types-BQ9vv0nD.d.ts → types.d.ts} +6 -8
  72. package/package.json +69 -24
  73. package/skills/railway-do-notation/SKILL.md +9 -9
@@ -15,7 +15,7 @@
15
15
  * }
16
16
  * ```
17
17
  */
18
- type Result<T, E> = {
18
+ export type Result<T, E> = {
19
19
  readonly _tag: "Ok";
20
20
  readonly value: T;
21
21
  } | {
@@ -26,13 +26,13 @@ type Result<T, E> = {
26
26
  * The success branch of a {@link Result} — `{ _tag: "Ok"; value: T }`.
27
27
  * The `E` parameter keeps the type assignable to `Result<T, E>`.
28
28
  */
29
- type Ok<T, E = never> = Extract<Result<T, E>, {
29
+ export type Ok<T, E = never> = Extract<Result<T, E>, {
30
30
  _tag: "Ok";
31
31
  }>;
32
32
  /**
33
33
  * The failure branch of a {@link Result} — `{ _tag: "Err"; error: E }`.
34
34
  */
35
- type Err<T, E> = Extract<Result<T, E>, {
35
+ export type Err<T, E> = Extract<Result<T, E>, {
36
36
  _tag: "Err";
37
37
  }>;
38
38
  /**
@@ -47,9 +47,7 @@ type Err<T, E> = Extract<Result<T, E>, {
47
47
  * defect.cause; // the original thrown value
48
48
  * ```
49
49
  */
50
- declare class UnexpectedError extends Error {
51
- readonly cause?: unknown | undefined;
52
- constructor(message: string, cause?: unknown | undefined);
50
+ export declare class UnexpectedError extends Error {
51
+ readonly cause?: unknown;
52
+ constructor(message: string, cause?: unknown);
53
53
  }
54
-
55
- export { type Err as E, type Ok as O, type Result as R, UnexpectedError as U };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onrails/result",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "Tagged Result / ResultAsync for railway-oriented TypeScript — pure tagged unions, neverthrow-shaped compat shim, FL-friendly",
5
5
  "license": "MIT",
6
6
  "author": "Alan R. Soares",
@@ -50,51 +50,96 @@
50
50
  ],
51
51
  "exports": {
52
52
  ".": {
53
- "types": "./dist/index.d.ts",
54
- "import": "./dist/index.js",
55
- "require": "./dist/index.cjs"
53
+ "import": {
54
+ "types": "./dist/index.d.ts",
55
+ "default": "./dist/index.js"
56
+ },
57
+ "require": {
58
+ "types": "./dist/index.d.cts",
59
+ "default": "./dist/index.cjs"
60
+ }
56
61
  },
57
62
  "./fluent": {
58
- "types": "./dist/fluent.d.ts",
59
- "import": "./dist/fluent.js",
60
- "require": "./dist/fluent.cjs"
63
+ "import": {
64
+ "types": "./dist/fluent.d.ts",
65
+ "default": "./dist/fluent.js"
66
+ },
67
+ "require": {
68
+ "types": "./dist/fluent.d.cts",
69
+ "default": "./dist/fluent.cjs"
70
+ }
71
+ },
72
+ "./async": {
73
+ "import": {
74
+ "types": "./dist/async-fns.d.ts",
75
+ "default": "./dist/async-fns.js"
76
+ },
77
+ "require": {
78
+ "types": "./dist/async-fns.d.cts",
79
+ "default": "./dist/async-fns.cjs"
80
+ }
61
81
  },
62
82
  "./extra": {
63
- "types": "./dist/extra.d.ts",
64
- "import": "./dist/extra.js",
65
- "require": "./dist/extra.cjs"
83
+ "import": {
84
+ "types": "./dist/extra.d.ts",
85
+ "default": "./dist/extra.js"
86
+ },
87
+ "require": {
88
+ "types": "./dist/extra.d.cts",
89
+ "default": "./dist/extra.cjs"
90
+ }
66
91
  },
67
92
  "./pipe": {
68
- "types": "./dist/pipe.d.ts",
69
- "import": "./dist/pipe.js",
70
- "require": "./dist/pipe.cjs"
93
+ "import": {
94
+ "types": "./dist/pipe.d.ts",
95
+ "default": "./dist/pipe.js"
96
+ },
97
+ "require": {
98
+ "types": "./dist/pipe.d.cts",
99
+ "default": "./dist/pipe.cjs"
100
+ }
71
101
  },
72
102
  "./railway": {
73
- "types": "./dist/railway.d.ts",
74
- "import": "./dist/railway.js",
75
- "require": "./dist/railway.cjs"
103
+ "import": {
104
+ "types": "./dist/railway.d.ts",
105
+ "default": "./dist/railway.js"
106
+ },
107
+ "require": {
108
+ "types": "./dist/railway.d.cts",
109
+ "default": "./dist/railway.cjs"
110
+ }
76
111
  },
77
112
  "./try-gen": {
78
- "types": "./dist/try-gen.d.ts",
79
- "import": "./dist/try-gen.js",
80
- "require": "./dist/try-gen.cjs"
113
+ "import": {
114
+ "types": "./dist/try-gen.d.ts",
115
+ "default": "./dist/try-gen.js"
116
+ },
117
+ "require": {
118
+ "types": "./dist/try-gen.d.cts",
119
+ "default": "./dist/try-gen.cjs"
120
+ }
81
121
  },
82
122
  "./compat/neverthrow": {
83
- "types": "./dist/compat/neverthrow.d.ts",
84
- "import": "./dist/compat/neverthrow.js",
85
- "require": "./dist/compat/neverthrow.cjs"
123
+ "import": {
124
+ "types": "./dist/compat/neverthrow.d.ts",
125
+ "default": "./dist/compat/neverthrow.js"
126
+ },
127
+ "require": {
128
+ "types": "./dist/compat/neverthrow.d.cts",
129
+ "default": "./dist/compat/neverthrow.cjs"
130
+ }
86
131
  }
87
132
  },
88
133
  "scripts": {
89
134
  "typecheck": "tsc --noEmit",
90
135
  "test": "bun test",
91
136
  "check": "bun typecheck && bun test",
92
- "build": "tsup",
137
+ "build": "tsup && tsc -p tsconfig.build.json && bun run ../../scripts/dts-to-cts.ts",
93
138
  "clean": "rm -rf dist",
94
139
  "prepublishOnly": "bun run build"
95
140
  },
96
141
  "devDependencies": {
97
- "fast-check": "^3.23.2",
142
+ "fast-check": "^4.9.0",
98
143
  "ts-expect": "^1.3.0",
99
144
  "tsup": "^8.5.1"
100
145
  }
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: railway-do-notation
3
- description: "Ergonomic railway composition for `@onrails/result`: when to use low-level Result/ResultAsync helpers, fluent `Railway.*` workflows, or sync `tryGen`. Use when writing or refactoring Result-heavy TypeScript, Drizzle/Zod ETL workflows, nested `flatMapResult` chains, mixed sync/async railway code, or when a user asks about 'Railway', 'railway', 'do-notation', 'safe workflow', or 'expressive Result code'. Do NOT use workflow builders for tiny repository methods where `asyncAfter` or `.flatMap` is clearer."
3
+ description: "Ergonomic railway composition for `@onrails/result`: when to use low-level Result/ResultAsync helpers, fluent `Railway.*` workflows, or sync `tryGen`. Use when writing or refactoring Result-heavy TypeScript, Drizzle/Zod ETL workflows, nested `flatMap` chains, mixed sync/async railway code, or when a user asks about 'Railway', 'railway', 'do-notation', 'safe workflow', or 'expressive Result code'. Do NOT use workflow builders for tiny repository methods where `asyncAfter` or `.flatMap` is clearer."
4
4
  ---
5
5
 
6
6
  # Railway ergonomics in `@onrails/result`
@@ -16,7 +16,7 @@ The goal is expressive safe code without exposing plumbing at every composition
16
16
  Use the smallest layer that makes the code clear:
17
17
 
18
18
  1. **Low-level helpers** for small functions and library internals.
19
- - Use `trySync`, `tryAsync`, `fromResult`, `asyncAfter`, `flatMapResult`, `.flatMap`, `.andThen`.
19
+ - Use `trySync`, `fromThrowable`, `tryAsync`, `fromResult`, `asyncAfter`, `flatMap`, `.flatMap`, `.andThen`.
20
20
  - Best for one or two steps, repository helpers, and package internals.
21
21
 
22
22
  2. **Fluent `Railway.*`** for service workflows.
@@ -25,7 +25,7 @@ Use the smallest layer that makes the code clear:
25
25
  - For steps shared across workflows, extract plain context functions and plug them in via `.fromResult` / `.fromAsync`.
26
26
 
27
27
  3. **Sync `tryGen`** for dense synchronous `Result` chains only.
28
- - Use sparingly when no workflow builder is warranted and nested sync `flatMapResult` is hard to read.
28
+ - Use sparingly when no workflow builder is warranted and nested sync `flatMap` is hard to read.
29
29
  - Never use for `ResultAsync` or anything with `await`.
30
30
 
31
31
  ## Layer 1 — Low-Level Helpers
@@ -35,7 +35,7 @@ Use low-level helpers when the operation is small and direct.
35
35
  ```ts
36
36
  return asyncAfter(
37
37
  // Sync validation boundary. If Zod throws, map the thrown value to Error.
38
- trySync(() => ArtifactSchema.parse(artifact), toError)(),
38
+ trySync(() => ArtifactSchema.parse(artifact), toError),
39
39
 
40
40
  // Async IO boundary. Promise rejection becomes Err(Error).
41
41
  (validated) =>
@@ -169,7 +169,7 @@ function loadProfileSummary(id: string): ResultAsync<ProfileSummary, Error> {
169
169
  Use fluent `Railway.*` when it removes manual bridges like:
170
170
 
171
171
  - `fromResult(...)`
172
- - nested `flatMapResult(...)`
172
+ - nested `flatMap(...)`
173
173
  - positional tuple destructuring for parallel work
174
174
  - manual context-carrying objects after every step
175
175
 
@@ -212,7 +212,7 @@ Each step function is independently testable — call it with a hand-built conte
212
212
  Use it only when:
213
213
 
214
214
  - every step is sync
215
- - the function body has nested `flatMapResult` / `mapResult`
215
+ - the function body has nested `flatMap` / `map`
216
216
  - a full `Railway.*` workflow would be overkill
217
217
 
218
218
  ```ts
@@ -342,7 +342,7 @@ Better:
342
342
 
343
343
  ```ts
344
344
  return asyncAfter(
345
- trySync(() => ArtifactSchema.parse(artifact), toError)(),
345
+ trySync(() => ArtifactSchema.parse(artifact), toError),
346
346
  (validated) =>
347
347
  tryAsync(
348
348
  getDb()
@@ -375,14 +375,14 @@ Consider extracting reusable step functions when:
375
375
  Consider `tryGen` when:
376
376
 
377
377
  - all steps are sync
378
- - the current code is a nested `flatMapResult` tree
378
+ - the current code is a nested `flatMap` tree
379
379
  - a workflow builder would introduce unnecessary named context
380
380
 
381
381
  Stay low-level when:
382
382
 
383
383
  - the function has one or two steps
384
384
  - the code is library internals
385
- - the operation is already readable with `asyncAfter`, `.flatMap`, or `mapResult`
385
+ - the operation is already readable with `asyncAfter`, `.flatMap`, or `map`
386
386
 
387
387
  ## Maintainer Workflow For Case Studies
388
388