@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
62 lines (61 loc) • 3.35 kB
TypeScript
import type { MessageProcessingResult, MessageProcessingResultStatus } from '../types/MessageQueueTypes.ts';
/**
* Symbol that can be used in place of a message type value in `waitForMessage` or `checkForMessage`
* to match messages regardless of their type. Useful when using custom message type resolvers
* where the type may not be available in the message payload itself.
*
* @example
* // Match any message with a specific ID, regardless of type
* await spy.waitForMessage({ id: '123', type: ANY_MESSAGE_TYPE })
*/
export declare const ANY_MESSAGE_TYPE: unique symbol;
/**
* Symbol used to indicate that the message type could not be resolved.
* Typically used when message parsing failed before type resolution could occur.
*
* @example
* // For failed message parsing
* spy.addProcessedMessage({ message: null, processingResult }, messageId, TYPE_NOT_RESOLVED)
*/
export declare const TYPE_NOT_RESOLVED: unique symbol;
export type HandlerSpyParams = {
bufferSize?: number;
messageIdField?: string;
};
export type SpyResultInput<MessagePayloadSchemas extends object> = {
message: MessagePayloadSchemas | null;
processingResult: MessageProcessingResult;
};
export type SpyResultOutput<MessagePayloadSchemas extends object> = {
message: MessagePayloadSchemas;
processingResult: MessageProcessingResult;
};
export declare function isHandlerSpy<T extends object>(value: unknown): value is HandlerSpy<T>;
export type PublicHandlerSpy<MessagePayloadSchemas extends object> = Omit<HandlerSpy<MessagePayloadSchemas>, 'addProcessedMessage'>;
type DeepPartial<T> = T extends Function ? T : T extends object ? {
[P in keyof T]?: T[P] extends Array<infer U> ? Array<DeepPartial<U>> : T[P] extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> : DeepPartial<T[P]>;
} : T;
export declare class HandlerSpy<MessagePayloadSchemas extends object> {
name: string;
private readonly messageBuffer;
private readonly messageIdField;
private readonly spyPromises;
constructor(params?: HandlerSpyParams);
private messageMatchesFilter;
waitForMessageWithId<T extends MessagePayloadSchemas>(id: string, status?: MessageProcessingResultStatus): Promise<SpyResultOutput<T>>;
checkForMessage<T extends MessagePayloadSchemas>(expectedFields: DeepPartial<T>, status?: MessageProcessingResultStatus): SpyResultOutput<T> | undefined;
waitForMessage<T extends MessagePayloadSchemas>(expectedFields: DeepPartial<T>, status?: MessageProcessingResultStatus): Promise<SpyResultOutput<T>>;
clear(): void;
getAllReceivedMessages(): SpyResultOutput<MessagePayloadSchemas>[];
/**
* Add a processed message to the spy buffer.
* @param processingResult - The processing result containing the message and status
* @param messageId - Optional message ID override (used if message parsing failed)
* @param messageType - The resolved message type, or TYPE_NOT_RESOLVED symbol if type couldn't be determined
*/
addProcessedMessage(processingResult: SpyResultInput<MessagePayloadSchemas>, messageId: string | undefined, messageType: string | typeof TYPE_NOT_RESOLVED): void;
}
export declare function resolveHandlerSpy<T extends object>(queueOptions: {
handlerSpy?: HandlerSpy<object> | HandlerSpyParams | boolean;
}): HandlerSpy<T> | undefined;
export {};