import type { PathwaysBuilder, UserIdResolver } from "./builder.js"; import type { EventMetadata, PathwayWriteOptions } from "./types.js"; /** * SessionPathwayBuilder wraps a PathwaysBuilder instance and automatically * associates a session ID with all pathway writes. * * This provides a convenient way to track operations within a user session * by automatically including the session ID in metadata. * * Key features: * - Automatic session ID generation if none is provided * - Cross-platform UUID generation (works in Deno, Bun, and Node.js) * - Simple API for accessing the current session ID * - Convenient integration with session-specific user resolvers * - Automatic inclusion of session ID in all write operations * - Support for overriding the session ID on specific writes * * Use cases: * - Tracking user actions across multiple pathway writes * - Connecting related events in a single user session * - Supporting multi-user environments where different users' operations need to be tracked separately * - Building user activity logs with session grouping * * @example * ```typescript * // Create a session pathway with auto-generated ID * const session = new SessionPathwayBuilder(pathwaysBuilder); * * // Get the auto-generated session ID * const sessionId = session.getSessionId(); * * // Register a user resolver for this session * session.withUserResolver(async () => getCurrentUserId()); * * // Write events with session context * await session.write("order/placed", { data: orderData }); * await session.write("user/action", { data: actionData }); * * // Write batch events with session context * await session.write("user/actions", { * batch: true, * data: [actionData1, actionData2] * }); * * // All events will be associated with the same session ID * ``` */ export declare class SessionPathwayBuilder = {}, TWritablePaths extends keyof TPathway = never> { private readonly pathwaysBuilder; private readonly sessionId; /** * Creates a new SessionPathwayBuilder * * @param pathwaysBuilder The configured PathwaysBuilder instance to wrap * @param sessionId Optional session ID to associate with all operations. If not provided, one will be generated automatically. */ constructor(pathwaysBuilder: PathwaysBuilder, sessionId?: string); /** * Gets the current session ID * * @returns The session ID associated with this instance */ getSessionId(): string; /** * Registers a user resolver for this session * * This is a convenience method that calls `pathwaysBuilder.withSessionUserResolver` * with the current session ID, allowing you to set up a resolver specific to this session * without having to manually pass the session ID. * * The resolver will be called whenever events are written through this session, * and the resolved user ID will be included in the event metadata. * * @param resolver The function that resolves to the user ID for this session * @returns The SessionPathwayBuilder instance for chaining * * @throws Error if the underlying PathwaysBuilder does not have session user resolvers configured * * @example * ```typescript * const session = new SessionPathwayBuilder(pathwaysBuilder); * * // Register a user resolver for this session * session.withUserResolver(async () => { * // Get the user ID associated with this session * return getUserIdFromSession(); * }); * * // When writing events, the user ID will be automatically included * await session.write("user/action", { data: actionData }); * ``` */ withUserResolver(resolver: UserIdResolver): this; /** * Writes data to a pathway, proxying to the underlying PathwaysBuilder * * @param path The pathway to write to * @param input Object containing the data, metadata, options, and optional batch flag * @returns A promise that resolves to the event ID(s) */ write(path: TPath, input: { batch?: B; data: B extends true ? TPathway[TPath]["input"][] : TPathway[TPath]["input"]; metadata?: EventMetadata; options?: PathwayWriteOptions; }): Promise; } //# sourceMappingURL=session-pathway.d.ts.map