@autofleet/rabbit 1.0.8 → 1.0.10

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
@@ -8,7 +8,7 @@ declare class RabbitMq {
8
8
  static parseMsg(msg: any): any;
9
9
  constructor();
10
10
  ack: (msg: any) => Promise<void>;
11
- nack: (msg: any, queue: string, deadQueue: string, retries: number) => Promise<void>;
11
+ nack: (queue: string) => (msg: any, retries: number) => Promise<void>;
12
12
  assertChannel(): Promise<Channel>;
13
13
  assertExchange(exchange: string, options?: any): Promise<import("amqplib").Replies.AssertExchange>;
14
14
  assertQueue(queue: string, options?: any): Promise<import("amqplib").Replies.AssertQueue>;
package/dist/index.js CHANGED
@@ -10,7 +10,7 @@ class RabbitMq {
10
10
  await this.channel.ack(msg);
11
11
  }
12
12
  };
13
- this.nack = async (msg, queue, deadQueue, retries) => {
13
+ this.nack = (queue) => async (msg, retries) => {
14
14
  if (this.channel) {
15
15
  await this.channel.ack(msg);
16
16
  if (!msg.content['x-retry-count'] || msg.content['x-retry-count'] <= retries) {
@@ -18,6 +18,8 @@ class RabbitMq {
18
18
  await this.sendToQueue(queue, Buffer.from(JSON.stringify(msg.content)));
19
19
  }
20
20
  else {
21
+ const deadQueue = `${queue}-dead`;
22
+ await this.assertQueue(deadQueue);
21
23
  await this.sendToQueue(deadQueue, Buffer.from(JSON.stringify(msg.content)));
22
24
  }
23
25
  }
@@ -76,7 +78,7 @@ class RabbitMq {
76
78
  async consume(queue, callback) {
77
79
  const channel = await this.assertChannel();
78
80
  await this.assertQueue(queue);
79
- return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack));
81
+ return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
80
82
  }
81
83
  async consumeFromExchange(queue, exchange, callback) {
82
84
  const channel = await this.assertChannel();
@@ -85,7 +87,7 @@ class RabbitMq {
85
87
  this.assertQueue(queue),
86
88
  ]);
87
89
  await channel.bindQueue(queue, exchange, '');
88
- return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack));
90
+ return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
89
91
  }
90
92
  async publish(exchange, content) {
91
93
  const channel = await this.assertChannel();
package/dist/mock.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import 'jest';
2
2
  declare class RabbitMq {
3
3
  ack: jest.Mock<{}>;
4
+ nack: jest.Mock<{}>;
4
5
  assertChannel: jest.Mock<{}>;
5
6
  assertExchange: jest.Mock<{}>;
6
7
  assertQueue: jest.Mock<{}>;
package/dist/mock.js CHANGED
@@ -4,6 +4,7 @@ require("jest");
4
4
  class RabbitMq {
5
5
  constructor() {
6
6
  this.ack = jest.fn();
7
+ this.nack = jest.fn();
7
8
  this.assertChannel = jest.fn();
8
9
  this.assertExchange = jest.fn();
9
10
  this.assertQueue = jest.fn();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -41,13 +41,15 @@ class RabbitMq {
41
41
  }
42
42
  }
43
43
 
44
- public nack = async (msg: any, queue: string, deadQueue: string, retries: number) => {
44
+ public nack = (queue: string) => async (msg: any, retries: number) => {
45
45
  if (this.channel) {
46
46
  await this.channel.ack(msg);
47
47
  if (!msg.content['x-retry-count'] || msg.content['x-retry-count'] <= retries) {
48
48
  msg.content['x-retry-count'] = msg.content['x-retry-count'] ? msg.content['x-retry-count'] + 1 : 1;
49
49
  await this.sendToQueue(queue, Buffer.from(JSON.stringify(msg.content)));
50
50
  } else {
51
+ const deadQueue = `${queue}-dead`;
52
+ await this.assertQueue(deadQueue);
51
53
  await this.sendToQueue(deadQueue, Buffer.from(JSON.stringify(msg.content)));
52
54
  }
53
55
  }
@@ -91,7 +93,7 @@ class RabbitMq {
91
93
  async consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any) {
92
94
  const channel: Channel = await this.assertChannel();
93
95
  await this.assertQueue(queue);
94
- return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack));
96
+ return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
95
97
  }
96
98
 
97
99
  async consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any) {
@@ -101,7 +103,7 @@ class RabbitMq {
101
103
  this.assertQueue(queue),
102
104
  ]);
103
105
  await channel.bindQueue(queue, exchange, '');
104
- return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack));
106
+ return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
105
107
  }
106
108
 
107
109
  async publish(exchange: string, content: any) {
package/src/mock.ts CHANGED
@@ -2,6 +2,7 @@ import 'jest';
2
2
 
3
3
  class RabbitMq {
4
4
  public ack = jest.fn();
5
+ public nack = jest.fn();
5
6
  public assertChannel = jest.fn();
6
7
  public assertExchange = jest.fn();
7
8
  public assertQueue = jest.fn();