From 108cc127877b43bac6987ee51e5050d6211a2add Mon Sep 17 00:00:00 2001 From: mkfyi <112434983+mkfyi@users.noreply.github.com> Date: Mon, 19 Feb 2024 18:24:04 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Support=20for=20using=20a=20custom?= =?UTF-8?q?=20replacer=20for=20JSON.stringify?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/common/constants.ts | 1 + lib/common/interfaces/rabbitmq.module-options.ts | 1 + lib/core/json.service.ts | 8 ++++++-- lib/rabbitmq.module.ts | 8 ++++++++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/common/constants.ts b/lib/common/constants.ts index e7c9f6c..7cce0db 100644 --- a/lib/common/constants.ts +++ b/lib/common/constants.ts @@ -1,5 +1,6 @@ export const DEFAULT_CONNECTION_NAME = 'default'; export const JSON_SERVICE_PARSER = 'rmq.json_service.parser'; +export const JSON_SERVICE_REPLACER = 'rmq.json_service.replacer'; export const LISTENER_HANDLER_METADATA = 'rmq.meta.handler.listener'; export const PUB_SUB_ANDLER_METADATA = 'rmq.meta.handler.pub_sub'; export const ROUTING_HANDLER_METADATA = 'rmq.meta.handler.routing'; diff --git a/lib/common/interfaces/rabbitmq.module-options.ts b/lib/common/interfaces/rabbitmq.module-options.ts index 170b510..e9f1575 100644 --- a/lib/common/interfaces/rabbitmq.module-options.ts +++ b/lib/common/interfaces/rabbitmq.module-options.ts @@ -62,6 +62,7 @@ export interface BaseModuleOptions { | TopicQueueAdapterOptions )[]; parser?: ActionCallback; + replacer?: ActionCallback; } export interface RabbitMQModuleOptionsFactory diff --git a/lib/core/json.service.ts b/lib/core/json.service.ts index cd70d65..01a2eef 100644 --- a/lib/core/json.service.ts +++ b/lib/core/json.service.ts @@ -1,15 +1,19 @@ import { Inject, Injectable } from '@nestjs/common'; import { ActionCallback, Json } from '../common/interfaces/json.interface'; -import { JSON_SERVICE_PARSER } from '../common/constants'; +import { + JSON_SERVICE_PARSER, + JSON_SERVICE_REPLACER, +} from '../common/constants'; @Injectable() export class JsonService implements Json { public constructor( @Inject(JSON_SERVICE_PARSER) private readonly parser?: ActionCallback, + @Inject(JSON_SERVICE_REPLACER) private readonly replacer?: ActionCallback, ) {} public stringify(data: T, space?: number | string): string { - return JSON.stringify(data, undefined, space); + return JSON.stringify(data, this.replacer, space); } public parse(data: string): T { diff --git a/lib/rabbitmq.module.ts b/lib/rabbitmq.module.ts index 62c53d4..bfcb9e6 100644 --- a/lib/rabbitmq.module.ts +++ b/lib/rabbitmq.module.ts @@ -20,6 +20,7 @@ import { DEFAULT_CONNECTION_NAME, EXCEPTION_HANDLER_INJECTION_TOKEN, JSON_SERVICE_PARSER, + JSON_SERVICE_REPLACER, } from './common/constants'; import { buildConnectionToken } from './core/utils/build-connection-token'; import { ConnectionWrapper } from './core/wrappers/connection.wrapper'; @@ -108,6 +109,7 @@ export class RabbitMQModule implements OnApplicationBootstrap { return this.assemble( options.exceptionHandler, options.parser, + options.replacer, (Array.isArray(options.connection) ? options.connection : [{ name: DEFAULT_CONNECTION_NAME, ...options.connection }] @@ -133,6 +135,7 @@ export class RabbitMQModule implements OnApplicationBootstrap { return this.assemble( options.exceptionHandler, options.parser, + options.replacer, (Array.isArray(options.connection) ? options.connection : [{ name: DEFAULT_CONNECTION_NAME, ...options.connection }] @@ -163,6 +166,7 @@ export class RabbitMQModule implements OnApplicationBootstrap { private static assemble( exceptionHandler: Type | undefined, parser: ActionCallback | undefined, + replacer: ActionCallback | undefined, connections: (FactoryProvider | ValueProvider)[], adapters?: RabbitMQModuleOptions['adapters'], imports?: DynamicModule['imports'], @@ -178,6 +182,10 @@ export class RabbitMQModule implements OnApplicationBootstrap { provide: JSON_SERVICE_PARSER, useValue: parser, }, + { + provide: JSON_SERVICE_REPLACER, + useValue: null, + }, JsonService, exceptionHandler ? {