@ccci/micro-server 1.0.151 → 1.0.153

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,30 +1,32 @@
1
1
  export type { RequestQuery, QueryOptions, ResponseType, IncludeType, CommonType } from './types/BaseControllerTypes';
2
- export type { AttachmentMetadataType } from './types/AttachmentMetadataTypes';
3
- export type { ApplicationOptionType } from './types/ApplicationOptionType';
4
- export { default as ApplicationServer } from './utils/ApplicationServer';
5
- export { default as RouterFactory } from './utils/RouterFactory';
6
- export { default as BaseRouter } from './utils/BaseRouter';
7
- export { default as BaseController } from './utils/BaseController';
8
- export { ConstructQuery, ResponseHelper, ErrorResponseHandler, } from './utils/BaseControllerHelper';
9
- export { default as IBaseModel } from './utils/IBaseModel';
10
- export { default as BaseModel } from './utils/BaseModel';
11
- export { default as ErrorHandler } from './utils/ErrorHandler';
12
- export { generate, verify, decode } from './utils/TokenGenerator';
13
- export { default as DatabaseConnector } from './utils/DatabaseConnector';
2
+ export type { AttachmentMetadataType } from '@/types/AttachmentMetadataTypes';
3
+ export type { ApplicationOptionType } from '@/types/ApplicationOptionType';
4
+ export { default as ApplicationServer } from '@/utils/ApplicationServer';
5
+ export { default as RouterFactory } from '@/utils/RouterFactory';
6
+ export { default as BaseRouter } from '@/utils/BaseRouter';
7
+ export { default as BaseController } from '@/utils/BaseController';
8
+ export { ConstructQuery, ResponseHelper, ErrorResponseHandler, } from '@/utils/BaseControllerHelper';
9
+ export { default as IBaseModel } from '@/utils/IBaseModel';
10
+ export { default as BaseModel } from '@/utils/BaseModel';
11
+ export { default as ErrorHandler } from '@/utils/ErrorHandler';
12
+ export { generate, verify, decode } from '@/utils/TokenGenerator';
13
+ export { default as DatabaseConnector } from '@/utils/DatabaseConnector';
14
14
  export { default as Logger } from '@/utils/Logger';
15
15
  export { default as WebSocketServer } from '@/utils/WebsocketServer';
16
- export { default as BaseSocketHandler } from './utils/BaseSocketHandler';
17
- export type { WebSocketData } from './types/BaseSocketType';
18
- export type { UploadedFileType } from './types/UploadedFileType';
19
- export { default as Uploader } from './utils/Uploader';
20
- export { default as Mailer } from './utils/Mailer';
21
- export { default as UploaderS3 } from './utils/uploader-s3';
16
+ export { default as BaseSocketHandler } from '@/utils/BaseSocketHandler';
17
+ export type { WebSocketData } from '@/types/BaseSocketType';
18
+ export type { UploadedFileType } from '@/types/UploadedFileType';
19
+ export { default as Uploader } from '@/utils/Uploader';
20
+ export { default as Mailer } from '@/utils/Mailer';
21
+ export { default as UploaderS3 } from '@/utils/uploader-s3';
22
22
  export { default as Sequelize } from 'sequelize';
23
- export * from './utils/Mixins';
24
- export type { NotificationType, NotificationOptions } from './types/NotificationTypes';
25
- export { default as PushNotifier } from './utils/PushNotifier';
26
- export { PublicAccess, PrivateAccess, endpoint } from './decorators/Endpoints';
27
- export { BaseConsumer } from './utils/BaseConsumer';
28
- export { BaseProducer } from './utils/BaseProducer';
29
- export { KafkaServer } from './utils/KafkaServer';
23
+ export * from '@/utils/Mixins';
24
+ export type { NotificationType, NotificationOptions } from '@/types/NotificationTypes';
25
+ export { default as PushNotifier } from '@/utils/PushNotifier';
26
+ export { PublicAccess, PrivateAccess, endpoint } from '@/decorators/Endpoints';
27
+ export { BaseConsumer } from '@/utils/BaseConsumer';
28
+ export { BaseProducer } from '@/utils/BaseProducer';
29
+ export { BaseKafka } from '@/utils/BaseKafka';
30
+ export { ServeKafka } from '@/utils/ServeKafka';
31
+ export type { MessageHandler, MessageType, PublishType } from '@/types/BrokerType';
30
32
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -298227,70 +298227,64 @@ class UploaderS3 {
298227
298227
  }
298228
298228
  }
298229
298229
 
298230
- // src/utils/KafkaServer.ts
298230
+ // src/utils/ServeKafka.ts
298231
298231
  import fs7 from "fs";
298232
298232
  import path4 from "path";
298233
298233
 
298234
- class KafkaServer {
298235
- consumersPath;
298236
- producersPath;
298237
- consumers = [];
298238
- producers = [];
298239
- kafkaBroker;
298240
- constructor(config) {
298241
- this.consumersPath = config.consumersPath ?? "consumers";
298242
- this.producersPath = config.producersPath ?? "producers";
298243
- this.kafkaBroker = config.kafkaBroker ?? [];
298234
+ class ServeKafka {
298235
+ consumerPath;
298236
+ producerPath;
298237
+ config;
298238
+ constructor(config, consumerPath = "kafka/consumers", producerPath = "kafka/producers") {
298239
+ this.config = config;
298240
+ this.consumerPath = consumerPath;
298241
+ this.producerPath = producerPath;
298244
298242
  }
298245
298243
  async startConsumers() {
298246
- const consumerFiles = fs7.readdirSync(this.consumersPath).filter((file) => file.endsWith(".consumer.ts"));
298247
- for (const file of consumerFiles) {
298248
- const filePath = path4.resolve(this.consumersPath, file);
298249
- console.log("filePath :>> ", filePath);
298250
- const baseName = file.replace(".consumer.ts", "");
298251
- const clientId = `${baseName}-client`;
298252
- const groupId = `${baseName}-group`;
298253
- const ConsumerClass = (await import(filePath)).default;
298254
- if (!ConsumerClass) {
298255
- console.warn(`File ${file} does not export a default class.`);
298256
- continue;
298257
- }
298258
- const consumer = new ConsumerClass(clientId, this.kafkaBroker, groupId);
298259
- if (typeof consumer.connect === "function") {
298260
- await consumer.connect();
298261
- await consumer.handleMessages();
298262
- console.log(`${file} connected with clientId: ${clientId}, groupId: ${groupId}`);
298263
- } else {
298264
- console.warn(`${file} does not have a connect method.`);
298244
+ try {
298245
+ const consumerFiles = fs7.readdirSync(this.consumerPath).filter((file) => file.endsWith(".consumer.ts"));
298246
+ for (const file of consumerFiles) {
298247
+ const filePath = path4.resolve(this.consumerPath, file);
298248
+ const baseName = file.replace(".consumer.ts", "");
298249
+ const groupId = `${baseName}-group`;
298250
+ try {
298251
+ const ConsumerClass = (await import(filePath)).default;
298252
+ if (!ConsumerClass || typeof ConsumerClass !== "function") {
298253
+ throw new Error(`File ${file} does not export a valid class.`);
298254
+ }
298255
+ new ConsumerClass(this.config);
298256
+ await ConsumerClass._.connect(groupId);
298257
+ ConsumerClass.init();
298258
+ console.log(`Started consumer: ${baseName}, groupId: ${groupId}`);
298259
+ } catch (error) {
298260
+ console.error(`Failed to start consumer from ${file}:`, error);
298261
+ }
298265
298262
  }
298263
+ } catch (error) {
298264
+ console.error("Error reading consumer files:", error);
298266
298265
  }
298267
298266
  }
298268
298267
  async startProducers() {
298269
- const producerFiles = fs7.readdirSync(this.producersPath).filter((file) => file.endsWith(".producer.ts"));
298270
- for (const file of producerFiles) {
298271
- const filePath = path4.resolve(this.producersPath, file);
298272
- console.log("filePath :>> ", filePath);
298273
- const baseName = file.replace(".producer.ts", "");
298274
- const clientId = `${baseName}-client`;
298275
- const ProducerClass = (await import(filePath)).default;
298276
- if (!ProducerClass) {
298277
- console.warn(`File ${file} does not export a default class.`);
298278
- continue;
298279
- }
298280
- const producer = new ProducerClass(clientId, this.kafkaBroker);
298281
- if (typeof producer.connect === "function") {
298282
- console.log(`${file} connected with clientId: ${clientId}`);
298283
- } else {
298284
- console.warn(`${file} does not have a connect method.`);
298268
+ try {
298269
+ const producerFiles = fs7.readdirSync(this.producerPath).filter((file) => file.endsWith(".producer.ts"));
298270
+ for (const file of producerFiles) {
298271
+ const filePath = path4.resolve(this.producerPath, file);
298272
+ const baseName = file.replace(".producer.ts", "");
298273
+ const clientId = this.config.clientId;
298274
+ try {
298275
+ const ProducerClass = (await import(filePath)).default;
298276
+ if (!ProducerClass) {
298277
+ throw new Error(`File ${file} does not export a valid class.`);
298278
+ }
298279
+ new ProducerClass(this.config);
298280
+ await ProducerClass._.connect(clientId);
298281
+ console.log(`Started producer: ${baseName}, clientId: ${clientId}`);
298282
+ } catch (error) {
298283
+ console.error(`Failed to start producer from ${file}:`, error);
298284
+ }
298285
298285
  }
298286
- }
298287
- }
298288
- async disconnectAll() {
298289
- for (const producer of this.producers) {
298290
- await producer.disconnect();
298291
- }
298292
- for (const consumer of this.consumers) {
298293
- await consumer.disconnect();
298286
+ } catch (error) {
298287
+ console.error("Error reading producer files:", error);
298294
298288
  }
298295
298289
  }
298296
298290
  }
@@ -298322,9 +298316,10 @@ class ApplicationServer {
298322
298316
  await Uploader.init(options);
298323
298317
  }
298324
298318
  if (options?.enableKafka) {
298325
- const kafkaServer = new KafkaServer(options);
298326
- await kafkaServer.startConsumers();
298327
- await kafkaServer.startProducers();
298319
+ const config = { clientId: options.clientId, brokers: options.brokers ?? [] };
298320
+ const serveKafka = new ServeKafka(config);
298321
+ await serveKafka.startConsumers();
298322
+ await serveKafka.startProducers();
298328
298323
  }
298329
298324
  if (options?.enableS3) {
298330
298325
  await UploaderS3.init(options);
@@ -298980,82 +298975,123 @@ var PrivateAccess = (constructor) => {
298980
298975
  };
298981
298976
  };
298982
298977
  // src/utils/BaseConsumer.ts
298983
- var import_kafkajs = __toESM(require_kafkajs(), 1);
298984
-
298985
298978
  class BaseConsumer {
298979
+ consumer = null;
298986
298980
  kafka;
298987
- consumer;
298988
- groupId;
298989
- isRunning = false;
298990
- constructor(clientId, brokers, groupId) {
298991
- this.kafka = new import_kafkajs.Kafka({ clientId, brokers });
298992
- this.consumer = this.kafka.consumer({ groupId });
298993
- this.groupId = groupId;
298994
- }
298995
- async connect() {
298981
+ constructor(baseKafka) {
298982
+ this.kafka = baseKafka.getKafka();
298983
+ }
298984
+ async connect(groupId, config) {
298985
+ if (this.consumer) {
298986
+ console.log("Consumer already connected");
298987
+ return;
298988
+ }
298989
+ this.consumer = this.kafka.consumer({
298990
+ groupId,
298991
+ ...config
298992
+ });
298996
298993
  await this.consumer.connect();
298997
- console.log(`Consumer connected to group: ${this.groupId}`);
298994
+ console.log("Consumer connected");
298998
298995
  }
298999
- async subscribe(topic, fromBeginning = true) {
299000
- await this.consumer.subscribe({ topic, fromBeginning });
299001
- console.log(`Subscribed to topic: ${topic}`);
298996
+ async disconnect() {
298997
+ if (this.consumer) {
298998
+ await this.consumer.disconnect();
298999
+ this.consumer = null;
299000
+ }
299002
299001
  }
299003
- async run(eachMessageHandler) {
299004
- this.isRunning = true;
299002
+ async subscribe(topic, messageHandler) {
299003
+ if (!this.consumer) {
299004
+ throw new Error("Consumer not connected");
299005
+ }
299006
+ await this.consumer.subscribe({ topic, fromBeginning: true });
299005
299007
  await this.consumer.run({
299006
- eachMessage: async (payload) => {
299007
- if (!this.isRunning) {
299008
- return;
299009
- }
299010
- try {
299011
- await eachMessageHandler(payload);
299012
- } catch (error) {
299013
- console.error("Error in message handler:", error);
299014
- }
299008
+ eachMessage: async ({ topic: topic2, partition, message }) => {
299009
+ const parsedMessage = {
299010
+ headers: message.headers,
299011
+ event: message.key?.toString() || "",
299012
+ data: message.value ? JSON.parse(message.value.toString()) : null
299013
+ };
299014
+ await messageHandler(parsedMessage);
299015
+ await this.consumer?.commitOffsets([
299016
+ { topic: topic2, partition, offset: (Number(message.offset) + 1).toString() }
299017
+ ]);
299015
299018
  }
299016
299019
  });
299017
- console.log(`Consumer is running.`);
299018
- }
299019
- async disconnect() {
299020
- this.isRunning = false;
299021
- await this.consumer.disconnect();
299022
- await this.consumer.stop();
299023
- console.log("Consumer disconnected.");
299024
299020
  }
299025
299021
  }
299026
299022
  // src/utils/BaseProducer.ts
299027
- var import_kafkajs2 = __toESM(require_kafkajs(), 1);
299023
+ var import_kafkajs = __toESM(require_kafkajs(), 1);
299028
299024
 
299029
299025
  class BaseProducer {
299030
- static instance = null;
299026
+ producer = null;
299031
299027
  kafka;
299032
- producer;
299033
- clientId;
299034
- constructor(clientId, brokers) {
299035
- this.kafka = new import_kafkajs2.Kafka({ clientId, brokers });
299036
- this.producer = this.kafka.producer();
299037
- this.clientId = clientId;
299028
+ constructor(baseKafka) {
299029
+ this.kafka = baseKafka.getKafka();
299038
299030
  }
299039
- static getInstance(clientId, brokers) {
299040
- if (!BaseProducer.instance) {
299041
- BaseProducer.instance = new BaseProducer(clientId, brokers);
299031
+ async connect(config) {
299032
+ if (this.producer) {
299033
+ console.log("Producer already connected");
299034
+ return;
299042
299035
  }
299043
- return BaseProducer.instance;
299044
- }
299045
- async connect() {
299036
+ this.producer = this.kafka.producer({
299037
+ createPartitioner: import_kafkajs.Partitioners.DefaultPartitioner,
299038
+ ...config
299039
+ });
299046
299040
  await this.producer.connect();
299047
- console.log(`Producer connected with clientId: ${this.clientId}`);
299041
+ console.log("Producer connected");
299048
299042
  }
299049
- async produceMessage(topic, message) {
299050
- await this.producer.send({
299051
- topic,
299052
- messages: [{ value: message }]
299043
+ async disconnect() {
299044
+ if (this.producer) {
299045
+ await this.producer.disconnect();
299046
+ this.producer = null;
299047
+ }
299048
+ }
299049
+ async publish(data) {
299050
+ if (!this.producer) {
299051
+ throw new Error("Producer not connected");
299052
+ }
299053
+ const result = await this.producer.send({
299054
+ topic: data.topic,
299055
+ messages: [
299056
+ {
299057
+ headers: data.headers,
299058
+ key: data.event,
299059
+ value: JSON.stringify(data.message)
299060
+ }
299061
+ ]
299053
299062
  });
299054
- console.log(`Message sent to topic: ${topic}`);
299063
+ console.log("Publishing result:", result);
299064
+ return result.length > 0;
299055
299065
  }
299056
- async disconnect() {
299057
- await this.producer.disconnect();
299058
- console.log("Producer disconnected.");
299066
+ }
299067
+ // src/utils/BaseKafka.ts
299068
+ var import_kafkajs2 = __toESM(require_kafkajs(), 1);
299069
+
299070
+ class BaseKafka {
299071
+ kafka;
299072
+ consumerPath = "kafka/consumers";
299073
+ producerPath = "kafka/producerPath";
299074
+ constructor(config) {
299075
+ this.kafka = new import_kafkajs2.Kafka({
299076
+ ...config,
299077
+ logLevel: config.logLevel || import_kafkajs2.logLevel.INFO
299078
+ });
299079
+ }
299080
+ getKafka() {
299081
+ return this.kafka;
299082
+ }
299083
+ async createTopics(topics) {
299084
+ const admin2 = this.kafka.admin();
299085
+ await admin2.connect();
299086
+ const existingTopics = await admin2.listTopics();
299087
+ for (const { topic, partitions, replicationFactor } of topics) {
299088
+ if (!existingTopics.includes(topic)) {
299089
+ await admin2.createTopics({
299090
+ topics: [{ topic, numPartitions: partitions, replicationFactor }]
299091
+ });
299092
+ }
299093
+ }
299094
+ await admin2.disconnect();
299059
299095
  }
299060
299096
  }
299061
299097
  export {
@@ -299070,6 +299106,7 @@ export {
299070
299106
  WebSocketServer,
299071
299107
  UploaderS3,
299072
299108
  Uploader,
299109
+ ServeKafka,
299073
299110
  lib_default as Sequelize,
299074
299111
  RouterFactory,
299075
299112
  ResponseHelper,
@@ -299078,7 +299115,6 @@ export {
299078
299115
  PrivateAccess,
299079
299116
  Mailer,
299080
299117
  Logger,
299081
- KafkaServer,
299082
299118
  ErrorResponseHandler,
299083
299119
  ErrorHandler,
299084
299120
  DatabaseConnector,
@@ -299087,6 +299123,7 @@ export {
299087
299123
  BaseRouter,
299088
299124
  BaseProducer,
299089
299125
  BaseModel,
299126
+ BaseKafka,
299090
299127
  BaseController,
299091
299128
  BaseConsumer,
299092
299129
  ApplicationServer
@@ -13,7 +13,8 @@ export type ApplicationOptionType = {
13
13
  enableWebSocket?: boolean;
14
14
  enableFirebase?: boolean;
15
15
  enableEmail?: boolean;
16
- kafkaBroker?: string[];
16
+ clientId?: string;
17
+ brokers?: string[];
17
18
  consumersPath?: string;
18
19
  producersPath?: string;
19
20
  minioHost?: string;
@@ -0,0 +1,13 @@
1
+ export type PublishType = {
2
+ topic: string;
3
+ headers?: Record<string, string>;
4
+ event: string;
5
+ message: Record<string, any>;
6
+ };
7
+ export type MessageType = {
8
+ headers?: Record<string, string>;
9
+ event: string;
10
+ data: any;
11
+ };
12
+ export type MessageHandler = (message: MessageType) => Promise<void>;
13
+ //# sourceMappingURL=BrokerType.d.ts.map
@@ -1,42 +1,44 @@
1
- import { type EachMessagePayload } from 'kafkajs';
1
+ import { type ConsumerConfig } from "kafkajs";
2
+ import { BaseKafka } from "./BaseKafka";
3
+ import type { MessageHandler } from "../types/BrokerType";
2
4
  /**
3
- * BaseConsumer class to handle Kafka consumer operations such as connecting,
4
- * subscribing to topics, consuming messages, and disconnecting.
5
+ * BaseConsumer class provides basic functionality for consuming messages from a Kafka topic.
6
+ * It manages the connection, subscription, and message handling logic for Kafka consumers.
5
7
  */
6
8
  export declare class BaseConsumer {
7
- private kafka;
8
9
  private consumer;
9
- private groupId;
10
- private isRunning;
11
- /**
12
- * @param clientId - The client ID used to identify the consumer instance.
13
- * @param brokers - An array of Kafka brokers to connect to.
14
- * @param groupId - The consumer group ID to which this consumer belongs.
15
- */
16
- constructor(clientId: string, brokers: string[], groupId: string);
17
- /**
18
- * Connects the consumer to the Kafka cluster.
19
- * This method establishes the connection between the consumer and the Kafka brokers.
20
- */
21
- connect(): Promise<void>;
10
+ private kafka;
22
11
  /**
23
- * Subscribes the consumer to a Kafka topic.
12
+ * Creates an instance of BaseConsumer using a BaseKafka instance.
24
13
  *
25
- * @param topic - The Kafka topic to subscribe to.
26
- * @param fromBeginning - Whether to start reading from the beginning of the topic (default: true).
14
+ * @param {BaseKafka} baseKafka - The instance of the BaseKafka class which provides access to the Kafka client.
27
15
  */
28
- subscribe(topic: string, fromBeginning?: boolean): Promise<void>;
16
+ constructor(baseKafka: BaseKafka);
29
17
  /**
30
- * Starts consuming messages from the Kafka topic.
31
- * The provided `eachMessageHandler` will be executed for each consumed message.
18
+ * Connects the consumer to the Kafka cluster with the specified group ID and optional consumer configuration.
19
+ * If the consumer is already connected, it logs a message and does nothing.
32
20
  *
33
- * @param eachMessageHandler - A callback function that processes each consumed message.
21
+ * @param {string} groupId - The consumer group ID to join.
22
+ * @param {ConsumerConfig} [config] - Optional additional configuration for the consumer.
23
+ * @returns {Promise<void>} - A promise that resolves once the consumer is connected.
34
24
  */
35
- run(eachMessageHandler: (payload: EachMessagePayload) => Promise<void>): Promise<void>;
25
+ connect(groupId: string, config?: ConsumerConfig): Promise<void>;
36
26
  /**
37
27
  * Disconnects the consumer from the Kafka cluster.
38
- * This method gracefully shuts down the consumer connection.
28
+ * If the consumer is already disconnected, it logs a message and does nothing.
29
+ *
30
+ * @returns {Promise<void>} - A promise that resolves once the consumer is disconnected.
39
31
  */
40
32
  disconnect(): Promise<void>;
33
+ /**
34
+ * Subscribes the consumer to a Kafka topic and starts processing messages from the topic.
35
+ * The consumer runs the provided message handler for each received message.
36
+ *
37
+ * @param {string} topic - The Kafka topic to subscribe to.
38
+ * @param {MessageHandler} messageHandler - A function to handle the processed messages.
39
+ * @returns {Promise<void>} - A promise that resolves once the consumer has successfully subscribed and started processing messages.
40
+ * @throws {Error} - Throws an error if the consumer is not connected before subscribing.
41
+ */
42
+ subscribe(topic: string, messageHandler: MessageHandler): Promise<void>;
41
43
  }
42
44
  //# sourceMappingURL=BaseConsumer.d.ts.map
@@ -0,0 +1,37 @@
1
+ import { Kafka, type KafkaConfig } from "kafkajs";
2
+ /**
3
+ * BaseKafka class provides basic functionality for interacting with a Kafka cluster.
4
+ * It allows creating Kafka topics and fetching the Kafka instance for producing/consuming messages.
5
+ */
6
+ export declare class BaseKafka {
7
+ private kafka;
8
+ private consumerPath;
9
+ private producerPath;
10
+ /**
11
+ * Creates an instance of BaseKafka with the provided Kafka configuration.
12
+ *
13
+ * @param {KafkaConfig} config - Kafka configuration object containing client settings.
14
+ */
15
+ constructor(config: KafkaConfig);
16
+ /**
17
+ * Returns the Kafka instance to be used for producing and consuming messages.
18
+ *
19
+ * @returns {Kafka} - Kafka instance.
20
+ */
21
+ getKafka(): Kafka;
22
+ /**
23
+ * Creates Kafka topics if they don't already exist.
24
+ *
25
+ * This method checks if the specified topics exist, and if not, it creates them with the given
26
+ * number of partitions and replication factor.
27
+ *
28
+ * @param {Array<{ topic: string; partitions: number; replicationFactor: number }>} topics - List of topics to be created with their partitions and replication factor.
29
+ * @returns {Promise<void>} - A promise that resolves once the topics are created (or skipped if they already exist).
30
+ */
31
+ createTopics(topics: {
32
+ topic: string;
33
+ partitions: number;
34
+ replicationFactor: number;
35
+ }[]): Promise<void>;
36
+ }
37
+ //# sourceMappingURL=BaseKafka.d.ts.map
@@ -1,45 +1,42 @@
1
+ import { type ProducerConfig } from "kafkajs";
2
+ import { BaseKafka } from "./BaseKafka";
3
+ import type { PublishType } from "../types/BrokerType";
1
4
  /**
2
- * BaseProducer class to handle Kafka producer operations such as connecting,
3
- * producing messages, and disconnecting.
4
- * This class uses the Singleton pattern to ensure only one instance of the producer exists.
5
+ * BaseProducer class provides basic functionality for producing messages to a Kafka topic.
6
+ * It manages the connection, disconnection, and message publishing logic for Kafka producers.
5
7
  */
6
8
  export declare class BaseProducer {
7
- protected static instance: BaseProducer | null;
8
- private kafka;
9
9
  private producer;
10
- private clientId;
10
+ private kafka;
11
11
  /**
12
- * Constructs a new producer instance with the given client ID and Kafka brokers.
12
+ * Creates an instance of BaseProducer using a BaseKafka instance.
13
13
  *
14
- * @param clientId - The client ID to uniquely identify the producer instance.
15
- * @param brokers - An array of Kafka broker addresses to connect to.
14
+ * @param {BaseKafka} baseKafka - The instance of the BaseKafka class which provides access to the Kafka client.
16
15
  */
17
- constructor(clientId: string, brokers: string[]);
16
+ constructor(baseKafka: BaseKafka);
18
17
  /**
19
- * Public method to get the single instance of BaseProducer (Singleton pattern).
20
- * If no instance exists, it creates a new one. If an instance already exists, it returns the existing one.
18
+ * Connects the producer to the Kafka cluster with the specified optional configuration.
19
+ * If the producer is already connected, it logs a message and does nothing.
21
20
  *
22
- * @param clientId - The client ID used to identify the producer instance.
23
- * @param brokers - An array of Kafka brokers to connect to.
24
- * @returns The single instance of the BaseProducer.
21
+ * @param {ProducerConfig} [config] - Optional additional configuration for the producer.
22
+ * @returns {Promise<void>} - A promise that resolves once the producer is connected.
25
23
  */
26
- static getInstance(clientId: string, brokers: string[]): BaseProducer;
24
+ connect(config?: ProducerConfig): Promise<void>;
27
25
  /**
28
- * Connects the producer to the Kafka cluster.
29
- * This method establishes the connection between the producer and the Kafka brokers.
30
- */
31
- connect(): Promise<void>;
32
- /**
33
- * Sends a message to a Kafka topic.
26
+ * Disconnects the producer from the Kafka cluster.
27
+ * If the producer is already disconnected, it logs a message and does nothing.
34
28
  *
35
- * @param topic - The Kafka topic to which the message will be sent.
36
- * @param message - The message content to send to the topic.
29
+ * @returns {Promise<void>} - A promise that resolves once the producer is disconnected.
37
30
  */
38
- produceMessage(topic: string, message: string): Promise<void>;
31
+ disconnect(): Promise<void>;
39
32
  /**
40
- * Disconnects the producer from the Kafka cluster.
41
- * This method gracefully shuts down the producer connection.
33
+ * Publishes a message to a Kafka topic.
34
+ * If the producer is not connected, it throws an error.
35
+ *
36
+ * @param {PublishType} data - The data to publish, including the topic, event, headers, and message content.
37
+ * @returns {Promise<boolean>} - A promise that resolves to true if the message was successfully sent, false otherwise.
38
+ * @throws {Error} - Throws an error if the producer is not connected.
42
39
  */
43
- disconnect(): Promise<void>;
40
+ publish(data: PublishType): Promise<boolean>;
44
41
  }
45
42
  //# sourceMappingURL=BaseProducer.d.ts.map
@@ -0,0 +1,16 @@
1
+ import type { KafkaConfig } from "kafkajs";
2
+ export declare class ServeKafka {
3
+ private consumerPath;
4
+ private producerPath;
5
+ private config;
6
+ constructor(config: KafkaConfig, consumerPath?: string, producerPath?: string);
7
+ /**
8
+ * Starts all consumers by dynamically importing and initializing consumer classes.
9
+ */
10
+ startConsumers(): Promise<void>;
11
+ /**
12
+ * Starts all producers by dynamically importing and initializing producer classes.
13
+ */
14
+ startProducers(): Promise<void>;
15
+ }
16
+ //# sourceMappingURL=ServeKafka.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ccci/micro-server",
3
- "version": "1.0.151",
3
+ "version": "1.0.153",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",