sb-rabbitmq-provider 1.0.13 → 1.0.14
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 +57 -0
- package/dist/index.js +117 -0
- package/package.json +1 -1
package/dist/index.d.ts
ADDED
@@ -0,0 +1,57 @@
|
|
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
|
+
type MyCallback = (data: any) => any;
|
7
|
+
declare class RabbitMqConnection {
|
8
|
+
url: string;
|
9
|
+
exchange: string;
|
10
|
+
exchangeType: string;
|
11
|
+
connection: any;
|
12
|
+
/**
|
13
|
+
* constructor for initialising url, exchange name, exchange type
|
14
|
+
* @param url rabbitmq url
|
15
|
+
* @param exchange rabbitmq exchange name
|
16
|
+
* @param exchangeType type of exchange
|
17
|
+
*/
|
18
|
+
constructor(url?: string, exchange?: string, exchangeType?: string);
|
19
|
+
/**
|
20
|
+
* creates the rabbitmq connection
|
21
|
+
* connection must be single, with multiple channel
|
22
|
+
* @returns connection object
|
23
|
+
*/
|
24
|
+
createConnection(): Promise<RabbitmqClientConnection>;
|
25
|
+
/**
|
26
|
+
* single connection can have multiple channel like publishChannel and consumeChannel
|
27
|
+
* @returns channel object
|
28
|
+
*/
|
29
|
+
createChannel(): Promise<RabbitMQChannel>;
|
30
|
+
/**
|
31
|
+
* it will publish the data into a specific queue
|
32
|
+
* @param channel channel object
|
33
|
+
* @param queueName queue name
|
34
|
+
* @param message object
|
35
|
+
* @returns
|
36
|
+
*/
|
37
|
+
publish(channel: RabbitMQChannel, queueName: string, message: object): Promise<{
|
38
|
+
status: boolean;
|
39
|
+
message: string;
|
40
|
+
}>;
|
41
|
+
/**
|
42
|
+
* must be called once
|
43
|
+
* it will starting consuming/listening the data from the queue
|
44
|
+
* if callback is passed then data will be sent to the callback
|
45
|
+
* @param channel channel object
|
46
|
+
* @param queueName queue name
|
47
|
+
* @param callback function for handling the data
|
48
|
+
* @returns
|
49
|
+
*/
|
50
|
+
consume(channel: RabbitMQChannel, queueName: string, onMessage: MyCallback, options: Options.Consume): Promise<{
|
51
|
+
status: boolean;
|
52
|
+
message: string;
|
53
|
+
}>;
|
54
|
+
}
|
55
|
+
export default RabbitMqConnection;
|
56
|
+
export type Connection = RabbitmqClientConnection;
|
57
|
+
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(url = 'amqp://127.0.0.1:5672', exchange, exchangeType) {
|
28
|
+
this.exchange = 'EXCHANGE';
|
29
|
+
this.exchangeType = 'direct';
|
30
|
+
this.url = url;
|
31
|
+
if (exchange && typeof exchange === 'string') {
|
32
|
+
this.exchange = exchange;
|
33
|
+
}
|
34
|
+
if (exchangeType && typeof exchangeType === 'string') {
|
35
|
+
this.exchangeType = exchangeType;
|
36
|
+
}
|
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.url);
|
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;
|