@nestjstools/messaging-amazon-sqs-extension 1.0.0 → 1.2.0

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 CHANGED
@@ -6,6 +6,13 @@
6
6
 
7
7
  A NestJS library for managing asynchronous and synchronous messages with support for buses, handlers, channels, and consumers. This library simplifies building scalable and decoupled applications by facilitating robust message handling pipelines while ensuring flexibility and reliability.
8
8
 
9
+ ---
10
+ ## Documentation
11
+
12
+ https://nestjstools.gitbook.io/nestjstools-messaging-docs
13
+
14
+ ---
15
+
9
16
  ## Installation
10
17
 
11
18
  ```bash
@@ -97,9 +104,64 @@ export class CreateUserHandler implements IMessageHandler<CreateUser>{
97
104
  }
98
105
  }
99
106
  ```
107
+ ## 📨 Communicating Beyond a NestJS Application (Cross-Language Messaging)
108
+
109
+ ### To enable communication with a Handler from services written in other languages, follow these steps:
110
+
111
+ 1. **Publish a Message to the queue**
112
+
113
+ 2. **Include the Routing Key Header**
114
+ Your message **must** include a header attribute named `messagingRoutingKey`.
115
+ The value should correspond to the routing key defined in your NestJS message handler:
100
116
 
117
+ ```ts
118
+ @MessageHandler('my_app_command.create_user') // <-- Use this value as the routing key
119
+ ```
120
+
121
+ 3. **You're Done!**
122
+ Once the message is published with the correct routing key, it will be automatically routed to the appropriate handler within the NestJS application.
101
123
  ---
102
124
 
125
+ ## 🏷️ Sending Custom SQS Message Attributes
126
+
127
+ In addition to the required `messagingRoutingKey` header, you can include **custom attributes** in your SQS messages to enrich the message with metadata such as request IDs, user types, or priority levels.
128
+
129
+ ### Example: Sending a Message with Attributes
130
+
131
+ ```ts
132
+ const exampleAttributes = {
133
+ requestId: {
134
+ DataType: "String",
135
+ StringValue: "req-" + Math.random().toString(36).substring(2, 10),
136
+ },
137
+ timestamp: {
138
+ DataType: "Number",
139
+ StringValue: Date.now().toString(),
140
+ },
141
+ userType: {
142
+ DataType: "String",
143
+ StringValue: "admin",
144
+ },
145
+ priority: {
146
+ DataType: "Number",
147
+ StringValue: "1",
148
+ },
149
+ };
150
+
151
+ this.sqsMessageBus.dispatch(
152
+ new RoutingMessage(
153
+ new CreateUser('John FROM Sqs'),
154
+ 'my_app_command.create_user',
155
+ new AmazonSqsMessageOptions(exampleAttributes)
156
+ )
157
+ );
158
+ ```
159
+
160
+ > ⚠️ Don't forget that `messagingRoutingKey` must still be present — it's used to route the message to the correct handler.
161
+
162
+ ---
163
+
164
+
103
165
  ### Key Features:
104
166
 
105
167
  * Amazon SQS Integration: Easily send and receive messages with Amazon SQS.
package/lib/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './messaging-amazon-sqs-extension.module';
2
2
  export * from './channel/amazon-sqs.channel-config';
3
3
  export * from './message-bus/amazon-sqs-message.bus';
4
+ export * from './message/amazon-sqs-message-options';
package/lib/index.js CHANGED
@@ -17,4 +17,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./messaging-amazon-sqs-extension.module"), exports);
18
18
  __exportStar(require("./channel/amazon-sqs.channel-config"), exports);
19
19
  __exportStar(require("./message-bus/amazon-sqs-message.bus"), exports);
20
+ __exportStar(require("./message/amazon-sqs-message-options"), exports);
20
21
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0EAAuD;AACvD,sEAAmD;AACnD,uEAAoD"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0EAAuD;AACvD,sEAAmD;AACnD,uEAAoD;AACpD,uEAAoD"}
@@ -0,0 +1,8 @@
1
+ import { MessageOptions } from '@nestjstools/messaging';
2
+ import { MessageAttributeValue } from '@aws-sdk/client-sqs/dist-types/models/models_0';
3
+ export declare class AmazonSqsMessageOptions implements MessageOptions {
4
+ readonly attributes: Record<string, MessageAttributeValue>;
5
+ readonly middlewares: any[];
6
+ readonly avoidErrorsWhenNotExistedHandler: boolean;
7
+ constructor(attributes?: Record<string, MessageAttributeValue>);
8
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AmazonSqsMessageOptions = void 0;
4
+ class AmazonSqsMessageOptions {
5
+ attributes;
6
+ middlewares = [];
7
+ avoidErrorsWhenNotExistedHandler = false;
8
+ constructor(attributes = {}) {
9
+ this.attributes = attributes;
10
+ }
11
+ }
12
+ exports.AmazonSqsMessageOptions = AmazonSqsMessageOptions;
13
+ //# sourceMappingURL=amazon-sqs-message-options.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"amazon-sqs-message-options.js","sourceRoot":"","sources":["../../src/message/amazon-sqs-message-options.ts"],"names":[],"mappings":";;;AAGA,MAAa,uBAAuB;IAKhB;IAJF,WAAW,GAAU,EAAE,CAAC;IACxB,gCAAgC,GAAY,KAAK,CAAC;IAElE,YACkB,aAAoD,EAAE;QAAtD,eAAU,GAAV,UAAU,CAA4C;IAExE,CAAC;CACF;AARD,0DAQC"}
@@ -13,21 +13,29 @@ exports.AmazonSqsMessageBus = void 0;
13
13
  const common_1 = require("@nestjs/common");
14
14
  const amazon_sqs_channel_1 = require("../channel/amazon-sqs.channel");
15
15
  const client_sqs_1 = require("@aws-sdk/client-sqs");
16
+ const amazon_sqs_message_options_1 = require("../message/amazon-sqs-message-options");
16
17
  let AmazonSqsMessageBus = class AmazonSqsMessageBus {
17
18
  channel;
18
19
  constructor(channel) {
19
20
  this.channel = channel;
20
21
  }
21
22
  async dispatch(message) {
23
+ const messageOptions = message.messageOptions;
24
+ let attributes = {};
25
+ if (messageOptions !== undefined && !(messageOptions instanceof amazon_sqs_message_options_1.AmazonSqsMessageOptions)) {
26
+ throw new Error(`Message options must be a ${amazon_sqs_message_options_1.AmazonSqsMessageOptions.name} object`);
27
+ }
28
+ if (messageOptions instanceof amazon_sqs_message_options_1.AmazonSqsMessageOptions) {
29
+ attributes = messageOptions.attributes;
30
+ }
31
+ attributes.messagingRoutingKey = {
32
+ DataType: "String",
33
+ StringValue: message.messageRoutingKey,
34
+ };
22
35
  const command = new client_sqs_1.SendMessageCommand({
23
36
  QueueUrl: this.channel.config.queueUrl,
24
37
  MessageBody: JSON.stringify(message.message),
25
- MessageAttributes: {
26
- messagingRoutingKey: {
27
- DataType: "String",
28
- StringValue: message.messageRoutingKey,
29
- },
30
- }
38
+ MessageAttributes: attributes,
31
39
  });
32
40
  await this.channel.client.send(command);
33
41
  }
@@ -1 +1 @@
1
- {"version":3,"file":"amazon-sqs-message.bus.js","sourceRoot":"","sources":["../../src/message-bus/amazon-sqs-message.bus.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,2CAA4C;AAC5C,sEAAiE;AACjE,oDAAyD;AAGlD,IAAM,mBAAmB,GAAzB,MAAM,mBAAmB;IAEX;IADnB,YACmB,OAAyB;QAAzB,YAAO,GAAP,OAAO,CAAkB;IAE5C,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,OAAuB;QACpC,MAAM,OAAO,GAAG,IAAI,+BAAkB,CAAC;YACrC,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ;YACtC,WAAW,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,OAAO,CAAC;YAC5C,iBAAiB,EAAE;gBACjB,mBAAmB,EAAE;oBACnB,QAAQ,EAAE,QAAQ;oBAClB,WAAW,EAAE,OAAO,CAAC,iBAAiB;iBACvC;aACF;SACF,CAAC,CAAC;QACH,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IACzC,CAAC;CACF,CAAA;AAnBY,kDAAmB;8BAAnB,mBAAmB;IAD/B,IAAA,mBAAU,GAAE;qCAGiB,qCAAgB;GAFjC,mBAAmB,CAmB/B"}
1
+ {"version":3,"file":"amazon-sqs-message.bus.js","sourceRo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