@adobe/spacecat-shared-utils 1.12.2 → 1.13.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.13.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.12.3...@adobe/spacecat-shared-utils-v1.13.0) (2024-02-29)
2
+
3
+
4
+ ### Features
5
+
6
+ * helper to detect audits are disabled ([#173](https://github.com/adobe/spacecat-shared/issues/173)) ([e698a76](https://github.com/adobe/spacecat-shared/commit/e698a76899399a7155175527b47c0fe5528b123a))
7
+
8
+ # [@adobe/spacecat-shared-utils-v1.12.3](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.12.2...@adobe/spacecat-shared-utils-v1.12.3) (2024-02-28)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * enhance dateAfterDays ([189895a](https://github.com/adobe/spacecat-shared/commit/189895acc254703ae90bdebb965346ffe39350c3))
14
+
1
15
  # [@adobe/spacecat-shared-utils-v1.12.2](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.12.1...@adobe/spacecat-shared-utils-v1.12.2) (2024-02-27)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-utils",
3
- "version": "1.12.2",
3
+ "version": "1.13.0",
4
4
  "description": "Shared modules of the Spacecat Services - utils",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -42,5 +42,8 @@
42
42
  "@adobe/fetch": "4.1.1",
43
43
  "@aws-sdk/client-s3": "3.521.0",
44
44
  "@aws-sdk/client-sqs": "3.521.0"
45
+ },
46
+ "peerDependencies": {
47
+ "@adobe/spacecat-shared-data-access": "1.x"
45
48
  }
46
49
  }
package/src/functions.js CHANGED
@@ -169,6 +169,7 @@ const arrayEquals = (a, b) => isArray(a)
169
169
  * Calculates the date after a specified number of days from the current date.
170
170
  *
171
171
  * @param {number} days - The number of days to add to the current date.
172
+ * @param {string=} dateString - The reference date in string format.
172
173
  * @returns {Date} A new Date object representing the calculated date after the specified days.
173
174
  * @throws {TypeError} If the provided 'days' parameter is not a number.
174
175
  * @throws {RangeError} If the calculated date is outside the valid JavaScript date range.
@@ -178,9 +179,9 @@ const arrayEquals = (a, b) => isArray(a)
178
179
  * const sevenDaysLater = dateAfterDays(7);
179
180
  * console.log(sevenDaysLater); // Outputs a Date object representing the date 7 days from now
180
181
  */
181
- function dateAfterDays(days) {
182
- const currentDate = new Date();
183
- currentDate.setDate(currentDate.getDate() + days);
182
+ function dateAfterDays(days, dateString) {
183
+ const currentDate = !dateString ? new Date() : new Date(dateString);
184
+ currentDate.setUTCDate(currentDate.getDate() + days);
184
185
  return currentDate;
185
186
  }
186
187
 
package/src/helpers.js CHANGED
@@ -10,7 +10,7 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import { isString } from './functions.js';
13
+ import { hasText, isString } from './functions.js';
14
14
 
15
15
  /**
16
16
  * Resolves the name of the secret based on the function version.
@@ -19,7 +19,7 @@ import { isString } from './functions.js';
19
19
  * @param {string} defaultPath - The default path for the secret.
20
20
  * @returns {string} - The resolved secret name.
21
21
  */
22
- const resolveSecretsName = (opts, ctx, defaultPath) => {
22
+ export function resolveSecretsName(opts, ctx, defaultPath) {
23
23
  let funcVersion = ctx?.func?.version;
24
24
 
25
25
  if (!isString(funcVersion)) {
@@ -33,8 +33,25 @@ const resolveSecretsName = (opts, ctx, defaultPath) => {
33
33
  funcVersion = /^ci\d+$/i.test(funcVersion) ? 'ci' : funcVersion;
34
34
 
35
35
  return `${defaultPath}/${funcVersion}`;
36
- };
36
+ }
37
37
 
38
- export {
39
- resolveSecretsName,
40
- };
38
+ export function isAuditsDisabled(site, organization, auditType) {
39
+ // return early if all audits are disabled for the organization
40
+ if (organization.getAuditConfig().auditsDisabled()) {
41
+ return true;
42
+ }
43
+
44
+ // return early if all audits are disabled for the site
45
+ if (site.getAuditConfig().auditsDisabled()) {
46
+ return true;
47
+ }
48
+
49
+ if (hasText(auditType)) {
50
+ const disabledAtOrg = organization.getAuditConfig().getAuditTypeConfig(auditType)?.disabled();
51
+ const disabledAtSite = site.getAuditConfig().getAuditTypeConfig(auditType)?.disabled();
52
+
53
+ return !!disabledAtOrg || !!disabledAtSite;
54
+ }
55
+
56
+ return false;
57
+ }
package/src/index.d.ts CHANGED
@@ -35,13 +35,13 @@ export function toBoolean(value: unknown): boolean;
35
35
 
36
36
  export function isValidUrl(urlString: string): boolean;
37
37
 
38
- export function dateAfterDays(days: number): Date;
38
+ export function dateAfterDays(days: number, dateString: string): Date;
39
39
 
40
40
  export function sqsWrapper(fn: (message: object, context: object) => Promise<Response>):
41
- (request: object, context: object) => Promise<Response>;
41
+ (request: object, context: object) => Promise<Response>;
42
42
 
43
43
  export function sqsEventAdapter(fn: (message: object, context: object) => Promise<Response>):
44
- (request: object, context: object) => Promise<Response>;
44
+ (request: object, context: object) => Promise<Response>;
45
45
 
46
46
  /**
47
47
  * Prepends 'https://' schema to the URL if it's not already present.
@@ -91,3 +91,20 @@ declare function composeBaseURL(domain: string): string;
91
91
  * @returns a promise that resolves the composed audit URL.
92
92
  */
93
93
  declare function composeAuditURL(url: string): Promise<string>;
94
+
95
+ /**
96
+ * Checks whether audits are disabled for a given site by inspecting the audit configurations
97
+ * in the respective organization and site models.
98
+ *
99
+ * If the optional parameter `auditType` is NOT provided, only the root-level "auditsDisabled" flag
100
+ * in the site and organization is checked.
101
+ *
102
+ * If the optional parameter `auditType` is provided, then the specific audit configuration for the
103
+ * specified type is also checked.
104
+ *
105
+ * @param {object} site - The site object.
106
+ * @param {object} organization - The organization object.
107
+ * @param {string} [auditType] - The type of audit.
108
+ * @returns {boolean} - True if the audit(s) are disabled, otherwise false.
109
+ */
110
+ declare function isAuditsDisabled(site: object, organization: object, auditType?: string): boolean
package/src/index.js CHANGED
@@ -27,7 +27,10 @@ export {
27
27
  dateAfterDays,
28
28
  } from './functions.js';
29
29
 
30
- export { resolveSecretsName } from './helpers.js';
30
+ export {
31
+ isAuditsDisabled,
32
+ resolveSecretsName,
33
+ } from './helpers.js';
31
34
 
32
35
  export { sqsWrapper } from './sqs.js';
33
36
  export { sqsEventAdapter } from './sqs.js';