sb-rabbitmq-provider 1.0.21 → 1.0.25

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/dist/index.d.ts CHANGED
@@ -15,13 +15,16 @@ interface rabbitMQConfig {
15
15
  vhost?: string | undefined;
16
16
  exchangeName?: string;
17
17
  exchangeType: string;
18
+ dropMessageOnChannelFailure?: boolean;
18
19
  }
19
20
  type MyCallback = (data: any) => any;
20
21
  declare class RabbitMqConnection {
21
22
  config: rabbitMQConfig;
22
23
  exchange: string;
23
24
  exchangeType: string;
25
+ dropMessageOnChannelFailure: boolean;
24
26
  connection: any;
27
+ channel: any;
25
28
  /**
26
29
  * constructor for initialising url, exchange name, exchange type
27
30
  * @param url rabbitmq url
@@ -38,6 +41,7 @@ declare class RabbitMqConnection {
38
41
  /**
39
42
  * single connection can have multiple channel like publishChannel and consumeChannel
40
43
  * @returns channel object
44
+ * No need to use this method, as channel is created in createConnection method.
41
45
  */
42
46
  createChannel(): Promise<RabbitMQChannel>;
43
47
  /**
@@ -60,7 +64,10 @@ declare class RabbitMqConnection {
60
64
  * @param callback function for handling the data
61
65
  * @returns
62
66
  */
63
- consume(channel: RabbitMQChannel, queueName: string, onMessage: MyCallback, options: Options.Consume): Promise<{
67
+ consume(channel: RabbitMQChannel, queueName: string, onMessage: MyCallback, options: Options.Consume, queueOptions?: {
68
+ durable: boolean;
69
+ autoDelete: boolean;
70
+ }): Promise<{
64
71
  status: boolean;
65
72
  message: string;
66
73
  }>;
package/dist/index.js CHANGED
@@ -27,12 +27,16 @@ class RabbitMqConnection {
27
27
  constructor(config) {
28
28
  this.exchange = 'EXCHANGE';
29
29
  this.exchangeType = 'direct';
30
+ this.dropMessageOnChannelFailure = false;
30
31
  if (config.exchangeName && typeof config.exchangeName === 'string') {
31
32
  this.exchange = config.exchangeName;
32
33
  }
33
34
  if (config.exchangeType && typeof config.exchangeType === 'string') {
34
35
  this.exchangeType = config.exchangeType;
35
36
  }
37
+ if (config.dropMessageOnChannelFailure) {
38
+ this.dropMessageOnChannelFailure = true;
39
+ }
36
40
  this.config = config;
37
41
  }
38
42
  /**
@@ -44,6 +48,11 @@ class RabbitMqConnection {
44
48
  return __awaiter(this, void 0, void 0, function* () {
45
49
  try {
46
50
  const connection = yield amqplib_1.default.connect(this.config);
51
+ if (this.exchange && this.exchangeType) {
52
+ const channel = yield this.connection.createChannel();
53
+ yield channel.assertExchange(this.exchange, this.exchangeType);
54
+ this.channel = channel;
55
+ }
47
56
  // console.log('---RabbitMQ Connection Created---')
48
57
  this.connection = connection;
49
58
  return connection;
@@ -56,6 +65,7 @@ class RabbitMqConnection {
56
65
  /**
57
66
  * single connection can have multiple channel like publishChannel and consumeChannel
58
67
  * @returns channel object
68
+ * No need to use this method, as channel is created in createConnection method.
59
69
  */
60
70
  createChannel() {
61
71
  return __awaiter(this, void 0, void 0, function* () {
@@ -97,14 +107,25 @@ class RabbitMqConnection {
97
107
  * @param callback function for handling the data
98
108
  * @returns
99
109
  */
100
- consume(channel, queueName, onMessage, options) {
110
+ consume(channel, queueName, onMessage, options, queueOptions = { durable: true, autoDelete: false }) {
101
111
  return __awaiter(this, void 0, void 0, function* () {
102
112
  try {
103
- const { queue } = yield channel.assertQueue(queueName, { durable: true, autoDelete: false });
113
+ const { queue } = yield channel.assertQueue(queueName, queueOptions);
104
114
  yield channel.bindQueue(queue, this.exchange, queueName);
105
115
  channel.consume(queueName, (message) => {
106
- // channel.ack(message);
107
- onMessage(message);
116
+ try {
117
+ // channel.ack(message);
118
+ onMessage(message);
119
+ }
120
+ catch (e) {
121
+ console.log('Rabbitmq consume catch block ', e);
122
+ if (message !== null) {
123
+ if (this.dropMessageOnChannelFailure) {
124
+ return channel.nack(message, false, false);
125
+ }
126
+ return channel.nack(message);
127
+ }
128
+ }
108
129
  }, options);
109
130
  return { status: true, message: `Started consuming Queue: ${queueName}` };
110
131
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sb-rabbitmq-provider",
3
- "version": "1.0.21",
3
+ "version": "1.0.25",
4
4
  "description": "Pub/Sub model for RabbitMq",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",