@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,194 +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 Joi from 'joi';
14
-
15
- const Configuration = (data = {}) => {
16
- const state = { ...data };
17
- const self = { state };
18
- self.getSlackRoles = () => self.state.slackRoles;
19
- self.getJobs = () => self.state.jobs;
20
- self.getVersion = () => self.state.version;
21
- self.getQueues = () => self.state.queues;
22
- self.getHandlers = () => self.state.handlers;
23
- self.getHandler = (type) => self.state.handlers[type];
24
- self.addHandler = (type, handlerData) => {
25
- state.handlers = state.handlers || {};
26
- state.handlers[type] = { ...handlerData };
27
- };
28
- self.getSlackRoleMembersByRole = (role) => {
29
- const roles = self.getSlackRoles();
30
- return roles ? roles[role] : [];
31
- };
32
- self.getEnabledSiteIdsForHandler = (type) => {
33
- const handler = state.handlers?.[type];
34
- return handler?.enabled?.sites || [];
35
- };
36
- self.isHandlerEnabledForSite = (type, site) => {
37
- const handler = state.handlers[type];
38
- if (!handler) return false;
39
-
40
- const siteId = site.getId();
41
- const orgId = site.getOrganizationId();
42
-
43
- if (handler.enabled) {
44
- const sites = handler.enabled.sites || [];
45
- const orgs = handler.enabled.orgs || [];
46
- return sites.includes(siteId) || orgs.includes(orgId);
47
- }
48
-
49
- if (handler.disabled) {
50
- const sites = handler.disabled.sites || [];
51
- const orgs = handler.disabled.orgs || [];
52
- return !(sites.includes(siteId) || orgs.includes(orgId));
53
- }
54
-
55
- return handler.enabledByDefault;
56
- };
57
-
58
- self.isHandlerEnabledForOrg = (type, org) => {
59
- const handler = state.handlers[type];
60
- if (!handler) return false;
61
-
62
- const orgId = org.getId();
63
-
64
- if (handler.enabled) {
65
- return handler.enabled.orgs.includes(orgId);
66
- }
67
-
68
- if (handler.disabled) {
69
- return !handler.disabled.orgs.includes(orgId);
70
- }
71
-
72
- return handler.enabledByDefault;
73
- };
74
-
75
- const updateHandlerOrgs = (type, orgId, enabled) => {
76
- const handler = state.handlers[type];
77
- if (!handler) return;
78
-
79
- if (enabled) {
80
- if (handler.enabledByDefault) {
81
- handler.disabled.orgs = handler.disabled.orgs?.filter((id) => id !== orgId) || [];
82
- } else {
83
- handler.enabled = handler.enabled || { orgs: [] };
84
- handler.enabled.orgs = Array.from(new Set([...(handler.enabled?.orgs || []), orgId]));
85
- }
86
- } else if (handler.enabledByDefault) {
87
- handler.disabled = handler.disabled || { orgs: [] };
88
- handler.disabled.orgs = Array.from(new Set([...(handler.disabled?.orgs || []), orgId]));
89
- } else {
90
- handler.enabled.orgs = handler.enabled.orgs?.filter((id) => id !== orgId) || [];
91
- }
92
- };
93
-
94
- const updateHandlerSites = (type, siteId, enabled) => {
95
- const handler = state.handlers[type];
96
- if (!handler) return;
97
-
98
- if (enabled) {
99
- if (handler.enabledByDefault) {
100
- handler.disabled.sites = handler.disabled.sites?.filter((id) => id !== siteId) || [];
101
- } else {
102
- handler.enabled = handler.enabled || { sites: [] };
103
- handler.enabled.sites = Array.from(new Set([...(handler.enabled.sites || []), siteId]));
104
- }
105
- } else if (handler.enabledByDefault) {
106
- handler.disabled = handler.disabled || { sites: [] };
107
- handler.disabled.sites = Array.from(new Set([...(handler.disabled.sites || []), siteId]));
108
- } else {
109
- handler.enabled.sites = handler.enabled.sites?.filter((id) => id !== siteId) || [];
110
- }
111
- };
112
-
113
- self.enableHandlerForSite = (type, site) => {
114
- const siteId = site.getId();
115
- if (self.isHandlerEnabledForSite(type, site)) return;
116
-
117
- updateHandlerSites(type, siteId, true);
118
- };
119
-
120
- self.enableHandlerForOrg = (type, org) => {
121
- const orgId = org.getId();
122
- if (self.isHandlerEnabledForOrg(type, org)) return;
123
-
124
- updateHandlerOrgs(type, orgId, true);
125
- };
126
-
127
- self.disableHandlerForSite = (type, site) => {
128
- const siteId = site.getId();
129
- if (!self.isHandlerEnabledForSite(type, site)) return;
130
-
131
- updateHandlerSites(type, siteId, false);
132
- };
133
-
134
- self.disableHandlerForOrg = (type, org) => {
135
- const orgId = org.getId();
136
- if (!self.isHandlerEnabledForOrg(type, org)) return;
137
-
138
- updateHandlerOrgs(type, orgId, false);
139
- };
140
-
141
- return Object.freeze(self);
142
- };
143
-
144
- /**
145
- *
146
- * @param configuration
147
- * @returns {any}
148
- */
149
-
150
- export const checkConfiguration = (configuration) => {
151
- const schema = Joi.object({
152
- version: Joi.number().required(),
153
- queues: Joi.object().required(),
154
- handlers: Joi.object().pattern(Joi.string(), Joi.object(
155
- {
156
- enabled: Joi.object({
157
- sites: Joi.array().items(Joi.string()),
158
- orgs: Joi.array().items(Joi.string()),
159
- }),
160
- disabled: Joi.object({
161
- sites: Joi.array().items(Joi.string()),
162
- orgs: Joi.array().items(Joi.string()),
163
- }),
164
- enabledByDefault: Joi.boolean().required(),
165
- dependencies: Joi.array().items(Joi.object(
166
- {
167
- handler: Joi.string(),
168
- actions: Joi.array().items(Joi.string()),
169
- },
170
- )),
171
- },
172
- )),
173
- jobs: Joi.array().required(),
174
- }).unknown(true);
175
- const { error, value } = schema.validate(configuration);
176
-
177
- if (error) {
178
- throw new Error(`Configuration validation error: ${error.message}`);
179
- }
180
-
181
- return value; // Validated and sanitized configuration
182
- };
183
-
184
- /**
185
- * Creates a new Configuration.
186
- *
187
- * @param {object} data - configuration data
188
- * @returns {Readonly<Configuration>} configuration - new configuration
189
- */
190
- export const createConfiguration = (data) => {
191
- const value = checkConfiguration(data);
192
- const newState = { ...value };
193
- return Configuration(newState);
194
- };
@@ -1,67 +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 { hasText } from '@adobe/spacecat-shared-utils';
14
-
15
- import { Base } from './base.js';
16
-
17
- export const DEFAULT_UPDATED_BY = 'spacecat';
18
-
19
- /**
20
- * Creates a new Experiment.
21
- *
22
- * @param {object} data - experiment data
23
- * @returns {Readonly<SiteCandidate>} new experiment
24
- */
25
- const Experiment = (data = {}) => {
26
- const self = Base(data);
27
-
28
- self.getSiteId = () => self.state.siteId;
29
- self.getExperimentId = () => self.state.experimentId;
30
- self.getName = () => self.state.name;
31
- self.getUrl = () => self.state.url;
32
- self.getStatus = () => self.state.status;
33
- self.getType = () => self.state.type;
34
- self.getStartDate = () => self.state.startDate;
35
- self.getEndDate = () => self.state.endDate;
36
- self.getVariants = () => self.state.variants;
37
- self.getUpdatedAt = () => self.state.updatedAt;
38
- self.getUpdatedBy = () => self.state.updatedBy;
39
- self.getConversionEventName = () => self.state.conversionEventName;
40
- self.getConversionEventValue = () => self.state.conversionEventValue;
41
-
42
- return Object.freeze(self);
43
- };
44
-
45
- /**
46
- * Creates a new Experiment.
47
- *
48
- * @param {object} data - experiment data
49
- * @returns {Readonly<SiteCandidate>} new experiment
50
- */
51
- export const createExperiment = (data) => {
52
- const newState = { ...data };
53
-
54
- if (!hasText(newState.siteId)) {
55
- throw new Error('Site ID must be provided');
56
- }
57
-
58
- if (!hasText(newState.experimentId)) {
59
- throw new Error('Experiment ID must be provided');
60
- }
61
-
62
- if (!hasText(newState.updatedBy)) {
63
- newState.updatedBy = DEFAULT_UPDATED_BY;
64
- }
65
-
66
- return Experiment(newState);
67
- };
@@ -1,40 +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
- /**
14
- * Supported Import Options.
15
- */
16
- export const ImportOptions = {
17
- ENABLE_JAVASCRIPT: 'enableJavascript',
18
- PAGE_LOAD_TIMEOUT: 'pageLoadTimeout',
19
- };
20
-
21
- /**
22
- * Import Job Status types.
23
- * Any changes to this object needs to be reflected in the index.d.ts file as well.
24
- */
25
- export const ImportJobStatus = {
26
- RUNNING: 'RUNNING',
27
- COMPLETE: 'COMPLETE',
28
- FAILED: 'FAILED',
29
- STOPPED: 'STOPPED',
30
- };
31
-
32
- /**
33
- * ImportURL Status types.
34
- * Any changes to this object needs to be reflected in the index.d.ts file as well.
35
- */
36
- export const ImportUrlStatus = {
37
- PENDING: 'PENDING',
38
- REDIRECT: 'REDIRECT',
39
- ...ImportJobStatus,
40
- };
@@ -1,252 +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, isValidUrl, isObject, isString, isNumber, isInteger, isBoolean,
15
- } from '@adobe/spacecat-shared-utils';
16
- import { Base } from '../base.js';
17
- import { ImportJobStatus, ImportOptions } from './import-constants.js';
18
-
19
- /**
20
- * Creates a new ImportJob object.
21
- *
22
- * @param {Object} data - The data for the ImportJob object.
23
- * @returns {ImportJob} The new ImportJob object.
24
- */
25
- const ImportJob = (data) => {
26
- const self = Base(data);
27
-
28
- // generate get methods for all properties of our base object
29
- self.getBaseURL = () => self.state.baseURL;
30
- self.getHashedApiKey = () => self.state.hashedApiKey;
31
- self.getOptions = () => self.state.options;
32
- self.getStartTime = () => self.state.startTime;
33
- self.getEndTime = () => self.state.endTime;
34
- self.getDuration = () => self.state.duration;
35
- self.getStatus = () => self.state.status;
36
- self.getUrlCount = () => self.state.urlCount;
37
- self.getSuccessCount = () => self.state.successCount;
38
- self.getFailedCount = () => self.state.failedCount;
39
- self.getRedirectCount = () => self.state.redirectCount;
40
- self.getImportQueueId = () => self.state.importQueueId;
41
- self.getInitiatedBy = () => self.state.initiatedBy;
42
- self.hasCustomHeaders = () => self.state.hasCustomHeaders || false;
43
- self.hasCustomImportJs = () => self.state.hasCustomImportJs || false;
44
-
45
- /**
46
- * Updates the state of the ImportJob.
47
- * @param key - The key to update.
48
- * @param value - The new value.
49
- * @param validator - An optional validation function to use before updating the value.
50
- * @returns {ImportJob} The updated ImportJob object.
51
- */
52
- const updateState = (key, value, validator) => {
53
- if (validator && typeof validator === 'function') {
54
- validator(value);
55
- }
56
-
57
- self.state[key] = value;
58
- self.touch();
59
-
60
- return self;
61
- };
62
-
63
- /**
64
- * Updates the end time of the ImportJob.
65
- * @param {string} endTime - The new end time in JavaScript ISO date string in Zulu (UTC)
66
- * timezone. eg. 2024-05-29T14:36:00.000Z.
67
- */
68
- self.updateEndTime = (endTime) => updateState('endTime', endTime, (value) => {
69
- if (!isIsoDate(value)) {
70
- throw new Error(`Invalid end time during update: ${endTime}`);
71
- }
72
- });
73
-
74
- /**
75
- * Updates the duration of the ImportJob.
76
- * @param {number} duration - The new duration.
77
- */
78
- self.updateDuration = (duration) => updateState('duration', duration, (value) => {
79
- if (!isNumber(value)) {
80
- throw new Error(`Invalid duration during update: ${value}`);
81
- }
82
- });
83
-
84
- /**
85
- * Updates the status of the ImportJob.
86
- * @param {string} status - The new status.
87
- */
88
- self.updateStatus = (status) => updateState('status', status, (value) => {
89
- if (!Object.values(ImportJobStatus).includes(value)) {
90
- throw new Error(`Invalid Import Job status during update: ${value}`);
91
- }
92
- });
93
-
94
- /**
95
- * Updates the Url count of the ImportJob
96
- * @param {number} urlCount - The new url count.
97
- */
98
- self.updateUrlCount = (urlCount) => updateState('urlCount', urlCount, (value) => {
99
- if (!isInteger(value)) {
100
- throw new Error(`Invalid url count during update: ${value}`);
101
- }
102
- });
103
-
104
- /**
105
- * Updates the success count of the ImportJob.
106
- * @param {number} successCount - The new success count.
107
- * @returns {ImportJob} The updated ImportJob object.
108
- */
109
- self.updateSuccessCount = (successCount) => updateState('successCount', successCount, (value) => {
110
- if (!isInteger(value) || value < 0) {
111
- throw new Error(`Invalid success count during update: ${value}`);
112
- }
113
- });
114
-
115
- /**
116
- * Updates the failed count of the ImportJob.
117
- * @param {number} failedCount - The new failed count.
118
- * @returns {ImportJob} The updated ImportJob object.
119
- */
120
- self.updateFailedCount = (failedCount) => updateState('failedCount', failedCount, (value) => {
121
- if (!isInteger(value) || value < 0) {
122
- throw new Error(`Invalid failed count during update: ${value}`);
123
- }
124
- });
125
-
126
- /**
127
- * Updates the redirect count of the ImportJob.
128
- * @param {number} redirectCount - The new redirect count.
129
- * @returns {ImportJob} The updated ImportJob object.
130
- */
131
- self.updateRedirectCount = (redirectCount) => updateState('redirectCount', redirectCount, (value) => {
132
- if (!isInteger(value) || value < 0) {
133
- throw new Error(`Invalid redirect count during update: ${value}`);
134
- }
135
- });
136
-
137
- /**
138
- * Updates the import queue id of the ImportJob.
139
- * @param {string} importQueueId - The new import queue id.
140
- * @returns {ImportJob} The updated ImportJob object.
141
- */
142
- self.updateImportQueueId = (importQueueId) => updateState(
143
- 'importQueueId',
144
- importQueueId,
145
- (value) => {
146
- if (!hasText(importQueueId)) {
147
- throw new Error(`Invalid import queue id during update: ${value}`);
148
- }
149
- },
150
- );
151
-
152
- /**
153
- * Update the hasCustomHeaders value to true if the ImportJob has custom headers, false otherwise.
154
- * @param {boolean} hasCustomHeaders - The new value for hasCustomHeaders.
155
- * @return {ImportJob} The updated ImportJob object.
156
- */
157
- self.updateHasCustomHeaders = (hasCustomHeaders) => updateState('hasCustomHeaders', hasCustomHeaders, (value) => {
158
- if (!isBoolean(value)) {
159
- throw new Error(`Invalid hasCustomHeaders value: ${value}`);
160
- }
161
- });
162
-
163
- /**
164
- * Update the hasCustomImportJs value to true if the ImportJob has custom import js, false
165
- * otherwise.
166
- * @param {boolean} hasCustomImportJs - The new value for hasCustomImportJs.
167
- * @return {ImportJob} The updated ImportJob object.
168
- */
169
- self.updateHasCustomImportJs = (hasCustomImportJs) => updateState('hasCustomImportJs', hasCustomImportJs, (value) => {
170
- if (!isBoolean(value)) {
171
- throw new Error(`Invalid hasCustomImportJs value: ${value}`);
172
- }
173
- });
174
-
175
- return Object.freeze(self);
176
- };
177
-
178
- /**
179
- * Creates a new ImportJob object.
180
- * @param {Object} data - The data for the ImportJob object.
181
- * @returns {ImportJob} The new ImportJob object.
182
- */
183
- export const createImportJob = (data) => {
184
- // Define a list of data type validators for each import option
185
- const ImportOptionTypeValidator = {
186
- [ImportOptions.ENABLE_JAVASCRIPT]: (value) => {
187
- if (value !== true && value !== false) {
188
- throw new Error(`Invalid value for ${ImportOptions.ENABLE_JAVASCRIPT}: ${value}`);
189
- }
190
- },
191
- [ImportOptions.PAGE_LOAD_TIMEOUT]: (value) => {
192
- if (!isInteger(value) || value < 0) {
193
- throw new Error(`Invalid value for ${ImportOptions.PAGE_LOAD_TIMEOUT}: ${value}`);
194
- }
195
- },
196
- };
197
-
198
- const newState = { ...data };
199
-
200
- // set default values for the start time if one is not provided
201
- if (!hasText(newState.startTime)) {
202
- newState.startTime = new Date().toISOString();
203
- }
204
-
205
- if (!isValidUrl(newState.baseURL)) {
206
- throw new Error(`Invalid base URL: ${newState.baseURL}`);
207
- }
208
-
209
- if (!isString(newState.hashedApiKey)) {
210
- throw new Error(`Invalid API key: ${newState.hashedApiKey}`);
211
- }
212
-
213
- if (hasText(newState.startTime) && !isIsoDate(newState.startTime)) {
214
- throw new Error('"StartTime" should be a valid ISO string');
215
- }
216
-
217
- if (!Object.values(ImportJobStatus).includes(newState.status)) {
218
- throw new Error(`Invalid Import Job status ${newState.status}`);
219
- }
220
-
221
- if (newState.options) {
222
- if (!isObject(newState.options)) {
223
- throw new Error(`Invalid options: ${newState.options}`);
224
- }
225
-
226
- const invalidOptions = Object.keys(newState.options).filter(
227
- (key) => !Object.values(ImportOptions)
228
- .some((value) => value.toLowerCase() === key.toLowerCase()),
229
- );
230
-
231
- if (invalidOptions.length > 0) {
232
- throw new Error(`Invalid options: ${invalidOptions}`);
233
- }
234
-
235
- // validate each option for it's expected data type
236
- Object.keys(newState.options).forEach((key) => {
237
- if (ImportOptionTypeValidator[key]) {
238
- ImportOptionTypeValidator[key](data.options[key]);
239
- }
240
- });
241
- }
242
-
243
- if (newState.hasCustomImportJs && !isBoolean(newState.hasCustomImportJs)) {
244
- throw new Error(`Invalid hasCustomImportJs value: ${newState.hasCustomImportJs}`);
245
- }
246
-
247
- if (newState.hasCustomHeaders && !isBoolean(newState.hasCustomHeaders)) {
248
- throw new Error(`Invalid hasCustomHeaders value: ${newState.hasCustomHeaders}`);
249
- }
250
-
251
- return ImportJob(newState);
252
- };
@@ -1,115 +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 { hasText, isValidUrl } from '@adobe/spacecat-shared-utils';
14
- import { Base } from '../base.js';
15
- import { ImportUrlStatus } from './import-constants.js';
16
-
17
- export const IMPORT_URL_EXPIRES_IN_DAYS = 30;
18
-
19
- /**
20
- * Creates a new ImportUrl object
21
- *
22
- * @param {Object} data
23
- * @returns {ImportUrl}
24
- */
25
- const ImportUrl = (data) => {
26
- const self = Base(data);
27
-
28
- self.getJobId = () => self.state.jobId;
29
- self.getUrl = () => self.state.url;
30
- self.getStatus = () => self.state.status;
31
- self.getReason = () => self.state.reason;
32
- // Absolute path to the resource that is being imported for the given URL
33
- self.getPath = () => self.state.path;
34
- // Resulting path and filename of the imported .docx file
35
- self.getFile = () => self.state.file;
36
- // Expiration date for the URL
37
- self.getExpiresAt = () => self.state.expiresAt;
38
-
39
- /**
40
- * Updates the state of the ImportJob.
41
- * @param key - The key to update.
42
- * @param value - The new value.
43
- * @param {Function} validator - An optional validation function to use before updating the value.
44
- * The validator can return false to indicate that the value isn't worth throwing an exception,
45
- * but continue to use the previous value.
46
- */
47
- const updateState = (key, value, validator) => {
48
- if (validator && typeof validator === 'function') {
49
- // a validator can return true or false to indicate if the value is valid
50
- // however if a validator throws an error, it is considered critical and invalid.
51
- if (!validator(value)) {
52
- return self;
53
- }
54
- }
55
-
56
- self.state[key] = value;
57
- self.touch();
58
-
59
- return self;
60
- };
61
-
62
- /**
63
- * Updates the status of the ImportUrl
64
- */
65
- self.setStatus = (status) => updateState('status', status, (value) => {
66
- if (!ImportUrlStatus[value]) {
67
- throw new Error(`Invalid Import URL status during update: ${value}`);
68
- }
69
- return true;
70
- });
71
-
72
- /**
73
- * Updates the reason that the import of this URL was not successful.
74
- */
75
- self.setReason = (reason) => updateState('reason', reason, hasText);
76
-
77
- /**
78
- * Updates the path of the ImportUrl
79
- */
80
- self.setPath = (path) => updateState('path', path, hasText);
81
-
82
- /**
83
- * Updates the file of the ImportUrl. This is the path and file name of the file which
84
- * was imported.
85
- */
86
- self.setFile = (file) => updateState('file', file, hasText);
87
-
88
- return Object.freeze(self);
89
- };
90
-
91
- /**
92
- * Creates a new ImportUrl object
93
- */
94
- export const createImportUrl = (data) => {
95
- const newState = { ...data };
96
-
97
- if (!isValidUrl(newState.url)) {
98
- throw new Error(`Invalid Url: ${newState.url}`);
99
- }
100
-
101
- if (!hasText(newState.jobId)) {
102
- throw new Error(`Invalid Job ID: ${newState.jobId}`);
103
- }
104
-
105
- if (!Object.values(ImportUrlStatus).includes(newState.status)) {
106
- throw new Error(`Invalid Import URL status: ${newState.status}`);
107
- }
108
-
109
- if (!newState.expiresAt) {
110
- newState.expiresAt = new Date();
111
- newState.expiresAt.setDate(newState.expiresAt.getDate() + IMPORT_URL_EXPIRES_IN_DAYS);
112
- }
113
-
114
- return ImportUrl(newState);
115
- };