@adobe/spacecat-shared-utils 1.9.0 → 1.11.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.11.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.10.0...@adobe/spacecat-shared-utils-v1.11.0) (2024-02-07)
2
+
3
+
4
+ ### Features
5
+
6
+ * url helpers in utils ([#139](https://github.com/adobe/spacecat-shared/issues/139)) ([0a90c1b](https://github.com/adobe/spacecat-shared/commit/0a90c1b80a1fd7fb0cd267b7f7fca45fb1186c48))
7
+
8
+ # [@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)
9
+
10
+
11
+ ### Features
12
+
13
+ * move sqs wrapper to scpacecat-shared ([#136](https://github.com/adobe/spacecat-shared/issues/136)) ([b9f6d75](https://github.com/adobe/spacecat-shared/commit/b9f6d75d755cdd3d093f1bc016e643de6ef0abcd))
14
+
1
15
  # [@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
16
 
3
17
 
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.11.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/index.d.ts CHANGED
@@ -39,3 +39,45 @@ export function dateAfterDays(days: string): Date;
39
39
 
40
40
  export function sqsEventAdapter(fn: (message: object, context: object) => Promise<Response>):
41
41
  (request: object, context: object) => Promise<Response>;
42
+
43
+ /**
44
+ * Prepends 'https://' schema to the URL if it's not already present.
45
+ * @param url - The URL to modify.
46
+ * @returns The URL with 'https://' schema prepended.
47
+ */
48
+ declare function prependSchema(url: string): string;
49
+
50
+ /**
51
+ * Strips the port number from the end of the URL.
52
+ * @param url - The URL to modify.
53
+ * @returns The URL with the port removed.
54
+ */
55
+ declare function stripPort(url: string): string;
56
+
57
+ /**
58
+ * Strips the trailing dot from the end of the URL.
59
+ * @param url - The URL to modify.
60
+ * @returns The URL with the trailing dot removed.
61
+ */
62
+ declare function stripTrailingDot(url: string): string;
63
+
64
+ /**
65
+ * Strips the trailing slash from the end of the URL.
66
+ * @param url - The URL to modify.
67
+ * @returns The URL with the trailing slash removed.
68
+ */
69
+ declare function stripTrailingSlash(url: string): string;
70
+
71
+ /**
72
+ * Strips 'www.' from the beginning of the URL if present.
73
+ * @param url - The URL to modify.
74
+ * @returns The URL with 'www.' removed.
75
+ */
76
+ declare function stripWWW(url: string): string;
77
+
78
+ /**
79
+ * Composes a base URL by applying a series of transformations to the given domain.
80
+ * @param domain - The domain to compose the base URL from.
81
+ * @returns The composed base URL.
82
+ */
83
+ declare function composeBaseURL(domain: string): string;
package/src/index.js CHANGED
@@ -30,3 +30,12 @@ export {
30
30
  export { resolveSecretsName } from './helpers.js';
31
31
 
32
32
  export { sqsEventAdapter } from './sqs.js';
33
+
34
+ export {
35
+ composeBaseURL,
36
+ prependSchema,
37
+ stripPort,
38
+ stripTrailingDot,
39
+ stripTrailingSlash,
40
+ stripWWW,
41
+ } from './url-helpers.js';
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
@@ -0,0 +1,81 @@
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
+ /**
14
+ * Prepends 'https://' schema to the URL if it's not already present.
15
+ * @param {string} url - The URL to modify.
16
+ * @returns {string} - The URL with 'https://' schema prepended.
17
+ */
18
+ function prependSchema(url) {
19
+ return url.startsWith('http://') || url.startsWith('https://') ? url : `https://${url}`;
20
+ }
21
+
22
+ /**
23
+ * Strips the port number from the end of the URL.
24
+ * @param {string} url - The URL to modify.
25
+ * @returns {string} - The URL with the port removed.
26
+ */
27
+ function stripPort(url) {
28
+ return url.replace(/:\d{1,5}(\/|$)/, '');
29
+ }
30
+
31
+ /**
32
+ * Strips the trailing dot from the end of the URL.
33
+ * @param {string} url - The URL to modify.
34
+ * @returns {string} - The URL with the trailing dot removed.
35
+ */
36
+ function stripTrailingDot(url) {
37
+ return url.endsWith('.') ? url.slice(0, -1) : url;
38
+ }
39
+
40
+ /**
41
+ * Strips the trailing slash from the end of the URL.
42
+ * @param {string} url - The URL to modify.
43
+ * @returns {string} - The URL with the trailing slash removed.
44
+ */
45
+ function stripTrailingSlash(url) {
46
+ return url.endsWith('/') ? url.slice(0, -1) : url;
47
+ }
48
+
49
+ /**
50
+ * Strips 'www.' from the beginning of the URL if present.
51
+ * @param {string} url - The URL to modify.
52
+ * @returns {string} - The URL with 'www.' removed.
53
+ */
54
+ function stripWWW(url) {
55
+ const regex = /^(https?:\/\/)?(www\.)?/;
56
+ // Replace "www." with an empty string, preserving the schema if present
57
+ return url.replace(regex, (match, schema) => (schema || ''));
58
+ }
59
+
60
+ /**
61
+ * Composes a base URL by applying a series of transformations to the given domain.
62
+ * @param {string} domain - The domain to compose the base URL from.
63
+ * @returns {string} - The composed base URL.
64
+ */
65
+ function composeBaseURL(domain) {
66
+ let baseURL = stripTrailingDot(domain);
67
+ baseURL = stripPort(baseURL);
68
+ baseURL = stripTrailingSlash(baseURL);
69
+ baseURL = stripWWW(baseURL);
70
+ baseURL = prependSchema(baseURL);
71
+ return baseURL;
72
+ }
73
+
74
+ export {
75
+ composeBaseURL,
76
+ prependSchema,
77
+ stripPort,
78
+ stripTrailingDot,
79
+ stripTrailingSlash,
80
+ stripWWW,
81
+ };