@vishalkhandal/common-rabbitmq 1.0.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.
Files changed (3) hide show
  1. package/package.json +24 -0
  2. package/src/index.ts +112 -0
  3. package/tsconfig.json +44 -0
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@vishalkhandal/common-rabbitmq",
3
+ "version": "1.0.0",
4
+ "description": "Shared RabbitMQ package for microservices",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "dev": "tsc --watch",
10
+ "test": "echo \"Error: no test specified\" && exit 1"
11
+ },
12
+ "keywords": [],
13
+ "author": "",
14
+ "license": "ISC",
15
+ "type": "module",
16
+ "devDependencies": {
17
+ "@types/amqplib": "^0.10.8",
18
+ "@types/node": "^25.5.0",
19
+ "typescript": "^6.0.2"
20
+ },
21
+ "dependencies": {
22
+ "amqplib": "^1.0.2"
23
+ }
24
+ }
package/src/index.ts ADDED
@@ -0,0 +1,112 @@
1
+ import * as amqp from "amqplib"
2
+
3
+ export type RabbitMQConfig = {
4
+ url?: string
5
+ queues?: string[]
6
+ serviceName?: string
7
+ }
8
+
9
+ class RabbitMQClient {
10
+ private config: any
11
+ private connection: any
12
+ private channel: any
13
+ constructor(config: RabbitMQConfig = {}) {
14
+ this.config = config
15
+ }
16
+
17
+ async connect() {
18
+ try {
19
+ const rabbitMQUrl = this.config.url || "amqp://localhost:5672"
20
+ this.connection = await amqp.connect(rabbitMQUrl)
21
+ this.channel = this.connection.createChannel()
22
+
23
+ if (this.config.queues) {
24
+ for (const queue of this.config.queues) {
25
+ await this.channel.assertQueue(queue, { durable: true })
26
+ }
27
+ }
28
+ const serviceName = this.config.serviceName || "Service"
29
+ console.log(`${serviceName}: RabbitMQ connected successfully`)
30
+ } catch (error) {
31
+ const serviceName = this.config.serviceName || "Service"
32
+ console.error(`${serviceName}: RabbitMQ connection error:`, error)
33
+ }
34
+ }
35
+
36
+ async publishToQueue(queue: string, message: object) {
37
+ try {
38
+ if (!this.channel) {
39
+ console.error("RabbitMQ channel not available")
40
+ return false
41
+ }
42
+
43
+ await this.channel.assertQueue(queue, { durable: true })
44
+ this.channel.sendToQueue(queue, Buffer.from(JSON.stringify(message)), {
45
+ persistent: true,
46
+ })
47
+ console.log(`Message published to queue ${queue}:`, message)
48
+ return true
49
+ } catch (error) {
50
+ console.error("Error publishing message to queue")
51
+ return false
52
+ }
53
+ }
54
+
55
+ async consumeFromQueue(
56
+ queue: string,
57
+ handler: (message: any) => Promise<void>,
58
+ options: { noAck?: boolean } = { noAck: false }
59
+ ) {
60
+ try {
61
+ if (!this.channel) {
62
+ console.error("RabbitMQ channel not available")
63
+ return
64
+ }
65
+ await this.channel.assertQueue(queue, { durable: true })
66
+ this.channel.consume(queue, async (msg: any) => {
67
+ if (msg) {
68
+ try {
69
+ const content = JSON.parse(msg.content.toString())
70
+ await handler(content)
71
+ if (!options.noAck) {
72
+ this.channel?.ack(msg)
73
+ }
74
+ console.log(`Started consuming from queue: ${queue}`)
75
+ } catch (error) {
76
+ console.error("Error consuming message to queue")
77
+ return false
78
+ }
79
+ }
80
+ })
81
+ } catch (error) {
82
+ console.error("Error publishing message to queue")
83
+ return false
84
+ }
85
+ }
86
+
87
+ getChannel() {
88
+ return this.channel
89
+ }
90
+
91
+ isConnected() {
92
+ return this.channel !== null && this.connection !== null
93
+ }
94
+
95
+ async close(): Promise<void> {
96
+ try {
97
+ if (this.channel) {
98
+ await this.channel.close()
99
+ this.channel = null
100
+ }
101
+ if (this.connection) {
102
+ await this.connection.close()
103
+ this.connection = null
104
+ }
105
+ console.log("RabbitMQ connection closed")
106
+ } catch (error) {
107
+ console.error("Error closing RabbitMQ connection:", error)
108
+ }
109
+ }
110
+ }
111
+
112
+ export default RabbitMQClient
package/tsconfig.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ // Visit https://aka.ms/tsconfig to read more about this file
3
+ "compilerOptions": {
4
+ // File Layout
5
+ "rootDir": "./src",
6
+ "outDir": "./dist",
7
+
8
+ // Environment Settings
9
+ // See also https://aka.ms/tsconfig/module
10
+ "module": "nodenext",
11
+ "target": "esnext",
12
+ "types": [],
13
+ // For nodejs:
14
+ // "lib": ["esnext"],
15
+ // "types": ["node"],
16
+ // and npm install -D @types/node
17
+
18
+ // Other Outputs
19
+ "sourceMap": true,
20
+ "declaration": true,
21
+ "declarationMap": true,
22
+
23
+ // Stricter Typechecking Options
24
+ "noUncheckedIndexedAccess": true,
25
+ "exactOptionalPropertyTypes": true,
26
+
27
+ // Style Options
28
+ // "noImplicitReturns": true,
29
+ // "noImplicitOverride": true,
30
+ // "noUnusedLocals": true,
31
+ // "noUnusedParameters": true,
32
+ // "noFallthroughCasesInSwitch": true,
33
+ // "noPropertyAccessFromIndexSignature": true,
34
+
35
+ // Recommended Options
36
+ "strict": true,
37
+ "jsx": "react-jsx",
38
+ "verbatimModuleSyntax": true,
39
+ "isolatedModules": true,
40
+ "noUncheckedSideEffectImports": true,
41
+ "moduleDetection": "force",
42
+ "skipLibCheck": true,
43
+ }
44
+ }