@konker.dev/neverthrow-r 0.0.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/LICENSE +15 -0
- package/README.md +142 -0
- package/dist/async.d.ts +179 -0
- package/dist/async.d.ts.map +1 -0
- package/dist/async.js +178 -0
- package/dist/async.js.map +1 -0
- package/dist/bridges.d.ts +132 -0
- package/dist/bridges.d.ts.map +1 -0
- package/dist/bridges.js +131 -0
- package/dist/bridges.js.map +1 -0
- package/dist/constructors.d.ts +214 -0
- package/dist/constructors.d.ts.map +1 -0
- package/dist/constructors.js +213 -0
- package/dist/constructors.js.map +1 -0
- package/dist/do.d.ts +137 -0
- package/dist/do.d.ts.map +1 -0
- package/dist/do.js +137 -0
- package/dist/do.js.map +1 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +40 -0
- package/dist/index.js.map +1 -0
- package/dist/pipe.d.ts +84 -0
- package/dist/pipe.d.ts.map +1 -0
- package/dist/pipe.js +31 -0
- package/dist/pipe.js.map +1 -0
- package/dist/provide.d.ts +115 -0
- package/dist/provide.d.ts.map +1 -0
- package/dist/provide.js +35 -0
- package/dist/provide.js.map +1 -0
- package/dist/sync.d.ts +317 -0
- package/dist/sync.d.ts.map +1 -0
- package/dist/sync.js +316 -0
- package/dist/sync.js.map +1 -0
- package/dist/types.d.ts +171 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +44 -0
- package/dist/types.js.map +1 -0
- package/package.json +117 -0
package/dist/bridges.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sync→async bridge operators: take a `ResultR` and return a `ResultAsyncR`,
|
|
3
|
+
* promoting the chain onto the async track mid-pipeline.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* Reach for this module when a chain that starts synchronously needs to
|
|
7
|
+
* await — typically an I/O call partway through. Three flavours, mirroring
|
|
8
|
+
* the corresponding sync combinators but lifting the operand into
|
|
9
|
+
* `ResultAsyncR`:
|
|
10
|
+
*
|
|
11
|
+
* - {@link asyncMap} — promote via a `Promise`-returning transform.
|
|
12
|
+
* - {@link asyncAndThen} — promote via a `ResultAsyncR`-returning step.
|
|
13
|
+
* - {@link asyncAndThrough} — promote via a fallible async tee.
|
|
14
|
+
*
|
|
15
|
+
* Every step *after* the bridge must come from {@link async} (the
|
|
16
|
+
* `*Async`-suffixed combinators); there is no async→sync bridge.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { okR } from '@konker.dev/neverthrow-r/constructors';
|
|
21
|
+
* import { mapAsync } from '@konker.dev/neverthrow-r/async';
|
|
22
|
+
* import { asyncMap } from '@konker.dev/neverthrow-r/bridges';
|
|
23
|
+
* import { pipe } from '@konker.dev/neverthrow-r/pipe';
|
|
24
|
+
*
|
|
25
|
+
* const program = pipe(
|
|
26
|
+
* okR<number>(2),
|
|
27
|
+
* asyncMap(async (n) => n + 1), // sync → async here
|
|
28
|
+
* mapAsync(async (n) => n * 10), // continue async
|
|
29
|
+
* );
|
|
30
|
+
*
|
|
31
|
+
* program(undefined); // ResultAsync resolving to Ok(30)
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @module
|
|
35
|
+
*/
|
|
36
|
+
/**
|
|
37
|
+
* Promotes a `ResultR` to a `ResultAsyncR` by applying a `Promise`-returning
|
|
38
|
+
* transform to the success value.
|
|
39
|
+
*
|
|
40
|
+
* @remarks
|
|
41
|
+
* The sync→async sibling of {@link map}. After this step the chain is async;
|
|
42
|
+
* follow with {@link mapAsync}, {@link andThenAsync}, etc.
|
|
43
|
+
*
|
|
44
|
+
* @typeParam A - The input success type.
|
|
45
|
+
* @typeParam B - The output success type.
|
|
46
|
+
*
|
|
47
|
+
* @param f - `Promise`-returning transform from `A` to `B`.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { okR } from '@konker.dev/neverthrow-r/constructors';
|
|
52
|
+
* import { asyncMap } from '@konker.dev/neverthrow-r/bridges';
|
|
53
|
+
* import { pipe } from '@konker.dev/neverthrow-r/pipe';
|
|
54
|
+
*
|
|
55
|
+
* const program = pipe(okR<number>(2), asyncMap(async (n) => n * 2));
|
|
56
|
+
* program(undefined); // ResultAsync resolving to Ok(4)
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* @see {@link map}
|
|
60
|
+
* @see {@link mapAsync}
|
|
61
|
+
*/
|
|
62
|
+
export const asyncMap = (f) => (rr) => (r) => rr(r).asyncMap(f);
|
|
63
|
+
/**
|
|
64
|
+
* Promotes a `ResultR` to a `ResultAsyncR` by chaining a
|
|
65
|
+
* `ResultAsyncR`-returning step.
|
|
66
|
+
*
|
|
67
|
+
* @remarks
|
|
68
|
+
* Sync→async sibling of {@link andThen}. The continuation's requirements
|
|
69
|
+
* `R2` are intersected (`R1 & R2`); its error type `E2` is unioned
|
|
70
|
+
* (`E1 | E2`).
|
|
71
|
+
*
|
|
72
|
+
* @typeParam A - The previous step's success type.
|
|
73
|
+
* @typeParam R2 - The continuation's requirements.
|
|
74
|
+
* @typeParam B - The continuation's success type.
|
|
75
|
+
* @typeParam E2 - The continuation's error type.
|
|
76
|
+
*
|
|
77
|
+
* @param f - Function from the previous success value to a `ResultAsyncR`.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* import { okAsyncR, okR } from '@konker.dev/neverthrow-r/constructors';
|
|
82
|
+
* import { asyncAndThen } from '@konker.dev/neverthrow-r/bridges';
|
|
83
|
+
* import { pipe } from '@konker.dev/neverthrow-r/pipe';
|
|
84
|
+
*
|
|
85
|
+
* const program = pipe(
|
|
86
|
+
* okR<number>(2),
|
|
87
|
+
* asyncAndThen((n) => okAsyncR<string>(`got ${n}`)),
|
|
88
|
+
* );
|
|
89
|
+
*
|
|
90
|
+
* program(undefined); // ResultAsync resolving to Ok('got 2')
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* @see {@link andThen}
|
|
94
|
+
* @see {@link andThenAsync}
|
|
95
|
+
*/
|
|
96
|
+
export const asyncAndThen = (f) => (rr) => (r) => rr(r).asyncAndThen((a) => f(a)(r));
|
|
97
|
+
/**
|
|
98
|
+
* Promotes a `ResultR` to a `ResultAsyncR` via a fallible async tee that
|
|
99
|
+
* propagates the *original* success value.
|
|
100
|
+
*
|
|
101
|
+
* @remarks
|
|
102
|
+
* Sync→async sibling of {@link andThrough}. The tee step is itself a
|
|
103
|
+
* `ResultAsyncR` that can fail; on success its value is discarded and the
|
|
104
|
+
* chain continues with the original success value.
|
|
105
|
+
*
|
|
106
|
+
* @typeParam T - The success type (preserved through the step on success).
|
|
107
|
+
* @typeParam R2 - The tee's requirements.
|
|
108
|
+
* @typeParam F - The tee's error type.
|
|
109
|
+
*
|
|
110
|
+
* @param f - Function from the success value to a `ResultAsyncR` whose value
|
|
111
|
+
* is ignored on success.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* import { okAsyncR, okR } from '@konker.dev/neverthrow-r/constructors';
|
|
116
|
+
* import { asyncAndThrough } from '@konker.dev/neverthrow-r/bridges';
|
|
117
|
+
* import { pipe } from '@konker.dev/neverthrow-r/pipe';
|
|
118
|
+
*
|
|
119
|
+
* const program = pipe(
|
|
120
|
+
* okR<number>(2),
|
|
121
|
+
* asyncAndThrough((n) => okAsyncR<unknown>(`logged ${n}`)),
|
|
122
|
+
* );
|
|
123
|
+
*
|
|
124
|
+
* program(undefined); // ResultAsync resolving to Ok(2)
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* @see {@link andThrough}
|
|
128
|
+
* @see {@link andThroughAsync}
|
|
129
|
+
*/
|
|
130
|
+
export const asyncAndThrough = (f) => (rr) => (r) => rr(r).asyncAndThrough((t) => f(t)(r));
|
|
131
|
+
//# sourceMappingURL=bridges.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bridges.js","sourceRoot":"","sources":["../src/bridges.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAIH;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,CAAC,MAAM,QAAQ,GACnB,CAAO,CAAuB,EAAE,EAAE,CAClC,CAAO,EAAoB,EAAyB,EAAE,CACtD,CAAC,CAAC,EAAE,EAAE,CACJ,EAAE,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AAEtB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,CAAC,MAAM,YAAY,GACvB,CAAe,CAAoC,EAAE,EAAE,CACvD,CAAS,EAAsB,EAAqC,EAAE,CACtE,CAAC,CAAC,EAAE,EAAE,CACJ,EAAE,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAEvC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,CAAC,MAAM,eAAe,GAC1B,CAAW,CAAyC,EAAE,EAAE,CACxD,CAAQ,EAAqB,EAAmC,EAAE,CAClE,CAAC,CAAC,EAAE,EAAE,CACJ,EAAE,CAAC,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Entry points for building `ResultR` / `ResultAsyncR` values from scratch or
|
|
3
|
+
* from existing `neverthrow` values. Use these where a pipeline starts.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* - {@link okR} / {@link errR} / {@link okAsyncR} / {@link errAsyncR} — lift a
|
|
7
|
+
* plain value (or error) into a `ResultR` / `ResultAsyncR` with no
|
|
8
|
+
* requirements (`R = unknown`).
|
|
9
|
+
* - {@link fromResult} / {@link fromResultAsync} — lift an existing neverthrow
|
|
10
|
+
* `Result` / `ResultAsync` into the `R`-channel layer with no requirements.
|
|
11
|
+
* - {@link asks} / {@link ask} — build a `ResultR` whose only effect is to
|
|
12
|
+
* read from the environment, declaring `R` in the process.
|
|
13
|
+
*
|
|
14
|
+
* Any non-trivial requirements (a database handle, a clock, …) are introduced
|
|
15
|
+
* with `asks`; the rest of the pipeline picks them up via intersection in
|
|
16
|
+
* `andThen`-style operators.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { asks, okR } from '@konker.dev/neverthrow-r/constructors';
|
|
21
|
+
* import { andThen, map } from '@konker.dev/neverthrow-r/sync';
|
|
22
|
+
* import { pipe } from '@konker.dev/neverthrow-r/pipe';
|
|
23
|
+
*
|
|
24
|
+
* type Clock = { now: () => Date };
|
|
25
|
+
*
|
|
26
|
+
* const year = asks((r: Clock) => r.now().getFullYear());
|
|
27
|
+
*
|
|
28
|
+
* const program = pipe(
|
|
29
|
+
* okR<number>(2024),
|
|
30
|
+
* map((n) => n + 1),
|
|
31
|
+
* andThen(() => year),
|
|
32
|
+
* );
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @module
|
|
36
|
+
*/
|
|
37
|
+
import type { Result, ResultAsync } from 'neverthrow';
|
|
38
|
+
import type { ResultAsyncR, ResultR } from './types.js';
|
|
39
|
+
/**
|
|
40
|
+
* Lifts a plain success value into a `ResultR` with no requirements.
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* Equivalent to `() => ok(value)`. The `R` parameter defaults to `unknown`
|
|
44
|
+
* because the constructor doesn't read from the environment.
|
|
45
|
+
*
|
|
46
|
+
* @typeParam T - The success type.
|
|
47
|
+
* @typeParam E - The error type (defaults to `never` since this always
|
|
48
|
+
* succeeds).
|
|
49
|
+
*
|
|
50
|
+
* @param value - The value to wrap in an `Ok`.
|
|
51
|
+
* @returns A `ResultR<unknown, T, E>` that ignores its environment and yields
|
|
52
|
+
* `Ok(value)`.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { okR } from '@konker.dev/neverthrow-r/constructors';
|
|
57
|
+
*
|
|
58
|
+
* const two = okR<number>(2);
|
|
59
|
+
* two(undefined); // Ok(2)
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @see {@link errR}
|
|
63
|
+
* @see {@link okAsyncR}
|
|
64
|
+
*/
|
|
65
|
+
export declare const okR: <T, E = never>(value: T) => ResultR<unknown, T, E>;
|
|
66
|
+
/**
|
|
67
|
+
* Lifts a plain error value into a `ResultR` with no requirements.
|
|
68
|
+
*
|
|
69
|
+
* @typeParam E - The error type.
|
|
70
|
+
* @typeParam T - The success type (defaults to `never` since this always
|
|
71
|
+
* fails).
|
|
72
|
+
*
|
|
73
|
+
* @param error - The error value to wrap in an `Err`.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* import { errR } from '@konker.dev/neverthrow-r/constructors';
|
|
78
|
+
*
|
|
79
|
+
* const fail = errR<string>('boom');
|
|
80
|
+
* fail(undefined); // Err('boom')
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* @see {@link okR}
|
|
84
|
+
* @see {@link errAsyncR}
|
|
85
|
+
*/
|
|
86
|
+
export declare const errR: <E, T = never>(error: E) => ResultR<unknown, T, E>;
|
|
87
|
+
/**
|
|
88
|
+
* Async sibling of {@link okR}: lifts a plain success value into a
|
|
89
|
+
* `ResultAsyncR` with no requirements.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* import { okAsyncR } from '@konker.dev/neverthrow-r/constructors';
|
|
94
|
+
*
|
|
95
|
+
* const program = okAsyncR<number>(2);
|
|
96
|
+
* program(undefined); // ResultAsync resolving to Ok(2)
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* @see {@link okR}
|
|
100
|
+
*/
|
|
101
|
+
export declare const okAsyncR: <T, E = never>(value: T) => ResultAsyncR<unknown, T, E>;
|
|
102
|
+
/**
|
|
103
|
+
* Async sibling of {@link errR}: lifts a plain error value into a
|
|
104
|
+
* `ResultAsyncR` with no requirements.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* import { errAsyncR } from '@konker.dev/neverthrow-r/constructors';
|
|
109
|
+
*
|
|
110
|
+
* const fail = errAsyncR<string>('boom');
|
|
111
|
+
* fail(undefined); // ResultAsync resolving to Err('boom')
|
|
112
|
+
* ```
|
|
113
|
+
*
|
|
114
|
+
* @see {@link errR}
|
|
115
|
+
*/
|
|
116
|
+
export declare const errAsyncR: <E, T = never>(error: E) => ResultAsyncR<unknown, T, E>;
|
|
117
|
+
/**
|
|
118
|
+
* Lifts an existing neverthrow `Result<T, E>` into a `ResultR<unknown, T, E>`.
|
|
119
|
+
*
|
|
120
|
+
* @remarks
|
|
121
|
+
* Useful at the seam where existing neverthrow-using code is being adapted
|
|
122
|
+
* into a pipeline that wants the `R` channel — wrap the legacy value once and
|
|
123
|
+
* compose normally thereafter.
|
|
124
|
+
*
|
|
125
|
+
* @typeParam T - The success type.
|
|
126
|
+
* @typeParam E - The error type.
|
|
127
|
+
*
|
|
128
|
+
* @param r - The existing `Result` to lift.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```ts
|
|
132
|
+
* import { ok } from 'neverthrow';
|
|
133
|
+
* import { fromResult } from '@konker.dev/neverthrow-r/constructors';
|
|
134
|
+
*
|
|
135
|
+
* const existing = ok<number, string>(42);
|
|
136
|
+
* const lifted = fromResult(existing);
|
|
137
|
+
* lifted(undefined); // Ok(42)
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* @see {@link fromResultAsync}
|
|
141
|
+
*/
|
|
142
|
+
export declare const fromResult: <T, E>(r: Result<T, E>) => ResultR<unknown, T, E>;
|
|
143
|
+
/**
|
|
144
|
+
* Async sibling of {@link fromResult}: lifts an existing `ResultAsync<T, E>`
|
|
145
|
+
* into a `ResultAsyncR<unknown, T, E>`.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* import { okAsync } from 'neverthrow';
|
|
150
|
+
* import { fromResultAsync } from '@konker.dev/neverthrow-r/constructors';
|
|
151
|
+
*
|
|
152
|
+
* const existing = okAsync<number, string>(42);
|
|
153
|
+
* const lifted = fromResultAsync(existing);
|
|
154
|
+
* lifted(undefined); // ResultAsync resolving to Ok(42)
|
|
155
|
+
* ```
|
|
156
|
+
*
|
|
157
|
+
* @see {@link fromResult}
|
|
158
|
+
*/
|
|
159
|
+
export declare const fromResultAsync: <T, E>(ra: ResultAsync<T, E>) => ResultAsyncR<unknown, T, E>;
|
|
160
|
+
/**
|
|
161
|
+
* Builds a `ResultR` whose value is computed from the environment. This is
|
|
162
|
+
* how requirements are *introduced* into a chain.
|
|
163
|
+
*
|
|
164
|
+
* @remarks
|
|
165
|
+
* `asks(f)` is equivalent to `(r) => ok(f(r))`. The argument's parameter type
|
|
166
|
+
* fixes the `R` channel for the rest of the pipeline; downstream operators
|
|
167
|
+
* intersect any further requirements.
|
|
168
|
+
*
|
|
169
|
+
* Use this when a step depends on something in the environment (a config
|
|
170
|
+
* value, a service handle, the current time). For just reading the entire
|
|
171
|
+
* environment unchanged, see {@link ask}.
|
|
172
|
+
*
|
|
173
|
+
* @typeParam R - The requirements (environment) read from.
|
|
174
|
+
* @typeParam A - The value computed from the environment.
|
|
175
|
+
*
|
|
176
|
+
* @param f - A pure function from environment to value.
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```ts
|
|
180
|
+
* import { asks } from '@konker.dev/neverthrow-r/constructors';
|
|
181
|
+
*
|
|
182
|
+
* type Config = { multiplier: number };
|
|
183
|
+
*
|
|
184
|
+
* const multiplier = asks((r: Config) => r.multiplier);
|
|
185
|
+
* multiplier({ multiplier: 10 }); // Ok(10)
|
|
186
|
+
* ```
|
|
187
|
+
*
|
|
188
|
+
* @see {@link ask}
|
|
189
|
+
*/
|
|
190
|
+
export declare const asks: <R, A>(f: (r: R) => A) => ResultR<R, A, never>;
|
|
191
|
+
/**
|
|
192
|
+
* Reads the entire environment as the success value.
|
|
193
|
+
*
|
|
194
|
+
* @remarks
|
|
195
|
+
* Specialisation of {@link asks} with the identity function. Most useful when
|
|
196
|
+
* a step wants the whole environment passed downstream — e.g. to forward it
|
|
197
|
+
* into a sub-pipeline.
|
|
198
|
+
*
|
|
199
|
+
* @typeParam R - The requirements (environment), surfaced as the success type.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```ts
|
|
203
|
+
* import { ask } from '@konker.dev/neverthrow-r/constructors';
|
|
204
|
+
*
|
|
205
|
+
* type Config = { multiplier: number };
|
|
206
|
+
*
|
|
207
|
+
* const env = ask<Config>();
|
|
208
|
+
* env({ multiplier: 10 }); // Ok({ multiplier: 10 })
|
|
209
|
+
* ```
|
|
210
|
+
*
|
|
211
|
+
* @see {@link asks}
|
|
212
|
+
*/
|
|
213
|
+
export declare const ask: <R>() => ResultR<R, R, never>;
|
|
214
|
+
//# sourceMappingURL=constructors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constructors.d.ts","sourceRoot":"","sources":["../src/constructors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAGtD,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAExD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,GAAG,GACb,CAAC,EAAE,CAAC,G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,GAAG,GAAI,CAAC,OAAK,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,CAAsB,CAAC"}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Entry points for building `ResultR` / `ResultAsyncR` values from scratch or
|
|
3
|
+
* from existing `neverthrow` values. Use these where a pipeline starts.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* - {@link okR} / {@link errR} / {@link okAsyncR} / {@link errAsyncR} — lift a
|
|
7
|
+
* plain value (or error) into a `ResultR` / `ResultAsyncR` with no
|
|
8
|
+
* requirements (`R = unknown`).
|
|
9
|
+
* - {@link fromResult} / {@link fromResultAsync} — lift an existing neverthrow
|
|
10
|
+
* `Result` / `ResultAsync` into the `R`-channel layer with no requirements.
|
|
11
|
+
* - {@link asks} / {@link ask} — build a `ResultR` whose only effect is to
|
|
12
|
+
* read from the environment, declaring `R` in the process.
|
|
13
|
+
*
|
|
14
|
+
* Any non-trivial requirements (a database handle, a clock, …) are introduced
|
|
15
|
+
* with `asks`; the rest of the pipeline picks them up via intersection in
|
|
16
|
+
* `andThen`-style operators.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { asks, okR } from '@konker.dev/neverthrow-r/constructors';
|
|
21
|
+
* import { andThen, map } from '@konker.dev/neverthrow-r/sync';
|
|
22
|
+
* import { pipe } from '@konker.dev/neverthrow-r/pipe';
|
|
23
|
+
*
|
|
24
|
+
* type Clock = { now: () => Date };
|
|
25
|
+
*
|
|
26
|
+
* const year = asks((r: Clock) => r.now().getFullYear());
|
|
27
|
+
*
|
|
28
|
+
* const program = pipe(
|
|
29
|
+
* okR<number>(2024),
|
|
30
|
+
* map((n) => n + 1),
|
|
31
|
+
* andThen(() => year),
|
|
32
|
+
* );
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @module
|
|
36
|
+
*/
|
|
37
|
+
import { err, errAsync, ok, okAsync } from 'neverthrow';
|
|
38
|
+
/**
|
|
39
|
+
* Lifts a plain success value into a `ResultR` with no requirements.
|
|
40
|
+
*
|
|
41
|
+
* @remarks
|
|
42
|
+
* Equivalent to `() => ok(value)`. The `R` parameter defaults to `unknown`
|
|
43
|
+
* because the constructor doesn't read from the environment.
|
|
44
|
+
*
|
|
45
|
+
* @typeParam T - The success type.
|
|
46
|
+
* @typeParam E - The error type (defaults to `never` since this always
|
|
47
|
+
* succeeds).
|
|
48
|
+
*
|
|
49
|
+
* @param value - The value to wrap in an `Ok`.
|
|
50
|
+
* @returns A `ResultR<unknown, T, E>` that ignores its environment and yields
|
|
51
|
+
* `Ok(value)`.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* import { okR } from '@konker.dev/neverthrow-r/constructors';
|
|
56
|
+
*
|
|
57
|
+
* const two = okR<number>(2);
|
|
58
|
+
* two(undefined); // Ok(2)
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @see {@link errR}
|
|
62
|
+
* @see {@link okAsyncR}
|
|
63
|
+
*/
|
|
64
|
+
export const okR = (value) => () => ok(value);
|
|
65
|
+
/**
|
|
66
|
+
* Lifts a plain error value into a `ResultR` with no requirements.
|
|
67
|
+
*
|
|
68
|
+
* @typeParam E - The error type.
|
|
69
|
+
* @typeParam T - The success type (defaults to `never` since this always
|
|
70
|
+
* fails).
|
|
71
|
+
*
|
|
72
|
+
* @param error - The error value to wrap in an `Err`.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* import { errR } from '@konker.dev/neverthrow-r/constructors';
|
|
77
|
+
*
|
|
78
|
+
* const fail = errR<string>('boom');
|
|
79
|
+
* fail(undefined); // Err('boom')
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* @see {@link okR}
|
|
83
|
+
* @see {@link errAsyncR}
|
|
84
|
+
*/
|
|
85
|
+
export const errR = (error) => () => err(error);
|
|
86
|
+
/**
|
|
87
|
+
* Async sibling of {@link okR}: lifts a plain success value into a
|
|
88
|
+
* `ResultAsyncR` with no requirements.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```ts
|
|
92
|
+
* import { okAsyncR } from '@konker.dev/neverthrow-r/constructors';
|
|
93
|
+
*
|
|
94
|
+
* const program = okAsyncR<number>(2);
|
|
95
|
+
* program(undefined); // ResultAsync resolving to Ok(2)
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* @see {@link okR}
|
|
99
|
+
*/
|
|
100
|
+
export const okAsyncR = (value) => () => okAsync(value);
|
|
101
|
+
/**
|
|
102
|
+
* Async sibling of {@link errR}: lifts a plain error value into a
|
|
103
|
+
* `ResultAsyncR` with no requirements.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* import { errAsyncR } from '@konker.dev/neverthrow-r/constructors';
|
|
108
|
+
*
|
|
109
|
+
* const fail = errAsyncR<string>('boom');
|
|
110
|
+
* fail(undefined); // ResultAsync resolving to Err('boom')
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* @see {@link errR}
|
|
114
|
+
*/
|
|
115
|
+
export const errAsyncR = (error) => () => errAsync(error);
|
|
116
|
+
/**
|
|
117
|
+
* Lifts an existing neverthrow `Result<T, E>` into a `ResultR<unknown, T, E>`.
|
|
118
|
+
*
|
|
119
|
+
* @remarks
|
|
120
|
+
* Useful at the seam where existing neverthrow-using code is being adapted
|
|
121
|
+
* into a pipeline that wants the `R` channel — wrap the legacy value once and
|
|
122
|
+
* compose normally thereafter.
|
|
123
|
+
*
|
|
124
|
+
* @typeParam T - The success type.
|
|
125
|
+
* @typeParam E - The error type.
|
|
126
|
+
*
|
|
127
|
+
* @param r - The existing `Result` to lift.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* import { ok } from 'neverthrow';
|
|
132
|
+
* import { fromResult } from '@konker.dev/neverthrow-r/constructors';
|
|
133
|
+
*
|
|
134
|
+
* const existing = ok<number, string>(42);
|
|
135
|
+
* const lifted = fromResult(existing);
|
|
136
|
+
* lifted(undefined); // Ok(42)
|
|
137
|
+
* ```
|
|
138
|
+
*
|
|
139
|
+
* @see {@link fromResultAsync}
|
|
140
|
+
*/
|
|
141
|
+
export const fromResult = (r) => () => r;
|
|
142
|
+
/**
|
|
143
|
+
* Async sibling of {@link fromResult}: lifts an existing `ResultAsync<T, E>`
|
|
144
|
+
* into a `ResultAsyncR<unknown, T, E>`.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* import { okAsync } from 'neverthrow';
|
|
149
|
+
* import { fromResultAsync } from '@konker.dev/neverthrow-r/constructors';
|
|
150
|
+
*
|
|
151
|
+
* const existing = okAsync<number, string>(42);
|
|
152
|
+
* const lifted = fromResultAsync(existing);
|
|
153
|
+
* lifted(undefined); // ResultAsync resolving to Ok(42)
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* @see {@link fromResult}
|
|
157
|
+
*/
|
|
158
|
+
export const fromResultAsync = (ra) => () => ra;
|
|
159
|
+
/**
|
|
160
|
+
* Builds a `ResultR` whose value is computed from the environment. This is
|
|
161
|
+
* how requirements are *introduced* into a chain.
|
|
162
|
+
*
|
|
163
|
+
* @remarks
|
|
164
|
+
* `asks(f)` is equivalent to `(r) => ok(f(r))`. The argument's parameter type
|
|
165
|
+
* fixes the `R` channel for the rest of the pipeline; downstream operators
|
|
166
|
+
* intersect any further requirements.
|
|
167
|
+
*
|
|
168
|
+
* Use this when a step depends on something in the environment (a config
|
|
169
|
+
* value, a service handle, the current time). For just reading the entire
|
|
170
|
+
* environment unchanged, see {@link ask}.
|
|
171
|
+
*
|
|
172
|
+
* @typeParam R - The requirements (environment) read from.
|
|
173
|
+
* @typeParam A - The value computed from the environment.
|
|
174
|
+
*
|
|
175
|
+
* @param f - A pure function from environment to value.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* import { asks } from '@konker.dev/neverthrow-r/constructors';
|
|
180
|
+
*
|
|
181
|
+
* type Config = { multiplier: number };
|
|
182
|
+
*
|
|
183
|
+
* const multiplier = asks((r: Config) => r.multiplier);
|
|
184
|
+
* multiplier({ multiplier: 10 }); // Ok(10)
|
|
185
|
+
* ```
|
|
186
|
+
*
|
|
187
|
+
* @see {@link ask}
|
|
188
|
+
*/
|
|
189
|
+
export const asks = (f) => (r) => ok(f(r));
|
|
190
|
+
/**
|
|
191
|
+
* Reads the entire environment as the success value.
|
|
192
|
+
*
|
|
193
|
+
* @remarks
|
|
194
|
+
* Specialisation of {@link asks} with the identity function. Most useful when
|
|
195
|
+
* a step wants the whole environment passed downstream — e.g. to forward it
|
|
196
|
+
* into a sub-pipeline.
|
|
197
|
+
*
|
|
198
|
+
* @typeParam R - The requirements (environment), surfaced as the success type.
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```ts
|
|
202
|
+
* import { ask } from '@konker.dev/neverthrow-r/constructors';
|
|
203
|
+
*
|
|
204
|
+
* type Config = { multiplier: number };
|
|
205
|
+
*
|
|
206
|
+
* const env = ask<Config>();
|
|
207
|
+
* env({ multiplier: 10 }); // Ok({ multiplier: 10 })
|
|
208
|
+
* ```
|
|
209
|
+
*
|
|
210
|
+
* @see {@link asks}
|
|
211
|
+
*/
|
|
212
|
+
export const ask = () => asks((r) => r);
|
|
213
|
+
//# sourceMappingURL=constructors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constructors.js","sourceRoot":"","sources":["../src/constructors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAGH,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAIxD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,M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