@hastehaul/common 1.0.12 → 1.0.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. package/build/connections-wrappers/nats-wrapper.d.ts +27 -0
  2. package/build/connections-wrappers/nats-wrapper.js +50 -0
  3. package/build/events/base/base-consumer.d.ts +83 -0
  4. package/build/events/base/base-consumer.js +129 -0
  5. package/build/events/base/base-publisher.d.ts +33 -0
  6. package/build/events/base/base-publisher.js +45 -0
  7. package/build/events/base/base-stream.d.ts +34 -0
  8. package/build/events/base/base-stream.js +40 -0
  9. package/build/events/common/enums/durable-names/index.d.ts +8 -0
  10. package/build/events/common/enums/durable-names/index.js +2 -0
  11. package/build/events/common/enums/durable-names/order-dname.d.ts +17 -0
  12. package/build/events/common/enums/durable-names/order-dname.js +21 -0
  13. package/build/events/common/enums/durable-names/payment-dname.d.ts +13 -0
  14. package/build/events/common/enums/durable-names/payment-dname.js +17 -0
  15. package/build/events/common/enums/index.d.ts +18 -0
  16. package/build/events/common/enums/index.js +35 -0
  17. package/build/events/common/enums/streams/index.d.ts +8 -0
  18. package/build/events/common/enums/streams/index.js +2 -0
  19. package/build/events/common/enums/streams/order-stream.d.ts +9 -0
  20. package/build/events/common/enums/streams/order-stream.js +13 -0
  21. package/build/events/common/enums/streams/payment-stream.d.ts +9 -0
  22. package/build/events/common/enums/streams/payment-stream.js +13 -0
  23. package/build/events/common/enums/subjects/index.d.ts +8 -0
  24. package/build/events/common/enums/subjects/index.js +2 -0
  25. package/build/events/common/enums/subjects/main-subjects.d.ts +39 -0
  26. package/build/events/common/enums/subjects/main-subjects.js +22 -0
  27. package/build/events/common/enums/subjects/order-subjects.d.ts +17 -0
  28. package/build/events/common/enums/subjects/order-subjects.js +21 -0
  29. package/build/events/common/enums/subjects/payment-subjects.d.ts +13 -0
  30. package/build/events/common/enums/subjects/payment-subjects.js +17 -0
  31. package/build/events/common/interfaces/order-events-interfaces/order-cancelled-event.d.ts +15 -0
  32. package/build/events/common/interfaces/order-events-interfaces/order-cancelled-event.js +2 -0
  33. package/build/events/common/interfaces/order-events-interfaces/order-completed-event.d.ts +15 -0
  34. package/build/events/common/interfaces/order-events-interfaces/order-completed-event.js +2 -0
  35. package/build/events/common/interfaces/order-events-interfaces/order-requested-event.d.ts +15 -0
  36. package/build/events/common/interfaces/order-events-interfaces/order-requested-event.js +2 -0
  37. package/build/events/common/interfaces/payment-event-interfaces/payment-created-event.d.ts +0 -0
  38. package/build/events/common/interfaces/payment-event-interfaces/payment-created-event.js +1 -0
  39. package/build/events/common/interfaces/payment-event-interfaces/payment-failed-event.d.ts +0 -0
  40. package/build/events/common/interfaces/payment-event-interfaces/payment-failed-event.js +1 -0
  41. package/build/events/common/interfaces/stream-interfaces/order-stream-interface.d.ts +6 -0
  42. package/build/events/common/interfaces/stream-interfaces/order-stream-interface.js +2 -0
  43. package/package.json +2 -1
@@ -0,0 +1,27 @@
1
+ import { ConnectionOptions, NatsConnection } from 'nats';
2
+ /**
3
+ * A wrapper class for the NATS client to manage connections and provide a centralized client instance.
4
+ */
5
+ declare class NatsWrapper {
6
+ private _client?;
7
+ /**
8
+ * Get the NATS client instance.
9
+ *
10
+ * @throws {Error} - Throws an error if the NATS client is accessed before connecting.
11
+ * @returns {NatsConnection} - The NATS client instance.
12
+ */
13
+ get client(): NatsConnection;
14
+ /**
15
+ * Connect to the NATS server using the specified connection options.
16
+ *
17
+ * The client instance will be stored in the `_client` property for later access.
18
+ *
19
+ * @param {ConnectionOptions} opts - The options for connecting to the NATS server.
20
+ */
21
+ connect(opts?: ConnectionOptions): Promise<void>;
22
+ }
23
+ /**
24
+ * Singleton instance of the NatsWrapper class to provide a centralized NATS client connection.
25
+ */
26
+ export declare const natsWrapper: NatsWrapper;
27
+ export {};
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.natsWrapper = void 0;
13
+ const nats_1 = require("nats");
14
+ /**
15
+ * A wrapper class for the NATS client to manage connections and provide a centralized client instance.
16
+ */
17
+ class NatsWrapper {
18
+ /**
19
+ * Get the NATS client instance.
20
+ *
21
+ * @throws {Error} - Throws an error if the NATS client is accessed before connecting.
22
+ * @returns {NatsConnection} - The NATS client instance.
23
+ */
24
+ get client() {
25
+ if (!this._client)
26
+ throw new Error("Cannot access NATS CLIENT before connecting");
27
+ return this._client;
28
+ }
29
+ /**
30
+ * Connect to the NATS server using the specified connection options.
31
+ *
32
+ * The client instance will be stored in the `_client` property for later access.
33
+ *
34
+ * @param {ConnectionOptions} opts - The options for connecting to the NATS server.
35
+ */
36
+ connect(opts) {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ try {
39
+ this._client = yield (0, nats_1.connect)(opts);
40
+ }
41
+ catch (error) {
42
+ console.log(`error connecting to ${JSON.stringify(opts === null || opts === void 0 ? void 0 : opts.servers)}`);
43
+ }
44
+ });
45
+ }
46
+ }
47
+ /**
48
+ * Singleton instance of the NatsWrapper class to provide a centralized NATS client connection.
49
+ */
50
+ exports.natsWrapper = new NatsWrapper();
@@ -0,0 +1,83 @@
1
+ import { NatsConnection, ConsumerConfig, JsMsg } from "nats";
2
+ import { DurableName, Subjects, Streams } from "../common/enums";
3
+ import { MainSubjects, ValidateSubjectFormat } from "../common/enums/subjects/main-subjects";
4
+ /**
5
+ * Interface representing an event published to NATS.
6
+ */
7
+ interface Event {
8
+ subject: ValidateSubjectFormat<Subjects, MainSubjects>;
9
+ durableName?: DurableName;
10
+ stream: Streams;
11
+ data: any;
12
+ }
13
+ /**
14
+ * Abstract class representing a NATS consumer for processing messages from a JetStream stream.
15
+ *
16
+ * @typeparam {T} - The type of event to be consumed, extending the Event interface.
17
+ */
18
+ export declare abstract class NatsConsumer<T extends Event> {
19
+ protected client: NatsConnection;
20
+ protected jc: import("nats").Codec<T>;
21
+ protected sc: import("nats").Codec<string>;
22
+ /**
23
+ * The name of the JetStream stream to consume messages from.
24
+ */
25
+ abstract stream: T["stream"];
26
+ /**
27
+ * The subject to filter messages for this consumer.
28
+ */
29
+ abstract subject: T["subject"];
30
+ /**
31
+ * The name of the durable consumer, if applicable.
32
+ */
33
+ abstract durableName: T["durableName"];
34
+ /**
35
+ * The maximum number of messages that can be processed concurrently.
36
+ */
37
+ abstract maxConcurrentMessages: number;
38
+ protected ackWait: number;
39
+ /**
40
+ * Creates an instance of NatsConsumer.
41
+ *
42
+ * @param {NatsConnection} client - The NATS connection instance.
43
+ */
44
+ constructor(client: NatsConnection);
45
+ /**
46
+ * Provides the consumer options for JetStream.
47
+ *
48
+ * @returns {Partial<ConsumerConfig>} - The partial consumer options.
49
+ */
50
+ get consumerOptions(): Partial<ConsumerConfig>;
51
+ /**
52
+ * Consume messages from the JetStream stream and process them concurrently.
53
+ *
54
+ * Messages are processed asynchronously up to the maximum concurrent limit.
55
+ */
56
+ consume(): Promise<void>;
57
+ /**
58
+ * Abstract method to process a single message received from the stream.
59
+ *
60
+ * This method should be implemented by subclasses to define custom logic for processing messages.
61
+ *
62
+ * @param {T["data"]} data - The data representing the message payload.
63
+ * @param {JsMsg} msg - The message object received from the stream.
64
+ * @returns {Promise<void>} - A Promise that resolves once the message processing is completed successfully.
65
+ */
66
+ abstract processMessage(data: T["data"], msg: JsMsg): Promise<void>;
67
+ /**
68
+ * Closes the NATS connection when message processing is finished.
69
+ *
70
+ * @protected
71
+ * @returns {Promise<void>} - A Promise that resolves once the NATS connection is drained and closed.
72
+ */
73
+ protected close(): Promise<void>;
74
+ /**
75
+ * Utility method to parse the message data using the appropriate codec (JSON or string).
76
+ *
77
+ * @protected
78
+ * @param {JsMsg} msg - The message object received from the stream.
79
+ * @returns {T["data"]} - The parsed message data.
80
+ */
81
+ protected parsedMessage: (msg: JsMsg) => string | T;
82
+ }
83
+ export {};
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
12
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
13
+ var m = o[Symbol.asyncIterator], i;
14
+ 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);
15
+ 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); }); }; }
16
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
17
+ };
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.NatsConsumer = void 0;
20
+ const nats_1 = require("nats");
21
+ /**
22
+ * Abstract class representing a NATS consumer for processing messages from a JetStream stream.
23
+ *
24
+ * @typeparam {T} - The type of event to be consumed, extending the Event interface.
25
+ */
26
+ class NatsConsumer {
27
+ /**
28
+ * Creates an instance of NatsConsumer.
29
+ *
30
+ * @param {NatsConnection} client - The NATS connection instance.
31
+ */
32
+ constructor(client) {
33
+ this.jc = (0, nats_1.JSONCodec)();
34
+ this.sc = (0, nats_1.StringCodec)();
35
+ this.ackWait = (0, nats_1.nanos)(5 * 1000);
36
+ /**
37
+ * Utility method to parse the message data using the appropriate codec (JSON or string).
38
+ *
39
+ * @protected
40
+ * @param {JsMsg} msg - The message object received from the stream.
41
+ * @returns {T["data"]} - The parsed message data.
42
+ */
43
+ this.parsedMessage = (msg) => typeof msg.data === "string" ? this.sc.decode(msg.data) : this.jc.decode(msg.data);
44
+ this.client = client;
45
+ }
46
+ /**
47
+ * Provides the consumer options for JetStream.
48
+ *
49
+ * @returns {Partial<ConsumerConfig>} - The partial consumer options.
50
+ */
51
+ get consumerOptions() {
52
+ return {
53
+ deliver_policy: nats_1.DeliverPolicy.All,
54
+ ack_policy: nats_1.AckPolicy.Explicit,
55
+ ack_wait: this.ackWait,
56
+ durable_name: this.durableName,
57
+ replay_policy: nats_1.ReplayPolicy.Instant,
58
+ filter_subject: this.subject,
59
+ };
60
+ }
61
+ /**
62
+ * Consume messages from the JetStream stream and process them concurrently.
63
+ *
64
+ * Messages are processed asynchronously up to the maximum concurrent limit.
65
+ */
66
+ consume() {
67
+ var _a, e_1, _b, _c;
68
+ return __awaiter(this, void 0, void 0, function* () {
69
+ console.log(this.stream, this.durableName, this.subject);
70
+ try {
71
+ const jsm = yield this.client.jetstreamManager();
72
+ yield jsm.consumers.add(this.stream, this.consumerOptions);
73
+ const js = this.client.jetstream();
74
+ const consumer = yield js.consumers.get(this.stream, this.durableName);
75
+ const messages = yield consumer.consume({ max_messages: 5 });
76
+ let pendingMessages = [];
77
+ let inFlight = 0;
78
+ try {
79
+ for (var _d = true, messages_1 = __asyncValues(messages), messages_1_1; messages_1_1 = yield messages_1.next(), _a = messages_1_1.done, !_a; _d = true) {
80
+ _c = messages_1_1.value;
81
+ _d = false;
82
+ const msg = _c;
83
+ // Process messages asynchronously up to the maximum concurrent limit
84
+ if (inFlight < this.maxConcurrentMessages) {
85
+ const parsedMessage = this.parsedMessage(msg);
86
+ inFlight++;
87
+ yield this.processMessage(parsedMessage, msg).then(() => { inFlight--; }).catch(() => { inFlight--; });
88
+ }
89
+ else {
90
+ console.log("PENDING MESSAGES: ", pendingMessages);
91
+ pendingMessages.push(msg); // Store the message to be processed later
92
+ }
93
+ // Check if there are pending messages that can be processed
94
+ while (pendingMessages.length && inFlight < this.maxConcurrentMessages) {
95
+ console.log("PENDING MESSAGES: ", pendingMessages);
96
+ const pendingMessage = pendingMessages.shift();
97
+ const parsedMessage = this.parsedMessage(pendingMessage);
98
+ inFlight++;
99
+ yield this.processMessage(parsedMessage, pendingMessage).then(() => { inFlight--; }).catch(() => { inFlight--; });
100
+ }
101
+ }
102
+ }
103
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
104
+ finally {
105
+ try {
106
+ if (!_d && !_a && (_b = messages_1.return)) yield _b.call(messages_1);
107
+ }
108
+ finally { if (e_1) throw e_1.error; }
109
+ }
110
+ }
111
+ catch (error) {
112
+ console.error(error.message);
113
+ // throw new Error("Failed to consume messages");
114
+ }
115
+ });
116
+ }
117
+ /**
118
+ * Closes the NATS connection when message processing is finished.
119
+ *
120
+ * @protected
121
+ * @returns {Promise<void>} - A Promise that resolves once the NATS connection is drained and closed.
122
+ */
123
+ close() {
124
+ return __awaiter(this, void 0, void 0, function* () {
125
+ yield this.client.drain();
126
+ });
127
+ }
128
+ }
129
+ exports.NatsConsumer = NatsConsumer;
@@ -0,0 +1,33 @@
1
+ import { PubAck, NatsConnection } from "nats";
2
+ import { Subjects } from "../common/enums";
3
+ import { MainSubjects, ValidateSubjectFormat } from "../common/enums/subjects/main-subjects";
4
+ /**
5
+ * Interface representing an event to be published to NATS.
6
+ */
7
+ interface Event {
8
+ subject: ValidateSubjectFormat<Subjects, MainSubjects>;
9
+ data: any;
10
+ }
11
+ /**
12
+ * Abstract class representing a NATS publisher for publishing events to NATS.
13
+ *
14
+ * @typeparam {T} - The type of event to be published, extending the Event interface.
15
+ */
16
+ export declare abstract class NatsPublisher<T extends Event> {
17
+ private jc;
18
+ private sc;
19
+ /**
20
+ * The subject representing the event type to be published.
21
+ */
22
+ abstract subject: T["subject"];
23
+ /**
24
+ * Publishes an event to NATS using the provided NATS connection.
25
+ *
26
+ * @param {NatsConnection} nc - The NATS connection instance.
27
+ * @param {T["data"]} data - The data to be published as part of the event.
28
+ * @returns {Promise<PubAck>} - A Promise that resolves with the PubAck response from NATS.
29
+ * @throws {Error} - Throws an error if there is an issue while publishing the event.
30
+ */
31
+ publish(nc: NatsConnection, data: T["data"]): Promise<PubAck>;
32
+ }
33
+ export {};
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.NatsPublisher = void 0;
13
+ const nats_1 = require("nats");
14
+ /**
15
+ * Abstract class representing a NATS publisher for publishing events to NATS.
16
+ *
17
+ * @typeparam {T} - The type of event to be published, extending the Event interface.
18
+ */
19
+ class NatsPublisher {
20
+ constructor() {
21
+ this.jc = (0, nats_1.JSONCodec)();
22
+ this.sc = (0, nats_1.StringCodec)();
23
+ }
24
+ /**
25
+ * Publishes an event to NATS using the provided NATS connection.
26
+ *
27
+ * @param {NatsConnection} nc - The NATS connection instance.
28
+ * @param {T["data"]} data - The data to be published as part of the event.
29
+ * @returns {Promise<PubAck>} - A Promise that resolves with the PubAck response from NATS.
30
+ * @throws {Error} - Throws an error if there is an issue while publishing the event.
31
+ */
32
+ publish(nc, data) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ try {
35
+ const jetStreamClient = nc.jetstream();
36
+ return yield jetStreamClient.publish(this.subject, typeof data === "string" ? this.sc.encode(data) : this.jc.encode(data));
37
+ }
38
+ catch (error) {
39
+ console.error(error.message);
40
+ throw new Error(`Failed to publish message. Error: [${error.message}]`);
41
+ }
42
+ });
43
+ }
44
+ }
45
+ exports.NatsPublisher = NatsPublisher;
@@ -0,0 +1,34 @@
1
+ import { NatsConnection } from "nats";
2
+ import { Streams } from "../common/enums";
3
+ import { MainSubjects } from "../common/enums/subjects/main-subjects";
4
+ /**
5
+ * Interface representing an event to be used with NATS streams.
6
+ */
7
+ interface Event {
8
+ subject: MainSubjects;
9
+ stream: Streams;
10
+ }
11
+ /**
12
+ * Abstract class representing a NATS stream for handling events in a JetStream stream.
13
+ *
14
+ * @typeparam {T} - The type of event to be handled, extending the Event interface.
15
+ */
16
+ export declare abstract class NatsStream<T extends Event> {
17
+ /**
18
+ * The subject representing the event type to be handled.
19
+ */
20
+ abstract subject: T["subject"];
21
+ /**
22
+ * The name of the JetStream stream to handle events from.
23
+ */
24
+ abstract stream: T['stream'];
25
+ /**
26
+ * Initializes the NATS stream and adds it to the JetStream server using the provided NATS connection.
27
+ *
28
+ * @param {NatsConnection} client - The NATS connection instance.
29
+ * @returns {Promise<void>} - A Promise that resolves once the stream is successfully added to the JetStream server.
30
+ * @throws {Error} - Throws an error if there is an issue while adding the stream to the JetStream server.
31
+ */
32
+ init(client: NatsConnection): Promise<void>;
33
+ }
34
+ export {};
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.NatsStream = void 0;
13
+ /**
14
+ * Abstract class representing a NATS stream for handling events in a JetStream stream.
15
+ *
16
+ * @typeparam {T} - The type of event to be handled, extending the Event interface.
17
+ */
18
+ class NatsStream {
19
+ /**
20
+ * Initializes the NATS stream and adds it to the JetStream server using the provided NATS connection.
21
+ *
22
+ * @param {NatsConnection} client - The NATS connection instance.
23
+ * @returns {Promise<void>} - A Promise that resolves once the stream is successfully added to the JetStream server.
24
+ * @throws {Error} - Throws an error if there is an issue while adding the stream to the JetStream server.
25
+ */
26
+ init(client) {
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ try {
29
+ const jsm = yield client.jetstreamManager();
30
+ yield jsm.streams.add({ name: this.stream, subjects: [this.subject] });
31
+ return;
32
+ }
33
+ catch (error) {
34
+ console.error(error.message);
35
+ throw new Error("Failed to add stream");
36
+ }
37
+ });
38
+ }
39
+ }
40
+ exports.NatsStream = NatsStream;
@@ -0,0 +1,8 @@
1
+ import { PaymentDurableName } from "./payment-dname";
2
+ import { OrderDurableName } from "./order-dname";
3
+ /**
4
+ * Type alias representing a durable name that can be associated with different event consumers.
5
+ *
6
+ * The `DurableName` type can be one of the durable names defined in `OrderDurableName` or `PaymentDurableName`.
7
+ */
8
+ export type DurableName = OrderDurableName | PaymentDurableName;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Enum representing durable names for consumers associated with different order events.
3
+ */
4
+ export declare enum OrderDurableName {
5
+ /**
6
+ * The durable name for the consumer handling order cancelled events.
7
+ */
8
+ OrderCancelledDurablename = "order-cancelled-consumer",
9
+ /**
10
+ * The durable name for the consumer handling order requested events.
11
+ */
12
+ OrderRequestDurablename = "order-requested-consumer",
13
+ /**
14
+ * The durable name for the consumer handling order completed events.
15
+ */
16
+ OrderCompletedDurablename = "order-completed-consumer"
17
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OrderDurableName = void 0;
4
+ /**
5
+ * Enum representing durable names for consumers associated with different order events.
6
+ */
7
+ var OrderDurableName;
8
+ (function (OrderDurableName) {
9
+ /**
10
+ * The durable name for the consumer handling order cancelled events.
11
+ */
12
+ OrderDurableName["OrderCancelledDurablename"] = "order-cancelled-consumer";
13
+ /**
14
+ * The durable name for the consumer handling order requested events.
15
+ */
16
+ OrderDurableName["OrderRequestDurablename"] = "order-requested-consumer";
17
+ /**
18
+ * The durable name for the consumer handling order completed events.
19
+ */
20
+ OrderDurableName["OrderCompletedDurablename"] = "order-completed-consumer";
21
+ })(OrderDurableName || (exports.OrderDurableName = OrderDurableName = {}));
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Enum representing durable names for consumers associated with different payment events.
3
+ */
4
+ export declare enum PaymentDurableName {
5
+ /**
6
+ * The durable name for the consumer handling payment failed events.
7
+ */
8
+ PaymentFailedDurableName = "payment-failed-consumer",
9
+ /**
10
+ * The durable name for the consumer handling payment created events.
11
+ */
12
+ PaymentCreatedDurableName = "payment-created-consumer"
13
+ }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PaymentDurableName = void 0;
4
+ /**
5
+ * Enum representing durable names for consumers associated with different payment events.
6
+ */
7
+ var PaymentDurableName;
8
+ (function (PaymentDurableName) {
9
+ /**
10
+ * The durable name for the consumer handling payment failed events.
11
+ */
12
+ PaymentDurableName["PaymentFailedDurableName"] = "payment-failed-consumer";
13
+ /**
14
+ * The durable name for the consumer handling payment created events.
15
+ */
16
+ PaymentDurableName["PaymentCreatedDurableName"] = "payment-created-consumer";
17
+ })(PaymentDurableName || (exports.PaymentDurableName = PaymentDurableName = {}));
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Re-exports all the types related to durable names from the "durable-names" file.
3
+ *
4
+ * The types include enums and utility types used to define durable names for consumers of different event types.
5
+ */
6
+ export * from "./durable-names";
7
+ /**
8
+ * Re-exports the type related to streams from the "streams" file.
9
+ *
10
+ * The "Streams" type alias represents a JetStream stream that can handle different event types.
11
+ */
12
+ export * from "./streams";
13
+ /**
14
+ * Re-exports the type related to subjects from the "subjects" file.
15
+ *
16
+ * The "Subjects" type alias represents the subjects for different event types.
17
+ */
18
+ export * from "./subjects";
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ // Import and re-export types from other files for easier access.
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
16
+ };
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ /**
19
+ * Re-exports all the types related to durable names from the "durable-names" file.
20
+ *
21
+ * The types include enums and utility types used to define durable names for consumers of different event types.
22
+ */
23
+ __exportStar(require("./durable-names"), exports);
24
+ /**
25
+ * Re-exports the type related to streams from the "streams" file.
26
+ *
27
+ * The "Streams" type alias represents a JetStream stream that can handle different event types.
28
+ */
29
+ __exportStar(require("./streams"), exports);
30
+ /**
31
+ * Re-exports the type related to subjects from the "subjects" file.
32
+ *
33
+ * The "Subjects" type alias represents the subjects for different event types.
34
+ */
35
+ __exportStar(require("./subjects"), exports);
@@ -0,0 +1,8 @@
1
+ import { PaymentStream } from './payment-stream';
2
+ import { OrderStream } from './order-stream';
3
+ /**
4
+ * Type alias representing a JetStream stream that can handle different event types.
5
+ *
6
+ * The `Streams` type can be one of the streams defined in `OrderStream` or `PaymentStream`.
7
+ */
8
+ export type Streams = OrderStream | PaymentStream;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Enum representing the name of the JetStream stream used for handling order events.
3
+ */
4
+ export declare enum OrderStream {
5
+ /**
6
+ * The name of the JetStream stream for order events.
7
+ */
8
+ Order = "ORDER"
9
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OrderStream = void 0;
4
+ /**
5
+ * Enum representing the name of the JetStream stream used for handling order events.
6
+ */
7
+ var OrderStream;
8
+ (function (OrderStream) {
9
+ /**
10
+ * The name of the JetStream stream for order events.
11
+ */
12
+ OrderStream["Order"] = "ORDER";
13
+ })(OrderStream || (exports.OrderStream = OrderStream = {}));
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Enum representing the name of the JetStream stream used for handling payment events.
3
+ */
4
+ export declare enum PaymentStream {
5
+ /**
6
+ * The name of the JetStream stream for payment events.
7
+ */
8
+ Payment = "PAYMENT"
9
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PaymentStream = void 0;
4
+ /**
5
+ * Enum representing the name of the JetStream stream used for handling payment events.
6
+ */
7
+ var PaymentStream;
8
+ (function (PaymentStream) {
9
+ /**
10
+ * The name of the JetStream stream for payment events.
11
+ */
12
+ PaymentStream["Payment"] = "PAYMENT";
13
+ })(PaymentStream || (exports.PaymentStream = PaymentStream = {}));
@@ -0,0 +1,8 @@
1
+ import { PaymentSubjects } from './payment-subjects';
2
+ import { OrderSubjects } from './order-subjects';
3
+ /**
4
+ * Type alias representing the subjects for different event types.
5
+ *
6
+ * The `Subjects` type can be one of the subjects defined in `OrderSubjects` or `PaymentSubjects`.
7
+ */
8
+ export type Subjects = OrderSubjects | PaymentSubjects;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Enum representing the main subjects used for NATS subjects.
3
+ */
4
+ export declare enum MainSubjects {
5
+ ORDER = "order.*",
6
+ PAYMENT = "payment.*",
7
+ ETA = "eta.*"
8
+ }
9
+ /**
10
+ * Type utility to concatenate two strings into a valid subject format.
11
+ *
12
+ * This utility is used to combine a prefix and a postfix with a period (".") in between to form a valid subject format.
13
+ * The resulting subject format must adhere to the pattern `${T}.${U}`.
14
+ */
15
+ type SubjectFormat<T extends string, U extends string> = `${T}.${U}`;
16
+ /**
17
+ * Type utility to extract the string before the first colon (".") in a subject format.
18
+ *
19
+ * This utility extracts the substring before the first colon in a subject format of the pattern `${T}.*`.
20
+ */
21
+ type ExtractStringBeforeColon<T extends string> = T extends `${infer StringBeforeColon}.*` ? StringBeforeColon : never;
22
+ /**
23
+ * Type utility to validate and enforce subject formats based on main subject patterns.
24
+ *
25
+ * The utility ensures that the given subject format adheres to the pattern `${T}.${U}`.
26
+ * The `T` part of the subject format is validated against the prefix of the main subjects defined in the `MainSubjects` enum.
27
+ * The `U` part of the subject format can be any string.
28
+ */
29
+ export type ValidateSubjectFormat<T extends string, U extends string> = U extends MainSubjects ? T extends SubjectFormat<ExtractStringBeforeColon<MainSubjects>, string> ? T : never : never;
30
+ /**
31
+ * Utility function to spread enum values into an array of strings.
32
+ *
33
+ * This utility function takes an object representing an enum and returns an array containing all the string values of the enum.
34
+ *
35
+ * @param {T} enumObject - The object representing the enum.
36
+ * @returns {string[]} - An array containing all the string values of the enum.
37
+ */
38
+ export declare const spreadEnumValues: <T extends Record<string, string>>(enumObject: T) => string[];
39
+ export {};
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.spreadEnumValues = exports.MainSubjects = void 0;
4
+ /**
5
+ * Enum representing the main subjects used for NATS subjects.
6
+ */
7
+ var MainSubjects;
8
+ (function (MainSubjects) {
9
+ MainSubjects["ORDER"] = "order.*";
10
+ MainSubjects["PAYMENT"] = "payment.*";
11
+ MainSubjects["ETA"] = "eta.*";
12
+ })(MainSubjects || (exports.MainSubjects = MainSubjects = {}));
13
+ /**
14
+ * Utility function to spread enum values into an array of strings.
15
+ *
16
+ * This utility function takes an object representing an enum and returns an array containing all the string values of the enum.
17
+ *
18
+ * @param {T} enumObject - The object representing the enum.
19
+ * @returns {string[]} - An array containing all the string values of the enum.
20
+ */
21
+ const spreadEnumValues = (enumObject) => Object.values(enumObject);
22
+ exports.spreadEnumValues = spreadEnumValues;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Enum representing the subjects for different order events.
3
+ */
4
+ export declare enum OrderSubjects {
5
+ /**
6
+ * The subject for order cancelled events.
7
+ */
8
+ OrderCancelled = "order.cancelled",
9
+ /**
10
+ * The subject for order requested events.
11
+ */
12
+ OrderRequested = "order.requested",
13
+ /**
14
+ * The subject for order completed events.
15
+ */
16
+ OrderCompleted = "order.completed"
17
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OrderSubjects = void 0;
4
+ /**
5
+ * Enum representing the subjects for different order events.
6
+ */
7
+ var OrderSubjects;
8
+ (function (OrderSubjects) {
9
+ /**
10
+ * The subject for order cancelled events.
11
+ */
12
+ OrderSubjects["OrderCancelled"] = "order.cancelled";
13
+ /**
14
+ * The subject for order requested events.
15
+ */
16
+ OrderSubjects["OrderRequested"] = "order.requested";
17
+ /**
18
+ * The subject for order completed events.
19
+ */
20
+ OrderSubjects["OrderCompleted"] = "order.completed";
21
+ })(OrderSubjects || (exports.OrderSubjects = OrderSubjects = {}));
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Enum representing the subjects for different payment events.
3
+ */
4
+ export declare enum PaymentSubjects {
5
+ /**
6
+ * The subject for payment created events.
7
+ */
8
+ PaymentCreated = "payment.created",
9
+ /**
10
+ * The subject for payment failed events.
11
+ */
12
+ PaymentFailed = "payment.failed"
13
+ }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PaymentSubjects = void 0;
4
+ /**
5
+ * Enum representing the subjects for different payment events.
6
+ */
7
+ var PaymentSubjects;
8
+ (function (PaymentSubjects) {
9
+ /**
10
+ * The subject for payment created events.
11
+ */
12
+ PaymentSubjects["PaymentCreated"] = "payment.created";
13
+ /**
14
+ * The subject for payment failed events.
15
+ */
16
+ PaymentSubjects["PaymentFailed"] = "payment.failed";
17
+ })(PaymentSubjects || (exports.PaymentSubjects = PaymentSubjects = {}));
@@ -0,0 +1,15 @@
1
+ import { OrderDurableName } from '../../enums/durable-names/order-dname';
2
+ import { OrderStream } from '../../enums/streams/order-stream';
3
+ import { OrderSubjects } from '../../enums/subjects/order-subjects';
4
+ export interface OrderCancelledEvent {
5
+ subject: OrderSubjects.OrderCancelled;
6
+ durableName?: OrderDurableName.OrderCancelledDurablename;
7
+ stream: OrderStream.Order;
8
+ data: {
9
+ id: string;
10
+ version: number;
11
+ status: string;
12
+ userid: string;
13
+ tripid: string;
14
+ };
15
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,15 @@
1
+ import { OrderDurableName } from '../../enums/durable-names/order-dname';
2
+ import { OrderStream } from '../../enums/streams/order-stream';
3
+ import { OrderSubjects } from '../../enums/subjects/order-subjects';
4
+ export interface OrderCompletedEvent {
5
+ subject: OrderSubjects.OrderCompleted;
6
+ durableName?: OrderDurableName.OrderCompletedDurablename;
7
+ stream: OrderStream.Order;
8
+ data: {
9
+ id: string;
10
+ version: number;
11
+ status: string;
12
+ userid: string;
13
+ tripid: string;
14
+ };
15
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,15 @@
1
+ import { OrderDurableName } from '../../enums/durable-names/order-dname';
2
+ import { OrderStream } from '../../enums/streams/order-stream';
3
+ import { OrderSubjects } from '../../enums/subjects/order-subjects';
4
+ export interface OrderRequestedEvent {
5
+ subject: OrderSubjects.OrderRequested;
6
+ durableName?: OrderDurableName.OrderRequestDurablename;
7
+ stream: OrderStream.Order;
8
+ data: {
9
+ id: string;
10
+ version: number;
11
+ status: string;
12
+ userid: string;
13
+ tripid: string;
14
+ };
15
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,6 @@
1
+ import { OrderStream } from '../../enums/streams/order-stream';
2
+ import { MainSubjects } from '../../enums/subjects/main-subjects';
3
+ export interface OrderStreamEvent {
4
+ subject: MainSubjects.ORDER;
5
+ stream: OrderStream.Order;
6
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hastehaul/common",
3
- "version": "1.0.12",
3
+ "version": "1.0.14",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -27,6 +27,7 @@
27
27
  "express": "^4.18.2",
28
28
  "ioredis": "^5.3.2",
29
29
  "jsonwebtoken": "^9.0.1",
30
+ "nats": "^2.15.1",
30
31
  "socket.io": "^4.7.1"
31
32
  }
32
33
  }