@bluexlab/kafka-ts
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,2 +0,2 @@ | ||
import { EachMessagePayload } from 'kafkajs'; | ||
import { EachMessagePayload, KafkaMessage } from 'kafkajs'; | ||
@@ -35,2 +35,7 @@ interface MessageProcessorType { | ||
export { type MessageProcessorType, NotificationConsumer, NotificationProducer }; | ||
interface NotificationDefinition<T> { | ||
fromJSON(data: Record<string, any>): T; | ||
} | ||
declare const parseNotification: <T>(message: KafkaMessage, notificationDefinition: NotificationDefinition<T>) => T; | ||
export { type MessageProcessorType, NotificationConsumer, NotificationProducer, parseNotification }; |
@@ -12,2 +12,13 @@ // src/consumer.ts | ||
import snakeCase from "lodash/snakeCase"; | ||
var toCamelCase = (obj) => { | ||
if (isArray(obj)) { | ||
return obj.map((v) => toCamelCase(v)); | ||
} | ||
if (isPlainObject(obj)) { | ||
return transform(obj, (result, value, key) => { | ||
result[camelCase(key)] = toCamelCase(value); | ||
}); | ||
} | ||
return obj; | ||
}; | ||
@@ -18,2 +29,10 @@ // src/common.ts | ||
var consumerGroupId = process.env.NOTIFICATION_CONSUMER_GROUP_ID; | ||
var parseNotification = (message, notificationDefinition) => { | ||
if (!message.value) throw new Error(`Message is empty ${message.key}`); | ||
const rawContent = message.value.toString(); | ||
const jsonObj = JSON.parse(rawContent); | ||
const keyConverted = toCamelCase(jsonObj); | ||
const notification = notificationDefinition.fromJSON(keyConverted); | ||
return notification; | ||
}; | ||
@@ -128,3 +147,4 @@ // src/consumer.ts | ||
NotificationConsumer, | ||
NotificationProducer | ||
NotificationProducer, | ||
parseNotification | ||
}; |
{ | ||
"name": "@bluexlab/kafka-ts", | ||
"type": "module", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "The kafka producer and consumer client", | ||
@@ -6,0 +6,0 @@ "exports": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
14644
318