@konplit-services/common 1.0.146 → 1.0.147
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/build/events/base-events/base-event-request.listener.d.ts +61 -0
- package/build/events/base-events/base-event-request.listener.js +56 -0
- package/build/events/base-events/base-event-response-publisher.d.ts +56 -0
- package/build/events/base-events/base-event-response-publisher.js +68 -0
- package/build/events/base-events/index.d.ts +2 -0
- package/build/events/base-events/index.js +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Listener Module
|
|
3
|
+
*
|
|
4
|
+
* This module defines an abstract `Listener` class for listening to events from a NATS JetStream.
|
|
5
|
+
* It ensures that the required stream and consumer exist before consuming events.
|
|
6
|
+
*
|
|
7
|
+
* @module Listener
|
|
8
|
+
*/
|
|
9
|
+
import { NatsConnection, Msg } from "nats";
|
|
10
|
+
import { StreamEvent, StreamName } from "../subjects";
|
|
11
|
+
/**
|
|
12
|
+
* Event Interface
|
|
13
|
+
*
|
|
14
|
+
* Represents an event that will be consumed from a NATS stream.
|
|
15
|
+
*
|
|
16
|
+
* @interface
|
|
17
|
+
*/
|
|
18
|
+
interface Event {
|
|
19
|
+
subject: string;
|
|
20
|
+
streamEvents: StreamEvent;
|
|
21
|
+
streamName: StreamName;
|
|
22
|
+
data: any;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Listener Abstract Class
|
|
26
|
+
*
|
|
27
|
+
* Abstract class for consuming events from a NATS JetStream.
|
|
28
|
+
* Subclasses should implement the specific subject, streamEvents, stream, and durableName properties.
|
|
29
|
+
*
|
|
30
|
+
* @abstract
|
|
31
|
+
* @template T - The type of event to be consumed
|
|
32
|
+
*/
|
|
33
|
+
export declare abstract class RequestListener<T extends Event> {
|
|
34
|
+
private nc;
|
|
35
|
+
private jc;
|
|
36
|
+
/**
|
|
37
|
+
* The subject associated with the event.
|
|
38
|
+
* @abstract
|
|
39
|
+
*/
|
|
40
|
+
abstract subject: T["subject"];
|
|
41
|
+
/**
|
|
42
|
+
* The durable name for the consumer.
|
|
43
|
+
* @abstract
|
|
44
|
+
*/
|
|
45
|
+
abstract durableName: string;
|
|
46
|
+
/**
|
|
47
|
+
* Handles the incoming message.
|
|
48
|
+
* @abstract
|
|
49
|
+
* @param {T["data"]} data - The decoded data from the message
|
|
50
|
+
* @param {JsMsg} msg - The raw NATS message
|
|
51
|
+
*/
|
|
52
|
+
abstract onMessageResponse(data: T["data"], msg: Msg): any;
|
|
53
|
+
/**
|
|
54
|
+
* Constructor for the Listener class.
|
|
55
|
+
*
|
|
56
|
+
* @param {JetStreamClient} client - The NATS JetStream client
|
|
57
|
+
*/
|
|
58
|
+
constructor(nc: NatsConnection);
|
|
59
|
+
listenForRequests(): Promise<void>;
|
|
60
|
+
}
|
|
61
|
+
export {};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Listener Module
|
|
4
|
+
*
|
|
5
|
+
* This module defines an abstract `Listener` class for listening to events from a NATS JetStream.
|
|
6
|
+
* It ensures that the required stream and consumer exist before consuming events.
|
|
7
|
+
*
|
|
8
|
+
* @module Listener
|
|
9
|
+
*/
|
|
10
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
11
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
12
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
13
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
14
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
15
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
16
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.RequestListener = void 0;
|
|
21
|
+
const nats_1 = require("nats");
|
|
22
|
+
/**
|
|
23
|
+
* Listener Abstract Class
|
|
24
|
+
*
|
|
25
|
+
* Abstract class for consuming events from a NATS JetStream.
|
|
26
|
+
* Subclasses should implement the specific subject, streamEvents, stream, and durableName properties.
|
|
27
|
+
*
|
|
28
|
+
* @abstract
|
|
29
|
+
* @template T - The type of event to be consumed
|
|
30
|
+
*/
|
|
31
|
+
class RequestListener {
|
|
32
|
+
/**
|
|
33
|
+
* Constructor for the Listener class.
|
|
34
|
+
*
|
|
35
|
+
* @param {JetStreamClient} client - The NATS JetStream client
|
|
36
|
+
*/
|
|
37
|
+
constructor(nc) {
|
|
38
|
+
this.jc = (0, nats_1.JSONCodec)();
|
|
39
|
+
this.nc = nc;
|
|
40
|
+
}
|
|
41
|
+
listenForRequests() {
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
this.nc.subscribe(this.subject, {
|
|
44
|
+
// NOT TRAPPING ERRORS FOR BREVITY (this should be simple enough to implement from this starting point)
|
|
45
|
+
callback: (err, msg) => __awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
// decode the symbol for use in the following query
|
|
47
|
+
const decodedRequest = this.jc.decode(msg.data);
|
|
48
|
+
console.log("Received Request:", decodedRequest);
|
|
49
|
+
const response = yield this.onMessageResponse(decodedRequest, msg);
|
|
50
|
+
msg.respond(this.jc.encode(response));
|
|
51
|
+
}),
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.RequestListener = RequestListener;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Publisher Module
|
|
3
|
+
*
|
|
4
|
+
* This module defines an abstract `Publisher` class for publishing events to a NATS JetStream.
|
|
5
|
+
* It ensures that the required stream exists before publishing events.
|
|
6
|
+
*
|
|
7
|
+
* @module Publisher
|
|
8
|
+
*/
|
|
9
|
+
import { NatsConnection } from "nats";
|
|
10
|
+
import { StreamEvent, StreamName } from "../subjects";
|
|
11
|
+
/**
|
|
12
|
+
* Event Interface
|
|
13
|
+
*
|
|
14
|
+
* Represents an event that will be published to a NATS stream.
|
|
15
|
+
*
|
|
16
|
+
* @interface
|
|
17
|
+
*/
|
|
18
|
+
interface Event {
|
|
19
|
+
subject: string;
|
|
20
|
+
streamEvents: StreamEvent;
|
|
21
|
+
streamName: StreamName;
|
|
22
|
+
data: any;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Publisher Abstract Class
|
|
26
|
+
*
|
|
27
|
+
* Abstract class for publishing events to a NATS JetStream.
|
|
28
|
+
* Subclasses should implement the specific subject, streamEvents, and stream properties.
|
|
29
|
+
*
|
|
30
|
+
* @abstract
|
|
31
|
+
* @template T - The type of event to be published
|
|
32
|
+
*/
|
|
33
|
+
export declare abstract class ResponsePublisher<T extends Event> {
|
|
34
|
+
private nc;
|
|
35
|
+
private jc;
|
|
36
|
+
/**
|
|
37
|
+
* The subject associated with the event.
|
|
38
|
+
* @abstract
|
|
39
|
+
*/
|
|
40
|
+
abstract subject: T["subject"];
|
|
41
|
+
/**
|
|
42
|
+
* Constructor for the Publisher class.
|
|
43
|
+
*
|
|
44
|
+
* @param {JetStreamClient} client - The NATS JetStream client
|
|
45
|
+
*/
|
|
46
|
+
constructor(nc: NatsConnection);
|
|
47
|
+
/**
|
|
48
|
+
* Sends a request to another service and waits for a reply.
|
|
49
|
+
*
|
|
50
|
+
* @param {T["data"]} data - The data to be sent in the request
|
|
51
|
+
* @param {number} timeout - The timeout duration to wait for a response
|
|
52
|
+
* @returns {Promise<any>} - Resolves with the reply data from the responding service
|
|
53
|
+
*/
|
|
54
|
+
request(data: T["data"], timeout?: number): Promise<any>;
|
|
55
|
+
}
|
|
56
|
+
export {};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Publisher Module
|
|
4
|
+
*
|
|
5
|
+
* This module defines an abstract `Publisher` class for publishing events to a NATS JetStream.
|
|
6
|
+
* It ensures that the required stream exists before publishing events.
|
|
7
|
+
*
|
|
8
|
+
* @module Publisher
|
|
9
|
+
*/
|
|
10
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
11
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
12
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
13
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
14
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
15
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
16
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.ResponsePublisher = void 0;
|
|
21
|
+
const nats_1 = require("nats");
|
|
22
|
+
/**
|
|
23
|
+
* Publisher Abstract Class
|
|
24
|
+
*
|
|
25
|
+
* Abstract class for publishing events to a NATS JetStream.
|
|
26
|
+
* Subclasses should implement the specific subject, streamEvents, and stream properties.
|
|
27
|
+
*
|
|
28
|
+
* @abstract
|
|
29
|
+
* @template T - The type of event to be published
|
|
30
|
+
*/
|
|
31
|
+
class ResponsePublisher {
|
|
32
|
+
/**
|
|
33
|
+
* Constructor for the Publisher class.
|
|
34
|
+
*
|
|
35
|
+
* @param {JetStreamClient} client - The NATS JetStream client
|
|
36
|
+
*/
|
|
37
|
+
constructor(nc) {
|
|
38
|
+
this.jc = (0, nats_1.JSONCodec)();
|
|
39
|
+
this.nc = nc;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Sends a request to another service and waits for a reply.
|
|
43
|
+
*
|
|
44
|
+
* @param {T["data"]} data - The data to be sent in the request
|
|
45
|
+
* @param {number} timeout - The timeout duration to wait for a response
|
|
46
|
+
* @returns {Promise<any>} - Resolves with the reply data from the responding service
|
|
47
|
+
*/
|
|
48
|
+
request(data, timeout = 5000) {
|
|
49
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
try {
|
|
51
|
+
// Send request and wait for the response
|
|
52
|
+
const response = yield this.nc.request(this.subject, this.jc.encode(data), { timeout } // Timeout for the response
|
|
53
|
+
);
|
|
54
|
+
if (!response) {
|
|
55
|
+
throw new Error("Failed to get Response");
|
|
56
|
+
}
|
|
57
|
+
const replyData = this.jc.decode(response.data);
|
|
58
|
+
console.log("Received Reply:", replyData);
|
|
59
|
+
return replyData;
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
console.error("Error sending request:", err);
|
|
63
|
+
throw err;
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.ResponsePublisher = ResponsePublisher;
|
|
@@ -16,3 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./base-events.listener"), exports);
|
|
18
18
|
__exportStar(require("./base-events.publisher"), exports);
|
|
19
|
+
__exportStar(require("./base-event-request.listener"), exports);
|
|
20
|
+
__exportStar(require("./base-event-response-publisher"), exports);
|