/** * @module act-builder * @category Builders * * Fluent builder for composing event-sourced applications. */ import { Act, type ActOptions } from "../act.js"; import { type DEFAULT_LANE } from "../ports.js"; import type { Actor, LaneConfig, Schema, SchemaRegister, Schemas, State } from "../types/index.js"; import type { BuilderBase } from "./builder-base.js"; import type { Slice } from "./slice-builder.js"; /** * Fluent builder interface for composing event-sourced applications. * * Provides a chainable API for: * - Registering states via `.withState()` * - Registering slices via `.withSlice()` * - Registering projections via `.withProjection()` * - Locking a custom actor type via `.withActor()` * - Declaring drain lanes via `.withLane({name, ...})` (ACT-1103) * - Defining event reactions via `.on()` → `.do()` → `.to()` * - Building the orchestrator via `.build()` * * @template TSchemaReg - Schema register for states (maps action names to state schemas) * @template TEvents - Event schemas (maps event names to event data schemas) * @template TActions - Action schemas (maps action names to action payload schemas) * @template TStateMap - Map of state names to state schemas * @template TActor - Actor type extending base Actor * @template TLanes - Union of declared lane names (ACT-1103). Narrowed by * `.withLane({name})` calls so `.to({lane})` and `ActOptions.onlyLanes` * reject typos at compile time. Starts at `"default"`. * * @see {@link act} for usage examples * @see {@link Act} for the built orchestrator API */ export interface ActBuilder, TEvents extends Schemas, TActions extends Schemas, TStateMap extends Record = {}, TActor extends Actor = Actor, TLanes extends string = typeof DEFAULT_LANE> extends BuilderBase, TEvents, TActions, TActor, TLanes> { /** * Registers a state definition with the builder. * * State names, action names, and event names must be unique across the * application (partial states with the same name are merged automatically). * * @throws {Error} If duplicate action or event names are detected */ withState: (state: State) => ActBuilder; /** * Registers a slice with the builder. * * Merges all the slice's states and reactions into the application. * State names, action names, and event names must be unique across the * application (partial states with the same name are merged automatically). * * @throws {Error} If duplicate action or event names are detected */ withSlice: , TNewEvents extends Schemas, TNewActions extends Schemas, TNewMap extends Record, TNewLanes extends string>(slice: Slice) => ActBuilder; /** * Locks a custom actor type for this application. * * This is a pure type-level method — it returns the same builder at * runtime but narrows the `TActor` generic so that `app.do()` and * reaction dispatchers require the richer actor shape. * * @template TNewActor - Custom actor type extending base Actor * @returns The same builder with `TActor` locked to `TNewActor` * * @example * ```typescript * type MyActor = { id: string; name: string; role: string; tenantId: string }; * * const app = act() * .withActor() * .withState(Counter) * .build(); * * // Now app.do() requires MyActor in the target * await app.do("increment", { * stream: "counter-1", * actor: { id: "1", name: "Alice", role: "admin", tenantId: "t1" } * }, { by: 5 }); * ``` */ withActor: () => ActBuilder; /** * Declares a drain lane (ACT-1103). Lane name narrows `TLanes` so * `.to({lane})` and `ActOptions.onlyLanes` type-check against it. * * @example * ```typescript * const app = act() * .withState(Counter) * .withLane({ name: "slow", leaseMillis: 60_000, streamLimit: 5 }) * .on("OrderConfirmed") * .do(deliverWebhook) * .to({ target: "webhooks-out", lane: "slow" }) * .build(); * ``` */ withLane: (config: TConfig) => ActBuilder; /** * Builds and returns the Act orchestrator instance. * * @param options - Optional runtime overrides (see {@link ActOptions}). * `options.onlyLanes` is narrowed to the declared `TLanes` union, so * `onlyLanes: ["typo"]` is a compile error when the lane wasn't * declared via `.withLane(...)`. * @returns The Act orchestrator instance * * @see {@link Act} for available orchestrator methods */ build: (options?: ActOptions) => Act; } /** * Creates a new Act orchestrator builder for composing event-sourced applications. * * @example Basic application with single state * ```typescript * const app = act() * .withState(Counter) * .build(); * ``` * * @example Application with custom actor type * ```typescript * type MyActor = { id: string; name: string; role: string }; * * const app = act() * .withActor() * .withState(Counter) * .build(); * ``` * * @example Application with slices (vertical slice architecture) * ```typescript * const CounterSlice = slice() * .withState(Counter) * .on("Incremented") * .do(async (event) => { console.log("incremented!"); }) * .to("counter-target") * .build(); * * const app = act() * .withSlice(CounterSlice) * .build(); * ``` * * * @see {@link ActBuilder} for available builder methods * @see {@link Act} for orchestrator API methods * @see {@link state} for defining states * @see {@link slice} for defining slices */ export declare function act = {}, TEvents extends Schemas = {}, TActions extends Schemas = {}, TStateMap extends Record = {}, TActor extends Actor = Actor>(): ActBuilder; //# sourceMappingURL=act-builder.d.ts.map