@zudojs/cqrs 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.
Files changed (110) hide show
  1. package/README.md +41 -0
  2. package/dist/command/command.core.d.ts +47 -0
  3. package/dist/command/command.core.d.ts.map +1 -0
  4. package/dist/command/command.core.js +58 -0
  5. package/dist/command/command.core.js.map +1 -0
  6. package/dist/command/commandBus.core.d.ts +91 -0
  7. package/dist/command/commandBus.core.d.ts.map +1 -0
  8. package/dist/command/commandBus.core.js +177 -0
  9. package/dist/command/commandBus.core.js.map +1 -0
  10. package/dist/command/commandHandler.core.d.ts +43 -0
  11. package/dist/command/commandHandler.core.d.ts.map +1 -0
  12. package/dist/command/commandHandler.core.js +57 -0
  13. package/dist/command/commandHandler.core.js.map +1 -0
  14. package/dist/command/commandResult.type.d.ts +57 -0
  15. package/dist/command/commandResult.type.d.ts.map +1 -0
  16. package/dist/command/commandResult.type.js +86 -0
  17. package/dist/command/commandResult.type.js.map +1 -0
  18. package/dist/command/index.d.ts +10 -0
  19. package/dist/command/index.d.ts.map +1 -0
  20. package/dist/command/index.js +10 -0
  21. package/dist/command/index.js.map +1 -0
  22. package/dist/cqrsContext/cqrsContext.core.d.ts +97 -0
  23. package/dist/cqrsContext/cqrsContext.core.d.ts.map +1 -0
  24. package/dist/cqrsContext/cqrsContext.core.js +171 -0
  25. package/dist/cqrsContext/cqrsContext.core.js.map +1 -0
  26. package/dist/cqrsContext/index.d.ts +7 -0
  27. package/dist/cqrsContext/index.d.ts.map +1 -0
  28. package/dist/cqrsContext/index.js +7 -0
  29. package/dist/cqrsContext/index.js.map +1 -0
  30. package/dist/cqrsDecorators/cqrsDecorators.core.d.ts +108 -0
  31. package/dist/cqrsDecorators/cqrsDecorators.core.d.ts.map +1 -0
  32. package/dist/cqrsDecorators/cqrsDecorators.core.js +181 -0
  33. package/dist/cqrsDecorators/cqrsDecorators.core.js.map +1 -0
  34. package/dist/cqrsDecorators/index.d.ts +7 -0
  35. package/dist/cqrsDecorators/index.d.ts.map +1 -0
  36. package/dist/cqrsDecorators/index.js +7 -0
  37. package/dist/cqrsDecorators/index.js.map +1 -0
  38. package/dist/cqrsErrors/cqrsError.base.d.ts +85 -0
  39. package/dist/cqrsErrors/cqrsError.base.d.ts.map +1 -0
  40. package/dist/cqrsErrors/cqrsError.base.js +221 -0
  41. package/dist/cqrsErrors/cqrsError.base.js.map +1 -0
  42. package/dist/cqrsErrors/index.d.ts +7 -0
  43. package/dist/cqrsErrors/index.d.ts.map +1 -0
  44. package/dist/cqrsErrors/index.js +7 -0
  45. package/dist/cqrsErrors/index.js.map +1 -0
  46. package/dist/cqrsEvents/cqrsEventBus.core.d.ts +9 -0
  47. package/dist/cqrsEvents/cqrsEventBus.core.d.ts.map +1 -0
  48. package/dist/cqrsEvents/cqrsEventBus.core.js +9 -0
  49. package/dist/cqrsEvents/cqrsEventBus.core.js.map +1 -0
  50. package/dist/cqrsEvents/cqrsEventResult.type.d.ts +81 -0
  51. package/dist/cqrsEvents/cqrsEventResult.type.d.ts.map +1 -0
  52. package/dist/cqrsEvents/cqrsEventResult.type.js +159 -0
  53. package/dist/cqrsEvents/cqrsEventResult.type.js.map +1 -0
  54. package/dist/cqrsEvents/cqrsEvents.type.d.ts +102 -0
  55. package/dist/cqrsEvents/cqrsEvents.type.d.ts.map +1 -0
  56. package/dist/cqrsEvents/cqrsEvents.type.js +103 -0
  57. package/dist/cqrsEvents/cqrsEvents.type.js.map +1 -0
  58. package/dist/cqrsEvents/index.d.ts +12 -0
  59. package/dist/cqrsEvents/index.d.ts.map +1 -0
  60. package/dist/cqrsEvents/index.js +12 -0
  61. package/dist/cqrsEvents/index.js.map +1 -0
  62. package/dist/cqrsMiddleware/cqrsMiddleware.core.d.ts +65 -0
  63. package/dist/cqrsMiddleware/cqrsMiddleware.core.d.ts.map +1 -0
  64. package/dist/cqrsMiddleware/cqrsMiddleware.core.js +185 -0
  65. package/dist/cqrsMiddleware/cqrsMiddleware.core.js.map +1 -0
  66. package/dist/cqrsMiddleware/index.d.ts +7 -0
  67. package/dist/cqrsMiddleware/index.d.ts.map +1 -0
  68. package/dist/cqrsMiddleware/index.js +7 -0
  69. package/dist/cqrsMiddleware/index.js.map +1 -0
  70. package/dist/cqrsTypes/cqrsTypes.type.d.ts +158 -0
  71. package/dist/cqrsTypes/cqrsTypes.type.d.ts.map +1 -0
  72. package/dist/cqrsTypes/cqrsTypes.type.js +34 -0
  73. package/dist/cqrsTypes/cqrsTypes.type.js.map +1 -0
  74. package/dist/cqrsTypes/index.d.ts +7 -0
  75. package/dist/cqrsTypes/index.d.ts.map +1 -0
  76. package/dist/cqrsTypes/index.js +7 -0
  77. package/dist/cqrsTypes/index.js.map +1 -0
  78. package/dist/handlerRegistry/handlerRegistry.core.d.ts +135 -0
  79. package/dist/handlerRegistry/handlerRegistry.core.d.ts.map +1 -0
  80. package/dist/handlerRegistry/handlerRegistry.core.js +209 -0
  81. package/dist/handlerRegistry/handlerRegistry.core.js.map +1 -0
  82. package/dist/handlerRegistry/index.d.ts +7 -0
  83. package/dist/handlerRegistry/index.d.ts.map +1 -0
  84. package/dist/handlerRegistry/index.js +7 -0
  85. package/dist/handlerRegistry/index.js.map +1 -0
  86. package/dist/index.d.ts +17 -0
  87. package/dist/index.d.ts.map +1 -0
  88. package/dist/index.js +26 -0
  89. package/dist/index.js.map +1 -0
  90. package/dist/query/index.d.ts +10 -0
  91. package/dist/query/index.d.ts.map +1 -0
  92. package/dist/query/index.js +10 -0
  93. package/dist/query/index.js.map +1 -0
  94. package/dist/query/query.core.d.ts +50 -0
  95. package/dist/query/query.core.d.ts.map +1 -0
  96. package/dist/query/query.core.js +61 -0
  97. package/dist/query/query.core.js.map +1 -0
  98. package/dist/query/queryBus.core.d.ts +91 -0
  99. package/dist/query/queryBus.core.d.ts.map +1 -0
  100. package/dist/query/queryBus.core.js +177 -0
  101. package/dist/query/queryBus.core.js.map +1 -0
  102. package/dist/query/queryHandler.core.d.ts +43 -0
  103. package/dist/query/queryHandler.core.d.ts.map +1 -0
  104. package/dist/query/queryHandler.core.js +57 -0
  105. package/dist/query/queryHandler.core.js.map +1 -0
  106. package/dist/query/queryResult.type.d.ts +56 -0
  107. package/dist/query/queryResult.type.d.ts.map +1 -0
  108. package/dist/query/queryResult.type.js +86 -0
  109. package/dist/query/queryResult.type.js.map +1 -0
  110. package/package.json +52 -0
package/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # @zudojs/cqrs
2
+
3
+ Command Query Responsibility Segregation (CQRS) primitives for separating read and write operations.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @zudojs/cqrs
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```typescript
14
+ import { createCommandBus, createQueryBus } from "@zudojs/cqrs";
15
+
16
+ const commandBus = createCommandBus();
17
+ const queryBus = createQueryBus();
18
+
19
+ commandBus.register("CreateUser", async (command) => {
20
+ return userRepository.create(command.data);
21
+ });
22
+
23
+ queryBus.register("GetUser", async (query) => {
24
+ return userRepository.findById(query.id);
25
+ });
26
+ ```
27
+
28
+ ## Features
29
+
30
+ - Command bus for write operations
31
+ - Query bus for read operations
32
+ - Middleware pipeline for both
33
+ - Command and query handlers
34
+ - Result types for explicit returns
35
+
36
+ ## Use Cases
37
+
38
+ - Complex domain logic
39
+ - Event-sourced systems
40
+ - Read/write model separation
41
+ - Audit trails and logging
@@ -0,0 +1,47 @@
1
+ import type { Command as CommandContract } from "../cqrsTypes/cqrsTypes.type.js";
2
+ /**
3
+ * Base abstract command.
4
+ *
5
+ * Commands represent requests to change application state.
6
+ * Each concrete command should define a unique static type and
7
+ * immutable command data.
8
+ */
9
+ export declare abstract class Command<TType extends string = string> implements CommandContract<TType> {
10
+ readonly type: TType;
11
+ protected constructor(type: TType);
12
+ }
13
+ /**
14
+ * Options used when constructing a concrete command.
15
+ */
16
+ export interface CommandOptions {
17
+ readonly metadata?: Readonly<Record<string, unknown>>;
18
+ }
19
+ /**
20
+ * Command containing immutable metadata.
21
+ */
22
+ export declare abstract class MetadataCommand<TType extends string = string> extends Command<TType> {
23
+ readonly metadata?: Readonly<Record<string, unknown>>;
24
+ protected constructor(type: TType, options?: CommandOptions);
25
+ }
26
+ /**
27
+ * Creates a simple immutable command object.
28
+ */
29
+ export declare function createCommand<TType extends string, TPayload extends Record<string, unknown> = Record<string, never>>(type: TType, payload?: TPayload): Readonly<{
30
+ readonly type: TType;
31
+ } & TPayload>;
32
+ /**
33
+ * Returns the command type discriminator.
34
+ */
35
+ export declare function getCommandType(command: CommandContract): string;
36
+ /**
37
+ * Determines whether a value is a command.
38
+ */
39
+ export declare function isCommand(value: unknown): value is CommandContract;
40
+ /**
41
+ * Creates a command type factory.
42
+ *
43
+ * This is useful when defining a family of related commands while
44
+ * keeping their discriminator values consistent.
45
+ */
46
+ export declare function commandType<TType extends string>(type: TType): () => TType;
47
+ //# sourceMappingURL=command.core.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command.core.d.ts","sourceRoot":"","sources":["../../src/command/command.core.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,gCAAgC,CAAC;AAEjF;;;;;;GAMG;AACH,8BAAsB,OAAO,CAC3B,KAAK,SAAS,MAAM,GAAG,MAAM,CAC7B,YAAW,eAAe,CAAC,KAAK,CAAC;IACjC,SAAgB,IAAI,EAAE,KAAK,CAAC;IAE5B,SAAS,aAAa,IAAI,EAAE,KAAK,EAEhC;CACF;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CACvD;AAED;;GAEG;AACH,8BAAsB,eAAe,CACnC,KAAK,SAAS,MAAM,GAAG,MAAM,CAC7B,SAAQ,OAAO,CAAC,KAAK,CAAC;IACtB,SAAgB,QAAQ,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IAE7D,SAAS,aAAa,IAAI,EAAE,KAAK,EAAE,OAAO,GAAE,cAAmB,EAI9D;CACF;AAED;;GAEG;AACH,wBAAgB,aAAa,CAC3B,KAAK,SAAS,MAAM,EACpB,QAAQ,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,EAEhE,IAAI,EAAE,KAAK,EACX,OAAO,CAAC,EAAE,QAAQ,GACjB,QAAQ,CACT;IACE,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC;CACtB,GAAG,QAAQ,CACb,CASA;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,eAAe,GAAG,MAAM,CAE/D;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,eAAe,CAgBlE;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,SAAS,MAAM,EAAE,IAAI,EAAE,KAAK,GAAG,MAAM,KAAK,CAE1E"}
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Base abstract command.
3
+ *
4
+ * Commands represent requests to change application state.
5
+ * Each concrete command should define a unique static type and
6
+ * immutable command data.
7
+ */
8
+ export class Command {
9
+ type;
10
+ constructor(type) {
11
+ this.type = type;
12
+ }
13
+ }
14
+ /**
15
+ * Command containing immutable metadata.
16
+ */
17
+ export class MetadataCommand extends Command {
18
+ metadata;
19
+ constructor(type, options = {}) {
20
+ super(type);
21
+ this.metadata = options.metadata;
22
+ }
23
+ }
24
+ /**
25
+ * Creates a simple immutable command object.
26
+ */
27
+ export function createCommand(type, payload) {
28
+ return Object.freeze({
29
+ type,
30
+ ...(payload ?? {}),
31
+ });
32
+ }
33
+ /**
34
+ * Returns the command type discriminator.
35
+ */
36
+ export function getCommandType(command) {
37
+ return command.type;
38
+ }
39
+ /**
40
+ * Determines whether a value is a command.
41
+ */
42
+ export function isCommand(value) {
43
+ return (typeof value === "object" &&
44
+ value !== null &&
45
+ "type" in value &&
46
+ typeof value.type === "string" &&
47
+ value.type.length > 0);
48
+ }
49
+ /**
50
+ * Creates a command type factory.
51
+ *
52
+ * This is useful when defining a family of related commands while
53
+ * keeping their discriminator values consistent.
54
+ */
55
+ export function commandType(type) {
56
+ return () => type;
57
+ }
58
+ //# sourceMappingURL=command.core.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command.core.js","sourceRoot":"","sources":["../../src/command/command.core.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,OAAgB,OAAO;IAGX,IAAI,CAAQ;IAE5B,YAAsB,IAAW;QAC/B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF;AASD;;GAEG;AACH,MAAM,OAAgB,eAEpB,SAAQ,OAAc;IACN,QAAQ,CAAqC;IAE7D,YAAsB,IAAW,EAAE,OAAO,GAAmB,EAAE;QAC7D,KAAK,CAAC,IAAI,CAAC,CAAC;QAEZ,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;IACnC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,UAAU,aAAa,CAI3B,IAAW,EACX,OAAkB;IAMlB,OAAO,MAAM,CAAC,MAAM,CAAC;QACnB,IAAI;QACJ,GAAG,CAAC,OAAO,IAAK,EAAe,CAAC;KACjC,CAIA,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,cAAc,CAAC,OAAwB;IACrD,OAAO,OAAO,CAAC,IAAI,CAAC;AACtB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,SAAS,CAAC,KAAc;IACtC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,MAAM,IAAI,KAAK;QACf,OACE,KAGD,CAAC,IAAI,KAAK,QAAQ;QAEjB,KAGD,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAClB,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAuB,IAAW;IAC3D,OAAO,GAAG,EAAE,CAAC,IAAI,CAAC;AACpB,CAAC"}
@@ -0,0 +1,91 @@
1
+ import type { Command, CommandBus as CommandBusContract, CommandHandlerLike, CqrsContext, CqrsMiddleware } from "../cqrsTypes/cqrsTypes.type.js";
2
+ /**
3
+ * Options for constructing a command bus.
4
+ */
5
+ export interface CommandBusOptions {
6
+ readonly middleware?: readonly CqrsMiddleware[];
7
+ /**
8
+ * Creates a default execution context when one is not supplied.
9
+ */
10
+ readonly contextFactory?: () => CqrsContext | Promise<CqrsContext>;
11
+ }
12
+ /**
13
+ * Registered command handler.
14
+ */
15
+ export interface CommandRegistration<TCommand extends Command = Command, TResult = void> {
16
+ readonly commandType: TCommand["type"];
17
+ readonly handler: CommandHandlerLike<TCommand, TResult>;
18
+ }
19
+ /**
20
+ * Command bus implementation.
21
+ *
22
+ * The command bus resolves a handler by command type and executes it
23
+ * through the registered middleware pipeline.
24
+ */
25
+ export declare class CommandBus implements CommandBusContract {
26
+ private readonly handlers;
27
+ private readonly middleware;
28
+ private readonly contextFactory?;
29
+ constructor(options?: CommandBusOptions);
30
+ /**
31
+ * Registers a command handler.
32
+ */
33
+ register<TCommand extends Command, TResult = void>(commandType: TCommand["type"], handler: CommandHandlerLike<TCommand, TResult>): this;
34
+ /**
35
+ * Registers multiple command handlers.
36
+ */
37
+ registerMany(registrations: readonly CommandRegistration[]): this;
38
+ /**
39
+ * Replaces an existing command handler.
40
+ */
41
+ replace<TCommand extends Command, TResult = void>(commandType: TCommand["type"], handler: CommandHandlerLike<TCommand, TResult>): this;
42
+ /**
43
+ * Removes a command handler.
44
+ */
45
+ unregister(commandType: string): boolean;
46
+ /**
47
+ * Determines whether a command handler is registered.
48
+ */
49
+ has(commandType: string): boolean;
50
+ /**
51
+ * Returns the registered handler for a command type.
52
+ */
53
+ getHandler<TCommand extends Command, TResult = void>(commandType: TCommand["type"]): CommandHandlerLike<TCommand, TResult> | undefined;
54
+ /**
55
+ * Executes a command.
56
+ */
57
+ execute<TCommand extends Command, TResult = void>(command: TCommand, context?: CqrsContext): Promise<TResult>;
58
+ /**
59
+ * Adds middleware to the end of the pipeline.
60
+ */
61
+ use(middleware: CqrsMiddleware): this;
62
+ /**
63
+ * Returns the number of registered handlers.
64
+ */
65
+ size(): number;
66
+ /**
67
+ * Removes all registered handlers.
68
+ */
69
+ clear(): void;
70
+ /**
71
+ * Returns all registered command types.
72
+ */
73
+ getCommandTypes(): readonly string[];
74
+ /**
75
+ * Builds the command execution pipeline.
76
+ */
77
+ private buildPipeline;
78
+ /**
79
+ * Resolves the execution context.
80
+ */
81
+ private resolveContext;
82
+ /**
83
+ * Validates a command before execution.
84
+ */
85
+ private validateCommand;
86
+ }
87
+ /**
88
+ * Creates a command bus.
89
+ */
90
+ export declare function createCommandBus(options?: CommandBusOptions): CommandBus;
91
+ //# sourceMappingURL=commandBus.core.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commandBus.core.d.ts","sourceRoot":"","sources":["../../src/command/commandBus.core.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EACV,OAAO,EACP,UAAU,IAAI,kBAAkB,EAChC,kBAAkB,EAClB,WAAW,EACX,cAAc,EACf,MAAM,gCAAgC,CAAC;AAOxC;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,UAAU,CAAC,EAAE,SAAS,cAAc,EAAE,CAAC;IAEhD;;OAEG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;CACpE;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB,CAClC,QAAQ,SAAS,OAAO,GAAG,OAAO,EAClC,OAAO,GAAG,IAAI;IAEd,QAAQ,CAAC,WAAW,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IAEvC,QAAQ,CAAC,OAAO,EAAE,kBAAkB,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OAEG;IACI,GAAG,CAAC,UAAU,EAAE,cAAc,GAAG,IAAI,CAQ3C;IAED;;OAEG;IACI,IAAI,IAAI,MAAM,CAEpB;IAED;;OAEG;IACI,KAAK,IAAI,IAAI,CAEnB;IAED;;OAEG;IACI,eAAe,IAAI,SAAS,MAAM,EAAE,CAE1C;IAED;;OAEG;IACH,OAAO,CAAC,aAAa;IAqCrB;;OAEG;YACW,cAAc;IAY5B;;OAEG;IACH,OAAO,CAAC,eAAe;CAuBxB;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,GAAE,iBAAsB,GAAG,UAAU,CAE5E"}
@@ -0,0 +1,177 @@
1
+ import { BaseError, ErrorCategory, ErrorCode, ErrorSeverity, } from "@zudojs/errors";
2
+ import { CommandHandler, executeCommandHandler, } from "../command/commandHandler.core.js";
3
+ /**
4
+ * Command bus implementation.
5
+ *
6
+ * The command bus resolves a handler by command type and executes it
7
+ * through the registered middleware pipeline.
8
+ */
9
+ export class CommandBus {
10
+ handlers = new Map();
11
+ middleware;
12
+ contextFactory;
13
+ constructor(options = {}) {
14
+ this.middleware = [...(options.middleware ?? [])];
15
+ this.contextFactory = options.contextFactory;
16
+ }
17
+ /**
18
+ * Registers a command handler.
19
+ */
20
+ register(commandType, handler) {
21
+ if (!commandType.trim()) {
22
+ throw new TypeError("Command type cannot be empty.");
23
+ }
24
+ if (!handler) {
25
+ throw new TypeError(`A handler is required for command "${commandType}".`);
26
+ }
27
+ if (this.handlers.has(commandType)) {
28
+ throw new Error(`A handler is already registered for command "${commandType}".`);
29
+ }
30
+ this.handlers.set(commandType, handler);
31
+ return this;
32
+ }
33
+ /**
34
+ * Registers multiple command handlers.
35
+ */
36
+ registerMany(registrations) {
37
+ for (const registration of registrations) {
38
+ this.register(registration.commandType, registration.handler);
39
+ }
40
+ return this;
41
+ }
42
+ /**
43
+ * Replaces an existing command handler.
44
+ */
45
+ replace(commandType, handler) {
46
+ if (!commandType.trim()) {
47
+ throw new TypeError("Command type cannot be empty.");
48
+ }
49
+ this.handlers.set(commandType, handler);
50
+ return this;
51
+ }
52
+ /**
53
+ * Removes a command handler.
54
+ */
55
+ unregister(commandType) {
56
+ return this.handlers.delete(commandType);
57
+ }
58
+ /**
59
+ * Determines whether a command handler is registered.
60
+ */
61
+ has(commandType) {
62
+ return this.handlers.has(commandType);
63
+ }
64
+ /**
65
+ * Returns the registered handler for a command type.
66
+ */
67
+ getHandler(commandType) {
68
+ return this.handlers.get(commandType);
69
+ }
70
+ /**
71
+ * Executes a command.
72
+ */
73
+ async execute(command, context) {
74
+ this.validateCommand(command);
75
+ const handler = this.getHandler(command.type);
76
+ if (!handler) {
77
+ throw new BaseError(`No handler is registered for command "${command.type}".`, {
78
+ code: ErrorCode.COMMAND_HANDLER_NOT_FOUND,
79
+ category: ErrorCategory.SYSTEM,
80
+ severity: ErrorSeverity.ERROR,
81
+ statusCode: 500,
82
+ expose: false,
83
+ isOperational: true,
84
+ metadata: {
85
+ commandType: command.type,
86
+ },
87
+ });
88
+ }
89
+ const executionContext = await this.resolveContext(context);
90
+ const pipeline = this.buildPipeline(handler);
91
+ return pipeline(command, executionContext);
92
+ }
93
+ /**
94
+ * Adds middleware to the end of the pipeline.
95
+ */
96
+ use(middleware) {
97
+ if (typeof middleware !== "function") {
98
+ throw new TypeError("Command middleware must be a function.");
99
+ }
100
+ this.middleware.push(middleware);
101
+ return this;
102
+ }
103
+ /**
104
+ * Returns the number of registered handlers.
105
+ */
106
+ size() {
107
+ return this.handlers.size;
108
+ }
109
+ /**
110
+ * Removes all registered handlers.
111
+ */
112
+ clear() {
113
+ this.handlers.clear();
114
+ }
115
+ /**
116
+ * Returns all registered command types.
117
+ */
118
+ getCommandTypes() {
119
+ return [...this.handlers.keys()];
120
+ }
121
+ /**
122
+ * Builds the command execution pipeline.
123
+ */
124
+ buildPipeline(handler) {
125
+ let next = async (command, context) => executeCommandHandler(handler, command, context);
126
+ for (let index = this.middleware.length - 1; index >= 0; index--) {
127
+ const middleware = this.middleware[index];
128
+ const current = next;
129
+ next = async (command, context) => middleware(command, context, current);
130
+ }
131
+ return next;
132
+ }
133
+ /**
134
+ * Resolves the execution context.
135
+ */
136
+ async resolveContext(context) {
137
+ if (context) {
138
+ return context;
139
+ }
140
+ if (this.contextFactory) {
141
+ return await this.contextFactory();
142
+ }
143
+ return {};
144
+ }
145
+ /**
146
+ * Validates a command before execution.
147
+ */
148
+ validateCommand(command) {
149
+ if (!command || typeof command !== "object") {
150
+ throw new BaseError("A valid command is required.", {
151
+ code: ErrorCode.INVALID_COMMAND,
152
+ category: ErrorCategory.VALIDATION,
153
+ severity: ErrorSeverity.WARNING,
154
+ statusCode: 400,
155
+ expose: true,
156
+ isOperational: true,
157
+ });
158
+ }
159
+ if (typeof command.type !== "string" || command.type.trim().length === 0) {
160
+ throw new BaseError("Command type is required.", {
161
+ code: ErrorCode.INVALID_COMMAND,
162
+ category: ErrorCategory.VALIDATION,
163
+ severity: ErrorSeverity.WARNING,
164
+ statusCode: 400,
165
+ expose: true,
166
+ isOperational: true,
167
+ });
168
+ }
169
+ }
170
+ }
171
+ /**
172
+ * Creates a command bus.
173
+ */
174
+ export function createCommandBus(options = {}) {
175
+ return new CommandBus(options);
176
+ }
177
+ //# sourceMappingURL=commandBus.core.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commandBus.core.js","sourceRoot":"","sources":["../../src/command/commandBus.core.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,aAAa,EACb,SAAS,EACT,aAAa,GACd,MAAM,gBAAgB,CAAC;AAUxB,OAAO,EACL,cAAc,EACd,qBAAqB,GACtB,MAAM,mCAAmC,CAAC;AA0B3C;;;;;GAKG;AACH,MAAM,OAAO,UAAU;IACJ,QAAQ,GAAG,IAAI,GAAG,EAA8B,CAAC;IAEjD,UAAU,CAAmB;IAE7B,cAAc,CAA4C;IAE3E,YAAY,OAAO,GAAsB,EAAE;QACzC,IAAI,CAAC,UAAU,GAAG,CAAC,GAAG,CAAC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QAAQ,EAAE,CAAC;YAC5C,MAAM,IAAI,SAAS,CAAC,8BAA8B,EAAE;gBAClD,IAAI,EAAE,SAAS,CAAC,eAAe;gBAC/B,QAAQ,EAAE,aAAa,CAAC,UAAU;gBAClC,QAAQ,EAAE,aAAa,CAAC,OAAO;gBAC/B,UAAU,EAAE,GAAG;gBACf,MAAM,EAAE,IAAI;gBACZ,aAAa,EAAE,IAAI;aACpB,CAAC,CAAC;QACL,CAAC;QAED,IAAI,OAAO,OAAO,CAAC,IAAI,KAAK,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACzE,MAAM,IAAI,SAAS,CAAC,2BAA2B,EAAE;gBAC/C,IAAI,EAAE,SAAS,CAAC,eAAe;gBAC/B,QAAQ,EAAE,aAAa,CAAC,UAAU;gBAClC,QAAQ,EAAE,aAAa,CAAC,OAAO;gBAC/B,UAAU,EAAE,GAAG;gBACf,MAAM,EAAE,IAAI;gBACZ,aAAa,EAAE,IAAI;aACpB,CAAC,CAAC;QACL,CAAC;IACH,CAAC;CACF;AAED;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAO,GAAsB,EAAE;IAC9D,OAAO,IAAI,UAAU,CAAC,OAAO,CAAC,CAAC;AACjC,CAAC"}
@@ -0,0 +1,43 @@
1
+ import type { CommandHandler as CommandHandlerContract, Command, CqrsContext } from "../cqrsTypes/cqrsTypes.type.js";
2
+ /**
3
+ * Abstract base class for command handlers.
4
+ *
5
+ * A command handler contains the application logic required to execute
6
+ * one specific command.
7
+ */
8
+ export declare abstract class CommandHandler<TCommand extends Command = Command, TResult = void> implements CommandHandlerContract<TCommand, TResult> {
9
+ /**
10
+ * Command type handled by this handler.
11
+ */
12
+ abstract readonly commandType: TCommand["type"];
13
+ /**
14
+ * Executes the command.
15
+ */
16
+ abstract execute(command: TCommand, context?: CqrsContext): Promise<TResult> | TResult;
17
+ }
18
+ /**
19
+ * Function-based command handler implementation.
20
+ */
21
+ export declare class FunctionCommandHandler<TCommand extends Command = Command, TResult = void> extends CommandHandler<TCommand, TResult> {
22
+ readonly commandType: TCommand["type"];
23
+ private readonly handler;
24
+ constructor(commandType: TCommand["type"], handler: (command: TCommand, context?: CqrsContext) => TResult | Promise<TResult>);
25
+ execute(command: TCommand, context?: CqrsContext): TResult | Promise<TResult>;
26
+ }
27
+ /**
28
+ * Creates a function-based command handler.
29
+ */
30
+ export declare function createCommandHandler<TCommand extends Command, TResult = void>(commandType: TCommand["type"], handler: (command: TCommand, context?: CqrsContext) => TResult | Promise<TResult>): FunctionCommandHandler<TCommand, TResult>;
31
+ /**
32
+ * Determines whether a value is a command handler instance.
33
+ */
34
+ export declare function isCommandHandler(value: unknown): value is CommandHandler;
35
+ /**
36
+ * Determines whether a value can be used as a command handler.
37
+ */
38
+ export declare function isCommandHandlerLike(value: unknown): value is CommandHandler | ((command: Command, context?: CqrsContext) => unknown);
39
+ /**
40
+ * Executes either an object-based or function-based command handler.
41
+ */
42
+ export declare function executeCommandHandler<TCommand extends Command, TResult = void>(handler: CommandHandler<TCommand, TResult> | ((command: TCommand, context?: CqrsContext) => TResult | Promise<TResult>), command: TCommand, context?: CqrsContext): Promise<TResult>;
43
+ //# sourceMappingURL=commandHandler.core.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commandHandler.core.d.ts","sourceRoot":"","sources":["../../src/command/commandHandler.core.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,IAAI,sBAAsB,EACxC,OAAO,EACP,WAAW,EACZ,MAAM,gCAAgC,CAAC;AAExC;;;;;GAKG;AACH,8BAAsB,cAAc,CAClC,QAAQ,SAAS,OAAO,GAAG,OAAO,EAClC,OAAO,GAAG,IAAI,CACd,YAAW,sBAAsB,CAAC,QAAQ,EAAE,OAAO,CAAC;IACpD;;OAEG;IACH,kBAAyB,WAAW,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IAEvD;;OAEG;IACH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ACH,wBAAsB,qBAAqB,CACzC,QAAQ,SAAS,OAAO,EACxB,OAAO,GAAG,IAAI,EAEd,OAAO,EACH,cAAc,CAAC,QAAQ,EAAE,OAAO,CAAC,GACjC,CAAC,CACC,OAAO,EAAE,QAAQ,EACjB,OAAO,CAAC,EAAE,WAAW,KAClB,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,EACpC,OAAO,EAAE,QAAQ,EACjB,OAAO,CAAC,EAAE,WAAW,GACpB,OAAO,CAAC,OAAO,CAAC,CAUlB"}
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Abstract base class for command handlers.
3
+ *
4
+ * A command handler contains the application logic required to execute
5
+ * one specific command.
6
+ */
7
+ export class CommandHandler {
8
+ }
9
+ /**
10
+ * Function-based command handler implementation.
11
+ */
12
+ export class FunctionCommandHandler extends CommandHandler {
13
+ commandType;
14
+ handler;
15
+ constructor(commandType, handler) {
16
+ super();
17
+ if (typeof handler !== "function") {
18
+ throw new TypeError("Command handler must be a function.");
19
+ }
20
+ this.commandType = commandType;
21
+ this.handler = handler;
22
+ }
23
+ execute(command, context) {
24
+ return this.handler(command, context);
25
+ }
26
+ }
27
+ /**
28
+ * Creates a function-based command handler.
29
+ */
30
+ export function createCommandHandler(commandType, handler) {
31
+ return new FunctionCommandHandler(commandType, handler);
32
+ }
33
+ /**
34
+ * Determines whether a value is a command handler instance.
35
+ */
36
+ export function isCommandHandler(value) {
37
+ return value instanceof CommandHandler;
38
+ }
39
+ /**
40
+ * Determines whether a value can be used as a command handler.
41
+ */
42
+ export function isCommandHandlerLike(value) {
43
+ return value instanceof CommandHandler || typeof value === "function";
44
+ }
45
+ /**
46
+ * Executes either an object-based or function-based command handler.
47
+ */
48
+ export async function executeCommandHandler(handler, command, context) {
49
+ if (handler instanceof CommandHandler) {
50
+ return await handler.execute(command, context);
51
+ }
52
+ if (typeof handler === "function") {
53
+ return await handler(command, context);
54
+ }
55
+ throw new TypeError(`Invalid command handler for "${command.type}".`);
56
+ }
57
+ //# sourceMappingURL=commandHandler.core.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commandHandler.core.js","sourceRoot":"","sources":["../../src/command/commandHandler.core.ts"],"names":[],"mappings":"AAMA;;;;;GAKG;AACH,MAAM,OAAgB,cAAc;CAgBnC;AAED;;GAEG;AACH,MAAM,OAAO,sBAGX,SAAQ,cAAiC;IACzB,WAAW,CAAmB;IAE7B,OAAO,CAGQ;IAEhC,YACE,WAA6B,EAC7B,OAG+B;QAE/B,KAAK,EAAE,CAAC;QAER,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE,CAAC;YAClC,MAAM,IAAI,SAAS,CAAC,qCAAqC,CAAC,CAAC;QAC7D,CAAC;QAED,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAE/B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAEM,OAAO,CACZ,OAAiB,EACjB,OAAqB;QAErB,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IACxC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAClC,WAA6B,EAC7B,OAG+B;IAE/B,OAAO,IAAI,sBAAsB,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;AAC1D,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAc;IAC7C,OAAO,KAAK,YAAY,cAAc,CAAC;AACzC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAClC,KAAc;IAGd,OAAO,KAAK,YAAY,cAAc,IAAI,OAAO,KAAK,KAAK,UAAU,CAAC;AACxE,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAIzC,OAKoC,EACpC,OAAiB,EACjB,OAAqB;IAErB,IAAI,OAAO,YAAY,cAAc,EAAE,CAAC;QACtC,OAAO,MAAM,OAAO,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IACjD,CAAC;IAED,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE,CAAC;QAClC,OAAO,MAAM,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IACzC,CAAC;IAED,MAAM,IAAI,SAAS,CAAC,gCAAgC,OAAO,CAAC,IAAI,IAAI,CAAC,CAAC;AACxE,CAAC"}
@@ -0,0 +1,57 @@
1
+ import type { Command } from "../cqrsTypes/cqrsTypes.type.js";
2
+ /**
3
+ * Status of a command execution.
4
+ */
5
+ export type CommandResultStatus = "success" | "failure";
6
+ /**
7
+ * Result returned after command execution.
8
+ *
9
+ * Command handlers may return any domain-specific value, while this
10
+ * wrapper provides consistent execution metadata for infrastructure
11
+ * consumers.
12
+ */
13
+ export interface CommandResult<TResult = void, TCommand extends Command = Command> {
14
+ readonly status: CommandResultStatus;
15
+ readonly result: TResult;
16
+ readonly commandType: TCommand["type"];
17
+ readonly executedAt: Date;
18
+ readonly durationMs?: number;
19
+ readonly metadata?: Readonly<Record<string, unknown>>;
20
+ }
21
+ /**
22
+ * Options for creating a command result.
23
+ */
24
+ export interface CreateCommandResultOptions<TCommand extends Command = Command> {
25
+ readonly command: TCommand;
26
+ readonly executedAt?: Date;
27
+ readonly durationMs?: number;
28
+ readonly metadata?: Readonly<Record<string, unknown>>;
29
+ }
30
+ /**
31
+ * Creates a successful command result.
32
+ */
33
+ export declare function createCommandResult<TResult, TCommand extends Command>(result: TResult, options: CreateCommandResultOptions<TCommand>): CommandResult<TResult, TCommand>;
34
+ /**
35
+ * Creates a failed command result.
36
+ *
37
+ * This helper is intended for infrastructure that represents failures
38
+ * as values. Command buses normally throw errors instead.
39
+ */
40
+ export declare function createFailedCommandResult<TResult = void, TCommand extends Command = Command>(result: TResult, options: CreateCommandResultOptions<TCommand>): CommandResult<TResult, TCommand>;
41
+ /**
42
+ * Determines whether a command result represents success.
43
+ */
44
+ export declare function isSuccessfulCommandResult<TResult, TCommand extends Command = Command>(result: CommandResult<TResult, TCommand>): boolean;
45
+ /**
46
+ * Determines whether a command result represents failure.
47
+ */
48
+ export declare function isFailedCommandResult<TResult, TCommand extends Command = Command>(result: CommandResult<TResult, TCommand>): boolean;
49
+ /**
50
+ * Maps a command result to its underlying value.
51
+ */
52
+ export declare function unwrapCommandResult<TResult, TCommand extends Command = Command>(result: CommandResult<TResult, TCommand>): TResult;
53
+ /**
54
+ * Adds execution metadata to an existing command result.
55
+ */
56
+ export declare function withCommandResultMetadata<TResult, TCommand extends Command = Command>(result: CommandResult<TResult, TCommand>, metadata: Readonly<Record<string, unknown>>): CommandResult<TResult, TCommand>;
57
+ //# sourceMappingURL=commandResult.type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commandResult.type.d.ts","sourceRoot":"","sources":["../../src/command/commandResult.type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gCAAgC,CAAC;AAE9D;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,SAAS,GAAG,SAAS,CAAC;AAExD;;;;;;GAMG;AACH,MAAM,WAAW,aAAa,CAC5B,OAAO,GAAG,IAAI,EACd,QAAQ,SAAS,OAAO,GAAG,OAAO;IAElC,QAAQ,CAAC,MAAM,EAAE,mBAAmB,CAAC;IACrC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,WAAW,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IACvC,QAAQ,CAAC,UAAU,EAAE,IAAI,CAAC;IAC1B,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CACvD;AAED;;GAEG;AACH,MAAM,WAAW,0BAA0B,CACzC,QAAQ,SAAS,OAAO,GAAG,OAAO;IAElC,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC;IAC3B,QAAQ,CAAC,UAAU,CAAC,EA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