@zireal/result-kit 1.0.1 → 1.0.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.
- package/dist/core/index.cjs +4 -0
- package/dist/core/index.d.cts +3 -0
- package/dist/core/index.d.mts +3 -0
- package/dist/core/index.mjs +2 -0
- package/dist/index-DioLW1WH.d.cts +482 -0
- package/dist/index-SLgCKCJe.d.mts +482 -0
- package/dist/index.cjs +5 -0
- package/dist/index.d.cts +3 -0
- package/dist/index.d.mts +3 -0
- package/dist/index.mjs +3 -0
- package/dist/nest/index.cjs +104 -0
- package/dist/nest/index.d.cts +92 -0
- package/dist/nest/index.d.mts +92 -0
- package/dist/nest/index.mjs +101 -0
- package/dist/result-DdlZMLaP.d.cts +44 -0
- package/dist/result-hklHYniG.d.mts +44 -0
- package/dist/result-kit-DdhY1ph2.cjs +565 -0
- package/dist/result-kit-pgireOSz.mjs +554 -0
- package/package.json +1 -1
|
@@ -0,0 +1,554 @@
|
|
|
1
|
+
//#region src/core/error.ts
|
|
2
|
+
/**
|
|
3
|
+
* Determines whether an unknown value satisfies the runtime contract of
|
|
4
|
+
* {@link TypedError}.
|
|
5
|
+
*
|
|
6
|
+
* A valid typed error must be an object with string `type` and `message`
|
|
7
|
+
* fields. When present, `details` must be a plain object-like record rather
|
|
8
|
+
* than `null` or an array.
|
|
9
|
+
*
|
|
10
|
+
* @param error Value to validate at runtime.
|
|
11
|
+
* @returns `true` when `error` matches the expected structured error shape.
|
|
12
|
+
*/
|
|
13
|
+
const isTypedError = (error) => {
|
|
14
|
+
if (!error || typeof error !== "object") return false;
|
|
15
|
+
const candidate = error;
|
|
16
|
+
if (typeof candidate.type !== "string" || typeof candidate.message !== "string") return false;
|
|
17
|
+
if ("details" in candidate && candidate.details !== void 0 && (candidate.details === null || typeof candidate.details !== "object" || Array.isArray(candidate.details))) return false;
|
|
18
|
+
return true;
|
|
19
|
+
};
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/core/result-kit.ts
|
|
22
|
+
/**
|
|
23
|
+
* Static utilities for creating, transforming, inspecting, and consuming
|
|
24
|
+
* {@link Result} values.
|
|
25
|
+
*
|
|
26
|
+
* The class is intentionally abstract because it acts as a namespaced toolbox
|
|
27
|
+
* rather than a type meant to be instantiated.
|
|
28
|
+
*/
|
|
29
|
+
var ResultKit = class {
|
|
30
|
+
/**
|
|
31
|
+
* Determines whether a result contains a successful value.
|
|
32
|
+
*
|
|
33
|
+
* Use this as a type guard when branching manually over a {@link Result}.
|
|
34
|
+
*
|
|
35
|
+
* @param result Result to inspect.
|
|
36
|
+
* @returns `true` when `result` is a {@link Success}.
|
|
37
|
+
*/
|
|
38
|
+
static isSuccess(result) {
|
|
39
|
+
return result.ok === true;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Determines whether a result contains a failure value.
|
|
43
|
+
*
|
|
44
|
+
* Use this as a type guard when branching manually over a {@link Result}.
|
|
45
|
+
*
|
|
46
|
+
* @param result Result to inspect.
|
|
47
|
+
* @returns `true` when `result` is a {@link Failure}.
|
|
48
|
+
*/
|
|
49
|
+
static isFailure(result) {
|
|
50
|
+
return result.ok === false;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Constructs a successful result.
|
|
54
|
+
*
|
|
55
|
+
* Prefer this helper over hand-writing `{ ok: true, value }` so result
|
|
56
|
+
* creation stays uniform across the codebase.
|
|
57
|
+
*
|
|
58
|
+
* @param value Successful value to wrap.
|
|
59
|
+
* @returns A {@link Success} containing `value`.
|
|
60
|
+
*/
|
|
61
|
+
static success(value) {
|
|
62
|
+
return {
|
|
63
|
+
ok: true,
|
|
64
|
+
value
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Constructs a failed result with any error payload.
|
|
69
|
+
*
|
|
70
|
+
* This is the generic failure constructor for non-`TypedError` failures.
|
|
71
|
+
*
|
|
72
|
+
* @param error Error payload to wrap.
|
|
73
|
+
* @returns A {@link Failure} containing `error`.
|
|
74
|
+
*/
|
|
75
|
+
static failure(error) {
|
|
76
|
+
return {
|
|
77
|
+
ok: false,
|
|
78
|
+
error
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Constructs a failed result from a {@link TypedError}.
|
|
83
|
+
*
|
|
84
|
+
* This is a convenience wrapper around {@link ResultKit.failure} for the
|
|
85
|
+
* package's structured error convention.
|
|
86
|
+
*
|
|
87
|
+
* @param error Structured error payload to wrap.
|
|
88
|
+
* @returns A failed result carrying the provided typed error.
|
|
89
|
+
*/
|
|
90
|
+
static fail(error) {
|
|
91
|
+
return this.failure(error);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Determines whether an unknown value satisfies the runtime shape of
|
|
95
|
+
* {@link TypedError}.
|
|
96
|
+
*
|
|
97
|
+
* @param error Value to validate.
|
|
98
|
+
* @returns `true` when `error` looks like a typed error object.
|
|
99
|
+
*/
|
|
100
|
+
static isTypedError(error) {
|
|
101
|
+
return isTypedError(error);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Transforms the success value of a result while leaving failures untouched.
|
|
105
|
+
*
|
|
106
|
+
* Use this when you want to project successful data without changing the
|
|
107
|
+
* failure channel.
|
|
108
|
+
*
|
|
109
|
+
* @param result Result whose success value may be transformed.
|
|
110
|
+
* @param fn Mapping function applied only when `result` is successful.
|
|
111
|
+
* @returns A new successful result with the mapped value, or the original
|
|
112
|
+
* failure when `result` is unsuccessful.
|
|
113
|
+
*/
|
|
114
|
+
static map(result, fn) {
|
|
115
|
+
return this.isSuccess(result) ? this.success(fn(result.value)) : result;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Transforms both branches of a result in one operation.
|
|
119
|
+
*
|
|
120
|
+
* Use this when success and failure values both need projection into new
|
|
121
|
+
* shapes.
|
|
122
|
+
*
|
|
123
|
+
* @param result Result to transform.
|
|
124
|
+
* @param onSuccess Mapper applied when `result` is successful.
|
|
125
|
+
* @param onFailure Mapper applied when `result` is failed.
|
|
126
|
+
* @returns A result whose success and failure payloads have been mapped into
|
|
127
|
+
* the target types.
|
|
128
|
+
*/
|
|
129
|
+
static bimap(result, onSuccess, onFailure) {
|
|
130
|
+
return this.isSuccess(result) ? this.success(onSuccess(result.value)) : this.failure(onFailure(result.error));
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Asynchronously transforms the success value of a result.
|
|
134
|
+
*
|
|
135
|
+
* Failures are returned unchanged and `fn` is never invoked for them.
|
|
136
|
+
*
|
|
137
|
+
* @param result Result whose success value may be transformed.
|
|
138
|
+
* @param fn Async mapping function applied only when `result` is successful.
|
|
139
|
+
* @returns A promise resolving to a mapped success result or the original
|
|
140
|
+
* failure.
|
|
141
|
+
*/
|
|
142
|
+
static async mapAsync(result, fn) {
|
|
143
|
+
return this.isSuccess(result) ? this.success(await fn(result.value)) : result;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Transforms the failure value of a result while leaving successes untouched.
|
|
147
|
+
*
|
|
148
|
+
* @param result Result whose error may be transformed.
|
|
149
|
+
* @param fn Mapping function applied only when `result` is failed.
|
|
150
|
+
* @returns The original success result or a new failed result containing the
|
|
151
|
+
* mapped error.
|
|
152
|
+
*/
|
|
153
|
+
static mapError(result, fn) {
|
|
154
|
+
return this.isFailure(result) ? this.failure(fn(result.error)) : result;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Asynchronously transforms the failure value of a result.
|
|
158
|
+
*
|
|
159
|
+
* Success values are returned unchanged and `fn` is never invoked for them.
|
|
160
|
+
*
|
|
161
|
+
* @param result Result whose error may be transformed.
|
|
162
|
+
* @param fn Async mapping function applied only when `result` is failed.
|
|
163
|
+
* @returns A promise resolving to the original success result or a new failed
|
|
164
|
+
* result containing the mapped error.
|
|
165
|
+
*/
|
|
166
|
+
static async mapErrorAsync(result, fn) {
|
|
167
|
+
return this.isFailure(result) ? this.failure(await fn(result.error)) : result;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Chains another result-producing operation onto a successful result.
|
|
171
|
+
*
|
|
172
|
+
* This is the standard flat-mapping operation for the success branch and is
|
|
173
|
+
* useful for sequencing dependent computations without nesting `Result`
|
|
174
|
+
* values.
|
|
175
|
+
*
|
|
176
|
+
* @param result Result to continue from.
|
|
177
|
+
* @param fn Function invoked only when `result` is successful.
|
|
178
|
+
* @returns The chained result, or the original failure unchanged.
|
|
179
|
+
*/
|
|
180
|
+
static andThen(result, fn) {
|
|
181
|
+
return this.isSuccess(result) ? fn(result.value) : result;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Asynchronously chains another result-producing operation onto a successful
|
|
185
|
+
* result.
|
|
186
|
+
*
|
|
187
|
+
* @param result Result to continue from.
|
|
188
|
+
* @param fn Async function invoked only when `result` is successful.
|
|
189
|
+
* @returns A promise resolving to the chained result or the original failure.
|
|
190
|
+
*/
|
|
191
|
+
static async andThenAsync(result, fn) {
|
|
192
|
+
return this.isSuccess(result) ? fn(result.value) : result;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Recovers from a failed result by mapping it into a new result.
|
|
196
|
+
*
|
|
197
|
+
* Use this to provide fallback logic, error translation, or alternate
|
|
198
|
+
* retrieval paths for the failure branch.
|
|
199
|
+
*
|
|
200
|
+
* @param result Result to recover from.
|
|
201
|
+
* @param fn Recovery function invoked only when `result` is failed.
|
|
202
|
+
* @returns The original success result or the recovery result.
|
|
203
|
+
*/
|
|
204
|
+
static orElse(result, fn) {
|
|
205
|
+
return this.isFailure(result) ? fn(result.error) : result;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Asynchronously recovers from a failed result by mapping it into a new
|
|
209
|
+
* result.
|
|
210
|
+
*
|
|
211
|
+
* @param result Result to recover from.
|
|
212
|
+
* @param fn Async recovery function invoked only when `result` is failed.
|
|
213
|
+
* @returns A promise resolving to the original success result or the recovery
|
|
214
|
+
* result.
|
|
215
|
+
*/
|
|
216
|
+
static async orElseAsync(result, fn) {
|
|
217
|
+
return this.isFailure(result) ? fn(result.error) : result;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Extracts the success value from a result when present.
|
|
221
|
+
*
|
|
222
|
+
* This helper is intentionally non-throwing. Failures resolve to
|
|
223
|
+
* `undefined`, making it suitable when absence is acceptable.
|
|
224
|
+
*
|
|
225
|
+
* @param result Result to unwrap.
|
|
226
|
+
* @returns The success value, or `undefined` when the result is failed.
|
|
227
|
+
*/
|
|
228
|
+
static unwrap(result) {
|
|
229
|
+
return this.isSuccess(result) ? result.value : void 0;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Extracts the value from a known {@link Success}.
|
|
233
|
+
*
|
|
234
|
+
* Use this when the success branch has already been narrowed and you want a
|
|
235
|
+
* small semantic wrapper around direct property access.
|
|
236
|
+
*
|
|
237
|
+
* @param result Successful result to unwrap.
|
|
238
|
+
* @returns The contained success value.
|
|
239
|
+
*/
|
|
240
|
+
static unwrapSuccess(result) {
|
|
241
|
+
return result.value;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Extracts the error from a known {@link Failure}.
|
|
245
|
+
*
|
|
246
|
+
* Use this when the failure branch has already been narrowed and you want a
|
|
247
|
+
* small semantic wrapper around direct property access.
|
|
248
|
+
*
|
|
249
|
+
* @param result Failed result to unwrap.
|
|
250
|
+
* @returns The contained error value.
|
|
251
|
+
*/
|
|
252
|
+
static unwrapFailure(result) {
|
|
253
|
+
return result.error;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Extracts the success value or returns a provided fallback.
|
|
257
|
+
*
|
|
258
|
+
* The fallback is evaluated eagerly before the call, so use
|
|
259
|
+
* {@link ResultKit.unwrapOrElse} when computing the fallback is expensive.
|
|
260
|
+
*
|
|
261
|
+
* @param result Result to unwrap.
|
|
262
|
+
* @param defaultValue Value to return when `result` is failed.
|
|
263
|
+
* @returns The success value or `defaultValue`.
|
|
264
|
+
*/
|
|
265
|
+
static unwrapOr(result, defaultValue) {
|
|
266
|
+
return this.isSuccess(result) ? result.value : defaultValue;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Extracts the success value or computes a fallback from the failure.
|
|
270
|
+
*
|
|
271
|
+
* The fallback callback is evaluated lazily and receives the failure value.
|
|
272
|
+
*
|
|
273
|
+
* @param result Result to unwrap.
|
|
274
|
+
* @param fn Function used to derive a fallback from the error.
|
|
275
|
+
* @returns The success value or the callback result.
|
|
276
|
+
*/
|
|
277
|
+
static unwrapOrElse(result, fn) {
|
|
278
|
+
return this.isSuccess(result) ? result.value : fn(result.error);
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Asynchronously extracts the success value or computes a fallback from the
|
|
282
|
+
* failure.
|
|
283
|
+
*
|
|
284
|
+
* @param result Result to unwrap.
|
|
285
|
+
* @param fn Async function used to derive a fallback from the error.
|
|
286
|
+
* @returns A promise resolving to the success value or the callback result.
|
|
287
|
+
*/
|
|
288
|
+
static async unwrapOrElseAsync(result, fn) {
|
|
289
|
+
return this.isSuccess(result) ? result.value : fn(result.error);
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Folds a result into a single output value.
|
|
293
|
+
*
|
|
294
|
+
* This is useful at application boundaries where both branches need to
|
|
295
|
+
* produce the same final shape.
|
|
296
|
+
*
|
|
297
|
+
* @param result Result to match against.
|
|
298
|
+
* @param handlers Branch handlers for success and failure.
|
|
299
|
+
* @returns The value returned by the matching handler.
|
|
300
|
+
*/
|
|
301
|
+
static match(result, handlers) {
|
|
302
|
+
return this.isSuccess(result) ? handlers.onSuccess(result.value) : handlers.onFailure(result.error);
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Asynchronously folds a result into a single output value.
|
|
306
|
+
*
|
|
307
|
+
* @param result Result to match against.
|
|
308
|
+
* @param handlers Async branch handlers for success and failure.
|
|
309
|
+
* @returns A promise resolving to the value returned by the matching handler.
|
|
310
|
+
*/
|
|
311
|
+
static async matchAsync(result, handlers) {
|
|
312
|
+
return this.isSuccess(result) ? handlers.onSuccess(result.value) : handlers.onFailure(result.error);
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Runs side effects for a result branch and returns the original result.
|
|
316
|
+
*
|
|
317
|
+
* Use this for logging, metrics, tracing, or other observational work that
|
|
318
|
+
* should not change the result payload.
|
|
319
|
+
*
|
|
320
|
+
* @param result Result to observe.
|
|
321
|
+
* @param handlers Optional side-effect handlers for either branch.
|
|
322
|
+
* @returns The original `result` instance.
|
|
323
|
+
*/
|
|
324
|
+
static tap(result, handlers) {
|
|
325
|
+
if (this.isSuccess(result)) handlers.onSuccess?.(result.value);
|
|
326
|
+
else handlers.onFailure?.(result.error);
|
|
327
|
+
return result;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Asynchronously runs side effects for a result branch and returns the
|
|
331
|
+
* original result.
|
|
332
|
+
*
|
|
333
|
+
* @param result Result to observe.
|
|
334
|
+
* @param handlers Optional async side-effect handlers for either branch.
|
|
335
|
+
* @returns A promise resolving to the original `result` instance after side
|
|
336
|
+
* effects complete.
|
|
337
|
+
*/
|
|
338
|
+
static async tapAsync(result, handlers) {
|
|
339
|
+
if (this.isSuccess(result)) await handlers.onSuccess?.(result.value);
|
|
340
|
+
else await handlers.onFailure?.(result.error);
|
|
341
|
+
return result;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Combines an array of results into a single result of an array.
|
|
345
|
+
*
|
|
346
|
+
* Combination short-circuits on the first failure encountered. When every
|
|
347
|
+
* result succeeds, the returned success contains the collected values in the
|
|
348
|
+
* same order as the input array.
|
|
349
|
+
*
|
|
350
|
+
* @param results Results to combine.
|
|
351
|
+
* @returns A success containing all values, or the first failure encountered.
|
|
352
|
+
*/
|
|
353
|
+
static combine(results) {
|
|
354
|
+
const values = [];
|
|
355
|
+
for (const result of results) {
|
|
356
|
+
if (this.isFailure(result)) return result;
|
|
357
|
+
values.push(result.value);
|
|
358
|
+
}
|
|
359
|
+
return this.success(values);
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Asynchronously combines multiple promised results into one result.
|
|
363
|
+
*
|
|
364
|
+
* All promises are awaited before combination, so this helper does not stop
|
|
365
|
+
* pending asynchronous work after an eventual failure.
|
|
366
|
+
*
|
|
367
|
+
* @param results Promises resolving to results.
|
|
368
|
+
* @returns A promise resolving to the same output as {@link ResultKit.combine}.
|
|
369
|
+
*/
|
|
370
|
+
static async combineAsync(results) {
|
|
371
|
+
return this.combine(await Promise.all(results));
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Combines an array of results while collecting every failure.
|
|
375
|
+
*
|
|
376
|
+
* Successful values are preserved in order. If one or more failures occur,
|
|
377
|
+
* the returned failure contains an array of all collected errors rather than
|
|
378
|
+
* only the first one.
|
|
379
|
+
*
|
|
380
|
+
* @param results Results to combine.
|
|
381
|
+
* @returns A success containing all values when every result succeeds, or a
|
|
382
|
+
* failure containing all collected errors.
|
|
383
|
+
*/
|
|
384
|
+
static combineWithAllErrors(results) {
|
|
385
|
+
const values = [];
|
|
386
|
+
const errors = [];
|
|
387
|
+
for (const result of results) if (this.isSuccess(result)) values.push(result.value);
|
|
388
|
+
else errors.push(result.error);
|
|
389
|
+
return errors.length > 0 ? this.failure(errors) : this.success(values);
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Asynchronously combines multiple promised results while collecting every
|
|
393
|
+
* failure.
|
|
394
|
+
*
|
|
395
|
+
* As with {@link ResultKit.combineAsync}, all promises are awaited before
|
|
396
|
+
* reduction.
|
|
397
|
+
*
|
|
398
|
+
* @param results Promises resolving to results.
|
|
399
|
+
* @returns A promise resolving to the same output as
|
|
400
|
+
* {@link ResultKit.combineWithAllErrors}.
|
|
401
|
+
*/
|
|
402
|
+
static async combineWithAllErrorsAsync(results) {
|
|
403
|
+
return this.combineWithAllErrors(await Promise.all(results));
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Removes one layer of `Result` nesting from the success branch.
|
|
407
|
+
*
|
|
408
|
+
* This is useful after operations that already return a `Result` have been
|
|
409
|
+
* wrapped in another success result.
|
|
410
|
+
*
|
|
411
|
+
* @param result Nested result to flatten.
|
|
412
|
+
* @returns The inner result when successful, or the outer failure unchanged.
|
|
413
|
+
*/
|
|
414
|
+
static flatten(result) {
|
|
415
|
+
return this.isSuccess(result) ? result.value : result;
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Converts a promise that may reject into a promise of `Result`.
|
|
419
|
+
*
|
|
420
|
+
* Rejections are caught and normalized through `errorFn`, allowing async
|
|
421
|
+
* exception sources to participate in explicit result-based control flow.
|
|
422
|
+
*
|
|
423
|
+
* @param promise Promise to execute.
|
|
424
|
+
* @param errorFn Function used to convert an unknown rejection reason into
|
|
425
|
+
* the desired error type.
|
|
426
|
+
* @returns A promise resolving to a successful result for fulfilled values or
|
|
427
|
+
* a failed result for rejected values.
|
|
428
|
+
*/
|
|
429
|
+
static async fromPromise(promise, errorFn) {
|
|
430
|
+
try {
|
|
431
|
+
return this.success(await promise);
|
|
432
|
+
} catch (error) {
|
|
433
|
+
return this.failure(errorFn(error));
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Wraps a synchronous function so thrown exceptions are returned as failures.
|
|
438
|
+
*
|
|
439
|
+
* The resulting function preserves the original parameter list while
|
|
440
|
+
* converting thrown errors with `errorFn`.
|
|
441
|
+
*
|
|
442
|
+
* @param fn Function that may throw.
|
|
443
|
+
* @param errorFn Function used to map an unknown thrown value into the
|
|
444
|
+
* desired error type.
|
|
445
|
+
* @returns A new function that returns `Result` instead of throwing.
|
|
446
|
+
*/
|
|
447
|
+
static fromThrowable(fn, errorFn) {
|
|
448
|
+
return (...args) => {
|
|
449
|
+
try {
|
|
450
|
+
return this.success(fn(...args));
|
|
451
|
+
} catch (error) {
|
|
452
|
+
return this.failure(errorFn(error));
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* Wraps an asynchronous function so thrown exceptions and rejected promises
|
|
458
|
+
* are returned as failures.
|
|
459
|
+
*
|
|
460
|
+
* @param fn Async function that may throw or reject.
|
|
461
|
+
* @param errorFn Function used to map an unknown failure reason into the
|
|
462
|
+
* desired error type.
|
|
463
|
+
* @returns A new function that resolves to `Result` instead of propagating
|
|
464
|
+
* exceptions.
|
|
465
|
+
*/
|
|
466
|
+
static fromThrowableAsync(fn, errorFn) {
|
|
467
|
+
return async (...args) => {
|
|
468
|
+
try {
|
|
469
|
+
return this.success(await fn(...args));
|
|
470
|
+
} catch (error) {
|
|
471
|
+
return this.failure(errorFn(error));
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Converts a nullable value into a result.
|
|
477
|
+
*
|
|
478
|
+
* `null` and `undefined` become failures carrying the provided error. Any
|
|
479
|
+
* other value becomes a success and is narrowed to `NonNullable<T>`.
|
|
480
|
+
*
|
|
481
|
+
* @param value Value to check for nullability.
|
|
482
|
+
* @param error Error to return when `value` is nullish.
|
|
483
|
+
* @returns A success for non-nullish values or a failure for nullish values.
|
|
484
|
+
*/
|
|
485
|
+
static fromNullable(value, error) {
|
|
486
|
+
return value == null ? this.failure(error) : this.success(value);
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Converts a predicate check into a result.
|
|
490
|
+
*
|
|
491
|
+
* @param value Value to validate.
|
|
492
|
+
* @param predicate Predicate that determines whether `value` is acceptable.
|
|
493
|
+
* @param error Error to return when the predicate fails.
|
|
494
|
+
* @returns A success containing `value` when the predicate passes, otherwise
|
|
495
|
+
* a failure containing `error`.
|
|
496
|
+
*/
|
|
497
|
+
static fromPredicate(value, predicate, error) {
|
|
498
|
+
return predicate(value) ? this.success(value) : this.failure(error);
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* Converts a result into a nullable value.
|
|
502
|
+
*
|
|
503
|
+
* Use this when crossing into APIs that model absence with `null` rather
|
|
504
|
+
* than an explicit failure channel.
|
|
505
|
+
*
|
|
506
|
+
* @param result Result to convert.
|
|
507
|
+
* @returns The success value or `null` when the result is failed.
|
|
508
|
+
*/
|
|
509
|
+
static toNullable(result) {
|
|
510
|
+
return this.isSuccess(result) ? result.value : null;
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Splits an array of results into parallel arrays of successes and failures.
|
|
514
|
+
*
|
|
515
|
+
* Ordering is preserved independently within each returned array.
|
|
516
|
+
*
|
|
517
|
+
* @param results Results to partition.
|
|
518
|
+
* @returns A tuple containing successful values at index `0` and failure
|
|
519
|
+
* values at index `1`.
|
|
520
|
+
*/
|
|
521
|
+
static partition(results) {
|
|
522
|
+
const values = [];
|
|
523
|
+
const errors = [];
|
|
524
|
+
for (const result of results) if (this.isSuccess(result)) values.push(result.value);
|
|
525
|
+
else errors.push(result.error);
|
|
526
|
+
return [values, errors];
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Collects only the success values from an array of results.
|
|
530
|
+
*
|
|
531
|
+
* Failures are discarded.
|
|
532
|
+
*
|
|
533
|
+
* @param results Results to filter.
|
|
534
|
+
* @returns An array of success values in input order.
|
|
535
|
+
*/
|
|
536
|
+
static filterSuccesses(results) {
|
|
537
|
+
return results.filter((result) => this.isSuccess(result)).map((result) => result.value);
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Collects only the failure values from an array of results.
|
|
541
|
+
*
|
|
542
|
+
* Successes are discarded.
|
|
543
|
+
*
|
|
544
|
+
* @param results Results to filter.
|
|
545
|
+
* @returns An array of failure values in input order.
|
|
546
|
+
*/
|
|
547
|
+
static filterFailures(results) {
|
|
548
|
+
return results.filter((result) => this.isFailure(result)).map((result) => result.error);
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
//#endregion
|
|
552
|
+
export { isTypedError as n, ResultKit as t };
|
|
553
|
+
|
|
554
|
+
//# sourceMappingURL=result-kit-pgireOSz.mjs.map
|
package/package.json
CHANGED