From 45afeb7ea8b6cc82ff9ccb64afa65fb59c64744d Mon Sep 17 00:00:00 2001 From: Tyler Encke Date: Fri, 19 Aug 2022 16:19:12 -0400 Subject: [PATCH] feat(rabbitmq): add generic type to publish for simple type checking (#491) --- packages/rabbitmq/README.md | 14 ++++++++++---- packages/rabbitmq/src/amqp/connection.ts | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/rabbitmq/README.md b/packages/rabbitmq/README.md index db2d7fdc2..caaa2e676 100644 --- a/packages/rabbitmq/README.md +++ b/packages/rabbitmq/README.md @@ -1,4 +1,3 @@ - # @golevelup/nestjs-rabbitmq

@@ -228,7 +227,6 @@ interceptedRpc() { } ``` - ```typescript @RabbitRPC({ routingKey: 'intercepted-rpc-2', @@ -412,10 +410,10 @@ export class AppController { If you just want to publish a message onto a RabbitMQ exchange, use the `publish` method of the `AmqpConnection` which has the following signature: ```typescript -public publish( +public publish( exchange: string, routingKey: string, - message: any, + message: T, options?: amqplib.Options.Publish ) ``` @@ -424,6 +422,14 @@ For example: ```typescript amqpConnection.publish('some-exchange', 'routing-key', { msg: 'hello world' }); + +// optionally specify a type for generic type checking support +interface CustomModel { + foo: string; + bar: string; +} +amqpConnection.publish('some-exchange', 'routing-key', {}); +// this will now show an error that you are missing properties: foo, bar ``` ### Requesting Data from an RPC diff --git a/packages/rabbitmq/src/amqp/connection.ts b/packages/rabbitmq/src/amqp/connection.ts index 44eee8b62..f83476142 100644 --- a/packages/rabbitmq/src/amqp/connection.ts +++ b/packages/rabbitmq/src/amqp/connection.ts @@ -513,10 +513,10 @@ export class AmqpConnection { return consumerTag; } - public publish( + public publish( exchange: string, routingKey: string, - message: any, + message: T, options?: Options.Publish ) { // source amqplib channel is used directly to keep the behavior of throwing connection related errors