import { z, type ZodRawShape, type ZodType, type ZodTypeAny } from "zod"; import type { ActorHandler, CommandHandler, EventReducer, Invariant, ProjectorReducer, StateReducer } from "./handlers"; import type { CommittedEvent, Message, Messages, State } from "./messages"; import type { Projection } from "./projection"; /** All artifact types */ export type ArtifactType = "aggregate" | "policy" | "process-manager" | "command-adapter" | "projector"; /** Empty message payload */ export type Empty = Record; /** * All artifacts must have * - `description` to help with documentation */ export type WithDescription = { description: string; }; /** Schemas define message validation */ export type Schemas = { [K in keyof M]: ZodType; }; /** Schema defines state validation */ export type Schema = ZodType; /** * Aggregate schemas for * - `schemas.state` state documentation * - `schemas.commands` input validation * - `schemas.events` output validation */ export type AggregateSchemas = { state: Schema; commands: Schemas; events: Schemas; }; /** * Policy schemas for * - `schemas.commands` output documentation * - `schemas.events` input validation */ export type PolicySchemas = { commands: Schemas; events: Schemas; }; /** * ProcessManager schemas for * - `schemas.state` state documentation * - `schemas.commands` output documentation * - `schemas.events` input validation */ export type ProcessManagerSchemas = { state: Schema; commands: Schemas; events: Schemas; }; /** * CommandAdapter schemas for * - `schemas.message` input validation * - `schemas.commands` output documentation */ export type CommandAdapterSchemas = { message: ZodType; commands: Schemas; }; /** * Projector schemas for * - `schemas.state` state documentation * - `schemas.events` input validation */ export type ProjectorSchemas = { state: Schema>; events: Schemas; }; /** Streamable artifacts commit events to named streams */ export type Streamable = WithDescription & { readonly stream: string; }; /** * Reducible artifacts reduce their state from event streams * - `init` state initializer * - `reduce` event reducers * - `reducer?` state reducer * @see `utils.clone` for default state reducer behavior */ export type Reducible = WithDescription & { init: () => Readonly; reduce: { [K in keyof E]: EventReducer; }; reducer?: StateReducer; }; /** * Aggregates handle commands and produce committed events while holding an internal reducible state. * Aggregates enforce a consistency boundaries around business models. * - `schemas` for message validation and documentation * - `given?` array of invariant handlers * - `on` command handlers */ export type Aggregate = Streamable & Reducible & { schemas: AggregateSchemas; given?: { [K in keyof C]?: Array>; }; on: { [K in keyof C]: CommandHandler; }; }; /** * Policies handle events and can invoke local commands synchronously * - `schemas` for message validation and documentation * - `on` event handlers */ export type Policy = WithDescription & { schemas: PolicySchemas; on: { [K in keyof E]: (event: CommittedEvent>) => Promise | undefined> | undefined; }; }; /** * Process managers are policies with reducible state, used to expand a consistency boundary around aggregates. * Each process is an actor with reduced state from the `O`utput events emitted by the aggregates it encapsulates. * - `schemas` for message validation and documentation * - `actor` actor id resolvers from input events * - `on` event handlers */ export type ProcessManager = Reducible & { schemas: ProcessManagerSchemas; actor: { [K in keyof E]: ActorHandler; }; on: { [K in keyof E]: (event: CommittedEvent>, state: Readonly) => Promise | undefined> | undefined; }; }; /** * Command adapters map message payloads to commands. * Equivalent to policies with generic inputs instead of committed events. * - `schemas` for message validation and documentation * - `on` command adapter */ export type CommandAdapter = WithDescription & { schemas: CommandAdapterSchemas; on: (message: Readonly) => Message; }; /** * Projectors handle events and produce a map of projection patches (driven by the cardinality of the projection). * The input stream can impact the memory used to store the projected state, * specially when the cardinality of the projection is high. It's recommended to filter/batch the input in such cases. * - `schemas` for message validation and documentation * - `on` projection handlers */ export type Projector = WithDescription & { schemas: ProjectorSchemas; on: { [K in keyof E]: ProjectorReducer; }; }; /** All command handling artifacts */ export type CommandHandlingArtifact = Aggregate; /** All event handling artifacts */ export type EventHandlingArtifact = ProcessManager | Policy; /** All message handling artifacts */ export type Artifact = CommandHandlingArtifact | EventHandlingArtifact | CommandAdapter | Projector; /** Helper to infer zod types */ export type Infer = T extends ZodRawShape ? { [K in keyof T]: z.infer; } : T extends ZodTypeAny ? z.infer : never; /** Helper to infer aggregate types from schemas */ export type InferAggregate = Z extends AggregateSchemas ? Aggregate : never; /** Helper to infer policy types from schemas */ export type InferPolicy = Z extends PolicySchemas ? Policy : never; /** Helper to infer process manager types from schemas */ export type InferProcessManager = Z extends ProcessManagerSchemas ? ProcessManager : never; /** Helper to infer command adapter types from schemas */ export type InferCommandAdapter = Z extends CommandAdapterSchemas ? CommandAdapter : never; /** Helper to infer projector types from schemas */ export type InferProjector = Z extends ProjectorSchemas ? Projector : never; //# sourceMappingURL=artifacts.d.ts.map