@opens/rabbitmq 1.0.0 → 1.0.1

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,105 @@
1
+ # OpensMQ
2
+
3
+ This library is a lightweight wrapper around RabbitMQ that simplifies publishing and subscribing to messages using `amqp-connection-manager` and `amqplib`. This library provides an easy-to-use API for event-driven applications.
4
+
5
+ ## Getting Started
6
+
7
+ ### Initialization
8
+
9
+ To start using `PubSubMQ`, create an instance and initialize the connection to RabbitMQ:
10
+
11
+ ```typescript
12
+ import { connect } from 'amqp-connection-manager';
13
+ import { pubsub } from './rabbitmq';
14
+
15
+
16
+ pubsub.start({
17
+ hostname: 'localhost',
18
+ port: 5672,
19
+ username: 'guest',
20
+ password: 'guest',
21
+ protocol: 'amqp',
22
+ });
23
+ ```
24
+
25
+ ### Publishing Messages
26
+
27
+ You can publish messages to a specific exchange with a routing key:
28
+
29
+ ```typescript
30
+ await pubsub.publish('my-exchange', 'my-routing-key', { message: 'Hello, World!' });
31
+ ```
32
+
33
+ ### Subscribing to Messages
34
+
35
+ To subscribe to a queue and process messages:
36
+
37
+ ```typescript
38
+ pubsub.subscribe({
39
+ name: 'my-queue',
40
+ exchange: 'my-exchange',
41
+ bindingKey: 'my-routing-key',
42
+ durable: true,
43
+ prefetch: 1,
44
+ consumer: async (data, metadata) => {
45
+ console.log('Received:', data);
46
+ },
47
+ up: async () => console.log('Consumer is up'),
48
+ down: async () => console.log('Consumer is down'),
49
+ });
50
+ ```
51
+
52
+ ### Handling Message Acknowledgments
53
+
54
+ The library automatically acknowledges messages if processed successfully. If an error occurs, the message is rejected (`nack`).
55
+
56
+ ## Advanced Configuration
57
+
58
+ ### Setting TTL (Time-To-Live)
59
+
60
+ To set a TTL for messages in the queue:
61
+
62
+ ```typescript
63
+ pubsub.subscribe({
64
+ name: 'expiring-queue',
65
+ exchange: 'my-exchange',
66
+ bindingKey: 'expiring-key',
67
+ ttl: 30000, // Messages expire after 30 seconds
68
+ consumer: async (data) => {
69
+ console.log('Processing expiring message:', data);
70
+ },
71
+ up: async () => console.log('Consumer is up'),
72
+ down: async () => console.log('Consumer is down'),
73
+ });
74
+ ```
75
+
76
+ ### Exclusive Queues
77
+
78
+ For a queue that only allows a single connection:
79
+
80
+ ```typescript
81
+ pubsub.subscribe({
82
+ name: 'exclusive-queue',
83
+ exchange: 'my-exchange',
84
+ bindingKey: 'exclusive-key',
85
+ exclusive: true,
86
+ consumer: async (data) => {
87
+ console.log('Processing exclusive message:', data);
88
+ },
89
+ up: async () => console.log('Consumer is up'),
90
+ down: async () => console.log('Consumer is down'),
91
+ });
92
+ ```
93
+
94
+ ## Error Handling
95
+
96
+ Errors in the consumer function cause messages to be rejected (`nack`). Ensure error handling is implemented inside your consumer to prevent message loss.
97
+
98
+ ## Contributing
99
+
100
+ Feel free to contribute by submitting issues or pull requests.
101
+
102
+ ## License
103
+
104
+ This project is licensed under the MIT License.
105
+
@@ -0,0 +1,39 @@
1
+ import { connect, ChannelWrapper } from 'amqp-connection-manager';
2
+ import { MessageFields, MessageProperties } from 'amqplib';
3
+
4
+ type ConsumerFunc = (data: any, metadata: {
5
+ fields: MessageFields;
6
+ properties: MessageProperties;
7
+ }) => Promise<void>;
8
+ interface SubscribeParams {
9
+ ttl?: number;
10
+ name: string;
11
+ exchange: string;
12
+ durable?: boolean;
13
+ prefetch?: number;
14
+ queueArguments?: Record<string, any>;
15
+ exclusive?: boolean;
16
+ bindingKey: string;
17
+ consumer?: ConsumerFunc;
18
+ up?: ConsumerFunc;
19
+ down?: ConsumerFunc;
20
+ }
21
+ declare class PubSubMQ {
22
+ private startConnection;
23
+ private connection;
24
+ private publisherChannel;
25
+ constructor(startConnection: typeof connect);
26
+ start(params: {
27
+ hostname: string;
28
+ password: string;
29
+ protocol: string;
30
+ username: string;
31
+ port: number;
32
+ }): void;
33
+ publish(exchange: string, routingKey: string, payload: any, options?: Object): Promise<boolean>;
34
+ subscribe(params: SubscribeParams): ChannelWrapper;
35
+ }
36
+
37
+ declare const pubsub: PubSubMQ;
38
+
39
+ export { pubsub };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opens/rabbitmq",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "A wrapper around common message brokers",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -9,7 +9,7 @@
9
9
  "dist"
10
10
  ],
11
11
  "scripts": {
12
- "build": "tsup",
12
+ "build": "tsup --dts",
13
13
  "dev": "tsup --watch",
14
14
  "test": "vitest run",
15
15
  "test:watch": "vitest"