nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.89k stars 7.55k forks source link

feat(microservices): add IncomingKafkaMessage interface #4175

Closed ruscon closed 4 years ago

ruscon commented 4 years ago

Feature Request

feat(microservice): add Kafka transport IncomingMessage interface

Is your feature request related to a problem? Please describe.

In the documentation https://docs.nestjs.com/microservices/kafka IncomingMessage interface is indicated

interface IncomingMessage {
  topic: string;
  partition: number;
  timestamp: string;
  size: number;
  attributes: number;
  offset: string;
  key: any;
  value: any;
  headers: Record<string, any>;
}

This interface is missing in the @nestjs/microservice

Describe the solution you'd like

Maybe it should be added to the @nestjs/microservice Kafka interfaces?

the real incoming message example

{
  magicByte: 2,
  attributes: 0,
  timestamp: '1582833272310',
  offset: '28',
  key: null,
  value: { action: 'test', body: { order_number: 'test' } },
  headers: { 'app.name': 'Conduktor' },
  isControlRecord: false,
  batchContext: {
    firstOffset: '28',
    firstTimestamp: '1582833272310',
    partitionLeaderEpoch: 0,
    inTransaction: false,
    isControlBatch: false,
    lastOffsetDelta: 0,
    producerId: '-1',
    producerEpoch: -1,
    firstSequence: -1,
    maxTimestamp: '1582833272310',
    magicByte: 2
  },
  topic: 'notify',
  partition: 0
}

so, the interface could be

interface IncomingKafkaMessage<V = any, K = any, H = Record<string, any>> {
  magicByte: number;
  topic: string;
  partition: number;
  timestamp: string;
  size: number;
  attributes: number;
  offset: string;
  key: K;
  value: V;
  headers: H;
  isControlRecord: boolean;
  batchContext: {
    firstOffset:  string;
    firstTimestamp: string;
    partitionLeaderEpoch: number;
    inTransaction: boolean;
    isControlBatch: boolean;
    lastOffsetDelta: number;
    producerId: string;
    producerEpoch: number;
    firstSequence: number;
    maxTimestamp: string;
    magicByte: number;
 }
}
kamilmysliwiec commented 4 years ago

Would you like to create a PR?

ruscon commented 4 years ago

Yep, if you OK with the IncomingKafkaMessage interface.

kamilmysliwiec commented 4 years ago

I've spent some time on this and I decided that we shouldn't be adding interfaces unrelated to NestJS. This particular one should be added to the kafkajs library instead (or typings of this library). The IncomingMessage doesn't contain anything specifically related to NestJS.