@kaushverse/rabbitmq-core 1.0.4 → 1.0.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 ADDED
@@ -0,0 +1,98 @@
1
+ # @kaushverse/rabbitmq-core
2
+
3
+ Production-ready RabbitMQ SDK for Node.js microservices.
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.
6
+
7
+ ---
8
+
9
+ ## Features
10
+
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)
19
+
20
+ ---
21
+
22
+ ## Installation
23
+
24
+ ```bash
25
+ npm install @kaushverse/rabbitmq-core
26
+ ```
27
+
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.
32
+
33
+ ### Example: `order-service`
34
+
35
+ ```ts
36
+ import "dotenv/config";
37
+ import express from "express";
38
+ import { bootstrap, rabbitHealth } from "@kaushverse/rabbitmq-core";
39
+
40
+ const app = express();
41
+ app.use(express.json());
42
+
43
+ // Health check endpoint
44
+ app.get("/health", (_, res) => {
45
+ res.json({
46
+ status: "ok",
47
+ ...rabbitHealth(),
48
+ });
49
+ });
50
+
51
+ // Bootstrap the service
52
+ bootstrap({
53
+ serviceName: "order-service",
54
+ rabbit: {
55
+ url: process.env.RABBITMQ_URL!,
56
+ tls: {
57
+ caPath: process.env.RABBITMQ_CA_PATH,
58
+ servername: process.env.RABBITMQ_SERVERNAME,
59
+ rejectUnauthorized: false, // set true in production
60
+ },
61
+ },
62
+ start: () => {
63
+ app.listen(3000, () => {
64
+ console.log("🚀 API running on port 3000");
65
+ });
66
+ },
67
+ });
68
+ ```
69
+
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
74
+ ```
75
+
76
+ ## Publishing Messages
77
+
78
+ Use `publishMessage` to publish events to RabbitMQ.
79
+ The SDK automatically handles channels, serialization, and persistence.
80
+
81
+ ### Import
82
+
83
+ ```js
84
+ import { publishMessage } from "@kaushverse/rabbitmq-core";
85
+
86
+ await publishMessage({
87
+ exchange: "order.events",
88
+ type: "topic",
89
+ routingKey: "order.created",
90
+ message: {
91
+ orderId: "ORD-123",
92
+ amount: 499,
93
+ },
94
+ headers: {
95
+ source: "order-service",
96
+ },
97
+ });
98
+ ```
package/dist/index.d.ts CHANGED
@@ -36,13 +36,15 @@ declare function consumeMessage(options: ConsumeOptions, handler: (data: any, ra
36
36
 
37
37
  declare function publishMessage({ exchange, type, routingKey, message, headers, }: PublishOptions): Promise<void>;
38
38
 
39
- declare function bootstrap({ serviceName, rabbit, start, }: {
39
+ type BootstrapOptions = {
40
40
  serviceName: string;
41
+ start: () => void | Promise<void>;
41
42
  rabbit: {
42
43
  url: string;
44
+ tls?: RabbitTLSOptions;
43
45
  };
44
- start: () => void | Promise<void>;
45
- }): Promise<void>;
46
+ };
47
+ declare function bootstrap({ serviceName, rabbit, start, }: BootstrapOptions): Promise<void>;
46
48
 
47
49
  declare function rabbitHealth(): {
48
50
  "\uD83D\uDC07": {
@@ -53,6 +55,7 @@ declare function rabbitHealth(): {
53
55
  };
54
56
 
55
57
  declare function setLoggerService(name: string): void;
58
+ declare function setPrettyLogs(enabled: boolean): void;
56
59
  declare const logger: {
57
60
  info: (msg: string, meta?: any) => void;
58
61
  warn: (msg: string, meta?: any) => void;
@@ -60,4 +63,4 @@ declare const logger: {
60
63
  debug: (msg: string, meta?: any) => void;
61
64
  };
62
65
 
63
- export { type ConsumeOptions, type ExchangeType, type LogLevel, type PublishOptions, type RabbitConnectionOptions, type RabbitTLSOptions, bootstrap, consumeMessage, getChannel, logger, publishMessage, rabbitEvents, rabbitHealth, setLoggerService };
66
+ export { type ConsumeOptions, type ExchangeType, type LogLevel, type PublishOptions, type RabbitConnectionOptions, type RabbitTLSOptions, bootstrap, consumeMessage, getChannel, logger, publishMessage, rabbitEvents, rabbitHealth, setLoggerService, setPrettyLogs };
package/dist/index.js CHANGED
@@ -37,7 +37,8 @@ __export(index_exports, {
37
37
  publishMessage: () => publishMessage,
38
38
  rabbitEvents: () => rabbitEvents,
39
39
  rabbitHealth: () => rabbitHealth,
40
- setLoggerService: () => setLoggerService
40
+ setLoggerService: () => setLoggerService,
41
+ setPrettyLogs: () => setPrettyLogs
41
42
  });
42
43
  module.exports = __toCommonJS(index_exports);
43
44
 
@@ -172,11 +173,23 @@ async function publishMessage({
172
173
  }
173
174
 
174
175
  // src/logger/index.ts
175
- var serviceName = "rabbitmq-core";
176
+ var serviceName = "service";
177
+ var prettyLogs = true;
176
178
  function setLoggerService(name) {
177
179
  serviceName = name;
178
180
  }
181
+ function setPrettyLogs(enabled) {
182
+ prettyLogs = enabled;
183
+ }
179
184
  function log(level, message, meta) {
185
+ if (prettyLogs) {
186
+ if (meta) {
187
+ console[level](`${message}`, meta);
188
+ } else {
189
+ console[level](`${message}`);
190
+ }
191
+ return;
192
+ }
180
193
  const entry = {
181
194
  level,
182
195
  service: serviceName,
@@ -201,11 +214,12 @@ async function bootstrap({
201
214
  }) {
202
215
  try {
203
216
  setLoggerService(serviceName2);
217
+ setPrettyLogs(process.env.NODE_ENV !== "production");
204
218
  logger.info("\u{1F9E9} Bootstrap started");
205
219
  await connectRabbitMQ(rabbit);
206
220
  logger.info("\u{1F407} RabbitMQ connected");
207
221
  await start();
208
- logger.info("\u{1F680} Service started successfully");
222
+ logger.info(`\u{1F680} ${serviceName2} started successfully`);
209
223
  const shutdown = async (signal) => {
210
224
  logger.warn("\u{1F6D1} Shutdown signal received", { signal });
211
225
  await closeRabbitMQ();
@@ -243,5 +257,6 @@ function rabbitHealth() {
243
257
  publishMessage,
244
258
  rabbitEvents,
245
259
  rabbitHealth,
246
- setLoggerService
260
+ setLoggerService,
261
+ setPrettyLogs
247
262
  });
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@kaushverse/rabbitmq-core",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "Reusable RabbitMQ (AMQP / AMQPS) core for Node.js & microservices",
5
5
  "author": "Kaushik (KaushVerse)",
6
6
  "license": "MIT",
7
7
  "main": "dist/index.js",
8
8
  "types": "dist/index.d.ts",
9
9
  "files": [
10
- "dist"
10
+ "dist",
11
+ "README.md"
11
12
  ],
12
13
  "scripts": {
13
14
  "build": "tsup src/index.ts --dts",