@adobe/spacecat-shared-utils 1.9.0 → 1.10.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.
Files changed (3) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/package.json +10 -3
  3. package/src/sqs.js +47 -0
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [@adobe/spacecat-shared-utils-v1.10.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.9.0...@adobe/spacecat-shared-utils-v1.10.0) (2024-02-07)
2
+
3
+
4
+ ### Features
5
+
6
+ * move sqs wrapper to scpacecat-shared ([#136](https://github.com/adobe/spacecat-shared/issues/136)) ([b9f6d75](https://github.com/adobe/spacecat-shared/commit/b9f6d75d755cdd3d093f1bc016e643de6ef0abcd))
7
+
1
8
  # [@adobe/spacecat-shared-utils-v1.9.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.8.0...@adobe/spacecat-shared-utils-v1.9.0) (2024-02-06)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-utils",
3
- "version": "1.9.0",
3
+ "version": "1.10.0",
4
4
  "description": "Shared modules of the Spacecat Services - utils",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -11,6 +11,7 @@
11
11
  "clean": "rm -rf package-lock.json node_modules"
12
12
  },
13
13
  "mocha": {
14
+ "require": "test/setup-env.js",
14
15
  "reporter": "mocha-multi-reporters",
15
16
  "reporter-options": "configFile=.mocha-multi.json",
16
17
  "spec": "test/*.test.js"
@@ -29,11 +30,17 @@
29
30
  "access": "public"
30
31
  },
31
32
  "devDependencies": {
33
+ "@adobe/helix-shared-wrap": "2.0.1",
32
34
  "chai": "4.4.1",
33
- "sinon": "17.0.1"
35
+ "chai-as-promised": "7.1.1",
36
+ "husky": "9.0.6",
37
+ "nock": "13.5.1",
38
+ "sinon": "17.0.1",
39
+ "sinon-chai": "3.7.0"
34
40
  },
35
41
  "dependencies": {
36
42
  "@adobe/fetch": "4.1.1",
37
- "@aws-sdk/client-s3": "3.507.0"
43
+ "@aws-sdk/client-s3": "3.507.0",
44
+ "@aws-sdk/client-sqs": "3.507.0"
38
45
  }
39
46
  }
package/src/sqs.js CHANGED
@@ -10,6 +10,53 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
+ import { SendMessageCommand, SQSClient } from '@aws-sdk/client-sqs';
14
+
15
+ /**
16
+ * @class SQS utility to send messages to SQS
17
+ * @param {string} region - AWS region
18
+ * @param {object} log - log object
19
+ */
20
+ class SQS {
21
+ constructor(region, log) {
22
+ this.sqsClient = new SQSClient({ region });
23
+ this.log = log;
24
+ }
25
+
26
+ async sendMessage(queueUrl, message) {
27
+ const body = {
28
+ ...message,
29
+ timestamp: new Date().toISOString(),
30
+ };
31
+
32
+ const msgCommand = new SendMessageCommand({
33
+ MessageBody: JSON.stringify(body),
34
+ QueueUrl: queueUrl,
35
+ });
36
+
37
+ try {
38
+ const data = await this.sqsClient.send(msgCommand);
39
+ this.log.info(`Success, message sent. MessageID: ${data.MessageId}`);
40
+ } catch (e) {
41
+ const { type, code, message: msg } = e;
42
+ this.log.error(`Message sent failed. Type: ${type}, Code: ${code}, Message: ${msg}`);
43
+ throw e;
44
+ }
45
+ }
46
+ }
47
+
48
+ export default function sqsWrapper(fn) {
49
+ return async (request, context) => {
50
+ if (!context.sqs) {
51
+ const { log } = context;
52
+ const { region } = context.runtime;
53
+ context.sqs = new SQS(region, log);
54
+ }
55
+
56
+ return fn(request, context);
57
+ };
58
+ }
59
+
13
60
  /**
14
61
  * Wrapper to turn an SQS record into a function param
15
62
  * Inspired by https://github.com/adobe/helix-admin/blob/main/src/index.js#L108-L133