UNPKG

cqrs-eda

Version:

Lightweight CQRS and Event-Driven Architecture library using TypeScript decorators, handlers and typings. Perfect for scalable event-driven apps.

40 lines 1.39 kB
import { Constructor, ICommandHandler } from "../types/base"; /** * Handles the registration and execution of command handlers. * Supports instantiation of command classes, optionally via a custom factory. * * @template C - A mapping of command names to their payload types. * * @example * ```ts * interface CommandsPayloads { * CREATE_USER: { name: string }; * } * * const commandHandler = new CommandHandler<CommandsPayloads>(); * await commandHandler.fire("CREATE_USER", { name: "Leandro" }); * ``` */ export declare class CommandHandler<C extends Record<string, any>> implements ICommandHandler<C> { private factory?; private commands; /** * @param factory Optional factory function to instantiate command classes. * Useful for integrating with DI containers or custom initialization. * * Example: * ```ts * const handler = new CommandHandler((cls) => container.resolve(cls)); * ``` */ constructor(factory?: ((cls: Constructor) => any) | undefined); /** * Executes a command by name with the given payload. * * @param commandName - The command name to execute. * @param payload - The data to pass to the command. * @throws Error if the command is not registered. */ fire<K extends keyof C>(commandName: K, payload: C[K]): Promise<void>; } //# sourceMappingURL=handler.d.ts.map