UNPKG

@joktec/kafka

Version:
34 lines 1.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KafkaSend = KafkaSend; const core_1 = require("@joktec/core"); const utils_1 = require("@joktec/utils"); const kafka_service_1 = require("../kafka.service"); function KafkaSend(topic, options = { useEnv: false }, conId = core_1.DEFAULT_CON_ID) { const kafkaOptions = options || {}; return (0, core_1.BaseMethodDecorator)(async (options) => { const { method, args, services } = options; const kafkaService = services.kafkaService; try { const result = await method(...args); if (!result || (Array.isArray(result) && !result.length)) return result; let topicName = topic; if (kafkaOptions?.useEnv) { topicName = services.configService.resolveConfigValue(topic, false); if (!topicName) { services.pinoLogger.warn("`%s` Can't resolve topic name from config: %s", conId, topic); topicName = topic; } } const messages = (0, utils_1.toArray)(result).map(value => JSON.stringify(value)); await kafkaService.send(topicName, messages, kafkaOptions, conId); return result; } catch (error) { services.pinoLogger.error(error, '`%s` [KafkaPublish] Failed to publish message', conId); throw error; } }, [kafka_service_1.KafkaService]); } //# sourceMappingURL=kafka.producer.js.map