@mohamed_ayad40/common 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
+ 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;
21
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,cAAc,GAAG;IACzB,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,MAAM,CAAC,EAAE,MAAM,EAAE,CAAA;IACjB,WAAW,CAAC,EAAE,MAAM,CAAA;CACvB,CAAA;AAED,cAAM,cAAc;IAChB,OAAO,CAAC,MAAM,CAAM;IACpB,OAAO,CAAC,UAAU,CAAM;IACxB,OAAO,CAAC,OAAO,CAAM;gBACT,MAAM,GAAE,cAAmB;IAIjC,OAAO;IAoBP,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAkBjD,gBAAgB,CAClB,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,OAAO,CAAC,IAAI,CAAC,EACxC,OAAO,GAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAqB;IA8B/C,UAAU;IAIV,WAAW;IAIL,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAgB/B;AAED,eAAe,cAAc,CAAC"}
@@ -1,114 +1,98 @@
1
- import amqp from "amqplib";
2
-
3
- export type RabbitMQConfig = {
4
- url?: string
5
- queues?: string[]
6
- serviceName?: string
7
- }
8
-
9
- class RabbitMQClient {
10
- private config: any;
11
- private connection: any;
12
- private channel: any;
13
- constructor(config: RabbitMQConfig = {}){
14
- this.config = config;
15
- }
16
-
17
- async connect() {
18
- try {
19
- const rabbitMQUrl = this.config.url || 'amqp://localhost:5672';
20
- this.connection = await amqp.connect(rabbitMQUrl);
21
- this.channel = this.connection.createChannel();
22
-
23
- if (this.config.queues) {
24
- for (const queue of this.config.queues) {
25
- await this.channel.assertQueue(queue, { durable: true });
26
- }
27
- }
28
- const serviceName = this.config.serviceName || "Service";
29
- console.log(`${serviceName}: RabbitMQ connected successfully`);
30
- } catch (error) {
31
- const serviceName = this.config.serviceName || "Service";
32
- console.error(`${serviceName}: RabbitMQ connected successfully`, error);
33
- }
34
- }
35
-
36
-
37
- async publishToQueue(queue: string, message: string) {
38
- try {
39
- if (!this.channel) {
40
- console.error("RabbitMQ channel not available");
41
- return false;
42
- }
43
- await this.channel.assertQueue(queue, { durable: true });
44
- this.channel.sendToQueue(queue, Buffer.from(JSON.stringify(message)), {
45
- persistent: true,
46
- });
47
- console.log(`Message published to queue ${queue}:`, message);
48
- return true;
49
- } catch (error) {
50
- console.error(`Error publishing message to the queue`);
51
- return false;
52
- }
53
- }
54
-
55
- async consumeFromQueue(
56
- queue: string,
57
- handler: (message: any) => Promise<void>,
58
- options: { noAck?: boolean } = { noAck: false }
59
- ) {
60
- try {
61
- if (!this.channel) {
62
- console.error("RabbitMQ channel not available")
63
- return
64
- }
65
- await this.channel.assertQueue(queue, { durable: true })
66
- this.channel.consume(queue, async (msg: any) => {
67
- if (msg) {
68
- try {
69
- const content = JSON.parse(msg.content.toString())
70
- await handler(content)
71
- if (!options.noAck) {
72
- this.channel?.ack(msg)
73
- }
74
- console.log(`Started consuming from queue: ${queue}`)
75
- } catch (error) {
76
- console.error("Error consuming message to queue")
77
- return false
78
- }
79
- }
80
- })
81
- } catch (error) {
82
- console.error("Error publishing message to queue")
83
- return false
84
- }
85
- }
86
-
87
-
88
- getChannel() {
89
- return this.channel;
90
- }
91
-
92
- isConnected() {
93
- return this.channel !== null && this.connection !== null;
94
- }
95
-
96
- async close(): Promise<void> {
97
- try {
98
- if (this.channel) {
99
- await this.channel.close();
100
- this.channel = null;
101
- }
102
- if (this.connection) {
103
- await this.connection.close();
104
- this.connection = null;
105
- }
106
- console.log("RabbitMQ connection closed");
107
- } catch (error) {
108
- console.error("Error closing RabbitMQ connection", error);
109
- }
110
- }
111
-
112
- }
113
-
114
- export default RabbitMQClient;
1
+ import amqp from "amqplib";
2
+ class RabbitMQClient {
3
+ config;
4
+ connection;
5
+ channel;
6
+ constructor(config = {}) {
7
+ this.config = config;
8
+ }
9
+ async connect() {
10
+ try {
11
+ const rabbitMQUrl = this.config.url || 'amqp://localhost:5672';
12
+ this.connection = await amqp.connect(rabbitMQUrl);
13
+ this.channel = this.connection.createChannel();
14
+ if (this.config.queues) {
15
+ for (const queue of this.config.queues) {
16
+ await this.channel.assertQueue(queue, { durable: true });
17
+ }
18
+ }
19
+ const serviceName = this.config.serviceName || "Service";
20
+ console.log(`${serviceName}: RabbitMQ connected successfully`);
21
+ }
22
+ catch (error) {
23
+ const serviceName = this.config.serviceName || "Service";
24
+ console.error(`${serviceName}: RabbitMQ connected successfully`, error);
25
+ }
26
+ }
27
+ async publishToQueue(queue, message) {
28
+ try {
29
+ if (!this.channel) {
30
+ console.error("RabbitMQ channel not available");
31
+ return false;
32
+ }
33
+ await this.channel.assertQueue(queue, { durable: true });
34
+ this.channel.sendToQueue(queue, Buffer.from(JSON.stringify(message)), {
35
+ persistent: true,
36
+ });
37
+ console.log(`Message published to queue ${queue}:`, message);
38
+ return true;
39
+ }
40
+ catch (error) {
41
+ console.error(`Error publishing message to the queue`);
42
+ return false;
43
+ }
44
+ }
45
+ async consumeFromQueue(queue, handler, options = { noAck: false }) {
46
+ try {
47
+ if (!this.channel) {
48
+ console.error("RabbitMQ channel not available");
49
+ return;
50
+ }
51
+ await this.channel.assertQueue(queue, { durable: true });
52
+ this.channel.consume(queue, async (msg) => {
53
+ if (msg) {
54
+ try {
55
+ const content = JSON.parse(msg.content.toString());
56
+ await handler(content);
57
+ if (!options.noAck) {
58
+ this.channel?.ack(msg);
59
+ }
60
+ console.log(`Started consuming from queue: ${queue}`);
61
+ }
62
+ catch (error) {
63
+ console.error("Error consuming message to queue");
64
+ return false;
65
+ }
66
+ }
67
+ });
68
+ }
69
+ catch (error) {
70
+ console.error("Error publishing message to queue");
71
+ return false;
72
+ }
73
+ }
74
+ getChannel() {
75
+ return this.channel;
76
+ }
77
+ isConnected() {
78
+ return this.channel !== null && this.connection !== null;
79
+ }
80
+ async close() {
81
+ try {
82
+ if (this.channel) {
83
+ await this.channel.close();
84
+ this.channel = null;
85
+ }
86
+ if (this.connection) {
87
+ await this.connection.close();
88
+ this.connection = null;
89
+ }
90
+ console.log("RabbitMQ connection closed");
91
+ }
92
+ catch (error) {
93
+ console.error("Error closing RabbitMQ connection", error);
94
+ }
95
+ }
96
+ }
97
+ export default RabbitMQClient;
98
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,SAAS,CAAC;AAQ3B,MAAM,cAAc;IACR,MAAM,CAAM;IACZ,UAAU,CAAM;IAChB,OAAO,CAAM;IACrB,YAAY,SAAyB,EAAE;QACnC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACzB,CAAC;IAED,KAAK,CAAC,OAAO;QACT,IAAI,CAAC;YACD,MAAM,WAAW,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,IAAI,uBAAuB,CAAC;YAC/D,IAAI,CAAC,UAAU,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;YAClD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;QACd,CAAC;IACH,CAAC;IAGC,UAAU;QACN,OAAO,IAAI,CAAC,OAAO,CAAC;IACxB,CAAC;IAED,WAAW;QACP,OAAO,IAAI,CAAC,OAAO,KAAK,IAAI,IAAI,IAAI,CAAC,UAAU,KAAK,IAAI,CAAC;IAC7D,CAAC;IAED,KAAK,CAAC,KAAK;QACP,IAAI,CAAC;YACD,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;gBACf,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;gBAC3B,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;YACxB,CAAC;YACD,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;gBAClB,MAAM,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;gBAC9B,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;YAC3B,CAAC;YACD,OAAO,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC;QAC9C,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,OAAO,CAAC,KAAK,CAAC,mCAAmC,EAAE,KAAK,CAAC,CAAC;QAC9D,CAAC;IACL,CAAC;CAEJ;AAED,eAAe,cAAc,CAAC"}
package/package.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "@mohamed_ayad40/common",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Shared RabbitMQ client for microservices",
5
- "main": "index.js",
6
- "types": "index.d.ts",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
7
10
  "scripts": {
8
11
  "build": "tsc",
9
12
  "dev": "tsc --watch"
package/tsconfig.json DELETED
@@ -1,44 +0,0 @@
1
- {
2
- // Visit https://aka.ms/tsconfig to read more about this file
3
- "compilerOptions": {
4
- // File Layout
5
- "rootDir": "./src",
6
- "outDir": "./dist",
7
-
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
17
-
18
- // Other Outputs
19
- "sourceMap": true,
20
- "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,
34
-
35
- // Recommended Options
36
- "strict": true,
37
- "jsx": "react-jsx",
38
- "verbatimModuleSyntax": true,
39
- "isolatedModules": true,
40
- "noUncheckedSideEffectImports": true,
41
- "moduleDetection": "force",
42
- "skipLibCheck": true,
43
- }
44
- }