sb-rabbitmq-provider 1.0.13 → 1.0.16

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.
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Connection event,
3
+ * if all connections are established then ready event will get triggered
4
+ */
5
+ import { Connection as RabbitmqClientConnection, Channel as RabbitMQChannel, Options } from 'amqplib';
6
+ interface rabbitMQConfig {
7
+ protocol?: string | undefined;
8
+ hostname?: string | undefined;
9
+ port?: number | undefined;
10
+ username?: string | undefined;
11
+ password?: string | undefined;
12
+ locale?: string | undefined;
13
+ frameMax?: number | undefined;
14
+ heartbeat?: number | undefined;
15
+ vhost?: string | undefined;
16
+ exchangeName?: string;
17
+ exchangeType: string;
18
+ }
19
+ type MyCallback = (data: any) => any;
20
+ declare class RabbitMqConnection {
21
+ config: rabbitMQConfig;
22
+ exchange: string;
23
+ exchangeType: string;
24
+ connection: any;
25
+ /**
26
+ * constructor for initialising url, exchange name, exchange type
27
+ * @param url rabbitmq url
28
+ * @param exchange rabbitmq exchange name
29
+ * @param exchangeType type of exchange
30
+ */
31
+ constructor(config: rabbitMQConfig);
32
+ /**
33
+ * creates the rabbitmq connection
34
+ * connection must be single, with multiple channel
35
+ * @returns connection object
36
+ */
37
+ createConnection(): Promise<RabbitmqClientConnection>;
38
+ /**
39
+ * single connection can have multiple channel like publishChannel and consumeChannel
40
+ * @returns channel object
41
+ */
42
+ createChannel(): Promise<RabbitMQChannel>;
43
+ /**
44
+ * it will publish the data into a specific queue
45
+ * @param channel channel object
46
+ * @param queueName queue name
47
+ * @param message object
48
+ * @returns
49
+ */
50
+ publish(channel: RabbitMQChannel, queueName: string, message: object): Promise<{
51
+ status: boolean;
52
+ message: string;
53
+ }>;
54
+ /**
55
+ * must be called once
56
+ * it will starting consuming/listening the data from the queue
57
+ * if callback is passed then data will be sent to the callback
58
+ * @param channel channel object
59
+ * @param queueName queue name
60
+ * @param callback function for handling the data
61
+ * @returns
62
+ */
63
+ consume(channel: RabbitMQChannel, queueName: string, onMessage: MyCallback, options: Options.Consume): Promise<{
64
+ status: boolean;
65
+ message: string;
66
+ }>;
67
+ }
68
+ export default RabbitMqConnection;
69
+ export type Connection = RabbitmqClientConnection;
70
+ export type Channel = RabbitMQChannel;
package/dist/index.js ADDED
@@ -0,0 +1,117 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ /**
16
+ * Connection event,
17
+ * if all connections are established then ready event will get triggered
18
+ */
19
+ const amqplib_1 = __importDefault(require("amqplib"));
20
+ class RabbitMqConnection {
21
+ /**
22
+ * constructor for initialising url, exchange name, exchange type
23
+ * @param url rabbitmq url
24
+ * @param exchange rabbitmq exchange name
25
+ * @param exchangeType type of exchange
26
+ */
27
+ constructor(config) {
28
+ this.exchange = 'EXCHANGE';
29
+ this.exchangeType = 'direct';
30
+ if (config.exchangeName && typeof config.exchangeName === 'string') {
31
+ this.exchange = config.exchangeName;
32
+ }
33
+ if (config.exchangeType && typeof config.exchangeType === 'string') {
34
+ this.exchangeType = config.exchangeType;
35
+ }
36
+ this.config = config;
37
+ }
38
+ /**
39
+ * creates the rabbitmq connection
40
+ * connection must be single, with multiple channel
41
+ * @returns connection object
42
+ */
43
+ createConnection() {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ try {
46
+ const connection = yield amqplib_1.default.connect(this.config);
47
+ // console.log('---RabbitMQ Connection Created---')
48
+ this.connection = connection;
49
+ return connection;
50
+ }
51
+ catch (error) {
52
+ throw error;
53
+ }
54
+ });
55
+ }
56
+ /**
57
+ * single connection can have multiple channel like publishChannel and consumeChannel
58
+ * @returns channel object
59
+ */
60
+ createChannel() {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ try {
63
+ const channel = yield this.connection.createChannel();
64
+ yield channel.assertExchange(this.exchange, this.exchangeType);
65
+ return channel;
66
+ }
67
+ catch (error) {
68
+ throw error;
69
+ }
70
+ });
71
+ }
72
+ /**
73
+ * it will publish the data into a specific queue
74
+ * @param channel channel object
75
+ * @param queueName queue name
76
+ * @param message object
77
+ * @returns
78
+ */
79
+ publish(channel, queueName, message) {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ try {
82
+ const bufferMessage = Buffer.from(JSON.stringify(message));
83
+ channel.publish(this.exchange, queueName, bufferMessage);
84
+ return { status: true, message: 'Data sent to Queue' };
85
+ }
86
+ catch (error) {
87
+ throw error;
88
+ }
89
+ });
90
+ }
91
+ /**
92
+ * must be called once
93
+ * it will starting consuming/listening the data from the queue
94
+ * if callback is passed then data will be sent to the callback
95
+ * @param channel channel object
96
+ * @param queueName queue name
97
+ * @param callback function for handling the data
98
+ * @returns
99
+ */
100
+ consume(channel, queueName, onMessage, options) {
101
+ return __awaiter(this, void 0, void 0, function* () {
102
+ try {
103
+ const { queue } = yield channel.assertQueue(queueName, { durable: true, autoDelete: false });
104
+ yield channel.bindQueue(queue, this.exchange, queueName);
105
+ channel.consume(queueName, (message) => {
106
+ // channel.ack(message);
107
+ onMessage(message);
108
+ }, options);
109
+ return { status: true, message: `Started consuming Queue: ${queueName}` };
110
+ }
111
+ catch (error) {
112
+ throw error;
113
+ }
114
+ });
115
+ }
116
+ }
117
+ exports.default = RabbitMqConnection;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sb-rabbitmq-provider",
3
- "version": "1.0.13",
3
+ "version": "1.0.16",
4
4
  "description": "Pub/Sub model for RabbitMq",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",