@message-in-the-middle/core
Version:
Framework-agnostic middleware pattern for message queue processing. Core package with all middlewares.
44 lines • 1.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValidateOutboundMiddleware = exports.ValidateInboundMiddleware = void 0;
class ValidateInboundMiddleware {
validator;
options;
constructor(validator, options = { throwOnError: true }) {
this.validator = validator;
this.options = options;
}
async process(context, next) {
try {
const validatedMessage = await this.validator(context.message);
context.message = validatedMessage;
context.metadata.validated = true;
await next();
}
catch (error) {
context.metadata.validationError = error.message;
context.metadata.validated = false;
if (this.options.throwOnError) {
throw new Error(`Message validation failed: ${error.message}`);
}
await next();
}
}
}
exports.ValidateInboundMiddleware = ValidateInboundMiddleware;
class ValidateOutboundMiddleware {
validator;
constructor(validator) {
this.validator = validator;
}
async processOutbound(context, next) {
const isValid = await this.validator(context.message);
if (!isValid) {
throw new Error('Outbound message validation failed');
}
context.metadata.validated = true;
await next();
}
}
exports.ValidateOutboundMiddleware = ValidateOutboundMiddleware;
//# sourceMappingURL=validation.middleware.js.map