typebus-cqrs
Version:
Simple, type-safe CQRS library with auto-registration and minimal boilerplate (typebus-cqrs)
110 lines • 3.15 kB
TypeScript
import { IMiddleware, IMessage } from '../types';
/**
* Options for configuring the LoggingMiddleware.
* @typedef {Object} LoggingOptions
* @property {'info'|'debug'|'verbose'|'error'} [logLevel]
* @property {boolean} [includeData]
* @property {boolean} [includeMetadata]
* @property {boolean} [colorOutput]
* @property {number} [maxDataLength]
*/
export interface LoggingOptions {
logLevel?: 'info' | 'debug' | 'verbose' | 'error';
includeData?: boolean;
includeMetadata?: boolean;
colorOutput?: boolean;
maxDataLength?: number;
}
/**
* Middleware for logging message execution in TypeBus-CQRS.
* @implements {IMiddleware}
*/
export declare class LoggingMiddleware implements IMiddleware {
private options;
/**
* Creates a new LoggingMiddleware instance.
* @param {LoggingOptions} [options]
*/
constructor(options?: LoggingOptions);
/**
* Executes the middleware logic for logging.
* @template T, R
* @param {T} message - The message to process.
* @param {(message: T) => Promise<R>} next - The next middleware or handler.
* @returns {Promise<R>}
*/
execute<T extends IMessage, R>(message: T, next: (message: T) => Promise<R>): Promise<R>;
/**
* Determines if the start of message processing should be logged.
* @returns {boolean}
*/
private shouldLogStart;
/**
* Extracts extra log data from the message (data, params, metadata).
* @param {IMessage} message
* @returns {object}
*/
private getExtraLogData;
/**
* Extracts result log data if logLevel is 'debug'.
* @param {any} result
* @returns {object}
*/
private getResultLogData;
/**
* Extracts stack trace from error if logLevel is 'debug'.
* @param {any} error
* @returns {object}
*/
private getStackTrace;
/**
* Calculates the duration in milliseconds from the given start time.
* @param {bigint} startTime
* @returns {number}
*/
private getDuration;
/**
* Returns an icon based on the message type.
* @param {string} type
* @returns {string}
*/
private getMessageIcon;
/**
* Determines if the message type is a command.
* @param {string} type
* @returns {boolean}
*/
private isCommand;
/**
* Determines if the message type is a query.
* @param {string} type
* @returns {boolean}
*/
private isQuery;
/**
* Determines if the message type is an event.
* @param {string} type
* @returns {boolean}
*/
private isEvent;
/**
* Adds color to the log output if enabled.
* @param {string} text
* @param {string} color
* @returns {string}
*/
private colorize;
/**
* Sanitizes data for logging, hiding sensitive fields and limiting length.
* @param {any} data
* @returns {any}
*/
private sanitizeData;
/**
* Sanitizes result for logging, limiting length.
* @param {any} result
* @returns {any}
*/
private sanitizeResult;
}
//# sourceMappingURL=LoggingMiddleware.d.ts.map