@adobe/spacecat-shared-utils 1.8.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
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
+
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)
9
+
10
+
11
+ ### Features
12
+
13
+ * introduce AWS S3 wrapper ([#135](https://github.com/adobe/spacecat-shared/issues/135)) ([4ad1bb6](https://github.com/adobe/spacecat-shared/commit/4ad1bb64414086af5d399317697debb6aff0fb74))
14
+
1
15
  # [@adobe/spacecat-shared-utils-v1.8.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.7.4...@adobe/spacecat-shared-utils-v1.8.0) (2024-02-05)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-utils",
3
- "version": "1.8.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,10 +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
- "@adobe/fetch": "4.1.1"
42
+ "@adobe/fetch": "4.1.1",
43
+ "@aws-sdk/client-s3": "3.507.0",
44
+ "@aws-sdk/client-sqs": "3.507.0"
37
45
  }
38
46
  }
package/src/s3.js ADDED
@@ -0,0 +1,28 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import { S3Client } from '@aws-sdk/client-s3';
14
+
15
+ export function s3Bucket(fn) {
16
+ return async (req, context) => {
17
+ if (!context.s3) {
18
+ const {
19
+ AWS_REGION: region,
20
+ S3_BUCKET_NAME: bucket,
21
+ } = context.env;
22
+
23
+ context.s3 = new S3Client({ region });
24
+ context.s3Bucket = bucket;
25
+ }
26
+ return fn(req, context);
27
+ };
28
+ }
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