import type { Either } from '@lokalise/node-core'; import type { CommonEventDefinition } from '@message-queue-toolkit/schemas'; import type { ZodSchema } from 'zod/v4'; import type { DoNotProcessMessageError } from '../errors/DoNotProcessError.ts'; import type { RetryMessageLaterError } from '../errors/RetryMessageLaterError.ts'; import { type MessageTypeResolverConfig } from './MessageTypeResolver.ts'; export type PreHandlingOutputs = { preHandlerOutput: PrehandlerOutput; barrierOutput: BarrierOutput; }; export type LogFormatter = (message: MessagePayloadSchema) => unknown; export type BarrierResult = BarrierResultPositive | BarrierResultNegative; export type BarrierResultPositive = { isPassing: true; output: BarrierOutput; }; export type BarrierResultNegative = { isPassing: false; output?: never; }; export type PrehandlerResult = Either; export type BarrierCallback = (message: MessagePayloadSchema, context: ExecutionContext, preHandlerOutput: PrehandlerOutput) => Promise>; export type Prehandler = (message: MessagePayloadSchema, context: ExecutionContext, preHandlerOutput: Partial, next: (result: PrehandlerResult) => void) => void; export type HandlerConfigOptions = { /** * Explicit message type for this handler. * Required when using a custom resolver function that cannot extract types from schemas. * Optional when using messageTypePath or literal resolver modes. */ messageType?: string; messageLogFormatter?: LogFormatter; preHandlerBarrier?: BarrierCallback; preHandlers?: Prehandler[]; }; export declare class MessageHandlerConfig { readonly schema: ZodSchema; readonly definition?: CommonEventDefinition; /** * Explicit message type for this handler, if provided. * Used for routing when type cannot be extracted from schema. */ readonly messageType?: string; readonly handler: Handler; readonly messageLogFormatter?: LogFormatter; readonly preHandlerBarrier?: BarrierCallback; readonly preHandlers: Prehandler[]; constructor(schema: ZodSchema, handler: Handler, options?: HandlerConfigOptions, eventDefinition?: CommonEventDefinition); } export declare class MessageHandlerConfigBuilder { private readonly configs; constructor(); /** * Add a handler configuration for a specific message type. * The schema is used for both routing (to match the message type) and validation (for the handler). * * The message type field (e.g., 'type' or 'detail-type') must be at the root level of the message * and must be a literal value in the schema for routing to work. * * Example: * ```typescript * const USER_CREATED_SCHEMA = z.object({ * type: z.literal('user.created'), * userId: z.string(), * email: z.string() * }) * * builder.addConfig(USER_CREATED_SCHEMA, async (message) => { * // message has type 'user.created', userId, and email * }) * ``` * * EventBridge example: * ```typescript * const USER_PRESENCE_SCHEMA = z.object({ * 'detail-type': z.literal('v2.users.{id}.presence'), * time: z.string(), * detail: z.object({ * userId: z.string(), * presenceStatus: z.string() * }) * }) * * builder.addConfig(USER_PRESENCE_SCHEMA, async (message) => { * // message is the full EventBridge envelope * const detail = message.detail // Access nested payload directly * }) * ``` */ addConfig(schema: ZodSchema | CommonEventDefinition, handler: Handler, options?: HandlerConfigOptions): this; build(): MessageHandlerConfig[]; } export type Handler = (message: MessagePayloadSchemas, context: ExecutionContext, preHandlingOutputs: PreHandlingOutputs, definition?: CommonEventDefinition) => Promise>; export type HandlerContainerOptions = { messageHandlers: MessageHandlerConfig[]; /** * Configuration for resolving message types. */ messageTypeResolver?: MessageTypeResolverConfig; }; export declare class HandlerContainer { private readonly messageHandlers; private readonly messageTypeResolver?; constructor(options: HandlerContainerOptions); /** * Resolves a handler for the given message type. */ resolveHandler(messageType: string): MessageHandlerConfig; /** * Resolves message type from message data and optional attributes using the configured resolver. * * @param messageData - The parsed message data * @param messageAttributes - Optional message-level attributes (e.g., PubSub attributes) * @returns The resolved message type * @throws Error if message type cannot be resolved */ resolveMessageType(messageData: unknown, messageAttributes?: Record): string; /** * Gets the field path used for extracting message type from schemas during registration. * Returns undefined for literal or custom resolver modes. */ private getMessageTypePathForSchema; /** * Gets the literal message type if configured. */ private getLiteralMessageType; private resolveHandlerMap; }