11import { PATTERN_HANDLER_METADATA , PATTERN_METADATA } from '../constants' ;
22import { PatternHandler } from '../enums/pattern-handler.enum' ;
33import { PatternMetadata } from '../interfaces/pattern-metadata.interface' ;
4+ import { ConsumerConfig } from '../external/kafka.interface' ;
45
56export enum GrpcMethodStreamingType {
67 NO_STREAMING = 'no_stream' ,
@@ -40,7 +41,7 @@ export function GrpcMethod(service: string, method?: string): MethodDecorator {
4041 key : string | symbol ,
4142 descriptor : PropertyDescriptor ,
4243 ) => {
43- const metadata = createMethodMetadata ( target , key , service , method ) ;
44+ const metadata = createGrpcMethodMetadata ( target , key , service , method ) ;
4445 return MessagePattern ( metadata ) ( target , key , descriptor ) ;
4546 } ;
4647}
@@ -62,7 +63,7 @@ export function GrpcStreamMethod(service: string, method?: string) {
6263 key : string | symbol ,
6364 descriptor : PropertyDescriptor ,
6465 ) => {
65- const metadata = createMethodMetadata (
66+ const metadata = createGrpcMethodMetadata (
6667 target ,
6768 key ,
6869 service ,
@@ -90,7 +91,7 @@ export function GrpcStreamCall(service: string, method?: string) {
9091 key : string | symbol ,
9192 descriptor : PropertyDescriptor ,
9293 ) => {
93- const metadata = createMethodMetadata (
94+ const metadata = createGrpcMethodMetadata (
9495 target ,
9596 key ,
9697 service ,
@@ -101,7 +102,7 @@ export function GrpcStreamCall(service: string, method?: string) {
101102 } ;
102103}
103104
104- export function createMethodMetadata (
105+ export function createGrpcMethodMetadata (
105106 target : any ,
106107 key : string | symbol ,
107108 service : string | undefined ,
@@ -124,3 +125,73 @@ export function createMethodMetadata(
124125 }
125126 return { service, rpc : method , streaming } ;
126127}
128+
129+ export enum KafkaConsumerHandlerType {
130+ EACH_MESSAGE = 'eachMessage' ,
131+ EACH_BATCH = 'eachBatch' ,
132+ }
133+
134+ export interface KafkaConsumerSubscriptionOptions {
135+ topic : string | RegExp ;
136+ fromBeginning ?: boolean ;
137+ }
138+
139+ export interface KafkaConsumerRunOptions {
140+ autoCommit ?: boolean ;
141+ autoCommitInterval ?: number | null ;
142+ autoCommitThreshold ?: number | null ;
143+ eachBatchAutoResolve ?: boolean ;
144+ partitionsConsumedConcurrently ?: number ;
145+ }
146+
147+ /**
148+ * Registers a consumer handler for processing each message of a topic individually.
149+ *
150+ * @param subscription The options for the subscription to the topic.
151+ * @param run The options for the consumption of messages from the topic.
152+ */
153+ export function KafkaConsumer ( subscription : KafkaConsumerSubscriptionOptions ) : MethodDecorator ;
154+ export function KafkaConsumer ( subscription : KafkaConsumerSubscriptionOptions , run ?: KafkaConsumerRunOptions ) : MethodDecorator ;
155+ export function KafkaConsumer ( subscription : KafkaConsumerSubscriptionOptions , run ?: KafkaConsumerRunOptions ) : MethodDecorator {
156+ return (
157+ target : any ,
158+ key : string | symbol ,
159+ descriptor : PropertyDescriptor ,
160+ ) => {
161+ const metadata = createKafkaMethodMetadata ( target , key , subscription , run ) ;
162+ return MessagePattern ( metadata ) ( target , key , descriptor ) ;
163+ } ;
164+ }
165+
166+ /**
167+ * Registers a consumer handler for processing each message of a topic in bitch.
168+ *
169+ * @param subscription The options for the subscription to the topic.
170+ * @param run The options for the consumption of messages from the topic.
171+ */
172+ export function KafkaBatchConsumer ( subscription : KafkaConsumerSubscriptionOptions ) : MethodDecorator ;
173+ export function KafkaBatchConsumer ( subscription : KafkaConsumerSubscriptionOptions , run ?: KafkaConsumerRunOptions ) : MethodDecorator ;
174+ export function KafkaBatchConsumer ( subscription : KafkaConsumerSubscriptionOptions , run ?: KafkaConsumerRunOptions ) : MethodDecorator {
175+ return (
176+ target : any ,
177+ key : string | symbol ,
178+ descriptor : PropertyDescriptor ,
179+ ) => {
180+ const metadata = createKafkaMethodMetadata ( target , key , subscription , run , KafkaConsumerHandlerType . EACH_BATCH ) ;
181+ return MessagePattern ( metadata ) ( target , key , descriptor ) ;
182+ } ;
183+ }
184+
185+ export function createKafkaMethodMetadata (
186+ target : any ,
187+ key : string | symbol ,
188+ subscription : KafkaConsumerSubscriptionOptions ,
189+ run : KafkaConsumerRunOptions | undefined ,
190+ handler = KafkaConsumerHandlerType . EACH_MESSAGE
191+ ) {
192+ return {
193+ subscription,
194+ run,
195+ handler
196+ } ;
197+ }
0 commit comments