Skip to content

Commit

Permalink
✨ Support for using a custom replacer for JSON.stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
mkfyi committed Feb 19, 2024
1 parent 0a4366e commit 108cc12
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/common/constants.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
1 change: 1 addition & 0 deletions lib/common/interfaces/rabbitmq.module-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export interface BaseModuleOptions<T> {
| TopicQueueAdapterOptions
)[];
parser?: ActionCallback;
replacer?: ActionCallback;
}

export interface RabbitMQModuleOptionsFactory
Expand Down
8 changes: 6 additions & 2 deletions lib/core/json.service.ts
Original file line number Diff line number Diff line change
@@ -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<T>(data: T, space?: number | string): string {
return JSON.stringify(data, undefined, space);
return JSON.stringify(data, this.replacer, space);
}

public parse<T = unknown>(data: string): T {
Expand Down
8 changes: 8 additions & 0 deletions lib/rabbitmq.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 }]
Expand All @@ -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 }]
Expand Down Expand Up @@ -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'],
Expand All @@ -178,6 +182,10 @@ export class RabbitMQModule implements OnApplicationBootstrap {
provide: JSON_SERVICE_PARSER,
useValue: parser,
},
{
provide: JSON_SERVICE_REPLACER,
useValue: null,
},
JsonService,
exceptionHandler
? {
Expand Down

0 comments on commit 108cc12

Please sign in to comment.