@kaushverse/rabbitmq-core 1.0.2 → 1.0.4

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 CHANGED
@@ -12,6 +12,7 @@ interface RabbitConnectionOptions {
12
12
  socketOptions?: Options.Connect;
13
13
  }
14
14
  type ExchangeType = "direct" | "topic" | "fanout" | "headers";
15
+ type LogLevel = "info" | "warn" | "error" | "debug";
15
16
  interface ConsumeOptions {
16
17
  queue: string;
17
18
  exchange: string;
@@ -29,13 +30,34 @@ interface PublishOptions {
29
30
  }
30
31
 
31
32
  declare const rabbitEvents: EventEmitter<[never]>;
32
- declare function connectRabbitMQ(options: RabbitConnectionOptions): Promise<Channel>;
33
33
  declare function getChannel(): Channel;
34
- declare function closeRabbitMQ(): Promise<void>;
35
- declare function isRabbitConnected(): boolean;
36
34
 
37
35
  declare function consumeMessage(options: ConsumeOptions, handler: (data: any, raw: ConsumeMessage) => Promise<void> | void): Promise<void>;
38
36
 
39
37
  declare function publishMessage({ exchange, type, routingKey, message, headers, }: PublishOptions): Promise<void>;
40
38
 
41
- export { type ConsumeOptions, type ExchangeType, type PublishOptions, type RabbitConnectionOptions, type RabbitTLSOptions, closeRabbitMQ, connectRabbitMQ, consumeMessage, getChannel, isRabbitConnected, publishMessage, rabbitEvents };
39
+ declare function bootstrap({ serviceName, rabbit, start, }: {
40
+ serviceName: string;
41
+ rabbit: {
42
+ url: string;
43
+ };
44
+ start: () => void | Promise<void>;
45
+ }): Promise<void>;
46
+
47
+ declare function rabbitHealth(): {
48
+ "\uD83D\uDC07": {
49
+ service: string;
50
+ status: string;
51
+ healthy: boolean;
52
+ };
53
+ };
54
+
55
+ declare function setLoggerService(name: string): void;
56
+ declare const logger: {
57
+ info: (msg: string, meta?: any) => void;
58
+ warn: (msg: string, meta?: any) => void;
59
+ error: (msg: string, meta?: any) => void;
60
+ debug: (msg: string, meta?: any) => void;
61
+ };
62
+
63
+ export { type ConsumeOptions, type ExchangeType, type LogLevel, type PublishOptions, type RabbitConnectionOptions, type RabbitTLSOptions, bootstrap, consumeMessage, getChannel, logger, publishMessage, rabbitEvents, rabbitHealth, setLoggerService };
package/dist/index.js CHANGED
@@ -30,13 +30,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
- closeRabbitMQ: () => closeRabbitMQ,
34
- connectRabbitMQ: () => connectRabbitMQ,
33
+ bootstrap: () => bootstrap,
35
34
  consumeMessage: () => consumeMessage,
36
35
  getChannel: () => getChannel,
37
- isRabbitConnected: () => isRabbitConnected,
36
+ logger: () => logger,
38
37
  publishMessage: () => publishMessage,
39
- rabbitEvents: () => rabbitEvents
38
+ rabbitEvents: () => rabbitEvents,
39
+ rabbitHealth: () => rabbitHealth,
40
+ setLoggerService: () => setLoggerService
40
41
  });
41
42
  module.exports = __toCommonJS(index_exports);
42
43
 
@@ -169,13 +170,78 @@ async function publishMessage({
169
170
  headers
170
171
  });
171
172
  }
173
+
174
+ // src/logger/index.ts
175
+ var serviceName = "rabbitmq-core";
176
+ function setLoggerService(name) {
177
+ serviceName = name;
178
+ }
179
+ function log(level, message, meta) {
180
+ const entry = {
181
+ level,
182
+ service: serviceName,
183
+ message,
184
+ time: (/* @__PURE__ */ new Date()).toISOString(),
185
+ ...meta && { meta }
186
+ };
187
+ console[level === "debug" ? "log" : level](JSON.stringify(entry));
188
+ }
189
+ var logger = {
190
+ info: (msg, meta) => log("info", msg, meta),
191
+ warn: (msg, meta) => log("warn", msg, meta),
192
+ error: (msg, meta) => log("error", msg, meta),
193
+ debug: (msg, meta) => log("debug", msg, meta)
194
+ };
195
+
196
+ // src/bootstrap.ts
197
+ async function bootstrap({
198
+ serviceName: serviceName2,
199
+ rabbit,
200
+ start
201
+ }) {
202
+ try {
203
+ setLoggerService(serviceName2);
204
+ logger.info("\u{1F9E9} Bootstrap started");
205
+ await connectRabbitMQ(rabbit);
206
+ logger.info("\u{1F407} RabbitMQ connected");
207
+ await start();
208
+ logger.info("\u{1F680} Service started successfully");
209
+ const shutdown = async (signal) => {
210
+ logger.warn("\u{1F6D1} Shutdown signal received", { signal });
211
+ await closeRabbitMQ();
212
+ logger.info("\u{1F512} RabbitMQ connection closed");
213
+ logger.info("\u{1F44B} Service stopped gracefully");
214
+ process.exit(0);
215
+ };
216
+ process.on("SIGINT", shutdown);
217
+ process.on("SIGTERM", shutdown);
218
+ } catch (err) {
219
+ logger.error("\u{1F4A5} Startup failed", {
220
+ error: err instanceof Error ? err.message : err
221
+ });
222
+ process.exit(1);
223
+ }
224
+ }
225
+
226
+ // src/health/index.ts
227
+ function rabbitHealth() {
228
+ const connected2 = isRabbitConnected();
229
+ return {
230
+ "\u{1F407}": {
231
+ service: "rabbitmq",
232
+ status: connected2 ? "\u{1F7E2} up" : "\u{1F534} down",
233
+ healthy: connected2
234
+ }
235
+ };
236
+ }
172
237
  // Annotate the CommonJS export names for ESM import in node:
173
238
  0 && (module.exports = {
174
- closeRabbitMQ,
175
- connectRabbitMQ,
239
+ bootstrap,
176
240
  consumeMessage,
177
241
  getChannel,
178
- isRabbitConnected,
242
+ logger,
179
243
  publishMessage,
180
- rabbitEvents
244
+ rabbitEvents,
245
+ rabbitHealth,
246
+ setLoggerService
181
247
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kaushverse/rabbitmq-core",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Reusable RabbitMQ (AMQP / AMQPS) core for Node.js & microservices",
5
5
  "author": "Kaushik (KaushVerse)",
6
6
  "license": "MIT",