@auto-engineer/message-bus
Version:
Message bus for handling commands, events, and queries
34 lines (28 loc) • 976 B
text/typescript
// Core CQRS types
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type DefaultRecord = Record<string, any>;
export type Command<CommandType extends string = string, CommandData extends DefaultRecord = DefaultRecord> = Readonly<{
type: CommandType;
data: Readonly<CommandData>;
timestamp?: Date;
requestId?: string;
correlationId?: string;
}>;
export type Event<EventType extends string = string, EventData extends DefaultRecord = DefaultRecord> = Readonly<{
type: EventType;
data: EventData;
timestamp?: Date;
requestId?: string;
correlationId?: string;
}>;
export type CommandHandler<TCommand extends Command = Command, TEvent extends Event = Event> = {
name: string;
handle: (command: TCommand) => Promise<TEvent | TEvent[] | void>;
};
export type EventHandler<TEvent extends Event = Event> = {
name: string;
handle: (event: TEvent) => Promise<void> | void;
};
export type EventSubscription = {
unsubscribe: () => void;
};