cqrs-eda
Version:
Lightweight CQRS and Event-Driven Architecture library using TypeScript decorators, handlers and typings. Perfect for scalable event-driven apps.
47 lines • 1.57 kB
TypeScript
import { Constructor, IQueryHandler } from "../types/base";
/**
* Handles the registration and execution of query handlers.
* Supports instantiation of query classes, optionally via a custom factory.
*
* @template Q - A mapping of query names to their parameter types.
* @template R - A mapping of query names to their return types.
*
* @example
* ```ts
* interface Queries {
* GET_USER: { id: number };
* }
* interface Results {
* GET_USER: User | null;
* }
*
* const queryHandler = new QueryHandler<Queries, Results>();
* const user = await queryHandler.fire("GET_USER", { id: 1 });
* ```
*/
export declare class QueryHandler<Q extends Record<string, any>, R extends {
[K in keyof Q]: any;
}> implements IQueryHandler<Q, R> {
private factory?;
private queries;
/**
* @param factory Optional factory function to instantiate query classes.
* Useful for integrating with DI containers or custom initialization.
*
* Example:
* ```ts
* const handler = new QueryHandler((cls) => container.resolve(cls));
* ```
*/
constructor(factory?: ((cls: Constructor) => any) | undefined);
/**
* Executes a query by name with the given parameters.
*
* @param queryName - The query name to execute.
* @param params - The parameters to pass to the query.
* @returns The result of the query execution.
* @throws Error if the query is not registered.
*/
fire<K extends keyof Q>(queryName: K, params: Q[K]): Promise<R[K]>;
}
//# sourceMappingURL=handler.d.ts.map