@shirudo/ddd-kit 0.9.3 → 0.9.4
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/README.md +17 -4
- package/dist/index.d.ts +3 -432
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/result-DulD8sL7.d.ts +269 -0
- package/dist/result.d.ts +204 -0
- package/dist/result.js +2 -0
- package/dist/result.js.map +1 -0
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -854,6 +854,8 @@ console.log(order.version); // 3 (one for each operation)
|
|
|
854
854
|
|
|
855
855
|
The `Result<T, E>` type provides composition utilities to avoid repetitive `if (isErr)` checks:
|
|
856
856
|
|
|
857
|
+
**Import Result utilities from the dedicated export path:**
|
|
858
|
+
|
|
857
859
|
```typescript
|
|
858
860
|
import {
|
|
859
861
|
ok,
|
|
@@ -866,9 +868,13 @@ import {
|
|
|
866
868
|
unwrapOr,
|
|
867
869
|
unwrapOrElse,
|
|
868
870
|
match,
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
871
|
+
matchAsync,
|
|
872
|
+
pipe,
|
|
873
|
+
type Result,
|
|
874
|
+
Outcome,
|
|
875
|
+
Success,
|
|
876
|
+
Erroneous
|
|
877
|
+
} from "@shirudo/ddd-kit/result";
|
|
872
878
|
|
|
873
879
|
type UserId = string;
|
|
874
880
|
|
|
@@ -925,7 +931,14 @@ if (isOk(result2)) {
|
|
|
925
931
|
- `mapErr<T, E, F>(result, fn)` - Transforms Err value. If Ok, returns value unchanged.
|
|
926
932
|
- `unwrapOr<T, E>(result, defaultValue)` - Returns value if Ok, otherwise returns default.
|
|
927
933
|
- `unwrapOrElse<T, E>(result, fn)` - Returns value if Ok, otherwise computes default from error.
|
|
928
|
-
- `match<T, E, R>(result, onOk, onErr)` - Pattern matching. Applies one function if Ok, another if Err.
|
|
934
|
+
- `match<T, E, R>(result, onOk, onErr)` - Pattern matching. Applies one function if Ok, another if Err. Supports both function and object syntax.
|
|
935
|
+
- `matchAsync<T, E, R>(result, onOk, onErr)` - Asynchronous pattern matching. Applies async functions for Ok/Err cases. Supports both function and object syntax.
|
|
936
|
+
- `pipe<T, E>(initial, ...fns)` - Pipes a Result through multiple operations. Stops on first error. Cleaner alternative to nested `andThen` calls.
|
|
937
|
+
|
|
938
|
+
**Class-based API (for method chaining):**
|
|
939
|
+
- `Outcome<T, E>` - Wrapper class for Result with method chaining support
|
|
940
|
+
- `Success<T>` - Class representing successful results (created via `Ok()` factory)
|
|
941
|
+
- `Erroneous<E>` - Class representing error results (created via `Err()` factory)
|
|
929
942
|
|
|
930
943
|
## API Documentation
|
|
931
944
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { R as Result } from './result-DulD8sL7.js';
|
|
2
|
+
|
|
1
3
|
type Id<Tag extends string> = string & {
|
|
2
4
|
readonly __brand: Tag;
|
|
3
5
|
};
|
|
@@ -362,437 +364,6 @@ declare abstract class AggregateBase<TState, TId extends Id<string>> implements
|
|
|
362
364
|
restoreFromSnapshot(snapshot: AggregateSnapshot<TState>): void;
|
|
363
365
|
}
|
|
364
366
|
|
|
365
|
-
type Ok<T> = {
|
|
366
|
-
ok: true;
|
|
367
|
-
value: T;
|
|
368
|
-
};
|
|
369
|
-
type Err<E> = {
|
|
370
|
-
ok: false;
|
|
371
|
-
error: E;
|
|
372
|
-
};
|
|
373
|
-
type Result<T, E> = Ok<T> | Err<E>;
|
|
374
|
-
/**
|
|
375
|
-
* Creates an Ok result with a value.
|
|
376
|
-
*
|
|
377
|
-
* @param value - The success value
|
|
378
|
-
* @returns An Ok result containing the value
|
|
379
|
-
*
|
|
380
|
-
* @example
|
|
381
|
-
* ```typescript
|
|
382
|
-
* const result = ok(42);
|
|
383
|
-
* // result is Ok<number>
|
|
384
|
-
* ```
|
|
385
|
-
*/
|
|
386
|
-
declare function ok<T>(value: T): Ok<T>;
|
|
387
|
-
/**
|
|
388
|
-
* Creates an Ok result with void (no value).
|
|
389
|
-
*
|
|
390
|
-
* @returns An Ok<void> result
|
|
391
|
-
*
|
|
392
|
-
* @example
|
|
393
|
-
* ```typescript
|
|
394
|
-
* const result = ok();
|
|
395
|
-
* // result is Ok<void>
|
|
396
|
-
* ```
|
|
397
|
-
*/
|
|
398
|
-
declare function ok(): Ok<void>;
|
|
399
|
-
/**
|
|
400
|
-
* Creates an Err result with an error.
|
|
401
|
-
*
|
|
402
|
-
* @param error - The error value
|
|
403
|
-
* @returns An Err result containing the error
|
|
404
|
-
*
|
|
405
|
-
* @example
|
|
406
|
-
* ```typescript
|
|
407
|
-
* const result = err("Something went wrong");
|
|
408
|
-
* // result is Err<string>
|
|
409
|
-
* ```
|
|
410
|
-
*/
|
|
411
|
-
declare function err<E>(error: E): Err<E>;
|
|
412
|
-
/**
|
|
413
|
-
* Creates an Err result with void (no error value).
|
|
414
|
-
*
|
|
415
|
-
* @returns An Err<void> result
|
|
416
|
-
*
|
|
417
|
-
* @example
|
|
418
|
-
* ```typescript
|
|
419
|
-
* const result = err();
|
|
420
|
-
* // result is Err<void>
|
|
421
|
-
* ```
|
|
422
|
-
*/
|
|
423
|
-
declare function err(): Err<void>;
|
|
424
|
-
/**
|
|
425
|
-
* Type guard to check if a Result is Ok.
|
|
426
|
-
* Narrows the type to Ok<T> when returning true.
|
|
427
|
-
*
|
|
428
|
-
* @param result - The result to check
|
|
429
|
-
* @returns true if the result is Ok, false otherwise
|
|
430
|
-
*
|
|
431
|
-
* @example
|
|
432
|
-
* ```typescript
|
|
433
|
-
* const result = voWithValidation(data, validator);
|
|
434
|
-
* if (isOk(result)) {
|
|
435
|
-
* // TypeScript knows result is Ok<ValueObject<T>>
|
|
436
|
-
* console.log(result.value);
|
|
437
|
-
* }
|
|
438
|
-
* ```
|
|
439
|
-
*/
|
|
440
|
-
declare function isOk<T, E>(result: Result<T, E>): result is Ok<T>;
|
|
441
|
-
/**
|
|
442
|
-
* Type guard to check if a Result is Err.
|
|
443
|
-
* Narrows the type to Err<E> when returning true.
|
|
444
|
-
*
|
|
445
|
-
* @param result - The result to check
|
|
446
|
-
* @returns true if the result is Err, false otherwise
|
|
447
|
-
*
|
|
448
|
-
* @example
|
|
449
|
-
* ```typescript
|
|
450
|
-
* const result = voWithValidation(data, validator);
|
|
451
|
-
* if (isErr(result)) {
|
|
452
|
-
* // TypeScript knows result is Err<string>
|
|
453
|
-
* console.error(result.error);
|
|
454
|
-
* }
|
|
455
|
-
* ```
|
|
456
|
-
*/
|
|
457
|
-
declare function isErr<T, E>(result: Result<T, E>): result is Err<E>;
|
|
458
|
-
/**
|
|
459
|
-
* Chains Result operations (flatMap/bind).
|
|
460
|
-
* If the result is Ok, applies the function to the value.
|
|
461
|
-
* If Err, returns the error unchanged.
|
|
462
|
-
*
|
|
463
|
-
* @param result - The result to chain
|
|
464
|
-
* @param fn - Function that takes the Ok value and returns a new Result
|
|
465
|
-
* @returns A new Result
|
|
466
|
-
*
|
|
467
|
-
* @example
|
|
468
|
-
* ```typescript
|
|
469
|
-
* const result = validateUserId("123")
|
|
470
|
-
* .andThen(userId => validateEmail("test@example.com")
|
|
471
|
-
* .map(email => ({ id: userId, email }))
|
|
472
|
-
* );
|
|
473
|
-
* ```
|
|
474
|
-
*/
|
|
475
|
-
declare function andThen<T, E, U>(result: Result<T, E>, fn: (value: T) => Result<U, E>): Result<U, E>;
|
|
476
|
-
/**
|
|
477
|
-
* Transforms the Ok value using a function.
|
|
478
|
-
* If the result is Err, returns the error unchanged.
|
|
479
|
-
*
|
|
480
|
-
* @param result - The result to transform
|
|
481
|
-
* @param fn - Function to transform the Ok value
|
|
482
|
-
* @returns A new Result with transformed value
|
|
483
|
-
*
|
|
484
|
-
* @example
|
|
485
|
-
* ```typescript
|
|
486
|
-
* const result = ok(5);
|
|
487
|
-
* const doubled = map(result, x => x * 2); // Ok<10>
|
|
488
|
-
* ```
|
|
489
|
-
*/
|
|
490
|
-
declare function map<T, E, U>(result: Result<T, E>, fn: (value: T) => U): Result<U, E>;
|
|
491
|
-
/**
|
|
492
|
-
* Transforms the Err value using a function.
|
|
493
|
-
* If the result is Ok, returns the value unchanged.
|
|
494
|
-
*
|
|
495
|
-
* @param result - The result to transform
|
|
496
|
-
* @param fn - Function to transform the Err value
|
|
497
|
-
* @returns A new Result with transformed error
|
|
498
|
-
*
|
|
499
|
-
* @example
|
|
500
|
-
* ```typescript
|
|
501
|
-
* const result = err("not found");
|
|
502
|
-
* const mapped = mapErr(result, e => `Error: ${e}`); // Err<"Error: not found">
|
|
503
|
-
* ```
|
|
504
|
-
*/
|
|
505
|
-
declare function mapErr<T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F>;
|
|
506
|
-
/**
|
|
507
|
-
* Returns the value if Ok, otherwise returns the default value.
|
|
508
|
-
*
|
|
509
|
-
* @param result - The result to unwrap
|
|
510
|
-
* @param defaultValue - Default value to return if Err
|
|
511
|
-
* @returns The Ok value or the default value
|
|
512
|
-
*
|
|
513
|
-
* @example
|
|
514
|
-
* ```typescript
|
|
515
|
-
* const result = validateUserId("123");
|
|
516
|
-
* const userId = unwrapOr(result, "default-id");
|
|
517
|
-
* ```
|
|
518
|
-
*/
|
|
519
|
-
declare function unwrapOr<T, E>(result: Result<T, E>, defaultValue: T): T;
|
|
520
|
-
/**
|
|
521
|
-
* Returns the value if Ok, otherwise computes default from error.
|
|
522
|
-
*
|
|
523
|
-
* @param result - The result to unwrap
|
|
524
|
-
* @param fn - Function to compute default from error
|
|
525
|
-
* @returns The Ok value or computed default
|
|
526
|
-
*
|
|
527
|
-
* @example
|
|
528
|
-
* ```typescript
|
|
529
|
-
* const result = validateUserId("");
|
|
530
|
-
* const userId = unwrapOrElse(result, err => `fallback-${Date.now()}`);
|
|
531
|
-
* ```
|
|
532
|
-
*/
|
|
533
|
-
declare function unwrapOrElse<T, E>(result: Result<T, E>, fn: (error: E) => T): T;
|
|
534
|
-
/**
|
|
535
|
-
* Pattern matching for Result.
|
|
536
|
-
* Applies one function if Ok, another if Err.
|
|
537
|
-
*
|
|
538
|
-
* @param result - The result to match
|
|
539
|
-
* @param onOk - Function to apply if Ok
|
|
540
|
-
* @param onErr - Function to apply if Err
|
|
541
|
-
* @returns The result of applying the appropriate function
|
|
542
|
-
*
|
|
543
|
-
* @example
|
|
544
|
-
* ```typescript
|
|
545
|
-
* const message = match(result,
|
|
546
|
-
* value => `Success: ${value}`,
|
|
547
|
-
* error => `Error: ${error}`
|
|
548
|
-
* );
|
|
549
|
-
* ```
|
|
550
|
-
*
|
|
551
|
-
* @example Using object syntax
|
|
552
|
-
* ```typescript
|
|
553
|
-
* const message = match(result, {
|
|
554
|
-
* ok: value => `Success: ${value}`,
|
|
555
|
-
* err: error => `Error: ${error}`
|
|
556
|
-
* });
|
|
557
|
-
* ```
|
|
558
|
-
*/
|
|
559
|
-
declare function match<T, E, R>(result: Result<T, E>, onOk: (value: T) => R, onErr: (error: E) => R): R;
|
|
560
|
-
declare function match<T, E, R>(result: Result<T, E>, handlers: {
|
|
561
|
-
ok: (value: T) => R;
|
|
562
|
-
err: (error: E) => R;
|
|
563
|
-
}): R;
|
|
564
|
-
/**
|
|
565
|
-
* Async pattern matching for Result.
|
|
566
|
-
* Applies one async function if Ok, another if Err.
|
|
567
|
-
* Both handlers must return Promises.
|
|
568
|
-
*
|
|
569
|
-
* @param result - The result to match
|
|
570
|
-
* @param onOk - Async function to apply if Ok
|
|
571
|
-
* @param onErr - Async function to apply if Err
|
|
572
|
-
* @returns Promise resolving to the result of applying the appropriate function
|
|
573
|
-
*
|
|
574
|
-
* @example
|
|
575
|
-
* ```typescript
|
|
576
|
-
* const message = await matchAsync(result,
|
|
577
|
-
* async (value) => `Success: ${value}`,
|
|
578
|
-
* async (error) => `Error: ${error}`
|
|
579
|
-
* );
|
|
580
|
-
* ```
|
|
581
|
-
*
|
|
582
|
-
* @example Using object syntax
|
|
583
|
-
* ```typescript
|
|
584
|
-
* const message = await matchAsync(result, {
|
|
585
|
-
* ok: async (value) => `Success: ${value}`,
|
|
586
|
-
* err: async (error) => `Error: ${error}`
|
|
587
|
-
* });
|
|
588
|
-
* ```
|
|
589
|
-
*/
|
|
590
|
-
declare function matchAsync<T, E, R>(result: Result<T, E>, onOk: (value: T) => Promise<R>, onErr: (error: E) => Promise<R>): Promise<R>;
|
|
591
|
-
declare function matchAsync<T, E, R>(result: Result<T, E>, handlers: {
|
|
592
|
-
ok: (value: T) => Promise<R>;
|
|
593
|
-
err: (error: E) => Promise<R>;
|
|
594
|
-
}): Promise<R>;
|
|
595
|
-
|
|
596
|
-
/**
|
|
597
|
-
* Base class for Result with method chaining support.
|
|
598
|
-
* Provides common methods for both Ok and Err classes.
|
|
599
|
-
*/
|
|
600
|
-
declare abstract class ResultBase<T, E> {
|
|
601
|
-
protected readonly _result: Result<T, E>;
|
|
602
|
-
protected constructor(result: Result<T, E>);
|
|
603
|
-
/**
|
|
604
|
-
* Returns the value if Ok, otherwise throws an error.
|
|
605
|
-
* If error is an Error instance, throws it directly.
|
|
606
|
-
* Otherwise, wraps the error in a new Error.
|
|
607
|
-
*
|
|
608
|
-
* @throws Error if the result is Err
|
|
609
|
-
*/
|
|
610
|
-
unwrap(): T;
|
|
611
|
-
/**
|
|
612
|
-
* Returns the value if Ok, otherwise returns the default value.
|
|
613
|
-
*/
|
|
614
|
-
unwrapOr(defaultValue: T): T;
|
|
615
|
-
/**
|
|
616
|
-
* Returns the value if Ok, otherwise computes default from error.
|
|
617
|
-
*/
|
|
618
|
-
unwrapOrElse(fn: (error: E) => T): T;
|
|
619
|
-
/**
|
|
620
|
-
* Pattern matching for Result.
|
|
621
|
-
* Applies one function if Ok, another if Err.
|
|
622
|
-
*
|
|
623
|
-
* @example
|
|
624
|
-
* ```typescript
|
|
625
|
-
* outcome.match(
|
|
626
|
-
* value => `Success: ${value}`,
|
|
627
|
-
* error => `Error: ${error}`
|
|
628
|
-
* );
|
|
629
|
-
* ```
|
|
630
|
-
*
|
|
631
|
-
* @example Using object syntax
|
|
632
|
-
* ```typescript
|
|
633
|
-
* outcome.match({
|
|
634
|
-
* ok: value => `Success: ${value}`,
|
|
635
|
-
* err: error => `Error: ${error}`
|
|
636
|
-
* });
|
|
637
|
-
* ```
|
|
638
|
-
*/
|
|
639
|
-
match<R>(onOk: (value: T) => R, onErr: (error: E) => R): R;
|
|
640
|
-
match<R>(handlers: {
|
|
641
|
-
ok: (value: T) => R;
|
|
642
|
-
err: (error: E) => R;
|
|
643
|
-
}): R;
|
|
644
|
-
/**
|
|
645
|
-
* Async pattern matching for Result.
|
|
646
|
-
* Applies one async function if Ok, another if Err.
|
|
647
|
-
*
|
|
648
|
-
* @example
|
|
649
|
-
* ```typescript
|
|
650
|
-
* await outcome.matchAsync(
|
|
651
|
-
* async (value) => `Success: ${value}`,
|
|
652
|
-
* async (error) => `Error: ${error}`
|
|
653
|
-
* );
|
|
654
|
-
* ```
|
|
655
|
-
*
|
|
656
|
-
* @example Using object syntax
|
|
657
|
-
* ```typescript
|
|
658
|
-
* await outcome.matchAsync({
|
|
659
|
-
* ok: async (value) => `Success: ${value}`,
|
|
660
|
-
* err: async (error) => `Error: ${error}`
|
|
661
|
-
* });
|
|
662
|
-
* ```
|
|
663
|
-
*/
|
|
664
|
-
matchAsync<R>(onOk: (value: T) => Promise<R>, onErr: (error: E) => Promise<R>): Promise<R>;
|
|
665
|
-
matchAsync<R>(handlers: {
|
|
666
|
-
ok: (value: T) => Promise<R>;
|
|
667
|
-
err: (error: E) => Promise<R>;
|
|
668
|
-
}): Promise<R>;
|
|
669
|
-
/**
|
|
670
|
-
* Type guard to check if the result is Ok.
|
|
671
|
-
*/
|
|
672
|
-
isOk(): this is Success<T>;
|
|
673
|
-
/**
|
|
674
|
-
* Type guard to check if the result is Err.
|
|
675
|
-
*/
|
|
676
|
-
isErr(): this is Erroneous<E>;
|
|
677
|
-
/**
|
|
678
|
-
* Gets the underlying Result value.
|
|
679
|
-
*/
|
|
680
|
-
get result(): Result<T, E>;
|
|
681
|
-
}
|
|
682
|
-
/**
|
|
683
|
-
* Class representing a successful result with method chaining support.
|
|
684
|
-
* Use this for class-based API with method chaining.
|
|
685
|
-
*
|
|
686
|
-
* @example
|
|
687
|
-
* ```typescript
|
|
688
|
-
* const okResult = Ok(1);
|
|
689
|
-
* const doubled = okResult.map(x => x * 2).unwrap(); // 2
|
|
690
|
-
*
|
|
691
|
-
* const chained = Ok(5)
|
|
692
|
-
* .andThen(x => Ok(x * 2))
|
|
693
|
-
* .map(x => x + 1)
|
|
694
|
-
* .unwrap(); // 11
|
|
695
|
-
* ```
|
|
696
|
-
*/
|
|
697
|
-
declare class Success<T> extends ResultBase<T, never> {
|
|
698
|
-
constructor(value: T);
|
|
699
|
-
/**
|
|
700
|
-
* Factory function to create an Ok instance.
|
|
701
|
-
* Can be called with or without `new`.
|
|
702
|
-
*/
|
|
703
|
-
static of<T>(value: T): Success<T>;
|
|
704
|
-
/**
|
|
705
|
-
* Chains Result operations (flatMap/bind).
|
|
706
|
-
* If the result is Ok, applies the function to the value.
|
|
707
|
-
* If Err, returns the error unchanged.
|
|
708
|
-
*/
|
|
709
|
-
andThen<U, E>(fn: (value: T) => Result<U, E>): Success<U> | Erroneous<E>;
|
|
710
|
-
/**
|
|
711
|
-
* Transforms the Ok value using a function.
|
|
712
|
-
* If the result is Err, returns the error unchanged.
|
|
713
|
-
*/
|
|
714
|
-
map<U>(fn: (value: T) => U): Success<U>;
|
|
715
|
-
/**
|
|
716
|
-
* Transforms the Err value using a function.
|
|
717
|
-
* If the result is Ok, returns the value unchanged.
|
|
718
|
-
*/
|
|
719
|
-
mapErr<F>(_fn: (error: never) => F): Success<T>;
|
|
720
|
-
}
|
|
721
|
-
|
|
722
|
-
/**
|
|
723
|
-
* Class representing an erroneous result with method chaining support.
|
|
724
|
-
* Use this for class-based API with method chaining.
|
|
725
|
-
*
|
|
726
|
-
* @example
|
|
727
|
-
* ```typescript
|
|
728
|
-
* const errResult = Err(new Error("error"));
|
|
729
|
-
* errResult.map(x => x * 2).unwrap(); // throws Error("error")
|
|
730
|
-
*
|
|
731
|
-
* const mapped = Err("original")
|
|
732
|
-
* .mapErr(e => `Error: ${e}`)
|
|
733
|
-
* .unwrap(); // throws Error("Error: original")
|
|
734
|
-
* ```
|
|
735
|
-
*/
|
|
736
|
-
declare class Erroneous<E> extends ResultBase<never, E> {
|
|
737
|
-
constructor(error: E);
|
|
738
|
-
/**
|
|
739
|
-
* Factory function to create an Err instance.
|
|
740
|
-
* Can be called with or without `new`.
|
|
741
|
-
*/
|
|
742
|
-
static of<E>(error: E): Erroneous<E>;
|
|
743
|
-
/**
|
|
744
|
-
* Chains Result operations (flatMap/bind).
|
|
745
|
-
* If the result is Ok, applies the function to the value.
|
|
746
|
-
* If Err, returns the error unchanged.
|
|
747
|
-
*/
|
|
748
|
-
andThen<U>(_fn: (value: never) => Result<U, E>): Erroneous<E>;
|
|
749
|
-
/**
|
|
750
|
-
* Transforms the Ok value using a function.
|
|
751
|
-
* If the result is Err, returns the error unchanged.
|
|
752
|
-
*/
|
|
753
|
-
map<U>(_fn: (value: never) => U): Erroneous<E>;
|
|
754
|
-
/**
|
|
755
|
-
* Transforms the Err value using a function.
|
|
756
|
-
* If the result is Ok, returns the value unchanged.
|
|
757
|
-
*/
|
|
758
|
-
mapErr<F>(fn: (error: E) => F): Erroneous<F>;
|
|
759
|
-
}
|
|
760
|
-
|
|
761
|
-
/**
|
|
762
|
-
* Class-based API for Result with method chaining support.
|
|
763
|
-
* Provides an object-oriented alternative to the functional Result type.
|
|
764
|
-
* Use `Outcome.from()` to wrap an existing Result, or `Outcome.ok()` / `Outcome.err()` to create new instances.
|
|
765
|
-
*
|
|
766
|
-
* @example
|
|
767
|
-
* ```typescript
|
|
768
|
-
* // Wrap an existing Result
|
|
769
|
-
* const result = Outcome.from(ok(1));
|
|
770
|
-
* const doubled = result.map(x => x * 2).unwrap(); // 2
|
|
771
|
-
*
|
|
772
|
-
* // Create directly
|
|
773
|
-
* const outcome = Outcome.ok(42);
|
|
774
|
-
* const value = outcome.map(x => x + 1).unwrap(); // 43
|
|
775
|
-
* ```
|
|
776
|
-
*/
|
|
777
|
-
declare class Outcome<T, E> extends ResultBase<T, E> {
|
|
778
|
-
private constructor();
|
|
779
|
-
/**
|
|
780
|
-
* Creates an Outcome from an Ok value.
|
|
781
|
-
*/
|
|
782
|
-
static ok<T>(value: T): Success<T>;
|
|
783
|
-
/**
|
|
784
|
-
* Creates an Outcome from an Err value.
|
|
785
|
-
*/
|
|
786
|
-
static err<E>(error: E): Erroneous<E>;
|
|
787
|
-
/**
|
|
788
|
-
* Creates an Outcome from a Result.
|
|
789
|
-
*/
|
|
790
|
-
static from<T, E>(result: Result<T, E>): Outcome<T, E>;
|
|
791
|
-
andThen<U>(fn: (value: T) => Result<U, E>): Outcome<U, E>;
|
|
792
|
-
map<U>(fn: (value: T) => U): Outcome<U, E>;
|
|
793
|
-
mapErr<F>(fn: (error: E) => F): Outcome<T, F>;
|
|
794
|
-
}
|
|
795
|
-
|
|
796
367
|
type Handler<TState, TEvent> = (state: TState, event: TEvent) => TState;
|
|
797
368
|
/**
|
|
798
369
|
* Extended configuration options for AggregateEventSourced behavior.
|
|
@@ -1735,4 +1306,4 @@ declare function voWithValidation<T>(t: T, validate: (value: T) => boolean, erro
|
|
|
1735
1306
|
*/
|
|
1736
1307
|
declare function voWithValidationUnsafe<T>(t: T, validate: (value: T) => boolean, errorMessage?: string): ValueObject<T>;
|
|
1737
1308
|
|
|
1738
|
-
export { type Aggregate, AggregateBase, type AggregateConfig, AggregateEventSourced, type AggregateEventSourcedConfig, type AggregateRoot, type AggregateSnapshot, type Clock, type Command, CommandBus, type CommandHandler, type DomainEvent, type Entity, type
|
|
1309
|
+
export { type Aggregate, AggregateBase, type AggregateConfig, AggregateEventSourced, type AggregateEventSourcedConfig, type AggregateRoot, type AggregateSnapshot, type Clock, type Command, CommandBus, type CommandHandler, type DomainEvent, type Entity, type EventBus, EventBusImpl, type EventHandler, type EventMetadata, type ICommandBus, type IQueryBus, type IRepository, type ISpecification, type Id, type IdGenerator, type Outbox, type Query, QueryBus, type QueryHandler, type RepoProvider, type UnitOfWork, type ValueObject, type Version, aggregate, bump, copyMetadata, createDomainEvent, createDomainEventWithMetadata, entityIds, findEntityById, guard, hasEntityId, mergeMetadata, removeEntityById, replaceEntityById, sameAggregate, sameEntity, updateEntityById, vo, voEquals, voWithValidation, voWithValidationUnsafe, withCommit, withEvent };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var m=Object.defineProperty;var o=(t,e)=>m(t,"name",{value:e,configurable:true});function I(t,e=0){return {state:t,version:e,pendingEvents:[]}}o(I,"aggregate");function _(t,e){return {...t,pendingEvents:[...t.pendingEvents,e]}}o(_,"withEvent");function A(t){return {...t,version:t.version+1}}o(A,"bump");function f(t,e,r){return {type:t,payload:e,occurredAt:r?.occurredAt??new Date,version:r?.version??1,metadata:r?.metadata}}o(f,"createDomainEvent");function P(t,e,r,n){return f(t,e,{...n,metadata:r})}o(P,"createDomainEventWithMetadata");function V(t,e){return {...t.metadata??{},...e??{}}}o(V,"copyMetadata");function Q(...t){return Object.assign({},...t.filter(Boolean))}o(Q,"mergeMetadata");function C(t,e){return t.id===e.id&&t.version===e.version}o(C,"sameAggregate");var u=class{static{o(this,"AggregateBase");}id;version=0;_config;_autoVersionBump;get state(){return this._state}_state;constructor(e,r,n){this.id=e,this._state=r,this._config=n??{},this._autoVersionBump=this._config.autoVersionBump??false;}bumpVersion(){this.version=this.version+1;}setState(e,r){this._state=e,(r??this._autoVersionBump)&&this.bumpVersion();}createSnapshot(){return {state:{...this._state},version:this.version,snapshotAt:new Date}}restoreFromSnapshot(e){this._state=e.state,this.version=e.version;}};function a(t){return {ok:true,value:t}}o(a,"ok");function i(t){return {ok:false,error:t}}o(i,"err");var p=class extends u{static{o(this,"AggregateEventSourced");}_eventConfig;_eventAutoVersionBump;_pendingEvents=[];constructor(e,r,n){super(e,r,n),this._eventConfig=n??{},this._eventAutoVersionBump=this._eventConfig.autoVersionBump??true;}get pendingEvents(){return this._pendingEvents}clearPendingEvents(){this._pendingEvents.length=0;}validateEvent(e){return a(true)}apply(e,r=true){let n=this.validateEvent(e);if(!n.ok)return i(`Event validation failed for ${e.type}: ${n.error}`);let s=this.handlers[e.type];return s?(this._state=s(this._state,e),r&&(this._pendingEvents.push(e),this._eventAutoVersionBump&&(this.version=this.version+1)),a()):i(`Missing handler for event type: ${e.type}`)}applyUnsafe(e,r=true){let n=this.validateEvent(e);if(!n.ok)throw new Error(`Event validation failed for ${e.type}: ${n.error}`);let s=this.handlers[e.type];if(!s)throw new Error(`Missing handler for event type: ${e.type}`);this._state=s(this._state,e),r&&(this._pendingEvents.push(e),this._eventAutoVersionBump&&(this.version=this.version+1));}bumpVersion(){this.version=this.version+1;}loadFromHistory(e){for(let r of e){let n=this.apply(r,false);if(!n.ok)return n}return this.version=e.length,a()}hasPendingEvents(){return this._pendingEvents.length>0}getEventCount(){return this._pendingEvents.length}getLatestEvent(){return this._pendingEvents[this._pendingEvents.length-1]}restoreFromSnapshotWithEvents(e,r){this._state=e.state,this.version=e.version;for(let n of r){let s=this.apply(n,false);if(!s.ok)return s}return this.version=e.version+r.length,a()}};var d=class{static{o(this,"CommandBus");}handlers=new Map;register(e,r){this.handlers.set(e,r);}async execute(e){let r=this.handlers.get(e.type);return r?r(e):i(`No handler registered for command type: ${e.type}`)}};function ne(t,e){return t.uow.transactional(async()=>{let{result:r,events:n}=await e();return await t.outbox.add(n),t.bus&&await t.bus.publish(n),r})}o(ne,"withCommit");var l=class{static{o(this,"QueryBus");}handlers=new Map;register(e,r){this.handlers.set(e,r);}async execute(e){let r=this.handlers.get(e.type);if(!r)return i(`No handler registered for query type: ${e.type}`);try{let n=await r(e);return a(n)}catch(n){return i(n instanceof Error?n.message:String(n))}}async executeUnsafe(e){let r=this.handlers.get(e.type);if(!r)throw new Error(`No handler registered for query type: ${e.type}`);return r(e)}};function de(t,e){return t?a(true):i(e)}o(de,"guard");function Ee(t,e){return t.id===e.id}o(Ee,"sameEntity");function Te(t,e){return t.find(r=>r.id===e)}o(Te,"findEntityById");function me(t,e){return t.some(r=>r.id===e)}o(me,"hasEntityId");function fe(t,e){return t.filter(r=>r.id!==e)}o(fe,"removeEntityById");function ve(t,e,r){return t.map(n=>n.id===e?r(n):n)}o(ve,"updateEntityById");function ge(t,e,r){return t.map(n=>n.id===e?r:n)}o(ge,"replaceEntityById");function he(t){return t.map(e=>e.id)}o(he,"entityIds");var c=class{static{o(this,"EventBusImpl");}handlers=new Map;subscribe(e,r){let n=e;this.handlers.has(n)||this.handlers.set(n,new Set);let s=this.handlers.get(n);return s.add(r),()=>{s.delete(r),s.size===0&&this.handlers.delete(n);}}async publish(e){for(let r of e){let n=this.handlers.get(r.type);n&&await Promise.all(Array.from(n).map(s=>s(r)));}}};function E(t,e=new WeakSet){if(t===null||typeof t!="object"||e.has(t))return t;e.add(t);let r=Object.getOwnPropertyNames(t);for(let n of r){let s=t[n];s&&(typeof s=="object"||Array.isArray(s))&&E(s,e);}return Object.freeze(t)}o(E,"deepFreeze");function T(t){return E({...t})}o(T,"vo");function Se(t,e){return JSON.stringify(t)===JSON.stringify(e)}o(Se,"voEquals");function be(t,e,r){return e(t)?a(T(t)):i(r??`Validation failed for value object: ${JSON.stringify(t)}`)}o(be,"voWithValidation");function Ie(t,e,r){if(!e(t))throw new Error(r??`Validation failed for value object: ${JSON.stringify(t)}`);return T(t)}o(Ie,"voWithValidationUnsafe");export{u as AggregateBase,p as AggregateEventSourced,d as CommandBus,c as EventBusImpl,l as QueryBus,I as aggregate,A as bump,V as copyMetadata,f as createDomainEvent,P as createDomainEventWithMetadata,he as entityIds,Te as findEntityById,de as guard,me as hasEntityId,Q as mergeMetadata,fe as removeEntityById,ge as replaceEntityById,C as sameAggregate,Ee as sameEntity,ve as updateEntityById,T as vo,Se as voEquals,be as voWithValidation,Ie as voWithValidationUnsafe,ne as withCommit,_ as withEvent};//# sourceMappingURL=index.js.map
|
|
2
2
|
//# sourceMappingURL=index.js.map
|