@message-queue-toolkit/core
Version:
Useful utilities, interfaces and base classes for message queue handling. Supports AMQP and SQS with a common abstraction on top currently
63 lines (62 loc) • 2.49 kB
TypeScript
import type { Either } from '@lokalise/node-core';
import type { CommonEventDefinition } from '@message-queue-toolkit/schemas';
import type { ZodSchema } from 'zod/v4';
import { type MessageTypeResolverConfig } from './MessageTypeResolver.ts';
export type SchemaEntry<MessagePayloadSchemas extends object> = {
schema: ZodSchema<MessagePayloadSchemas>;
/**
* Explicit message type for this schema.
* Required when using a custom resolver function.
*/
messageType?: string;
};
export type DefinitionEntry = {
definition: CommonEventDefinition;
/**
* Explicit message type for this definition.
* Required when using a custom resolver function.
*/
messageType?: string;
};
export type MessageSchemaContainerOptions<MessagePayloadSchemas extends object> = {
messageDefinitions: readonly DefinitionEntry[];
messageSchemas: readonly SchemaEntry<MessagePayloadSchemas>[];
/**
* Configuration for resolving message types.
*/
messageTypeResolver?: MessageTypeResolverConfig;
};
export declare class MessageSchemaContainer<MessagePayloadSchemas extends object> {
readonly messageDefinitions: Record<string | symbol, CommonEventDefinition>;
private readonly messageSchemas;
private readonly messageTypeResolver?;
constructor(options: MessageSchemaContainerOptions<MessagePayloadSchemas>);
/**
* Resolves the schema for a message based on its type.
*
* @param message - The parsed message data
* @param attributes - Optional message-level attributes (e.g., PubSub attributes)
* @returns Either an error or the resolved schema
*/
resolveSchema(message: Record<string, any>, attributes?: Record<string, unknown>): Either<Error, ZodSchema<MessagePayloadSchemas>>;
/**
* Resolves message type from message data and optional attributes.
* Only called when messageTypeResolver is configured.
*/
private resolveMessageTypeFromData;
/**
* 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;
/**
* Validates that multiple schemas can be properly mapped at registration time.
*/
private validateMultipleSchemas;
private resolveSchemaMap;
private resolveDefinitionMap;
}