Redningsselskapet / nestjs-plugins

Plugins for NestJS framework
ISC License
27 stars 22 forks source link

Allow overriding global `consumerOptions` on specific Message/Event patterns #59

Open SylvainMarty opened 6 months ago

SylvainMarty commented 6 months ago

Hey there! Thank you for this amazing plugin!

Is your feature request related to a problem? Please describe. We want to create multiple NATS Jetstream consumer callbacks (with the @EventPattern() decorator) with different replay and ack strategies. It would be super useful to be able to override the global consumerOptions on specific Message/Event patterns.

Describe the solution you'd like The decorators MessagePattern and EventPattern support the extra?: Record<string, any> property (see https://github.com/nestjs/nest/blob/master/packages/microservices/server/server.ts#L51) that can be used for this kind a needs.

IMO, a simple solution could be to use the extra property if available in the handler callback object and merge its content with the base consumerOptions in the server config (see https://github.com/Redningsselskapet/nestjs-plugins/blob/master/packages/nestjs-nats-jetstream-transport/src/server.ts#L65).

It could be something like this:

const options = !eventHandler.extra ? consumerOptions : Object.assign({}, consumerOptions, eventHandler.extra);
const subscription = await js.subscribe(subject, options);

What do you think?

Describe alternatives you've considered After looking at the plugin code, I cannot make this work without updating the plugin code.

I can submit a PR if you like, just tell me if you have any recommandation about the solution I gave.

fredriklaason commented 6 months ago

The gRPC transport exposes it's own decorator @GrpcMethod() doing the same here could help with types etc, the @GrpcMethod() is in the end just a wrapper for @EventHandler().