@modernlock/common 1.0.43 → 1.0.44
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.
|
@@ -8,9 +8,15 @@ interface Event {
|
|
|
8
8
|
export declare abstract class Producer<T extends Event> {
|
|
9
9
|
abstract exchange: Exchanges;
|
|
10
10
|
abstract exchangeType: ExchangeTypes;
|
|
11
|
-
abstract routingKey: RoutingKeys;
|
|
11
|
+
abstract routingKey: RoutingKeys | string;
|
|
12
12
|
private channel;
|
|
13
13
|
constructor(channel: amqp.Channel);
|
|
14
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Publishes a message to the exchange.
|
|
16
|
+
* @param data The payload of the message.
|
|
17
|
+
* @param dynamicRoutingKey (Optional) A routing key to use for this specific message,
|
|
18
|
+
* overriding the class's default. Ideal for topic exchanges.
|
|
19
|
+
*/
|
|
20
|
+
publish(data: T["data"], dynamicRoutingKey?: string): Promise<void>;
|
|
15
21
|
}
|
|
16
22
|
export {};
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// import * as amqp from 'amqplib';
|
|
3
|
+
// import { ExchangeTypes } from './exchange-types';
|
|
4
|
+
// import { Exchanges } from './exchanges';
|
|
5
|
+
// import { RoutingKeys } from "./routing-keys";
|
|
2
6
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
7
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
8
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -15,12 +19,21 @@ class Producer {
|
|
|
15
19
|
this.channel = channel;
|
|
16
20
|
}
|
|
17
21
|
;
|
|
18
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Publishes a message to the exchange.
|
|
24
|
+
* @param data The payload of the message.
|
|
25
|
+
* @param dynamicRoutingKey (Optional) A routing key to use for this specific message,
|
|
26
|
+
* overriding the class's default. Ideal for topic exchanges.
|
|
27
|
+
*/
|
|
28
|
+
publish(data, dynamicRoutingKey) {
|
|
19
29
|
return __awaiter(this, void 0, void 0, function* () {
|
|
20
30
|
try {
|
|
31
|
+
// MODIFICATION 2: Use the dynamic key if it exists, otherwise fall back to the default.
|
|
32
|
+
const finalRoutingKey = dynamicRoutingKey || this.routingKey;
|
|
21
33
|
yield this.channel.assertExchange(this.exchange, this.exchangeType, { durable: true });
|
|
22
|
-
|
|
23
|
-
|
|
34
|
+
// MODIFICATION 3: Use the finalRoutingKey for publishing.
|
|
35
|
+
yield this.channel.publish(this.exchange, finalRoutingKey, Buffer.from(JSON.stringify(data)));
|
|
36
|
+
console.log(`Message published to exchange '${this.exchange}' with key '${finalRoutingKey}':`, data);
|
|
24
37
|
}
|
|
25
38
|
catch (error) {
|
|
26
39
|
console.error('Failed to publish data:', error);
|