@adobe/spacecat-shared-utils 1.50.0 → 1.50.2

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 (3) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/package.json +9 -9
  3. package/src/aem.js +74 -7
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [@adobe/spacecat-shared-utils-v1.50.2](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.50.1...@adobe/spacecat-shared-utils-v1.50.2) (2025-09-06)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * get pageId from content-page-ref if content-page-id not available ([#935](https://github.com/adobe/spacecat-shared/issues/935)) ([df585ce](https://github.com/adobe/spacecat-shared/commit/df585ce09d01a1e1c3b282d3a967b9ab138e3fa4))
7
+
8
+ # [@adobe/spacecat-shared-utils-v1.50.1](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.50.0...@adobe/spacecat-shared-utils-v1.50.1) (2025-09-06)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * **deps:** update external fixes ([#920](https://github.com/adobe/spacecat-shared/issues/920)) ([1a6b1e1](https://github.com/adobe/spacecat-shared/commit/1a6b1e1ac9531a41c86406ada4bd4ab903307fdc))
14
+
1
15
  # [@adobe/spacecat-shared-utils-v1.50.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.49.0...@adobe/spacecat-shared-utils-v1.50.0) (2025-08-28)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-utils",
3
- "version": "1.50.0",
3
+ "version": "1.50.2",
4
4
  "description": "Shared modules of the Spacecat Services - utils",
5
5
  "type": "module",
6
6
  "engines": {
@@ -36,21 +36,21 @@
36
36
  "devDependencies": {
37
37
  "@adobe/helix-shared-wrap": "2.0.2",
38
38
  "@types/validator": "^13.15.2",
39
- "chai": "5.2.1",
40
- "chai-as-promised": "8.0.1",
41
- "esmock": "2.7.1",
39
+ "chai": "5.3.3",
40
+ "chai-as-promised": "8.0.2",
41
+ "esmock": "2.7.2",
42
42
  "husky": "9.1.7",
43
- "nock": "14.0.9",
43
+ "nock": "14.0.10",
44
44
  "sinon": "20.0.0",
45
- "sinon-chai": "4.0.0"
45
+ "sinon-chai": "4.0.1"
46
46
  },
47
47
  "dependencies": {
48
48
  "@adobe/fetch": "4.2.2",
49
49
  "@adobe/spacecat-shared-data-access": "2.45.0",
50
50
  "@adobe/spacecat-shared-ims-client": "1.8.3",
51
- "@aws-sdk/client-s3": "3.864.0",
52
- "@aws-sdk/client-secrets-manager": "3.864.0",
53
- "@aws-sdk/client-sqs": "3.864.0",
51
+ "@aws-sdk/client-s3": "3.883.0",
52
+ "@aws-sdk/client-secrets-manager": "3.883.0",
53
+ "@aws-sdk/client-sqs": "3.883.0",
54
54
  "@json2csv/plainjs": "7.0.6",
55
55
  "aws-xray-sdk": "3.10.3",
56
56
  "date-fns": "2.30.0",
package/src/aem.js CHANGED
@@ -201,22 +201,89 @@ export function detectAEMVersion(htmlSource, headers = {}) {
201
201
  }
202
202
 
203
203
  /**
204
- * Determines the AEM CS page ID for Content API, from the page URL
204
+ * Determines the AEM CS/AMS page ID for Content API, from the page URL
205
205
  * @param {string} pageURL - The URL of the page
206
- * @return {string|null} - The AEM CS page ID
206
+ * @param {string} authorURL - The URL of the author instance
207
+ * @param {string} bearerToken - The access token for the author instance
208
+ * @param {boolean} preferContentApi - Whether to prefer the Content API over the PSS, default is
209
+ * false
210
+ * @param {Object} log - The logger object, default is console
211
+ * @return {string|null} - The AEM page ID
207
212
  */
208
- export async function determineAEMCSPageId(pageURL) {
213
+ export async function determineAEMCSPageId(
214
+ pageURL,
215
+ authorURL,
216
+ bearerToken,
217
+ preferContentApi = false,
218
+ log = console,
219
+ ) {
220
+ log.info(`Fetching HTML from ${pageURL} to retrieve content-page-id for AEM CS Content API mode.`);
209
221
  const htmlResponse = await fetch(pageURL);
222
+
210
223
  if (!htmlResponse.ok) {
211
224
  return null;
212
225
  }
226
+
213
227
  const html = await htmlResponse.text();
214
- const metaTagRegex = /<meta\s+name=['"]content-page-id['"]\s+content=['"]([^'"]*)['"]\s*\/?>/i;
215
- const match = html.match(metaTagRegex);
228
+
229
+ // First try to find a content-page-ref meta tag
230
+ const contentPageRefRegex = /<meta\s+name=['"]content-page-ref['"]\s+content=['"]([^'"]*)['"]\s*\/?>/i;
231
+ const refMatch = html.match(contentPageRefRegex);
232
+
233
+ if (refMatch?.[1]?.trim()) {
234
+ if (!authorURL || !bearerToken) {
235
+ // If ref was present but resolution failed, return null per spec
236
+ log.warn('Content-page-ref found but authorURL or bearerToken is missing, skipping resolution.');
237
+ return null;
238
+ }
239
+ const contentPageRef = refMatch[1].trim();
240
+ try {
241
+ const base = preferContentApi
242
+ ? `${authorURL}/adobe/experimental/aspm-expires-20251231`
243
+ : `${authorURL}/adobe/experimental/pss`;
244
+ const resolveUrl = `${base}/pages/resolve?pageRef=${contentPageRef}`;
245
+ log.info(`Resolving content-page-ref via ${resolveUrl} (preferContentApi=${preferContentApi})`);
246
+ const resp = await fetch(resolveUrl, {
247
+ method: 'GET',
248
+ headers: { Authorization: bearerToken },
249
+ redirect: 'follow',
250
+ });
251
+ if (resp.status === 200) {
252
+ let pageId = null;
253
+ if (preferContentApi) {
254
+ const data = await resp.json();
255
+ pageId = data?.id || null;
256
+ } else {
257
+ const data = await resp.text();
258
+ pageId = data || null;
259
+ }
260
+
261
+ if (pageId) {
262
+ log.info(`Resolved pageId: "${pageId}" from JSON directly for ref "${contentPageRef}"`);
263
+ return pageId;
264
+ }
265
+ log.error('resolve response did not contain an "id" property.');
266
+ return null;
267
+ } else {
268
+ log.warn(`Unexpected status ${resp.status} when resolving content-page-ref.`);
269
+ }
270
+ } catch (e) {
271
+ log.error(`Error while resolving content-page-ref: ${e.message}`);
272
+ }
273
+ // If ref was present but resolution failed, return null per spec
274
+ return null;
275
+ }
276
+
277
+ // Fallback to content-page-id meta tag
278
+ const contentPageIdRegex = /<meta\s+name=['"]content-page-id['"]\s+content=['"]([^'"]*)['"]\s*\/?>/i;
279
+ const idMatch = html.match(contentPageIdRegex);
216
280
 
217
281
  let pageId = null;
218
- if (match && match[1] && match[1].trim() !== '') {
219
- pageId = match[1].trim();
282
+ if (idMatch?.[1]?.trim()) {
283
+ pageId = idMatch[1].trim();
284
+ if (pageId) {
285
+ log.info(`Extracted pageId: "${pageId}" from "content-page-id" meta tag at ${pageURL}`);
286
+ }
220
287
  }
221
288
  return pageId;
222
289
  }