UNPKG

@message-in-the-middle/core

Version:

Framework-agnostic middleware pattern for message queue processing. Core package with all middlewares.

53 lines 1.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StringifyJsonOutboundMiddleware = exports.ParseJsonInboundMiddleware = void 0; class ParseJsonInboundMiddleware { options; constructor(options = {}) { this.options = options; } async process(context, next) { if (typeof context.message === 'string') { try { const parsed = JSON.parse(context.message, this.options.reviver); context.message = parsed; context.metadata.jsonParsed = true; } catch (error) { context.metadata.jsonParseError = error.message; if (this.options.throwOnError !== false) { throw new Error(`Failed to parse JSON message: ${error.message}`); } } } else { context.metadata.jsonParsed = false; } await next(); } } exports.ParseJsonInboundMiddleware = ParseJsonInboundMiddleware; class StringifyJsonOutboundMiddleware { options; constructor(options = {}) { this.options = options; } async processOutbound(context, next) { if (typeof context.message === 'object' && context.message !== null) { try { const stringified = JSON.stringify(context.message, this.options.replacer, this.options.space); context.message = stringified; context.metadata.jsonStringified = true; } catch (error) { throw new Error(`Failed to stringify JSON message: ${error.message}`); } } else { context.metadata.jsonStringified = false; } await next(); } } exports.StringifyJsonOutboundMiddleware = StringifyJsonOutboundMiddleware; //# sourceMappingURL=json.middleware.js.map