@zireal/result-kit 1.0.0 → 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.
@@ -0,0 +1,482 @@
1
+ import { n as Result, r as Success, t as Failure } from "./result-hklHYniG.mjs";
2
+
3
+ //#region src/core/error.d.ts
4
+ /**
5
+ * Structured application error shape used throughout the package.
6
+ *
7
+ * `TypedError` gives failures a stable string discriminator through `type`
8
+ * while preserving a human-readable `message` and optional machine-readable
9
+ * metadata.
10
+ */
11
+ interface TypedError<TType extends string = string> {
12
+ /**
13
+ * Stable error discriminator used for narrowing, branching, and mapping.
14
+ */
15
+ readonly type: TType;
16
+ /**
17
+ * Human-readable description of the failure.
18
+ */
19
+ readonly message: string;
20
+ /**
21
+ * Optional serializable metadata describing the failure context.
22
+ */
23
+ readonly details?: Record<string, unknown>;
24
+ /**
25
+ * Optional original cause for debugging or tracing.
26
+ */
27
+ readonly cause?: unknown;
28
+ }
29
+ /**
30
+ * Alias that makes it explicit that a typed error is constrained to one
31
+ * concrete discriminator.
32
+ */
33
+ type TypedErrorOf<TType extends string> = TypedError<TType>;
34
+ /**
35
+ * Produces a union of {@link TypedError} variants from a union of string
36
+ * discriminators.
37
+ *
38
+ * This is useful for defining domain-specific error unions such as
39
+ * `"not_found" | "validation_error"`.
40
+ */
41
+ type TypedErrorUnion<TType extends string> = TType extends string ? TypedError<TType> : never;
42
+ /**
43
+ * Determines whether an unknown value satisfies the runtime contract of
44
+ * {@link TypedError}.
45
+ *
46
+ * A valid typed error must be an object with string `type` and `message`
47
+ * fields. When present, `details` must be a plain object-like record rather
48
+ * than `null` or an array.
49
+ *
50
+ * @param error Value to validate at runtime.
51
+ * @returns `true` when `error` matches the expected structured error shape.
52
+ */
53
+ declare const isTypedError: (error: unknown) => error is TypedError<string>;
54
+ //#endregion
55
+ //#region src/core/result-kit.d.ts
56
+ /**
57
+ * Static utilities for creating, transforming, inspecting, and consuming
58
+ * {@link Result} values.
59
+ *
60
+ * The class is intentionally abstract because it acts as a namespaced toolbox
61
+ * rather than a type meant to be instantiated.
62
+ */
63
+ declare abstract class ResultKit {
64
+ /**
65
+ * Determines whether a result contains a successful value.
66
+ *
67
+ * Use this as a type guard when branching manually over a {@link Result}.
68
+ *
69
+ * @param result Result to inspect.
70
+ * @returns `true` when `result` is a {@link Success}.
71
+ */
72
+ static isSuccess<T, E>(result: Result<T, E>): result is Success<T>;
73
+ /**
74
+ * Determines whether a result contains a failure value.
75
+ *
76
+ * Use this as a type guard when branching manually over a {@link Result}.
77
+ *
78
+ * @param result Result to inspect.
79
+ * @returns `true` when `result` is a {@link Failure}.
80
+ */
81
+ static isFailure<T, E>(result: Result<T, E>): result is Failure<E>;
82
+ /**
83
+ * Constructs a successful result.
84
+ *
85
+ * Prefer this helper over hand-writing `{ ok: true, value }` so result
86
+ * creation stays uniform across the codebase.
87
+ *
88
+ * @param value Successful value to wrap.
89
+ * @returns A {@link Success} containing `value`.
90
+ */
91
+ static success<T>(value: T): Success<T>;
92
+ /**
93
+ * Constructs a failed result with any error payload.
94
+ *
95
+ * This is the generic failure constructor for non-`TypedError` failures.
96
+ *
97
+ * @param error Error payload to wrap.
98
+ * @returns A {@link Failure} containing `error`.
99
+ */
100
+ static failure<E>(error: E): Failure<E>;
101
+ /**
102
+ * Constructs a failed result from a {@link TypedError}.
103
+ *
104
+ * This is a convenience wrapper around {@link ResultKit.failure} for the
105
+ * package's structured error convention.
106
+ *
107
+ * @param error Structured error payload to wrap.
108
+ * @returns A failed result carrying the provided typed error.
109
+ */
110
+ static fail<T extends string>(error: TypedError<T>): Failure<TypedError<T>>;
111
+ /**
112
+ * Determines whether an unknown value satisfies the runtime shape of
113
+ * {@link TypedError}.
114
+ *
115
+ * @param error Value to validate.
116
+ * @returns `true` when `error` looks like a typed error object.
117
+ */
118
+ static isTypedError(error: unknown): error is TypedError<string>;
119
+ /**
120
+ * Transforms the success value of a result while leaving failures untouched.
121
+ *
122
+ * Use this when you want to project successful data without changing the
123
+ * failure channel.
124
+ *
125
+ * @param result Result whose success value may be transformed.
126
+ * @param fn Mapping function applied only when `result` is successful.
127
+ * @returns A new successful result with the mapped value, or the original
128
+ * failure when `result` is unsuccessful.
129
+ */
130
+ static map<T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E>;
131
+ /**
132
+ * Transforms both branches of a result in one operation.
133
+ *
134
+ * Use this when success and failure values both need projection into new
135
+ * shapes.
136
+ *
137
+ * @param result Result to transform.
138
+ * @param onSuccess Mapper applied when `result` is successful.
139
+ * @param onFailure Mapper applied when `result` is failed.
140
+ * @returns A result whose success and failure payloads have been mapped into
141
+ * the target types.
142
+ */
143
+ static bimap<T, U, E, F>(result: Result<T, E>, onSuccess: (value: T) => U, onFailure: (error: E) => F): Result<U, F>;
144
+ /**
145
+ * Asynchronously transforms the success value of a result.
146
+ *
147
+ * Failures are returned unchanged and `fn` is never invoked for them.
148
+ *
149
+ * @param result Result whose success value may be transformed.
150
+ * @param fn Async mapping function applied only when `result` is successful.
151
+ * @returns A promise resolving to a mapped success result or the original
152
+ * failure.
153
+ */
154
+ static mapAsync<T, U, E>(result: Result<T, E>, fn: (value: T) => Promise<U>): Promise<Result<U, E>>;
155
+ /**
156
+ * Transforms the failure value of a result while leaving successes untouched.
157
+ *
158
+ * @param result Result whose error may be transformed.
159
+ * @param fn Mapping function applied only when `result` is failed.
160
+ * @returns The original success result or a new failed result containing the
161
+ * mapped error.
162
+ */
163
+ static mapError<T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F>;
164
+ /**
165
+ * Asynchronously transforms the failure value of a result.
166
+ *
167
+ * Success values are returned unchanged and `fn` is never invoked for them.
168
+ *
169
+ * @param result Result whose error may be transformed.
170
+ * @param fn Async mapping function applied only when `result` is failed.
171
+ * @returns A promise resolving to the original success result or a new failed
172
+ * result containing the mapped error.
173
+ */
174
+ static mapErrorAsync<T, E, F>(result: Result<T, E>, fn: (error: E) => Promise<F>): Promise<Result<T, F>>;
175
+ /**
176
+ * Chains another result-producing operation onto a successful result.
177
+ *
178
+ * This is the standard flat-mapping operation for the success branch and is
179
+ * useful for sequencing dependent computations without nesting `Result`
180
+ * values.
181
+ *
182
+ * @param result Result to continue from.
183
+ * @param fn Function invoked only when `result` is successful.
184
+ * @returns The chained result, or the original failure unchanged.
185
+ */
186
+ static andThen<T, U, E>(result: Result<T, E>, fn: (value: T) => Result<U, E>): Result<U, E>;
187
+ /**
188
+ * Asynchronously chains another result-producing operation onto a successful
189
+ * result.
190
+ *
191
+ * @param result Result to continue from.
192
+ * @param fn Async function invoked only when `result` is successful.
193
+ * @returns A promise resolving to the chained result or the original failure.
194
+ */
195
+ static andThenAsync<T, U, E>(result: Result<T, E>, fn: (value: T) => Promise<Result<U, E>>): Promise<Result<U, E>>;
196
+ /**
197
+ * Recovers from a failed result by mapping it into a new result.
198
+ *
199
+ * Use this to provide fallback logic, error translation, or alternate
200
+ * retrieval paths for the failure branch.
201
+ *
202
+ * @param result Result to recover from.
203
+ * @param fn Recovery function invoked only when `result` is failed.
204
+ * @returns The original success result or the recovery result.
205
+ */
206
+ static orElse<T, E, F>(result: Result<T, E>, fn: (error: E) => Result<T, F>): Result<T, F>;
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 orElseAsync<T, E, F>(result: Result<T, E>, fn: (error: E) => Promise<Result<T, F>>): Promise<Result<T, F>>;
217
+ /**
218
+ * Extracts the success value from a result when present.
219
+ *
220
+ * This helper is intentionally non-throwing. Failures resolve to
221
+ * `undefined`, making it suitable when absence is acceptable.
222
+ *
223
+ * @param result Result to unwrap.
224
+ * @returns The success value, or `undefined` when the result is failed.
225
+ */
226
+ static unwrap<T, E>(result: Result<T, E>): T | undefined;
227
+ /**
228
+ * Extracts the value from a known {@link Success}.
229
+ *
230
+ * Use this when the success branch has already been narrowed and you want a
231
+ * small semantic wrapper around direct property access.
232
+ *
233
+ * @param result Successful result to unwrap.
234
+ * @returns The contained success value.
235
+ */
236
+ static unwrapSuccess<T>(result: Success<T>): T;
237
+ /**
238
+ * Extracts the error from a known {@link Failure}.
239
+ *
240
+ * Use this when the failure branch has already been narrowed and you want a
241
+ * small semantic wrapper around direct property access.
242
+ *
243
+ * @param result Failed result to unwrap.
244
+ * @returns The contained error value.
245
+ */
246
+ static unwrapFailure<E>(result: Failure<E>): E;
247
+ /**
248
+ * Extracts the success value or returns a provided fallback.
249
+ *
250
+ * The fallback is evaluated eagerly before the call, so use
251
+ * {@link ResultKit.unwrapOrElse} when computing the fallback is expensive.
252
+ *
253
+ * @param result Result to unwrap.
254
+ * @param defaultValue Value to return when `result` is failed.
255
+ * @returns The success value or `defaultValue`.
256
+ */
257
+ static unwrapOr<T, E>(result: Result<T, E>, defaultValue: T): T;
258
+ /**
259
+ * Extracts the success value or computes a fallback from the failure.
260
+ *
261
+ * The fallback callback is evaluated lazily and receives the failure value.
262
+ *
263
+ * @param result Result to unwrap.
264
+ * @param fn Function used to derive a fallback from the error.
265
+ * @returns The success value or the callback result.
266
+ */
267
+ static unwrapOrElse<T, E>(result: Result<T, E>, fn: (error: E) => T): T;
268
+ /**
269
+ * Asynchronously extracts the success value or computes a fallback from the
270
+ * failure.
271
+ *
272
+ * @param result Result to unwrap.
273
+ * @param fn Async function used to derive a fallback from the error.
274
+ * @returns A promise resolving to the success value or the callback result.
275
+ */
276
+ static unwrapOrElseAsync<T, E>(result: Result<T, E>, fn: (error: E) => Promise<T>): Promise<T>;
277
+ /**
278
+ * Folds a result into a single output value.
279
+ *
280
+ * This is useful at application boundaries where both branches need to
281
+ * produce the same final shape.
282
+ *
283
+ * @param result Result to match against.
284
+ * @param handlers Branch handlers for success and failure.
285
+ * @returns The value returned by the matching handler.
286
+ */
287
+ static match<T, E, U>(result: Result<T, E>, handlers: {
288
+ onSuccess: (value: T) => U;
289
+ onFailure: (error: E) => U;
290
+ }): U;
291
+ /**
292
+ * Asynchronously folds a result into a single output value.
293
+ *
294
+ * @param result Result to match against.
295
+ * @param handlers Async branch handlers for success and failure.
296
+ * @returns A promise resolving to the value returned by the matching handler.
297
+ */
298
+ static matchAsync<T, E, U>(result: Result<T, E>, handlers: {
299
+ onSuccess: (value: T) => Promise<U>;
300
+ onFailure: (error: E) => Promise<U>;
301
+ }): Promise<U>;
302
+ /**
303
+ * Runs side effects for a result branch and returns the original result.
304
+ *
305
+ * Use this for logging, metrics, tracing, or other observational work that
306
+ * should not change the result payload.
307
+ *
308
+ * @param result Result to observe.
309
+ * @param handlers Optional side-effect handlers for either branch.
310
+ * @returns The original `result` instance.
311
+ */
312
+ static tap<T, E>(result: Result<T, E>, handlers: {
313
+ onSuccess?: (value: T) => void;
314
+ onFailure?: (error: E) => void;
315
+ }): Result<T, E>;
316
+ /**
317
+ * Asynchronously runs side effects for a result branch and returns the
318
+ * original result.
319
+ *
320
+ * @param result Result to observe.
321
+ * @param handlers Optional async side-effect handlers for either branch.
322
+ * @returns A promise resolving to the original `result` instance after side
323
+ * effects complete.
324
+ */
325
+ static tapAsync<T, E>(result: Result<T, E>, handlers: {
326
+ onSuccess?: (value: T) => Promise<void>;
327
+ onFailure?: (error: E) => Promise<void>;
328
+ }): Promise<Result<T, E>>;
329
+ /**
330
+ * Combines an array of results into a single result of an array.
331
+ *
332
+ * Combination short-circuits on the first failure encountered. When every
333
+ * result succeeds, the returned success contains the collected values in the
334
+ * same order as the input array.
335
+ *
336
+ * @param results Results to combine.
337
+ * @returns A success containing all values, or the first failure encountered.
338
+ */
339
+ static combine<T, E>(results: Result<T, E>[]): Result<T[], E>;
340
+ /**
341
+ * Asynchronously combines multiple promised results into one result.
342
+ *
343
+ * All promises are awaited before combination, so this helper does not stop
344
+ * pending asynchronous work after an eventual failure.
345
+ *
346
+ * @param results Promises resolving to results.
347
+ * @returns A promise resolving to the same output as {@link ResultKit.combine}.
348
+ */
349
+ static combineAsync<T, E>(results: Promise<Result<T, E>>[]): Promise<Result<T[], E>>;
350
+ /**
351
+ * Combines an array of results while collecting every failure.
352
+ *
353
+ * Successful values are preserved in order. If one or more failures occur,
354
+ * the returned failure contains an array of all collected errors rather than
355
+ * only the first one.
356
+ *
357
+ * @param results Results to combine.
358
+ * @returns A success containing all values when every result succeeds, or a
359
+ * failure containing all collected errors.
360
+ */
361
+ static combineWithAllErrors<T, E>(results: Result<T, E>[]): Result<T[], E[]>;
362
+ /**
363
+ * Asynchronously combines multiple promised results while collecting every
364
+ * failure.
365
+ *
366
+ * As with {@link ResultKit.combineAsync}, all promises are awaited before
367
+ * reduction.
368
+ *
369
+ * @param results Promises resolving to results.
370
+ * @returns A promise resolving to the same output as
371
+ * {@link ResultKit.combineWithAllErrors}.
372
+ */
373
+ static combineWithAllErrorsAsync<T, E>(results: Promise<Result<T, E>>[]): Promise<Result<T[], E[]>>;
374
+ /**
375
+ * Removes one layer of `Result` nesting from the success branch.
376
+ *
377
+ * This is useful after operations that already return a `Result` have been
378
+ * wrapped in another success result.
379
+ *
380
+ * @param result Nested result to flatten.
381
+ * @returns The inner result when successful, or the outer failure unchanged.
382
+ */
383
+ static flatten<T, E>(result: Result<Result<T, E>, E>): Result<T, E>;
384
+ /**
385
+ * Converts a promise that may reject into a promise of `Result`.
386
+ *
387
+ * Rejections are caught and normalized through `errorFn`, allowing async
388
+ * exception sources to participate in explicit result-based control flow.
389
+ *
390
+ * @param promise Promise to execute.
391
+ * @param errorFn Function used to convert an unknown rejection reason into
392
+ * the desired error type.
393
+ * @returns A promise resolving to a successful result for fulfilled values or
394
+ * a failed result for rejected values.
395
+ */
396
+ static fromPromise<T, E>(promise: Promise<T>, errorFn: (error: unknown) => E): Promise<Result<T, E>>;
397
+ /**
398
+ * Wraps a synchronous function so thrown exceptions are returned as failures.
399
+ *
400
+ * The resulting function preserves the original parameter list while
401
+ * converting thrown errors with `errorFn`.
402
+ *
403
+ * @param fn Function that may throw.
404
+ * @param errorFn Function used to map an unknown thrown value into the
405
+ * desired error type.
406
+ * @returns A new function that returns `Result` instead of throwing.
407
+ */
408
+ static fromThrowable<Args extends unknown[], T, E>(fn: (...args: Args) => T, errorFn: (error: unknown) => E): (...args: Args) => Result<T, E>;
409
+ /**
410
+ * Wraps an asynchronous function so thrown exceptions and rejected promises
411
+ * are returned as failures.
412
+ *
413
+ * @param fn Async function that may throw or reject.
414
+ * @param errorFn Function used to map an unknown failure reason into the
415
+ * desired error type.
416
+ * @returns A new function that resolves to `Result` instead of propagating
417
+ * exceptions.
418
+ */
419
+ static fromThrowableAsync<Args extends unknown[], T, E>(fn: (...args: Args) => Promise<T>, errorFn: (error: unknown) => E): (...args: Args) => Promise<Result<T, E>>;
420
+ /**
421
+ * Converts a nullable value into a result.
422
+ *
423
+ * `null` and `undefined` become failures carrying the provided error. Any
424
+ * other value becomes a success and is narrowed to `NonNullable<T>`.
425
+ *
426
+ * @param value Value to check for nullability.
427
+ * @param error Error to return when `value` is nullish.
428
+ * @returns A success for non-nullish values or a failure for nullish values.
429
+ */
430
+ static fromNullable<T, E>(value: T | null | undefined, error: E): Result<NonNullable<T>, E>;
431
+ /**
432
+ * Converts a predicate check into a result.
433
+ *
434
+ * @param value Value to validate.
435
+ * @param predicate Predicate that determines whether `value` is acceptable.
436
+ * @param error Error to return when the predicate fails.
437
+ * @returns A success containing `value` when the predicate passes, otherwise
438
+ * a failure containing `error`.
439
+ */
440
+ static fromPredicate<T, E>(value: T, predicate: (value: T) => boolean, error: E): Result<T, E>;
441
+ /**
442
+ * Converts a result into a nullable value.
443
+ *
444
+ * Use this when crossing into APIs that model absence with `null` rather
445
+ * than an explicit failure channel.
446
+ *
447
+ * @param result Result to convert.
448
+ * @returns The success value or `null` when the result is failed.
449
+ */
450
+ static toNullable<T, E>(result: Result<T, E>): T | null;
451
+ /**
452
+ * Splits an array of results into parallel arrays of successes and failures.
453
+ *
454
+ * Ordering is preserved independently within each returned array.
455
+ *
456
+ * @param results Results to partition.
457
+ * @returns A tuple containing successful values at index `0` and failure
458
+ * values at index `1`.
459
+ */
460
+ static partition<T, E>(results: Result<T, E>[]): [T[], E[]];
461
+ /**
462
+ * Collects only the success values from an array of results.
463
+ *
464
+ * Failures are discarded.
465
+ *
466
+ * @param results Results to filter.
467
+ * @returns An array of success values in input order.
468
+ */
469
+ static filterSuccesses<T, E>(results: Result<T, E>[]): T[];
470
+ /**
471
+ * Collects only the failure values from an array of results.
472
+ *
473
+ * Successes are discarded.
474
+ *
475
+ * @param results Results to filter.
476
+ * @returns An array of failure values in input order.
477
+ */
478
+ static filterFailures<T, E>(results: Result<T, E>[]): E[];
479
+ }
480
+ //#endregion
481
+ export { isTypedError as a, TypedErrorUnion as i, TypedError as n, TypedErrorOf as r, ResultKit as t };
482
+ //# sourceMappingURL=index-SLgCKCJe.d.mts.map
package/dist/index.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_result_kit = require("./result-kit-Ck1xAp-J.cjs");
2
+ const require_result_kit = require("./result-kit-DdhY1ph2.cjs");
3
3
  require("./core/index.cjs");
4
4
  exports.ResultKit = require_result_kit.ResultKit;
5
5
  exports.isTypedError = require_result_kit.isTypedError;
package/dist/index.d.cts CHANGED
@@ -1,3 +1,3 @@
1
- import { a as isTypedError, i as TypedErrorUnion, n as TypedError, r as TypedErrorOf, t as ResultKit } from "./index-EJ_Ol5Fu.cjs";
2
- import { n as Result, r as Success, t as Failure } from "./result-DbQ8o1Cs.cjs";
1
+ import { a as isTypedError, i as TypedErrorUnion, n as TypedError, r as TypedErrorOf, t as ResultKit } from "./index-DioLW1WH.cjs";
2
+ import { n as Result, r as Success, t as Failure } from "./result-DdlZMLaP.cjs";
3
3
  export { Failure, Result, ResultKit, Success, TypedError, TypedErrorOf, TypedErrorUnion, isTypedError };
package/dist/index.d.mts CHANGED
@@ -1,3 +1,3 @@
1
- import { a as isTypedError, i as TypedErrorUnion, n as TypedError, r as TypedErrorOf, t as ResultKit } from "./index-Ch6k0lVU.mjs";
2
- import { n as Result, r as Success, t as Failure } from "./result-CI_HBDHK.mjs";
1
+ import { a as isTypedError, i as TypedErrorUnion, n as TypedError, r as TypedErrorOf, t as ResultKit } from "./index-SLgCKCJe.mjs";
2
+ import { n as Result, r as Success, t as Failure } from "./result-hklHYniG.mjs";
3
3
  export { Failure, Result, ResultKit, Success, TypedError, TypedErrorOf, TypedErrorUnion, isTypedError };
package/dist/index.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import { n as isTypedError, t as ResultKit } from "./result-kit-C5ZY61pO.mjs";
1
+ import { n as isTypedError, t as ResultKit } from "./result-kit-pgireOSz.mjs";
2
2
  import "./core/index.mjs";
3
3
  export { ResultKit, isTypedError };
@@ -1,10 +1,30 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_result_kit = require("../result-kit-Ck1xAp-J.cjs");
2
+ const require_result_kit = require("../result-kit-DdhY1ph2.cjs");
3
3
  let _nestjs_common = require("@nestjs/common");
4
4
  //#region src/nest/http.ts
5
+ /**
6
+ * Default message used when the incoming error value does not expose a usable
7
+ * message.
8
+ */
5
9
  const DEFAULT_UNKNOWN_MESSAGE = "An unknown error occurred";
10
+ /**
11
+ * Default application error code used for internal server failures.
12
+ */
6
13
  const DEFAULT_ERROR_CODE = "INTERNAL_SERVER_ERROR";
14
+ /**
15
+ * Normalizes an arbitrary error type string into an uppercase code suitable
16
+ * for HTTP responses.
17
+ *
18
+ * @param value Raw error type value.
19
+ * @returns A sanitized, uppercase error code or the internal default code.
20
+ */
7
21
  const normalizeErrorCode = (value) => value.trim().replace(/[^a-zA-Z0-9]+/g, "_").replace(/^_+|_+$/g, "").toUpperCase() || DEFAULT_ERROR_CODE;
22
+ /**
23
+ * Builds a Nest `HttpException` from a lightweight descriptor object.
24
+ *
25
+ * @param descriptor Response descriptor to convert.
26
+ * @returns A concrete `HttpException` with normalized payload defaults.
27
+ */
8
28
  const toHttpExceptionFromDescriptor = (descriptor) => {
9
29
  const status = descriptor.status ?? _nestjs_common.HttpStatus.INTERNAL_SERVER_ERROR;
10
30
  return new _nestjs_common.HttpException({
@@ -14,6 +34,21 @@ const toHttpExceptionFromDescriptor = (descriptor) => {
14
34
  ...descriptor.error ? { error: descriptor.error } : {}
15
35
  }, status);
16
36
  };
37
+ /**
38
+ * Converts an arbitrary error value into a Nest `HttpException`.
39
+ *
40
+ * Resolution order is:
41
+ * 1. `options.mapError` returning a `HttpException`
42
+ * 2. `options.mapError` returning a {@link HttpExceptionDescriptor}
43
+ * 3. An incoming `HttpException`
44
+ * 4. A core typed error
45
+ * 5. A generic JavaScript `Error`
46
+ * 6. An unknown fallback internal server error
47
+ *
48
+ * @param error Error value to convert.
49
+ * @param options Optional mapping and fallback configuration.
50
+ * @returns A Nest `HttpException` representing the provided error.
51
+ */
17
52
  const toHttpException = (error, options) => {
18
53
  const mapped = options?.mapError?.(error);
19
54
  if (mapped instanceof _nestjs_common.HttpException) return mapped;
@@ -33,10 +68,33 @@ const toHttpException = (error, options) => {
33
68
  message: options?.fallbackMessage || DEFAULT_UNKNOWN_MESSAGE
34
69
  });
35
70
  };
71
+ /**
72
+ * Extracts the success value from a result or throws an HTTP exception derived
73
+ * from the failure.
74
+ *
75
+ * This is intended for Nest controller and adapter boundaries where a result
76
+ * should be converted into framework-native exception flow.
77
+ *
78
+ * @param result Result to unwrap.
79
+ * @param options Optional error mapping and fallback configuration.
80
+ * @returns The successful value when `result` is successful.
81
+ * @throws {HttpException} When `result` is failed.
82
+ */
36
83
  const unwrapOrThrow = (result, options) => {
37
84
  if (require_result_kit.ResultKit.isSuccess(result)) return result.value;
38
85
  throw toHttpException(result.error, options);
39
86
  };
87
+ /**
88
+ * Awaits a promised result and extracts its success value or throws a mapped
89
+ * HTTP exception.
90
+ *
91
+ * Use this when service methods already resolve to `Promise<Result<...>>`.
92
+ *
93
+ * @param promise Promise resolving to a result.
94
+ * @param options Optional error mapping and fallback configuration.
95
+ * @returns A promise resolving to the successful value.
96
+ * @throws {HttpException} When the resolved result is failed.
97
+ */
40
98
  const unwrapPromise = async (promise, options) => unwrapOrThrow(await promise, options);
41
99
  //#endregion
42
100
  exports.toHttpException = toHttpException;
@@ -1,20 +1,91 @@
1
- import { n as Result } from "../result-DbQ8o1Cs.cjs";
1
+ import { n as Result } from "../result-DdlZMLaP.cjs";
2
2
  import { HttpException } from "@nestjs/common";
3
3
 
4
4
  //#region src/nest/http.d.ts
5
+ /**
6
+ * Describes the HTTP exception payload that should be produced for a failure.
7
+ *
8
+ * Returning this shape from `mapError` lets callers control HTTP status and
9
+ * response body without constructing a Nest `HttpException` instance
10
+ * themselves.
11
+ */
5
12
  interface HttpExceptionDescriptor {
13
+ /**
14
+ * HTTP status code to use for the generated exception.
15
+ */
6
16
  readonly status?: number;
17
+ /**
18
+ * Stable application error code included in the response body.
19
+ */
7
20
  readonly code?: string | number;
21
+ /**
22
+ * Human-readable error message included in the response body.
23
+ */
8
24
  readonly message?: string;
25
+ /**
26
+ * Optional structured metadata added to the response body.
27
+ */
9
28
  readonly details?: Record<string, unknown>;
29
+ /**
30
+ * Optional top-level Nest-style error label.
31
+ */
10
32
  readonly error?: string;
11
33
  }
34
+ /**
35
+ * Configures how result failures should be mapped into Nest HTTP exceptions.
36
+ */
12
37
  interface NestErrorOptions<E> {
38
+ /**
39
+ * Optional custom mapper for converting a domain error into either a ready
40
+ * `HttpException` or a descriptor used to build one.
41
+ */
13
42
  readonly mapError?: (error: E) => HttpException | HttpExceptionDescriptor | undefined;
43
+ /**
44
+ * Fallback message used for unknown failures when a better message cannot be
45
+ * derived from the error value.
46
+ */
14
47
  readonly fallbackMessage?: string;
15
48
  }
49
+ /**
50
+ * Converts an arbitrary error value into a Nest `HttpException`.
51
+ *
52
+ * Resolution order is:
53
+ * 1. `options.mapError` returning a `HttpException`
54
+ * 2. `options.mapError` returning a {@link HttpExceptionDescriptor}
55
+ * 3. An incoming `HttpException`
56
+ * 4. A core typed error
57
+ * 5. A generic JavaScript `Error`
58
+ * 6. An unknown fallback internal server error
59
+ *
60
+ * @param error Error value to convert.
61
+ * @param options Optional mapping and fallback configuration.
62
+ * @returns A Nest `HttpException` representing the provided error.
63
+ */
16
64
  declare const toHttpException: <E>(error: E, options?: NestErrorOptions<E>) => HttpException;
65
+ /**
66
+ * Extracts the success value from a result or throws an HTTP exception derived
67
+ * from the failure.
68
+ *
69
+ * This is intended for Nest controller and adapter boundaries where a result
70
+ * should be converted into framework-native exception flow.
71
+ *
72
+ * @param result Result to unwrap.
73
+ * @param options Optional error mapping and fallback configuration.
74
+ * @returns The successful value when `result` is successful.
75
+ * @throws {HttpException} When `result` is failed.
76
+ */
17
77
  declare const unwrapOrThrow: <T, E>(result: Result<T, E>, options?: NestErrorOptions<E>) => T;
78
+ /**
79
+ * Awaits a promised result and extracts its success value or throws a mapped
80
+ * HTTP exception.
81
+ *
82
+ * Use this when service methods already resolve to `Promise<Result<...>>`.
83
+ *
84
+ * @param promise Promise resolving to a result.
85
+ * @param options Optional error mapping and fallback configuration.
86
+ * @returns A promise resolving to the successful value.
87
+ * @throws {HttpException} When the resolved result is failed.
88
+ */
18
89
  declare const unwrapPromise: <T, E>(promise: Promise<Result<T, E>>, options?: NestErrorOptions<E>) => Promise<T>;
19
90
  //#endregion
20
91
  export { type HttpExceptionDescriptor, type NestErrorOptions, toHttpException, unwrapOrThrow, unwrapPromise };