@kaushverse/rabbitmq-core 1.1.3 โ 1.1.6
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/README.md +103 -46
- package/dist/index.d.ts +2 -1
- package/dist/index.js +11 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,98 +1,155 @@
|
|
|
1
|
-
# @kaushverse/rabbitmq-core
|
|
1
|
+
# @kaushverse/rabbitmq-core ๐
|
|
2
2
|
|
|
3
|
-
Production-ready RabbitMQ SDK for Node.js microservices
|
|
3
|
+
๐ **Production-ready RabbitMQ SDK for Node.js microservices**
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
`@kaushverse/rabbitmq-core` provides a **clean, opinionated abstraction** over RabbitMQ for Node.js services.
|
|
6
|
+
|
|
7
|
+
It handles **connections, publishing, consuming, health checks, TLS, logging, and graceful shutdown** โ automatically โ so you can focus on business logic, not infra glue.
|
|
6
8
|
|
|
7
9
|
---
|
|
8
10
|
|
|
9
|
-
## Features
|
|
11
|
+
## โจ Features
|
|
10
12
|
|
|
11
|
-
- Single RabbitMQ connection per service
|
|
12
|
-
- Supports `amqp://` and `amqps://`
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
18
|
-
-
|
|
13
|
+
- ๐ **Single RabbitMQ connection per service**
|
|
14
|
+
- ๐ Supports `amqp://` and `amqps://` (TLS)
|
|
15
|
+
- ๐ค Easy message publishing helpers
|
|
16
|
+
- ๐ฅ Consumer utilities with retry control
|
|
17
|
+
- โค๏ธ Built-in RabbitMQ health check
|
|
18
|
+
- ๐งผ Graceful shutdown (`SIGINT`, `SIGTERM`)
|
|
19
|
+
- ๐ฆ Framework-agnostic (Express, Fastify, NestJS)
|
|
20
|
+
- ๐ Pretty logs in dev, structured JSON logs in prod
|
|
19
21
|
|
|
20
22
|
---
|
|
21
23
|
|
|
22
|
-
## Installation
|
|
24
|
+
## ๐ฆ Installation
|
|
23
25
|
|
|
24
26
|
```bash
|
|
25
27
|
npm install @kaushverse/rabbitmq-core
|
|
26
28
|
```
|
|
27
29
|
|
|
28
|
-
## Full Express Service Example
|
|
29
|
-
|
|
30
|
-
Below is a complete example showing how to use `@kaushverse/rabbitmq-core`
|
|
31
|
-
inside an Express-based microservice with health checks and publishing routes.
|
|
30
|
+
## ๐๏ธ Full Express Service Example
|
|
32
31
|
|
|
33
32
|
### Example: `order-service`
|
|
34
33
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
import express from "express";
|
|
38
|
-
import { bootstrap, rabbitHealth } from "@kaushverse/rabbitmq-core";
|
|
34
|
+
This example shows how to bootstrap a production-ready Express service using
|
|
35
|
+
`@kaushverse/rabbitmq-core`.
|
|
39
36
|
|
|
40
|
-
|
|
41
|
-
app.use(express.json());
|
|
37
|
+
---
|
|
42
38
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
});
|
|
39
|
+
### ๐ `src/index.ts`
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import "dotenv/config";
|
|
43
|
+
import { bootstrap } from "@kaushverse/rabbitmq-core";
|
|
44
|
+
import { startServer } from "./server.js";
|
|
50
45
|
|
|
51
|
-
// Bootstrap the service
|
|
52
46
|
bootstrap({
|
|
53
47
|
serviceName: "order-service",
|
|
48
|
+
|
|
54
49
|
rabbit: {
|
|
55
50
|
url: process.env.RABBITMQ_URL!,
|
|
56
51
|
tls: {
|
|
57
52
|
caPath: process.env.RABBITMQ_CA_PATH,
|
|
58
53
|
servername: process.env.RABBITMQ_SERVERNAME,
|
|
59
|
-
rejectUnauthorized:
|
|
54
|
+
rejectUnauthorized: true,
|
|
60
55
|
},
|
|
61
56
|
},
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
console.log("๐ API running on port 3000");
|
|
65
|
-
});
|
|
66
|
-
},
|
|
57
|
+
|
|
58
|
+
start: startServer,
|
|
67
59
|
});
|
|
68
60
|
```
|
|
61
|
+
### ๐ src/server.ts
|
|
69
62
|
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
63
|
+
```ts
|
|
64
|
+
import express from "express";
|
|
65
|
+
import { rabbitHealth } from "@kaushverse/rabbitmq-core";
|
|
66
|
+
import publishRoutes from "./routes/publish.routes.js";
|
|
67
|
+
|
|
68
|
+
export function startServer() {
|
|
69
|
+
const app = express();
|
|
70
|
+
app.use(express.json());
|
|
71
|
+
|
|
72
|
+
app.get("/health", (_, res) => {
|
|
73
|
+
res.json({
|
|
74
|
+
status: "ok",
|
|
75
|
+
rabbit: rabbitHealth(),
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
app.use("/publish", publishRoutes);
|
|
80
|
+
|
|
81
|
+
app.listen(3000, () => {
|
|
82
|
+
console.log("๐ API running on port 3000");
|
|
83
|
+
});
|
|
84
|
+
}
|
|
74
85
|
```
|
|
75
86
|
|
|
76
|
-
## Publishing Messages
|
|
87
|
+
## ๐ค Publishing Messages
|
|
77
88
|
|
|
78
|
-
Use
|
|
79
|
-
The SDK automatically handles channels, serialization, and persistence.
|
|
89
|
+
Use publishMessage to emit events to RabbitMQ.
|
|
80
90
|
|
|
81
|
-
|
|
91
|
+
Channels, serialization, exchange declaration, and message persistence are handled automatically.
|
|
82
92
|
|
|
83
|
-
```
|
|
93
|
+
```ts
|
|
84
94
|
import { publishMessage } from "@kaushverse/rabbitmq-core";
|
|
85
95
|
|
|
86
96
|
await publishMessage({
|
|
87
97
|
exchange: "order.events",
|
|
88
98
|
type: "topic",
|
|
89
99
|
routingKey: "order.created",
|
|
100
|
+
|
|
90
101
|
message: {
|
|
91
102
|
orderId: "ORD-123",
|
|
92
103
|
amount: 499,
|
|
93
104
|
},
|
|
105
|
+
|
|
94
106
|
headers: {
|
|
95
107
|
source: "order-service",
|
|
96
108
|
},
|
|
97
109
|
});
|
|
98
110
|
```
|
|
111
|
+
|
|
112
|
+
## ๐ Environment Variables
|
|
113
|
+
RABBITMQ_URL=amqps://user:password@mq.example.com:5671/vhost
|
|
114
|
+
RABBITMQ_CA_PATH=./certs/ca.pem
|
|
115
|
+
RABBITMQ_SERVERNAME=mq.example.com
|
|
116
|
+
PORT=3000
|
|
117
|
+
|
|
118
|
+
## โค๏ธ Health Check
|
|
119
|
+
|
|
120
|
+
Built-in RabbitMQ health reporting.
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
import { rabbitHealth } from "@kaushverse/rabbitmq-core";
|
|
124
|
+
|
|
125
|
+
app.get("/health", (_, res) => {
|
|
126
|
+
res.json({
|
|
127
|
+
status: "ok",
|
|
128
|
+
rabbit: rabbitHealth(),
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Example Response
|
|
134
|
+
|
|
135
|
+
{
|
|
136
|
+
"status": "ok",
|
|
137
|
+
"rabbit": {
|
|
138
|
+
"connected": true,
|
|
139
|
+
"url": "amqps://mq.example.com"
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
## ๐ก๏ธ Production Best Practices
|
|
144
|
+
|
|
145
|
+
โ
Always use amqps://
|
|
146
|
+
|
|
147
|
+
๐ Keep rejectUnauthorized: true
|
|
148
|
+
|
|
149
|
+
๐ One service = one RabbitMQ connection
|
|
150
|
+
|
|
151
|
+
๐ Handle retries at consumer level
|
|
152
|
+
|
|
153
|
+
๐งพ Use dedicated users & vhosts per service
|
|
154
|
+
|
|
155
|
+
๐ Monitor connections & queues via RabbitMQ Management UI
|
package/dist/index.d.ts
CHANGED
|
@@ -36,9 +36,10 @@ declare function isRabbitConnected(): boolean;
|
|
|
36
36
|
|
|
37
37
|
type BootstrapOptions = {
|
|
38
38
|
serviceName: string;
|
|
39
|
+
start: () => void | Promise<void>;
|
|
39
40
|
rabbit: RabbitConnectionOptions;
|
|
40
41
|
};
|
|
41
|
-
declare function bootstrap({ serviceName, rabbit }: BootstrapOptions): Promise<void>;
|
|
42
|
+
declare function bootstrap({ serviceName, rabbit, start, }: BootstrapOptions): Promise<void>;
|
|
42
43
|
|
|
43
44
|
declare function publishMessage({ exchange, type, routingKey, message, headers, }: PublishOptions): Promise<void>;
|
|
44
45
|
|
package/dist/index.js
CHANGED
|
@@ -164,23 +164,32 @@ var logger = {
|
|
|
164
164
|
};
|
|
165
165
|
|
|
166
166
|
// src/bootstrap.ts
|
|
167
|
-
async function bootstrap({
|
|
167
|
+
async function bootstrap({
|
|
168
|
+
serviceName: serviceName2,
|
|
169
|
+
rabbit,
|
|
170
|
+
start
|
|
171
|
+
}) {
|
|
168
172
|
try {
|
|
169
173
|
setLoggerService(serviceName2);
|
|
170
174
|
setPrettyLogs(process.env.NODE_ENV !== "production");
|
|
171
175
|
logger.info("\u{1F9E9} Bootstrap started");
|
|
172
176
|
await connectRabbitMQ(rabbit);
|
|
173
177
|
logger.info("\u{1F407} RabbitMQ connected");
|
|
178
|
+
await start();
|
|
179
|
+
logger.info(`\u{1F680} ${serviceName2} started successfully`);
|
|
174
180
|
const shutdown = async (signal) => {
|
|
175
181
|
logger.warn("\u{1F6D1} Shutdown signal received", { signal });
|
|
176
182
|
await closeRabbitMQ();
|
|
183
|
+
logger.info("\u{1F512} RabbitMQ connection closed");
|
|
177
184
|
logger.info("\u{1F44B} Service stopped gracefully");
|
|
178
185
|
process.exit(0);
|
|
179
186
|
};
|
|
180
187
|
process.on("SIGINT", shutdown);
|
|
181
188
|
process.on("SIGTERM", shutdown);
|
|
182
189
|
} catch (err) {
|
|
183
|
-
logger.error("\u{1F4A5}
|
|
190
|
+
logger.error("\u{1F4A5} Startup failed", {
|
|
191
|
+
error: err instanceof Error ? err : err
|
|
192
|
+
});
|
|
184
193
|
process.exit(1);
|
|
185
194
|
}
|
|
186
195
|
}
|