@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 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
- This package provides a clean and opinionated way to **connect**, **publish**, **consume**, and **monitor RabbitMQ**, while handling **TLS**, **logging**, **health checks**, and **graceful shutdown** automatically.
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
- - Message publishing and consuming helpers
14
- - Built-in bootstrap for service lifecycle
15
- - Structured logger (pretty logs in dev, JSON in prod)
16
- - Health check helper for monitoring
17
- - Graceful shutdown (SIGINT / SIGTERM)
18
- - Framework agnostic (Express, Fastify, NestJS)
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
- ```ts
36
- import "dotenv/config";
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
- const app = express();
41
- app.use(express.json());
37
+ ---
42
38
 
43
- // Health check endpoint
44
- app.get("/health", (_, res) => {
45
- res.json({
46
- status: "ok",
47
- ...rabbitHealth(),
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: false, // set true in production
54
+ rejectUnauthorized: true,
60
55
  },
61
56
  },
62
- start: () => {
63
- app.listen(3000, () => {
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
- ```js
71
- RABBITMQ_URL=amqps://user:password@mq.example.com:5671/vhost
72
- RABBITMQ_CA_PATH=./certs/ca.pem
73
- RABBITMQ_SERVERNAME=mq.example.com
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 `publishMessage` to publish events to RabbitMQ.
79
- The SDK automatically handles channels, serialization, and persistence.
89
+ Use publishMessage to emit events to RabbitMQ.
80
90
 
81
- ### Import
91
+ Channels, serialization, exchange declaration, and message persistence are handled automatically.
82
92
 
83
- ```js
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({ serviceName: serviceName2, rabbit }) {
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} Bootstrap failed", { err });
190
+ logger.error("\u{1F4A5} Startup failed", {
191
+ error: err instanceof Error ? err : err
192
+ });
184
193
  process.exit(1);
185
194
  }
186
195
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kaushverse/rabbitmq-core",
3
- "version": "1.1.3",
3
+ "version": "1.1.6",
4
4
  "description": "Reusable RabbitMQ (AMQP / AMQPS) core for Node.js & microservices",
5
5
  "author": "Kaushik (KaushVerse)",
6
6
  "license": "MIT",