@autofleet/rabbit 1.2.1 → 1.3.0

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
@@ -1,22 +1,18 @@
1
1
  /// <reference types="node" />
2
2
  import { AmqpConnectionManager, ChannelWrapper } from 'amqp-connection-manager';
3
3
  import { EventEmitter } from 'events';
4
- interface ExchangesCache {
5
- [key: string]: any;
6
- }
7
4
  declare class RabbitMq {
8
5
  static parseMsg(msg: any): any;
9
6
  channel: ChannelWrapper | null;
10
7
  connection: AmqpConnectionManager | null;
11
8
  em: EventEmitter;
12
9
  creatingConnection: Boolean;
13
- exchanges: ExchangesCache;
14
10
  constructor();
15
11
  ack: (msg: any) => Promise<void>;
16
12
  nack: (queue: string) => (msg: any, retries: number) => Promise<void>;
17
13
  getConnection(): Promise<AmqpConnectionManager>;
18
14
  assertChannel(): Promise<ChannelWrapper>;
19
- assertExchange(exchangeName: string, options?: any): Promise<any>;
15
+ assertExchange(exchange: string, options?: any): Promise<void>;
20
16
  assertQueue(queue: string, options?: any): Promise<void>;
21
17
  consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): Promise<void>;
22
18
  consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): Promise<void>;
package/dist/index.js CHANGED
@@ -28,7 +28,6 @@ class RabbitMq {
28
28
  this.channel = null;
29
29
  this.connection = null;
30
30
  this.creatingConnection = false;
31
- this.exchanges = {};
32
31
  }
33
32
  static parseMsg(msg) {
34
33
  let { content } = msg;
@@ -74,23 +73,16 @@ class RabbitMq {
74
73
  }
75
74
  });
76
75
  }
77
- async assertExchange(exchangeName, options) {
76
+ async assertExchange(exchange, options) {
78
77
  const channel = await this.assertChannel();
79
- if (this.exchanges[exchangeName]) {
80
- return this.exchanges[exchangeName];
81
- }
82
- return channel.addSetup(async (c) => {
83
- const exchange = await c.assertExchange(exchangeName, 'fanout');
84
- this.exchanges[exchangeName] = exchange;
85
- return exchange;
86
- });
78
+ return channel.addSetup((c) => c.assertExchange(exchange, 'fanout'));
87
79
  }
88
80
  async assertQueue(queue, options) {
89
81
  const channel = await this.assertChannel();
90
82
  return channel.addSetup((c) => c.assertQueue(queue));
91
83
  }
92
84
  async consume(queue, callback, options) {
93
- const { limit } = options ? options : 1;
85
+ const { limit } = (options && options.limit) ? options : { limit: 1 };
94
86
  const channel = await this.assertChannel();
95
87
  await this.assertQueue(queue);
96
88
  return channel.addSetup(async (c) => {
@@ -101,7 +93,7 @@ class RabbitMq {
101
93
  });
102
94
  }
103
95
  async consumeFromExchange(queue, exchange, callback, options) {
104
- const { limit } = options ? options : 1;
96
+ const { limit } = (options && options.limit) ? options : { limit: 1 };
105
97
  const channel = await this.assertChannel();
106
98
  await Promise.all([
107
99
  this.assertExchange(exchange),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -2,10 +2,6 @@ import { ConfirmChannel } from 'amqplib';
2
2
  import { connect, AmqpConnectionManager, ChannelWrapper } from 'amqp-connection-manager';
3
3
  import { EventEmitter } from 'events';
4
4
 
5
- interface ExchangesCache {
6
- [key: string]: any
7
- }
8
-
9
5
  const connectionCreatedConst = 'connectionCreated';
10
6
  class RabbitMq {
11
7
  static parseMsg(msg: any) {
@@ -26,14 +22,12 @@ class RabbitMq {
26
22
  connection: AmqpConnectionManager | null;
27
23
  em: EventEmitter;
28
24
  creatingConnection: Boolean;
29
- exchanges: ExchangesCache;
30
25
 
31
26
  constructor() {
32
27
  this.em = new EventEmitter;
33
28
  this.channel = null;
34
29
  this.connection = null;
35
30
  this.creatingConnection = false;
36
- this.exchanges = {};
37
31
  }
38
32
 
39
33
  public ack = async (msg: any) => {
@@ -94,16 +88,9 @@ class RabbitMq {
94
88
  })
95
89
  }
96
90
 
97
- async assertExchange(exchangeName: string, options?: any) {
91
+ async assertExchange(exchange: string, options?: any) {
98
92
  const channel: ChannelWrapper = await this.assertChannel();
99
- if(this.exchanges[exchangeName]) {
100
- return this.exchanges[exchangeName]
101
- }
102
- return channel.addSetup(async (c: ConfirmChannel) => {
103
- const exchange = await c.assertExchange(exchangeName, 'fanout')
104
- this.exchanges[exchangeName] = exchange;
105
- return exchange;
106
- });
93
+ return channel.addSetup((c: ConfirmChannel) => c.assertExchange(exchange, 'fanout'));
107
94
  }
108
95
 
109
96
  async assertQueue(queue: string, options?: any) {
@@ -112,7 +99,7 @@ class RabbitMq {
112
99
  }
113
100
 
114
101
  async consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any) {
115
- const { limit } = options ? options : 1;
102
+ const { limit } = (options && options.limit) ? options : { limit: 1 };
116
103
  const channel: ChannelWrapper = await this.assertChannel();
117
104
  await this.assertQueue(queue);
118
105
  return channel.addSetup(async (c: ConfirmChannel) => {
@@ -124,7 +111,7 @@ class RabbitMq {
124
111
  }
125
112
 
126
113
  async consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any) {
127
- const { limit } = options ? options : 1;
114
+ const { limit } = (options && options.limit) ? options : { limit: 1 };
128
115
  const channel: ChannelWrapper = await this.assertChannel();
129
116
  await Promise.all([
130
117
  this.assertExchange(exchange),