@taphealth/kafka 1.1.2 → 1.1.3

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.
@@ -0,0 +1,16 @@
1
+ import { Kafka, Message } from "kafkajs";
2
+ interface Event {
3
+ topic: string;
4
+ data: any;
5
+ }
6
+ export declare abstract class KafkaConsumer<T extends Event> {
7
+ abstract topic: T["topic"];
8
+ abstract groupId: string;
9
+ abstract onMessage(data: T["data"], msg: Message): void;
10
+ protected client: Kafka;
11
+ constructor(client: Kafka);
12
+ consume(): Promise<void>;
13
+ parseMessage(msg: Message): any;
14
+ disconnect(): Promise<void>;
15
+ }
16
+ export {};
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.KafkaConsumer = void 0;
4
+ class KafkaConsumer {
5
+ constructor(client) {
6
+ this.client = client;
7
+ }
8
+ async consume() {
9
+ const consumer = this.client.consumer({
10
+ groupId: this.groupId,
11
+ });
12
+ await consumer.connect();
13
+ await consumer.subscribe({ topic: this.topic });
14
+ await consumer.run({
15
+ eachMessage: async ({ message }) => {
16
+ console.log("Received message on topic", this.topic);
17
+ const data = this.parseMessage(message);
18
+ this.onMessage(data, message);
19
+ },
20
+ });
21
+ }
22
+ parseMessage(msg) {
23
+ var _a;
24
+ const data = JSON.parse(((_a = msg.value) === null || _a === void 0 ? void 0 : _a.toString()) || "");
25
+ return data;
26
+ }
27
+ async disconnect() {
28
+ await this.client.consumer({ groupId: this.groupId }).disconnect();
29
+ }
30
+ }
31
+ exports.KafkaConsumer = KafkaConsumer;
package/dist/index.d.ts CHANGED
@@ -1,2 +1,4 @@
1
- declare function init(): Promise<void>;
2
- export { init };
1
+ export * from "./topics";
2
+ export * from "./kafka";
3
+ export * from "./producer";
4
+ export * from "./consumer";
package/dist/index.js CHANGED
@@ -1,21 +1,20 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.init = void 0;
4
- const client_1 = require("./client");
5
- async function init() {
6
- const admin = client_1.kafka.admin();
7
- await admin.connect();
8
- console.log("Connected to Kafka as an admin");
9
- await admin.createTopics({
10
- topics: [
11
- {
12
- topic: "payment-captured",
13
- numPartitions: 2,
14
- replicationFactor: 2,
15
- },
16
- ],
17
- });
18
- console.log("Created topics");
19
- await admin.disconnect();
20
- }
21
- exports.init = init;
17
+ __exportStar(require("./topics"), exports);
18
+ __exportStar(require("./kafka"), exports);
19
+ __exportStar(require("./producer"), exports);
20
+ __exportStar(require("./consumer"), exports);
@@ -0,0 +1,9 @@
1
+ import { Admin } from "kafkajs";
2
+ export declare class KafkaClient {
3
+ private _client?;
4
+ private _admin?;
5
+ get admin(): Admin;
6
+ connect(brokers: string[]): Promise<void>;
7
+ createTopics(topics: string[]): Promise<void>;
8
+ disconnect(): Promise<void>;
9
+ }
package/dist/kafka.js ADDED
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.KafkaClient = void 0;
4
+ const kafkajs_1 = require("kafkajs");
5
+ const topics_1 = require("./topics");
6
+ class KafkaClient {
7
+ get admin() {
8
+ if (!this._admin) {
9
+ throw new Error("Cannot access admin before connecting");
10
+ }
11
+ return this._admin;
12
+ }
13
+ async connect(brokers) {
14
+ try {
15
+ this._client = new kafkajs_1.Kafka({
16
+ clientId: "taphealth",
17
+ brokers,
18
+ });
19
+ this._admin = this._client.admin();
20
+ const topics = Object.values(topics_1.Topics);
21
+ await this.createTopics(topics);
22
+ }
23
+ catch (err) {
24
+ console.log("Error in connecting to Kafka", err);
25
+ throw err;
26
+ }
27
+ }
28
+ async createTopics(topics) {
29
+ const admin = this.admin;
30
+ await admin.connect();
31
+ try {
32
+ await admin.createTopics({
33
+ topics: [
34
+ ...topics.map((topic) => ({
35
+ topic,
36
+ numPartitions: 2,
37
+ replicationFactor: 2,
38
+ })),
39
+ ],
40
+ });
41
+ await admin.disconnect();
42
+ }
43
+ catch (err) {
44
+ console.log("Error in creating topics", err);
45
+ }
46
+ }
47
+ async disconnect() {
48
+ if (this._client) {
49
+ await this._client.admin().disconnect();
50
+ await this._client.producer().disconnect();
51
+ }
52
+ }
53
+ }
54
+ exports.KafkaClient = KafkaClient;
@@ -0,0 +1,13 @@
1
+ import { Kafka } from "kafkajs";
2
+ import { Topics } from "./topics";
3
+ interface Event {
4
+ topic: Topics;
5
+ data: any;
6
+ }
7
+ export declare abstract class KafkaProducer<T extends Event> {
8
+ abstract topic: T["topic"];
9
+ protected client: Kafka;
10
+ constructor(client: Kafka);
11
+ send(data: T["data"]): Promise<void>;
12
+ }
13
+ export {};
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.KafkaProducer = void 0;
4
+ class KafkaProducer {
5
+ constructor(client) {
6
+ this.client = client;
7
+ }
8
+ async send(data) {
9
+ const producer = this.client.producer();
10
+ await producer.send({
11
+ topic: this.topic,
12
+ messages: [{ value: JSON.stringify(data) }],
13
+ });
14
+ }
15
+ }
16
+ exports.KafkaProducer = KafkaProducer;
@@ -0,0 +1,12 @@
1
+ export declare enum Topics {
2
+ AppointmentCreated = "appointment:created",
3
+ AppointmentUpdated = "appointment:updated",
4
+ OfflineAppointmentCreated = "offline-appointment:created",
5
+ OfflineAppointmentUpdated = "offline-appointment:updated",
6
+ OrderCreated = "order:created",
7
+ OrderCancelled = "order:cancelled",
8
+ OrderConfirmed = "order:confirmed",
9
+ PaymentCaptured = "payment:captured",
10
+ PaymentFailed = "payment:failed",
11
+ ExpirationComplete = "expiration:complete"
12
+ }
package/dist/topics.js ADDED
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Topics = void 0;
4
+ var Topics;
5
+ (function (Topics) {
6
+ Topics["AppointmentCreated"] = "appointment:created";
7
+ Topics["AppointmentUpdated"] = "appointment:updated";
8
+ Topics["OfflineAppointmentCreated"] = "offline-appointment:created";
9
+ Topics["OfflineAppointmentUpdated"] = "offline-appointment:updated";
10
+ Topics["OrderCreated"] = "order:created";
11
+ Topics["OrderCancelled"] = "order:cancelled";
12
+ Topics["OrderConfirmed"] = "order:confirmed";
13
+ Topics["PaymentCaptured"] = "payment:captured";
14
+ Topics["PaymentFailed"] = "payment:failed";
15
+ Topics["ExpirationComplete"] = "expiration:complete";
16
+ })(Topics || (exports.Topics = Topics = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taphealth/kafka",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
package/dist/client.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import { Kafka } from "kafkajs";
2
- declare const kafka: Kafka;
3
- export { kafka };
package/dist/client.js DELETED
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.kafka = void 0;
4
- const kafkajs_1 = require("kafkajs");
5
- const kafka = new kafkajs_1.Kafka({
6
- clientId: "taphealth",
7
- brokers: ["dev-cluster-kafka-bootstrap.kafka.svc.cluster.local:9092"],
8
- });
9
- exports.kafka = kafka;