@tim_madule/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,21 @@
1
+ type RabbitMQParameters = {
2
+ url?: string;
3
+ serviceName?: string;
4
+ queues?: string[];
5
+ };
6
+ declare class RabbitMQ {
7
+ private config;
8
+ private connection;
9
+ private channel;
10
+ constructor(config?: RabbitMQParameters);
11
+ connect(): Promise<void>;
12
+ sendToQueue(queue: string, message: any): Promise<boolean>;
13
+ consumeFromQueue(queue: string, callback: (message: any) => Promise<void>, options?: {
14
+ noAck?: boolean;
15
+ }): Promise<boolean>;
16
+ getChannel(): any;
17
+ isConnected(): boolean;
18
+ close(): Promise<void>;
19
+ }
20
+ export default RabbitMQ;
21
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAEA,KAAK,kBAAkB,GAAG;IACtB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;CACrB,CAAC;AAEF,cAAM,QAAQ;IAGE,OAAO,CAAC,MAAM;IAF1B,OAAO,CAAC,UAAU,CAAM;IACxB,OAAO,CAAC,OAAO,CAAM;gBACD,MAAM,GAAE,kBAAuB;IAE7C,OAAO;IAqBP,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG;IAqBvC,gBAAgB,CAClB,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,OAAO,CAAC,IAAI,CAAC,EACzC,OAAO,GAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAqB;IA6BnD,UAAU;IAIV,WAAW;IAIL,KAAK;CAgBd;AAED,eAAe,QAAQ,CAAC"}
@@ -1,114 +1,89 @@
1
- import amqp from 'amqplib';
2
-
3
- type RabbitMQParameters = {
4
- url?: string;
5
- serviceName?: string;
6
- queues?: string[];
7
- };
8
-
9
- class RabbitMQ {
10
- private connection: any; // Placeholder for the actual connection type
11
- private channel: any; // Placeholder for the actual channel type
12
- constructor(private config: RabbitMQParameters = {}) {}
13
-
14
- async connect() {
15
- const serviceName = this.config.serviceName || 'UnknownService';
16
- try {
17
- this.connection = await amqp.connect(
18
- this.config.url || 'amqp://localhost:5672',
19
- );
20
- this.channel = await this.connection.createChannel();
21
- if (this.config.queues) {
22
- for (const queue of this.config.queues) {
23
- await this.channel.assertQueue(queue, { durable: true });
24
- }
25
- }
26
- console.log(`Connected to RabbitMQ for service: ${serviceName}`);
27
- } catch (error) {
28
- console.error(
29
- `Error connecting to RabbitMQ for service ${serviceName}:`,
30
- error,
31
- );
32
- }
33
- }
34
-
35
- async sendToQueue(queue: string, message: any) {
36
- try {
37
- if (!this.channel) {
38
- console.error(
39
- 'Channel is not initialized. Call connect() first.',
40
- );
41
- return false;
42
- }
43
- await this.channel.sendToQueue(
44
- queue,
45
- Buffer.from(JSON.stringify(message)),
46
- { persistent: true },
47
- );
48
- console.log(`Message sent to queue ${queue}:`, message);
49
- return true;
50
- } catch (error) {
51
- console.error('Error sending message to queue:', error);
52
- return false;
53
- }
54
- }
55
-
56
- async consumeFromQueue(
57
- queue: string,
58
- callback: (message: any) => Promise<void>,
59
- options: { noAck?: boolean } = { noAck: false },
60
- ) {
61
- try {
62
- if (!this.channel) {
63
- console.error(
64
- 'Channel is not initialized. Call connect() first.',
65
- );
66
- return false;
67
- }
68
- await this.channel.consume(queue, async (message: any) => {
69
- if (message) {
70
- try {
71
- await callback(JSON.parse(message.content.toString()));
72
- if (!options.noAck) {
73
- await this.channel.ack(message);
74
- }
75
- } catch (error) {
76
- console.error('Error processing message:', error);
77
- }
78
- }
79
- });
80
- console.log(`Consuming from queue ${queue}`);
81
- return true;
82
- } catch (error) {
83
- console.error('Error consuming from queue:', error);
84
- return false;
85
- }
86
- }
87
-
88
- getChannel() {
89
- return this.channel;
90
- }
91
-
92
- isConnected() {
93
- return !!this.connection && !!this.channel;
94
- }
95
-
96
- async close() {
97
- try {
98
- if (this.channel) {
99
- await this.channel.close();
100
- this.channel = null;
101
- console.log('RabbitMQ channel closed.');
102
- }
103
- if (this.connection) {
104
- await this.connection.close();
105
- this.connection = null;
106
- console.log('RabbitMQ connection closed.');
107
- }
108
- } catch (error) {
109
- console.error('Error closing RabbitMQ connection:', error);
110
- }
111
- }
112
- }
113
-
114
- export default RabbitMQ;
1
+ import amqp from 'amqplib';
2
+ class RabbitMQ {
3
+ constructor(config = {}) {
4
+ this.config = config;
5
+ }
6
+ async connect() {
7
+ const serviceName = this.config.serviceName || 'UnknownService';
8
+ try {
9
+ this.connection = await amqp.connect(this.config.url || 'amqp://localhost:5672');
10
+ this.channel = await this.connection.createChannel();
11
+ if (this.config.queues) {
12
+ for (const queue of this.config.queues) {
13
+ await this.channel.assertQueue(queue, { durable: true });
14
+ }
15
+ }
16
+ console.log(`Connected to RabbitMQ for service: ${serviceName}`);
17
+ }
18
+ catch (error) {
19
+ console.error(`Error connecting to RabbitMQ for service ${serviceName}:`, error);
20
+ }
21
+ }
22
+ async sendToQueue(queue, message) {
23
+ try {
24
+ if (!this.channel) {
25
+ console.error('Channel is not initialized. Call connect() first.');
26
+ return false;
27
+ }
28
+ await this.channel.sendToQueue(queue, Buffer.from(JSON.stringify(message)), { persistent: true });
29
+ console.log(`Message sent to queue ${queue}:`, message);
30
+ return true;
31
+ }
32
+ catch (error) {
33
+ console.error('Error sending message to queue:', error);
34
+ return false;
35
+ }
36
+ }
37
+ async consumeFromQueue(queue, callback, options = { noAck: false }) {
38
+ try {
39
+ if (!this.channel) {
40
+ console.error('Channel is not initialized. Call connect() first.');
41
+ return false;
42
+ }
43
+ await this.channel.consume(queue, async (message) => {
44
+ if (message) {
45
+ try {
46
+ await callback(JSON.parse(message.content.toString()));
47
+ if (!options.noAck) {
48
+ await this.channel.ack(message);
49
+ }
50
+ }
51
+ catch (error) {
52
+ console.error('Error processing message:', error);
53
+ }
54
+ }
55
+ });
56
+ console.log(`Consuming from queue ${queue}`);
57
+ return true;
58
+ }
59
+ catch (error) {
60
+ console.error('Error consuming from queue:', error);
61
+ return false;
62
+ }
63
+ }
64
+ getChannel() {
65
+ return this.channel;
66
+ }
67
+ isConnected() {
68
+ return !!this.connection && !!this.channel;
69
+ }
70
+ async close() {
71
+ try {
72
+ if (this.channel) {
73
+ await this.channel.close();
74
+ this.channel = null;
75
+ console.log('RabbitMQ channel closed.');
76
+ }
77
+ if (this.connection) {
78
+ await this.connection.close();
79
+ this.connection = null;
80
+ console.log('RabbitMQ connection closed.');
81
+ }
82
+ }
83
+ catch (error) {
84
+ console.error('Error closing RabbitMQ connection:', error);
85
+ }
86
+ }
87
+ }
88
+ export default RabbitMQ;
89
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,SAAS,CAAC;AAQ3B,MAAM,QAAQ;IAGV,YAAoB,SAA6B,EAAE;QAA/B,WAAM,GAAN,MAAM,CAAyB;IAAG,CAAC;IAEvD,KAAK,CAAC,OAAO;QACT,MAAM,WAAW,GAAG,IAAI,CAAC,MAAM,CAAC,WAAW,IAAI,gBAAgB,CAAC;QAChE,IAAI,CAAC;YACD,IAAI,CAAC,UAAU,GAAG,MAAM,IAAI,CAAC,OAAO,CAChC,IAAI,CAAC,MAAM,CAAC,GAAG,IAAI,uBAAuB,CAC7C,CAAC;YACF,IAAI,CAAC,OAAO,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,aAAa,EAAE,CAAC;YACrD,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;gBACrB,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;oBACrC,MAAM,IAAI,CAAC,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AG,IAAI,CAAC;gBACvB,OAAO,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAC;YAC/C,CAAC;QACL,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,OAAO,CAAC,KAAK,CAAC,oCAAoC,EAAE,KAAK,CAAC,CAAC;QAC/D,CAAC;IACL,CAAC;CACJ;AAED,eAAe,QAAQ,CAAC"}
package/package.json CHANGED
@@ -1,9 +1,16 @@
1
1
  {
2
2
  "name": "@tim_madule/rabbitmq",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "type": "module",
5
5
  "description": "Shared RabbitMQ client for microservices",
6
- "main": "index.js",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": "./dist/index.js"
10
+ },
11
+ "files": [
12
+ "dist"
13
+ ],
7
14
  "scripts": {
8
15
  "build": "tsc"
9
16
  },
package/tsconfig.json DELETED
@@ -1,39 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- // Output code version
4
- "target": "es2020",
5
- "module": "nodenext",
6
-
7
- // Type Checking
8
- "strict": true,
9
- "noImplicitAny": true,
10
- "noImplicitThis": true,
11
- "noUnusedLocals": true,
12
- "noUnusedParameters": true,
13
- "strictNullChecks": true,
14
- "noFallthroughCasesInSwitch": true,
15
- "allowUnusedLabels": false,
16
- "noUncheckedSideEffectImports": true,
17
- "allowUnreachableCode": false,
18
- "exactOptionalPropertyTypes": true,
19
-
20
- // Module Resolution
21
- "moduleResolution": "nodenext",
22
- "esModuleInterop": true, // CommonJS/ES module interop
23
- "resolveJsonModule": true,
24
- "forceConsistentCasingInFileNames": true,
25
- "isolatedModules": true,
26
- "types": ["@types/node"],
27
- // File Location
28
- "rootDir": ".",
29
- "outDir": "dist",
30
- // Emit Options
31
- "sourceMap": true, // Generate source maps
32
- "removeComments": true, // Remove comments in output
33
- "noEmitOnError": true, // Don't emit if there are errors
34
- // Performance
35
- "skipLibCheck": true // Skip type checking of declaration files
36
- },
37
- "include": ["index.ts"],
38
- "exclude": ["node_modules"]
39
- }