@adobe/spacecat-shared-data-access 1.61.19 → 2.0.1

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 (151) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/package.json +5 -6
  3. package/src/index.d.ts +4 -934
  4. package/src/index.js +1 -91
  5. package/src/{v2 → models}/index.d.ts +14 -3
  6. package/src/{v2/models → models}/organization/organization.schema.js +1 -1
  7. package/src/{v2/models → models}/site/site.schema.js +1 -1
  8. package/src/service/index.d.ts +20 -0
  9. package/src/service/index.js +6 -46
  10. package/src/dto/api-key.js +0 -58
  11. package/src/dto/audit.js +0 -80
  12. package/src/dto/configuration.js +0 -49
  13. package/src/dto/dto-utils.js +0 -30
  14. package/src/dto/experiment.js +0 -66
  15. package/src/dto/import-job.js +0 -76
  16. package/src/dto/import-url.js +0 -55
  17. package/src/dto/key-event.js +0 -52
  18. package/src/dto/organization.js +0 -54
  19. package/src/dto/site-candidate.js +0 -54
  20. package/src/dto/site-top-page.js +0 -52
  21. package/src/dto/site.js +0 -62
  22. package/src/models/api-key/api-key.js +0 -182
  23. package/src/models/audit.js +0 -146
  24. package/src/models/base.js +0 -42
  25. package/src/models/configuration.js +0 -194
  26. package/src/models/experiment.js +0 -67
  27. package/src/models/importer/import-constants.js +0 -40
  28. package/src/models/importer/import-job.js +0 -252
  29. package/src/models/importer/import-url.js +0 -115
  30. package/src/models/key-event.js +0 -81
  31. package/src/models/organization.js +0 -120
  32. package/src/models/site-candidate.js +0 -111
  33. package/src/models/site-top-page.js +0 -62
  34. package/src/models/site.js +0 -218
  35. package/src/service/api-key/accessPatterns.js +0 -110
  36. package/src/service/api-key/index.js +0 -37
  37. package/src/service/audits/accessPatterns.js +0 -284
  38. package/src/service/audits/index.js +0 -72
  39. package/src/service/configurations/accessPatterns.js +0 -123
  40. package/src/service/configurations/index.js +0 -38
  41. package/src/service/experiments/accessPatterns.js +0 -98
  42. package/src/service/experiments/index.js +0 -40
  43. package/src/service/import-job/accessPatterns.js +0 -134
  44. package/src/service/import-job/index.js +0 -60
  45. package/src/service/import-url/accessPatterns.js +0 -153
  46. package/src/service/import-url/index.js +0 -53
  47. package/src/service/key-events/accessPatterns.js +0 -92
  48. package/src/service/key-events/index.js +0 -38
  49. package/src/service/organizations/accessPatterns.js +0 -160
  50. package/src/service/organizations/index.js +0 -51
  51. package/src/service/site-candidates/accessPatterns.js +0 -112
  52. package/src/service/site-candidates/index.js +0 -48
  53. package/src/service/site-top-pages/accessPatterns.js +0 -83
  54. package/src/service/site-top-pages/index.js +0 -42
  55. package/src/service/sites/accessPatterns.js +0 -467
  56. package/src/service/sites/index.js +0 -116
  57. package/src/v2/index.js +0 -15
  58. package/src/v2/models/index.d.ts +0 -26
  59. /package/src/{v2/errors → errors}/data-access.error.js +0 -0
  60. /package/src/{v2/errors → errors}/index.d.ts +0 -0
  61. /package/src/{v2/errors → errors}/index.js +0 -0
  62. /package/src/{v2/errors → errors}/reference.error.js +0 -0
  63. /package/src/{v2/errors → errors}/schema-validation.error.js +0 -0
  64. /package/src/{v2/errors → errors}/schema.builder.error.js +0 -0
  65. /package/src/{v2/errors → errors}/schema.error.js +0 -0
  66. /package/src/{v2/errors → errors}/validation.error.js +0 -0
  67. /package/src/{v2/models → models}/api-key/api-key.collection.js +0 -0
  68. /package/src/{v2/models → models}/api-key/api-key.model.js +0 -0
  69. /package/src/{v2/models → models}/api-key/api-key.schema.js +0 -0
  70. /package/src/{v2/models → models}/api-key/index.d.ts +0 -0
  71. /package/src/{v2/models → models}/api-key/index.js +0 -0
  72. /package/src/{v2/models → models}/audit/audit.collection.js +0 -0
  73. /package/src/{v2/models → models}/audit/audit.model.js +0 -0
  74. /package/src/{v2/models → models}/audit/audit.schema.js +0 -0
  75. /package/src/{v2/models → models}/audit/index.d.ts +0 -0
  76. /package/src/{v2/models → models}/audit/index.js +0 -0
  77. /package/src/{v2/models → models}/base/base.collection.js +0 -0
  78. /package/src/{v2/models → models}/base/base.model.js +0 -0
  79. /package/src/{v2/models → models}/base/entity.registry.js +0 -0
  80. /package/src/{v2/models → models}/base/index.d.ts +0 -0
  81. /package/src/{v2/models → models}/base/index.js +0 -0
  82. /package/src/{v2/models → models}/base/reference.js +0 -0
  83. /package/src/{v2/models → models}/base/schema.builder.js +0 -0
  84. /package/src/{v2/models → models}/base/schema.js +0 -0
  85. /package/src/{v2/models → models}/configuration/configuration.collection.js +0 -0
  86. /package/src/{v2/models → models}/configuration/configuration.model.js +0 -0
  87. /package/src/{v2/models → models}/configuration/configuration.schema.js +0 -0
  88. /package/src/{v2/models → models}/configuration/index.d.ts +0 -0
  89. /package/src/{v2/models → models}/configuration/index.js +0 -0
  90. /package/src/{v2/models → models}/experiment/experiment.collection.js +0 -0
  91. /package/src/{v2/models → models}/experiment/experiment.model.js +0 -0
  92. /package/src/{v2/models → models}/experiment/experiment.schema.js +0 -0
  93. /package/src/{v2/models → models}/experiment/index.d.ts +0 -0
  94. /package/src/{v2/models → models}/experiment/index.js +0 -0
  95. /package/src/{v2/models → models}/import-job/import-job.collection.js +0 -0
  96. /package/src/{v2/models → models}/import-job/import-job.model.js +0 -0
  97. /package/src/{v2/models → models}/import-job/import-job.schema.js +0 -0
  98. /package/src/{v2/models → models}/import-job/index.d.ts +0 -0
  99. /package/src/{v2/models → models}/import-job/index.js +0 -0
  100. /package/src/{v2/models → models}/import-url/import-url.collection.js +0 -0
  101. /package/src/{v2/models → models}/import-url/import-url.model.js +0 -0
  102. /package/src/{v2/models → models}/import-url/import-url.schema.js +0 -0
  103. /package/src/{v2/models → models}/import-url/index.d.ts +0 -0
  104. /package/src/{v2/models → models}/import-url/index.js +0 -0
  105. /package/src/{v2/models → models}/index.js +0 -0
  106. /package/src/{v2/models → models}/key-event/index.d.ts +0 -0
  107. /package/src/{v2/models → models}/key-event/index.js +0 -0
  108. /package/src/{v2/models → models}/key-event/key-event.collection.js +0 -0
  109. /package/src/{v2/models → models}/key-event/key-event.model.js +0 -0
  110. /package/src/{v2/models → models}/key-event/key-event.schema.js +0 -0
  111. /package/src/{v2/models → models}/latest-audit/index.d.ts +0 -0
  112. /package/src/{v2/models → models}/latest-audit/index.js +0 -0
  113. /package/src/{v2/models → models}/latest-audit/latest-audit.collection.js +0 -0
  114. /package/src/{v2/models → models}/latest-audit/latest-audit.model.js +0 -0
  115. /package/src/{v2/models → models}/latest-audit/latest-audit.schema.js +0 -0
  116. /package/src/{v2/models → models}/opportunity/index.d.ts +0 -0
  117. /package/src/{v2/models → models}/opportunity/index.js +0 -0
  118. /package/src/{v2/models → models}/opportunity/opportunity.collection.js +0 -0
  119. /package/src/{v2/models → models}/opportunity/opportunity.model.js +0 -0
  120. /package/src/{v2/models → models}/opportunity/opportunity.schema.js +0 -0
  121. /package/src/{v2/models → models}/organization/index.d.ts +0 -0
  122. /package/src/{v2/models → models}/organization/index.js +0 -0
  123. /package/src/{v2/models → models}/organization/organization.collection.js +0 -0
  124. /package/src/{v2/models → models}/organization/organization.model.js +0 -0
  125. /package/src/{v2/models → models}/site/index.d.ts +0 -0
  126. /package/src/{v2/models → models}/site/index.js +0 -0
  127. /package/src/{v2/models → models}/site/site.collection.js +0 -0
  128. /package/src/{v2/models → models}/site/site.model.js +0 -0
  129. /package/src/{v2/models → models}/site-candidate/index.d.ts +0 -0
  130. /package/src/{v2/models → models}/site-candidate/index.js +0 -0
  131. /package/src/{v2/models → models}/site-candidate/site-candidate.collection.js +0 -0
  132. /package/src/{v2/models → models}/site-candidate/site-candidate.model.js +0 -0
  133. /package/src/{v2/models → models}/site-candidate/site-candidate.schema.js +0 -0
  134. /package/src/{v2/models → models}/site-top-page/index.d.ts +0 -0
  135. /package/src/{v2/models → models}/site-top-page/index.js +0 -0
  136. /package/src/{v2/models → models}/site-top-page/site-top-page.collection.js +0 -0
  137. /package/src/{v2/models → models}/site-top-page/site-top-page.model.js +0 -0
  138. /package/src/{v2/models → models}/site-top-page/site-top-page.schema.js +0 -0
  139. /package/src/{v2/models → models}/suggestion/index.d.ts +0 -0
  140. /package/src/{v2/models → models}/suggestion/index.js +0 -0
  141. /package/src/{v2/models → models}/suggestion/suggestion.collection.js +0 -0
  142. /package/src/{v2/models → models}/suggestion/suggestion.model.js +0 -0
  143. /package/src/{v2/models → models}/suggestion/suggestion.schema.js +0 -0
  144. /package/src/{v2/readme.md → readme.md} +0 -0
  145. /package/src/{v2/util → util}/accessor.utils.js +0 -0
  146. /package/src/{v2/util → util}/guards.d.ts +0 -0
  147. /package/src/{v2/util → util}/guards.js +0 -0
  148. /package/src/{v2/util → util}/index.d.ts +0 -0
  149. /package/src/{v2/util → util}/index.js +0 -0
  150. /package/src/{v2/util → util}/patcher.js +0 -0
  151. /package/src/{v2/util → util}/util.js +0 -0
@@ -1,52 +0,0 @@
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 { createKeyEvent } from '../models/key-event.js';
14
-
15
- /**
16
- * Data transfer object for Key Event.
17
- */
18
- export const KeyEventDto = {
19
- /**
20
- * Converts a DynamoDB item into a KeyEvent object.
21
- * @param {object } dynamoItem - DynamoDB item.
22
- * @returns {Readonly<KeyEvent>} KeyEvent object.
23
- */
24
- fromDynamoItem: (dynamoItem) => {
25
- const keyEventData = {
26
- id: dynamoItem.id,
27
- siteId: dynamoItem.siteId,
28
- name: dynamoItem.name,
29
- type: dynamoItem.type,
30
- time: dynamoItem.time,
31
- createdAt: dynamoItem.createdAt,
32
- updatedAt: dynamoItem.updatedAt,
33
- };
34
-
35
- return createKeyEvent(keyEventData);
36
- },
37
-
38
- /**
39
- * Converts a KeyEvent object into a DynamoDB item.
40
- * @param {Readonly<KeyEvent>} keyEvent - KeyEvent object.
41
- * @returns {object} DynamoDB item.
42
- */
43
- toDynamoItem: (keyEvent) => ({
44
- id: keyEvent.getId(),
45
- siteId: keyEvent.getSiteId(),
46
- name: keyEvent.getName(),
47
- type: keyEvent.getType(),
48
- time: keyEvent.getTime(),
49
- createdAt: keyEvent.getCreatedAt(),
50
- updatedAt: keyEvent.getUpdatedAt(),
51
- }),
52
- };
@@ -1,54 +0,0 @@
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 { Config } from '../models/site/config.js';
14
- import { createOrganization } from '../models/organization.js';
15
-
16
- /**
17
- * Data transfer object for Organization.
18
- */
19
- export const OrganizationDto = {
20
- /**
21
- * Converts an Organization object into a DynamoDB item.
22
- * @param {Readonly<Organization>} organization - Organization object.
23
- * @returns {{createdAt, baseURL, GSI1PK: string, id, imsOrgId, updatedAt}}
24
- */
25
- toDynamoItem: (organization) => ({
26
- id: organization.getId(),
27
- name: organization.getName(),
28
- imsOrgId: organization.getImsOrgId(),
29
- createdAt: organization.getCreatedAt(),
30
- updatedAt: organization.getUpdatedAt(),
31
- GSI1PK: 'ALL_ORGANIZATIONS',
32
- config: Config.toDynamoItem(organization.getConfig()),
33
- fulfillableItems: organization.getFulfillableItems(),
34
- }),
35
-
36
- /**
37
- * Converts a DynamoDB item into a Organization object.
38
- * @param {object } dynamoItem - DynamoDB item.
39
- * @returns {Readonly<Organization>} Organization object.
40
- */
41
- fromDynamoItem: (dynamoItem) => {
42
- const organizationData = {
43
- id: dynamoItem.id,
44
- name: dynamoItem.name,
45
- imsOrgId: dynamoItem.imsOrgId,
46
- createdAt: dynamoItem.createdAt,
47
- updatedAt: dynamoItem.updatedAt,
48
- config: dynamoItem.config,
49
- fulfillableItems: dynamoItem.fulfillableItems,
50
- };
51
-
52
- return createOrganization(organizationData);
53
- },
54
- };
@@ -1,54 +0,0 @@
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 { createSiteCandidate } from '../models/site-candidate.js';
14
-
15
- /**
16
- * Data transfer object for Site Candidate.
17
- */
18
- export const SiteCandidateDto = {
19
- /**
20
- * Converts a Site Candidate object into a DynamoDB item.
21
- * @param {Readonly<SiteCandidate>} siteCandidate - Site Candidate object.
22
- * @returns {{baseURL, siteId, source, status, createdAt, updatedAt, updatedBy}}
23
- */
24
- toDynamoItem: (siteCandidate) => ({
25
- baseURL: siteCandidate.getBaseURL(),
26
- hlxConfig: siteCandidate.getHlxConfig(),
27
- siteId: siteCandidate.getSiteId(),
28
- source: siteCandidate.getSource(),
29
- status: siteCandidate.getStatus(),
30
- createdAt: siteCandidate.getCreatedAt(),
31
- updatedAt: siteCandidate.getUpdatedAt(),
32
- updatedBy: siteCandidate.getUpdatedBy(),
33
- }),
34
-
35
- /**
36
- * Converts a DynamoDB item into Site Candidate object;
37
- * @param {object } dynamoItem - DynamoDB item.
38
- * @returns {Readonly<SiteCandidate>} SiteCandidate object.
39
- */
40
- fromDynamoItem: (dynamoItem) => {
41
- const siteCandidateData = {
42
- baseURL: dynamoItem.baseURL,
43
- hlxConfig: dynamoItem.hlxConfig,
44
- siteId: dynamoItem.siteId,
45
- source: dynamoItem.source,
46
- status: dynamoItem.status,
47
- createdAt: dynamoItem.createdAt,
48
- updatedAt: dynamoItem.updatedAt,
49
- updatedBy: dynamoItem.updatedBy,
50
- };
51
-
52
- return createSiteCandidate(siteCandidateData);
53
- },
54
- };
@@ -1,52 +0,0 @@
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 { createSiteTopPage } from '../models/site-top-page.js';
14
-
15
- export function padWithZeros(number) {
16
- return String(number).padStart(12, '0');
17
- }
18
-
19
- export const SiteTopPageDto = {
20
- /**
21
- * Converts a SiteTopPage object into a DynamoDB item.
22
- * @param {Readonly<SiteTopPage>} siteTopPage - SiteTopPage object.
23
- * @returns {{siteId, url, traffic, source, geo, importedAt, SK: string}}
24
- */
25
- toDynamoItem: (siteTopPage) => ({
26
- siteId: siteTopPage.getSiteId(),
27
- url: siteTopPage.getURL(),
28
- traffic: siteTopPage.getTraffic(),
29
- topKeyword: siteTopPage.getTopKeyword(),
30
- source: siteTopPage.getSource(),
31
- geo: siteTopPage.getGeo(),
32
- importedAt: siteTopPage.getImportedAt(),
33
- SK: `${siteTopPage.getSource()}#${siteTopPage.getGeo()}#${padWithZeros(siteTopPage.getTraffic())}`,
34
- }),
35
-
36
- /**
37
- * Converts a DynamoDB item into a SiteTopPage object.
38
- * @param {
39
- * {siteId, url, traffic, topKeyword, source, geo, importedAt, SK: string}
40
- * } item - DynamoDB item.
41
- * @returns {SiteTopPage}
42
- */
43
- fromDynamoItem: (item) => createSiteTopPage({
44
- siteId: item.siteId,
45
- url: item.url,
46
- traffic: item.traffic,
47
- topKeyword: item.topKeyword,
48
- source: item.source,
49
- geo: item.geo,
50
- importedAt: item.importedAt,
51
- }),
52
- };
package/src/dto/site.js DELETED
@@ -1,62 +0,0 @@
1
- /*
2
- * Copyright 2023 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 { createSite } from '../models/site.js';
14
- import { Config } from '../models/site/config.js';
15
-
16
- /**
17
- * Data transfer object for Site.
18
- */
19
- export const SiteDto = {
20
- /**
21
- * Converts a Site object into a DynamoDB item.
22
- * @param {Readonly<Site>} site - Site object.
23
- * @returns {{createdAt, baseURL, GSI1PK: string, id, organizationId, updatedAt}}
24
- */
25
- toDynamoItem: (site) => ({
26
- id: site.getId(),
27
- baseURL: site.getBaseURL(),
28
- deliveryType: site.getDeliveryType(),
29
- gitHubURL: site.getGitHubURL() || '',
30
- hlxConfig: site.getHlxConfig(),
31
- organizationId: site.getOrganizationId() || '',
32
- isLive: site.isLive(),
33
- isLiveToggledAt: site.getIsLiveToggledAt(),
34
- createdAt: site.getCreatedAt(),
35
- updatedAt: site.getUpdatedAt(),
36
- GSI1PK: 'ALL_SITES',
37
- config: Config.toDynamoItem(site.getConfig()),
38
- }),
39
-
40
- /**
41
- * Converts a DynamoDB item into a Site object.
42
- * @param {object } dynamoItem - DynamoDB item.
43
- * @returns {Readonly<Site>} Site object.
44
- */
45
- fromDynamoItem: (dynamoItem) => {
46
- const siteData = {
47
- id: dynamoItem.id,
48
- baseURL: dynamoItem.baseURL,
49
- deliveryType: dynamoItem.deliveryType,
50
- gitHubURL: dynamoItem.gitHubURL,
51
- hlxConfig: dynamoItem.hlxConfig,
52
- organizationId: dynamoItem.organizationId,
53
- isLive: dynamoItem.isLive,
54
- isLiveToggledAt: dynamoItem.isLiveToggledAt,
55
- createdAt: dynamoItem.createdAt,
56
- updatedAt: dynamoItem.updatedAt,
57
- config: dynamoItem.config,
58
- };
59
-
60
- return createSite(siteData);
61
- },
62
- };
@@ -1,182 +0,0 @@
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 {
14
- hasText, isIsoDate, isObject, isValidUrl,
15
- } from '@adobe/spacecat-shared-utils';
16
- import { Base } from '../base.js';
17
-
18
- // List of known scope names that can be used with scoped API keys
19
- const scopeNames = [
20
- 'sites.read_all',
21
- 'sites.write_all',
22
- 'organizations.read_all',
23
- 'organizations.write_all',
24
- 'audits.read_all',
25
- 'audits.write_all',
26
- 'imports.read',
27
- 'imports.write',
28
- 'imports.delete',
29
- 'imports.read_all',
30
- 'imports.all_domains',
31
- 'imports.assistant',
32
- ];
33
-
34
- const ApiKey = (data) => {
35
- const self = Base(data);
36
-
37
- self.getHashedApiKey = () => self.state.hashedApiKey;
38
- self.getName = () => self.state.name;
39
- self.getImsUserId = () => self.state.imsUserId;
40
- self.getImsOrgId = () => self.state.imsOrgId;
41
- self.getCreatedAt = () => self.state.createdAt;
42
- self.getExpiresAt = () => self.state.expiresAt;
43
- self.getRevokedAt = () => self.state.revokedAt;
44
- self.getDeletedAt = () => self.state.deletedAt;
45
- self.getScopes = () => self.state.scopes;
46
-
47
- /**
48
- * Checks if the apiKey is valid.
49
- * @returns {boolean} True if the apiKey is valid, false otherwise
50
- */
51
- self.isValid = () => {
52
- const now = new Date();
53
-
54
- if (self.state.deletedAt && new Date(self.state.deletedAt) < now) {
55
- return false;
56
- }
57
-
58
- if (self.state.revokedAt && new Date(self.state.revokedAt) < now) {
59
- return false;
60
- }
61
-
62
- if (self.state.expiresAt && new Date(self.state.expiresAt) < now) {
63
- return false;
64
- }
65
-
66
- return true;
67
- };
68
-
69
- /**
70
- * Updates the state of the ApiKey.
71
- * @param key - The key to update.
72
- * @param value - The new value.
73
- * @param validator - An optional validation function to use before updating the value.
74
- * @returns {ApiKey} The updated ApiKey object.
75
- */
76
- const updateState = (key, value, validator) => {
77
- if (validator && typeof validator === 'function') {
78
- validator(value);
79
- }
80
-
81
- self.state[key] = value;
82
- self.touch();
83
-
84
- return self;
85
- };
86
-
87
- /**
88
- * Updates the deletedAt attribute of the ApiKey.
89
- * @param {string} deletedAt - The deletedAt timestamp - ISO 8601 date string.
90
- */
91
- self.updateDeletedAt = (deletedAt) => updateState('deletedAt', deletedAt, (value) => {
92
- if (!isIsoDate(value)) {
93
- throw new Error(`Invalid deletedAt during update: ${value}. Must be a valid ISO 8601 date string.`);
94
- }
95
- });
96
-
97
- /**
98
- * Updates the expiresAt attribute of the ApiKey.
99
- * @param {string} expiresAt - The expiresAt timestamp - ISO 8601 date string.
100
- */
101
- self.updateExpiresAt = (expiresAt) => updateState('expiresAt', expiresAt, (value) => {
102
- if (!isIsoDate(value)) {
103
- throw new Error(`Invalid expiresAt during update: ${value}. Must be a valid ISO 8601 date string.`);
104
- }
105
- });
106
-
107
- /**
108
- * Updates the revokedAt attribute of the ApiKey.
109
- * @param {string} revokedAt - The revokedAt timestamp - ISO 8601 date string.
110
- */
111
- self.updateRevokedAt = (revokedAt) => updateState('revokedAt', revokedAt, (value) => {
112
- if (!isIsoDate(value)) {
113
- throw new Error(`Invalid revokedAt during update: ${value}. Must be a valid ISO 8601 date string.`);
114
- }
115
- });
116
-
117
- return Object.freeze(self);
118
- };
119
-
120
- /**
121
- * Creates a new ApiKey object.
122
- * @param {Object} apiKeyData - The data for the ApiKey object.
123
- * @returns {ApiKey} The new ApiKey object.
124
- */
125
- export const createApiKey = (apiKeyData) => {
126
- const newState = { ...apiKeyData };
127
-
128
- if (!hasText(newState.hashedApiKey)) {
129
- throw new Error(`Invalid Hashed API Key: ${newState.hashedApiKey}`);
130
- }
131
-
132
- if (!hasText(newState.name)) {
133
- throw new Error(`Invalid Name: ${newState.name}`);
134
- }
135
-
136
- if (hasText(newState.createdAt) && !isIsoDate(newState.createdAt)) {
137
- throw new Error(`createdAt should be a valid ISO 8601 string: ${newState.createdAt}`);
138
- }
139
-
140
- if (!hasText(newState.createdAt)) {
141
- newState.createdAt = new Date().toISOString();
142
- }
143
-
144
- if (hasText(newState.expiresAt) && !isIsoDate(newState.expiresAt)) {
145
- throw new Error(`expiresAt should be a valid ISO 8601 string: ${newState.expiresAt}`);
146
- }
147
-
148
- if (hasText(newState.revokedAt) && !isIsoDate(newState.revokedAt)) {
149
- throw new Error(`revokedAt should be a valid ISO 8601 string: ${newState.revokedAt}`);
150
- }
151
-
152
- if (!Array.isArray(newState.scopes)) {
153
- throw new Error(`Invalid scopes: ${newState.scopes}`);
154
- }
155
-
156
- for (const scope of newState.scopes) {
157
- if (!isObject(scope)) {
158
- throw new Error(`Invalid scope: ${scope}`);
159
- }
160
-
161
- if (!hasText(scope.name)) {
162
- throw new Error(`Invalid scope name: ${scope.name}`);
163
- }
164
-
165
- if (!scopeNames.includes(scope.name)) {
166
- throw new Error(`Scope name is not part of the pre-defined scopes: ${scope.name}`);
167
- }
168
-
169
- if (scope.domains) {
170
- if (!Array.isArray(scope.domains)) {
171
- throw new Error(`Scope domains should be an array: ${scope.domains}`);
172
- }
173
- for (const domain of scope.domains) {
174
- if (!isValidUrl(domain)) {
175
- throw new Error(`Invalid domain: ${domain}`);
176
- }
177
- }
178
- }
179
- }
180
-
181
- return ApiKey(newState);
182
- };
@@ -1,146 +0,0 @@
1
- /*
2
- * Copyright 2023 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 { hasText, isIsoDate, isObject } from '@adobe/spacecat-shared-utils';
14
- import { Base } from './base.js';
15
-
16
- // some of these unused exports are being imported from other projects. Handle with care.
17
- export const AUDIT_TYPE_404 = '404';
18
- export const AUDIT_TYPE_BROKEN_BACKLINKS = 'broken-backlinks';
19
- export const AUDIT_TYPE_EXPERIMENTATION = 'experimentation';
20
- export const AUDIT_TYPE_ORGANIC_KEYWORDS = 'organic-keywords';
21
- export const AUDIT_TYPE_ORGANIC_TRAFFIC = 'organic-traffic';
22
- export const AUDIT_TYPE_CWV = 'cwv';
23
- export const AUDIT_TYPE_LHS_DESKTOP = 'lhs-desktop';
24
- export const AUDIT_TYPE_LHS_MOBILE = 'lhs-mobile';
25
- export const AUDIT_TYPE_EXPERIMENTATION_ESS_MONTHLY = 'experimentation-ess-monthly';
26
- export const AUDIT_TYPE_EXPERIMENTATION_ESS_DAILY = 'experimentation-ess-daily';
27
-
28
- const EXPIRES_IN_DAYS = 30 * 6;
29
-
30
- const AUDIT_TYPE_PROPERTIES = {
31
- [AUDIT_TYPE_LHS_DESKTOP]: ['performance', 'seo', 'accessibility', 'best-practices'],
32
- [AUDIT_TYPE_LHS_MOBILE]: ['performance', 'seo', 'accessibility', 'best-practices'],
33
- };
34
-
35
- /**
36
- * Validates if the auditResult contains the required properties for the given audit type.
37
- * @param {object} auditResult - The audit result to validate.
38
- * @param {string} auditType - The type of the audit.
39
- * @returns {boolean} - True if valid, false otherwise.
40
- */
41
- const validateScores = (auditResult, auditType) => {
42
- if (isObject(auditResult.runtimeError)) {
43
- return true;
44
- }
45
-
46
- if ((auditType === AUDIT_TYPE_LHS_DESKTOP || auditType === AUDIT_TYPE_LHS_MOBILE)
47
- && !isObject(auditResult.scores)) {
48
- throw new Error(`Missing scores property for audit type '${auditType}'`);
49
- }
50
-
51
- const expectedProperties = AUDIT_TYPE_PROPERTIES[auditType];
52
-
53
- if (expectedProperties) {
54
- for (const prop of expectedProperties) {
55
- if (!(prop in auditResult.scores)) {
56
- throw new Error(`Missing expected property '${prop}' for audit type '${auditType}'`);
57
- }
58
- }
59
- }
60
-
61
- return true;
62
- };
63
-
64
- /**
65
- * Creates a new Audit.
66
- * @param {object } data - audit data
67
- * @returns {Readonly<Audit>} audit - new audit
68
- */
69
- const Audit = (data = {}) => {
70
- const self = Base(data);
71
-
72
- self.getSiteId = () => self.state.siteId;
73
- self.getAuditedAt = () => self.state.auditedAt;
74
- self.getAuditResult = () => self.state.auditResult;
75
- self.getAuditType = () => self.state.auditType.toLowerCase();
76
- self.getExpiresAt = () => self.state.expiresAt;
77
- self.getFullAuditRef = () => self.state.fullAuditRef;
78
- self.isLive = () => self.state.isLive;
79
- self.isError = () => hasText(self.getAuditResult().runtimeError?.code);
80
- self.getPreviousAuditResult = () => self.state.previousAuditResult;
81
- self.setPreviousAuditResult = (previousAuditResult) => {
82
- validateScores(previousAuditResult, self.getAuditType());
83
- self.state.previousAuditResult = previousAuditResult;
84
- };
85
- self.getScores = () => self.getAuditResult().scores;
86
-
87
- return Object.freeze(self);
88
- };
89
-
90
- function isValidAuditResult(auditResult) {
91
- return isObject(auditResult) || Array.isArray(auditResult);
92
- }
93
-
94
- /**
95
- * Creates a new Audit.
96
- *
97
- * @param {object} data - audit data
98
- * @returns {Readonly<Audit>} audit - new audit
99
- */
100
- export const createAudit = (data) => {
101
- const newState = { ...data };
102
-
103
- if (!hasText(newState.siteId)) {
104
- throw new Error('Site ID must be provided');
105
- }
106
-
107
- if (!isIsoDate(newState.auditedAt)) {
108
- throw new Error('Audited at must be a valid ISO date');
109
- }
110
-
111
- if (!hasText(newState.auditType)) {
112
- throw new Error('Audit type must be provided');
113
- }
114
-
115
- if (!isValidAuditResult(newState.auditResult)) {
116
- throw new Error('Audit result must be an object or an array');
117
- }
118
-
119
- if (!newState.auditResult.scores) {
120
- newState.auditResult.scores = {};
121
- }
122
- validateScores(data.auditResult, data.auditType);
123
-
124
- if (data.previousAuditResult && !isValidAuditResult(data.previousAuditResult)) {
125
- throw new Error('Previous audit result must be an object or an array');
126
- }
127
-
128
- if (data.previousAuditResult) {
129
- validateScores(data.previousAuditResult, data.auditType);
130
- }
131
-
132
- if (!hasText(newState.fullAuditRef)) {
133
- throw new Error('Full audit ref must be provided');
134
- }
135
-
136
- if (!newState.expiresAt) {
137
- newState.expiresAt = new Date(newState.auditedAt);
138
- newState.expiresAt.setDate(newState.expiresAt.getDate() + EXPIRES_IN_DAYS);
139
- }
140
-
141
- if (!Object.prototype.hasOwnProperty.call(newState, 'isLive')) {
142
- newState.isLive = false;
143
- }
144
-
145
- return Audit(newState);
146
- };
@@ -1,42 +0,0 @@
1
- /*
2
- * Copyright 2023 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 { v4 as uuidv4 } from 'uuid';
14
- import { isString } from '@adobe/spacecat-shared-utils';
15
-
16
- /**
17
- * Base model.
18
- *
19
- * @param {object} data data
20
- * @returns {Base} base model
21
- */
22
- export const Base = (data = {}) => {
23
- const self = { state: { ...data } };
24
- const newRecord = !isString(self.state.id);
25
- const nowISO = new Date().toISOString();
26
-
27
- if (newRecord) {
28
- self.state.id = uuidv4();
29
- self.state.createdAt = nowISO;
30
- self.state.updatedAt = nowISO;
31
- }
32
-
33
- self.getId = () => self.state.id;
34
- self.getCreatedAt = () => self.state.createdAt;
35
- self.getUpdatedAt = () => self.state.updatedAt;
36
-
37
- self.touch = () => {
38
- self.state.updatedAt = new Date().toISOString();
39
- };
40
-
41
- return self;
42
- };