@konplit-services/common 1.0.96 → 1.0.98

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.
@@ -6,7 +6,7 @@
6
6
  *
7
7
  * @module Listener
8
8
  */
9
- import { JetStreamClient, JsMsg } from "nats";
9
+ import { JetStreamClient, JsMsg, NatsConnection, Msg } from "nats";
10
10
  import { StreamEvent, StreamName } from "../subjects";
11
11
  /**
12
12
  * Event Interface
@@ -32,6 +32,7 @@ interface Event {
32
32
  */
33
33
  export declare abstract class Listener<T extends Event> {
34
34
  private client;
35
+ private nc?;
35
36
  private jc;
36
37
  /**
37
38
  * The subject associated with the event.
@@ -65,12 +66,19 @@ export declare abstract class Listener<T extends Event> {
65
66
  * @param {JsMsg} msg - The raw NATS message
66
67
  */
67
68
  abstract onMessage(data: T["data"], msg: JsMsg): void;
69
+ /**
70
+ * Handles the incoming message.
71
+ * @abstract
72
+ * @param {T["data"]} data - The decoded data from the message
73
+ * @param {JsMsg} msg - The raw NATS message
74
+ */
75
+ abstract onMessageResponse(data: T["data"], msg: Msg): any;
68
76
  /**
69
77
  * Constructor for the Listener class.
70
78
  *
71
79
  * @param {JetStreamClient} client - The NATS JetStream client
72
80
  */
73
- constructor(client: JetStreamClient);
81
+ constructor(client: JetStreamClient, nc?: NatsConnection);
74
82
  /**
75
83
  * Sets up the listener by ensuring the stream and consumer exist,
76
84
  * then starts consuming messages.
@@ -78,6 +86,7 @@ export declare abstract class Listener<T extends Event> {
78
86
  * @returns {Promise<void>} - Resolves when the listener is set up and consuming messages
79
87
  */
80
88
  listen(): Promise<void>;
89
+ listenForRequests(): Promise<void>;
81
90
  /**
82
91
  * Ensures the stream exists in the NATS JetStream.
83
92
  * Creates the stream if it does not exist.
@@ -16,6 +16,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
16
16
  step((generator = generator.apply(thisArg, _arguments || [])).next());
17
17
  });
18
18
  };
19
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
20
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
21
+ var m = o[Symbol.asyncIterator], i;
22
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
23
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
24
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
25
+ };
19
26
  Object.defineProperty(exports, "__esModule", { value: true });
20
27
  exports.Listener = void 0;
21
28
  const nats_1 = require("nats");
@@ -35,7 +42,7 @@ class Listener {
35
42
  *
36
43
  * @param {JetStreamClient} client - The NATS JetStream client
37
44
  */
38
- constructor(client) {
45
+ constructor(client, nc) {
39
46
  this.jc = (0, nats_1.JSONCodec)();
40
47
  /**
41
48
  * Acknowledgment wait time of 5 seconds before retrying.
@@ -43,6 +50,7 @@ class Listener {
43
50
  */
44
51
  this.ackWait = (0, nats_1.nanos)(5 * 1000); // acknowledge time of 5 seconds before retry
45
52
  this.client = client;
53
+ this.nc = nc;
46
54
  }
47
55
  /**
48
56
  * Sets up the listener by ensuring the stream and consumer exist,
@@ -72,6 +80,31 @@ class Listener {
72
80
  }
73
81
  });
74
82
  }
83
+ listenForRequests() {
84
+ var e_1, _a;
85
+ var _b;
86
+ return __awaiter(this, void 0, void 0, function* () {
87
+ const subscription = (_b = this.nc) === null || _b === void 0 ? void 0 : _b.subscribe(this.subject);
88
+ try {
89
+ for (var subscription_1 = __asyncValues(subscription), subscription_1_1; subscription_1_1 = yield subscription_1.next(), !subscription_1_1.done;) {
90
+ const msg = subscription_1_1.value;
91
+ // Decode the request data
92
+ const decodedRequest = this.jc.decode(msg.data);
93
+ console.log("Received Request:", decodedRequest);
94
+ const response = this.onMessageResponse(decodedRequest, msg);
95
+ // Send a reply back
96
+ msg.respond(this.jc.encode({ data: response }));
97
+ }
98
+ }
99
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
100
+ finally {
101
+ try {
102
+ if (subscription_1_1 && !subscription_1_1.done && (_a = subscription_1.return)) yield _a.call(subscription_1);
103
+ }
104
+ finally { if (e_1) throw e_1.error; }
105
+ }
106
+ });
107
+ }
75
108
  /**
76
109
  * Ensures the stream exists in the NATS JetStream.
77
110
  * Creates the stream if it does not exist.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@konplit-services/common",
3
- "version": "1.0.96",
3
+ "version": "1.0.98",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",