@vishalkhandal/common-rabbitmq 1.0.0 → 1.0.2

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.
@@ -0,0 +1,20 @@
1
+ export type RabbitMQConfig = {
2
+ url?: string;
3
+ queues?: string[];
4
+ serviceName?: string;
5
+ };
6
+ declare class RabbitMQClient {
7
+ private config;
8
+ private connection;
9
+ private channel;
10
+ constructor(config?: RabbitMQConfig);
11
+ connect(): Promise<void>;
12
+ publishToQueue(queue: string, message: object): Promise<boolean>;
13
+ consumeFromQueue(queue: string, handler: (message: any) => Promise<void>, options?: {
14
+ noAck?: boolean;
15
+ }): Promise<false | undefined>;
16
+ getChannel(): any;
17
+ isConnected(): boolean;
18
+ close(): Promise<void>;
19
+ }
20
+ export default RabbitMQClient;
package/dist/index.js ADDED
@@ -0,0 +1,131 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ const amqp = __importStar(require("amqplib"));
37
+ class RabbitMQClient {
38
+ constructor(config = {}) {
39
+ this.config = config;
40
+ }
41
+ async connect() {
42
+ try {
43
+ const rabbitMQUrl = this.config.url || "amqp://localhost:5672";
44
+ this.connection = await amqp.connect(rabbitMQUrl);
45
+ this.channel = this.connection.createChannel();
46
+ if (this.config.queues) {
47
+ for (const queue of this.config.queues) {
48
+ await this.channel.assertQueue(queue, { durable: true });
49
+ }
50
+ }
51
+ const serviceName = this.config.serviceName || "Service";
52
+ console.log(`${serviceName}: RabbitMQ connected successfully`);
53
+ }
54
+ catch (error) {
55
+ const serviceName = this.config.serviceName || "Service";
56
+ console.error(`${serviceName}: RabbitMQ connection error:`, error);
57
+ }
58
+ }
59
+ async publishToQueue(queue, message) {
60
+ try {
61
+ if (!this.channel) {
62
+ console.error("RabbitMQ channel not available");
63
+ return false;
64
+ }
65
+ await this.channel.assertQueue(queue, { durable: true });
66
+ this.channel.sendToQueue(queue, Buffer.from(JSON.stringify(message)), {
67
+ persistent: true,
68
+ });
69
+ console.log(`Message published to queue ${queue}:`, message);
70
+ return true;
71
+ }
72
+ catch (error) {
73
+ console.error("Error publishing message to queue");
74
+ return false;
75
+ }
76
+ }
77
+ async consumeFromQueue(queue, handler, options = { noAck: false }) {
78
+ try {
79
+ if (!this.channel) {
80
+ console.error("RabbitMQ channel not available");
81
+ return;
82
+ }
83
+ await this.channel.assertQueue(queue, { durable: true });
84
+ this.channel.consume(queue, async (msg) => {
85
+ var _a;
86
+ if (msg) {
87
+ try {
88
+ const content = JSON.parse(msg.content.toString());
89
+ await handler(content);
90
+ if (!options.noAck) {
91
+ (_a = this.channel) === null || _a === void 0 ? void 0 : _a.ack(msg);
92
+ }
93
+ console.log(`Started consuming from queue: ${queue}`);
94
+ }
95
+ catch (error) {
96
+ console.error("Error consuming message to queue");
97
+ return false;
98
+ }
99
+ }
100
+ });
101
+ }
102
+ catch (error) {
103
+ console.error("Error publishing message to queue");
104
+ return false;
105
+ }
106
+ }
107
+ getChannel() {
108
+ return this.channel;
109
+ }
110
+ isConnected() {
111
+ return this.channel !== null && this.connection !== null;
112
+ }
113
+ async close() {
114
+ try {
115
+ if (this.channel) {
116
+ await this.channel.close();
117
+ this.channel = null;
118
+ }
119
+ if (this.connection) {
120
+ await this.connection.close();
121
+ this.connection = null;
122
+ }
123
+ console.log("RabbitMQ connection closed");
124
+ }
125
+ catch (error) {
126
+ console.error("Error closing RabbitMQ connection:", error);
127
+ }
128
+ }
129
+ }
130
+ exports.default = RabbitMQClient;
131
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,8CAA+B;AAQ/B,MAAM,cAAc;IAIlB,YAAY,SAAyB,EAAE;QACrC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;IACtB,CAAC;IAED,KAAK,CAAC,OAAO;QACX,IAAI,CAAC;YACH,MAAM,WAAW,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,IAAI,uBAAuB,CAAA;YAC9D,IAAI,CAAC,UAAU,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,CAAA;YACjD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,UAAU,CAAC,aAAa,EAAE,CAAA;YAE9C,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;gBACvB,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;oBACvC,MAAM,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,KAAK,EAAE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,EAAE,CAAC;gBACjB,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAA;gBAC1B,IAAI,CAAC,OAAO,GAAG,IAAI,CAAA;YACrB,CAAC;YACD,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;gBACpB,MAAM,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAA;gBAC7B,IAAI,CAAC,UAAU,GAAG,IAAI,CAAA;YACxB,CAAC;YACD,OAAO,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAA;QAC3C,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,oCAAoC,EAAE,KAAK,CAAC,CAAA;QAC5D,CAAC;IACH,CAAC;CACF;AAED,kBAAe,cAAc,CAAA"}
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@vishalkhandal/common-rabbitmq",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Shared RabbitMQ package for microservices",
5
- "main": "index.js",
6
- "types": "index.d.ts",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
7
  "scripts": {
8
8
  "build": "tsc",
9
9
  "dev": "tsc --watch",
@@ -12,7 +12,6 @@
12
12
  "keywords": [],
13
13
  "author": "",
14
14
  "license": "ISC",
15
- "type": "module",
16
15
  "devDependencies": {
17
16
  "@types/amqplib": "^0.10.8",
18
17
  "@types/node": "^25.5.0",
package/tsconfig.json CHANGED
@@ -1,44 +1,18 @@
1
1
  {
2
- // Visit https://aka.ms/tsconfig to read more about this file
3
2
  "compilerOptions": {
4
- // File Layout
5
3
  "rootDir": "./src",
6
4
  "outDir": "./dist",
7
5
 
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
6
+ "module": "CommonJS",
7
+ "target": "ES2019",
17
8
 
18
- // Other Outputs
19
- "sourceMap": true,
20
9
  "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,
10
+ "sourceMap": true,
34
11
 
35
- // Recommended Options
36
12
  "strict": true,
37
- "jsx": "react-jsx",
38
- "verbatimModuleSyntax": true,
39
- "isolatedModules": true,
40
- "noUncheckedSideEffectImports": true,
41
- "moduleDetection": "force",
42
- "skipLibCheck": true,
43
- }
44
- }
13
+ "esModuleInterop": true,
14
+
15
+ "skipLibCheck": true
16
+ },
17
+ "include": ["src"]
18
+ }