Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rabbitmq): add generic type to publish for simple type checking #491

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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