Skip to content

Commit

Permalink
feat(rabbitmq): add generic type to publish for simple type checking (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
iam10k authored Aug 19, 2022
1 parent 241a640 commit 45afeb7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
14 changes: 10 additions & 4 deletions packages/rabbitmq/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# @golevelup/nestjs-rabbitmq

<p align="center">
Expand Down Expand Up @@ -228,7 +227,6 @@ interceptedRpc() {
}
```


```typescript
@RabbitRPC({
routingKey: 'intercepted-rpc-2',
Expand Down Expand Up @@ -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<T = any>(
exchange: string,
routingKey: string,
message: any,
message: T,
options?: amqplib.Options.Publish
)
```
Expand All @@ -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<CustomModel>('some-exchange', 'routing-key', {});
// this will now show an error that you are missing properties: foo, bar
```

### Requesting Data from an RPC
Expand Down
4 changes: 2 additions & 2 deletions packages/rabbitmq/src/amqp/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,10 +513,10 @@ export class AmqpConnection {
return consumerTag;
}

public publish(
public publish<T = any>(
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
Expand Down

0 comments on commit 45afeb7

Please sign in to comment.