@amplifyup/sdk 0.1.33 → 0.1.35

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/dist/index.d.mts CHANGED
@@ -1,6 +1,7 @@
1
- import { P as PageConfig, C as ContentSource, A as AmplifyContentMeta, E as EventTrackingConfig$1, a as CDPAdapter, b as AmplifyUpConfig, c as EventProperties, I as IdentifyInput, d as IdentifyTraits, e as ProfileFieldsInput, f as AmplifyUpTarget } from './types-npF_lNoq.mjs';
2
- export { Z as ZoneConfig, i as isAmplifyUpTarget, n as normalizeAmplifyUpTarget, r as resolveAmplifyUpTarget, g as resolveAmplifyUpUrls } from './types-npF_lNoq.mjs';
1
+ import { P as PageConfig, C as ContentSource, A as AmplifyContentMeta, E as EventTrackingConfig$1, a as CDPAdapter, b as AmplifyUpConfig, c as EventProperties, I as IdentifyInput, d as IdentifyTraits, e as ProfileFieldsInput, f as AmplifyUpTarget } from './types-CBoVIFI8.mjs';
2
+ export { Z as ZoneConfig, i as isAmplifyUpTarget, n as normalizeAmplifyUpTarget, r as resolveAmplifyUpTarget, g as resolveAmplifyUpUrls } from './types-CBoVIFI8.mjs';
3
3
  export { i as isComposerPreview, a as isPreviewRequest, b as isWorkingContentRequest } from './preview-_vHmzRen.mjs';
4
+ export { g as FetchEdgeQueryOptions, F as FetchEdgeSelectOptions, Q as QueryContentError, e as QueryContentOptions, k as QueryFilter, l as QueryFilterOp, i as QueryPagination, h as QueryResult, m as QuerySort, j as QuerySpec, S as SelectVariantsError, a as SelectVariantsOptions, b as SelectVariantsResult, c as fetchEdgeQuery, f as fetchEdgeSelect, n as nextPageSpec, q as queryContent, d as searchSpec, s as selectVariants } from './query-6BmINKLR.mjs';
4
5
 
5
6
  interface FetchContentResult {
6
7
  pageConfig: PageConfig | null;
@@ -20,6 +21,10 @@ interface FetchContentOptions {
20
21
  /** Orchestrator layout fetch (SDK instance method or server helper). */
21
22
  fetchOrchestratorPage: (route: string, preview: boolean) => Promise<PageConfig | null>;
22
23
  fetchFn?: typeof fetch;
24
+ /** Next.js ISR for Edge resolve (server/SSG only). */
25
+ revalidate?: number | false;
26
+ /** Deterministic variants for ISR (no visitor id in the cache key). */
27
+ variantKeys?: Record<string, string> | null;
23
28
  }
24
29
  /**
25
30
  * Single content entry point:
@@ -80,7 +85,14 @@ declare function fetchEdgeResolve(options: {
80
85
  route: string;
81
86
  anonId?: string | null;
82
87
  siteId?: string | null;
88
+ /** Deterministic variant combo — cacheable; do not send with anonId. */
89
+ variantKeys?: Record<string, string> | null;
83
90
  fetch?: typeof fetch;
91
+ /**
92
+ * Next.js Data Cache / ISR. When set, the server fetch can be baked into
93
+ * SSG. Omit on the client so visitor-specific resolve stays fresh.
94
+ */
95
+ revalidate?: number | false;
84
96
  }): Promise<EdgeResolvedPayload>;
85
97
 
86
98
  /**
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { P as PageConfig, C as ContentSource, A as AmplifyContentMeta, E as EventTrackingConfig$1, a as CDPAdapter, b as AmplifyUpConfig, c as EventProperties, I as IdentifyInput, d as IdentifyTraits, e as ProfileFieldsInput, f as AmplifyUpTarget } from './types-npF_lNoq.js';
2
- export { Z as ZoneConfig, i as isAmplifyUpTarget, n as normalizeAmplifyUpTarget, r as resolveAmplifyUpTarget, g as resolveAmplifyUpUrls } from './types-npF_lNoq.js';
1
+ import { P as PageConfig, C as ContentSource, A as AmplifyContentMeta, E as EventTrackingConfig$1, a as CDPAdapter, b as AmplifyUpConfig, c as EventProperties, I as IdentifyInput, d as IdentifyTraits, e as ProfileFieldsInput, f as AmplifyUpTarget } from './types-CBoVIFI8.js';
2
+ export { Z as ZoneConfig, i as isAmplifyUpTarget, n as normalizeAmplifyUpTarget, r as resolveAmplifyUpTarget, g as resolveAmplifyUpUrls } from './types-CBoVIFI8.js';
3
3
  export { i as isComposerPreview, a as isPreviewRequest, b as isWorkingContentRequest } from './preview-_vHmzRen.js';
4
+ export { g as FetchEdgeQueryOptions, F as FetchEdgeSelectOptions, Q as QueryContentError, e as QueryContentOptions, k as QueryFilter, l as QueryFilterOp, i as QueryPagination, h as QueryResult, m as QuerySort, j as QuerySpec, S as SelectVariantsError, a as SelectVariantsOptions, b as SelectVariantsResult, c as fetchEdgeQuery, f as fetchEdgeSelect, n as nextPageSpec, q as queryContent, d as searchSpec, s as selectVariants } from './query-DjIuJ71C.js';
4
5
 
5
6
  interface FetchContentResult {
6
7
  pageConfig: PageConfig | null;
@@ -20,6 +21,10 @@ interface FetchContentOptions {
20
21
  /** Orchestrator layout fetch (SDK instance method or server helper). */
21
22
  fetchOrchestratorPage: (route: string, preview: boolean) => Promise<PageConfig | null>;
22
23
  fetchFn?: typeof fetch;
24
+ /** Next.js ISR for Edge resolve (server/SSG only). */
25
+ revalidate?: number | false;
26
+ /** Deterministic variants for ISR (no visitor id in the cache key). */
27
+ variantKeys?: Record<string, string> | null;
23
28
  }
24
29
  /**
25
30
  * Single content entry point:
@@ -80,7 +85,14 @@ declare function fetchEdgeResolve(options: {
80
85
  route: string;
81
86
  anonId?: string | null;
82
87
  siteId?: string | null;
88
+ /** Deterministic variant combo — cacheable; do not send with anonId. */
89
+ variantKeys?: Record<string, string> | null;
83
90
  fetch?: typeof fetch;
91
+ /**
92
+ * Next.js Data Cache / ISR. When set, the server fetch can be baked into
93
+ * SSG. Omit on the client so visitor-specific resolve stays fresh.
94
+ */
95
+ revalidate?: number | false;
84
96
  }): Promise<EdgeResolvedPayload>;
85
97
 
86
98
  /**
package/dist/index.js CHANGED
@@ -199,6 +199,7 @@ var AWSAdapter = class {
199
199
  // src/storage.ts
200
200
  var INSTANCE_ID_KEY = "amplifyup_instance_id";
201
201
  var USER_ID_KEY = "amplifyup_user_id";
202
+ var ANON_COOKIE_NAME = "amplifyup_anon_id";
202
203
  function generateGuid() {
203
204
  if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
204
205
  return crypto.randomUUID();
@@ -209,16 +210,39 @@ function generateGuid() {
209
210
  return v.toString(16);
210
211
  });
211
212
  }
213
+ function readCookie(name) {
214
+ if (typeof document === "undefined") return null;
215
+ const parts = document.cookie.split(";");
216
+ for (const part of parts) {
217
+ const idx = part.indexOf("=");
218
+ if (idx === -1) continue;
219
+ if (part.slice(0, idx).trim() !== name) continue;
220
+ const value = part.slice(idx + 1).trim();
221
+ try {
222
+ return value ? decodeURIComponent(value) : null;
223
+ } catch {
224
+ return value || null;
225
+ }
226
+ }
227
+ return null;
228
+ }
229
+ function writeCookie(name, value) {
230
+ if (typeof document === "undefined") return;
231
+ document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=31536000; SameSite=Lax`;
232
+ }
212
233
  function getInstanceId() {
213
234
  if (typeof window === "undefined") {
214
235
  return "";
215
236
  }
216
- const stored = localStorage.getItem(INSTANCE_ID_KEY);
217
- if (stored) {
218
- return stored;
237
+ const fromCookie = readCookie(ANON_COOKIE_NAME);
238
+ const fromLs = localStorage.getItem(INSTANCE_ID_KEY);
239
+ const instanceId = fromLs || fromCookie || `browser_${generateGuid()}`;
240
+ if (fromLs !== instanceId) {
241
+ localStorage.setItem(INSTANCE_ID_KEY, instanceId);
242
+ }
243
+ if (fromCookie !== instanceId) {
244
+ writeCookie(ANON_COOKIE_NAME, instanceId);
219
245
  }
220
- const instanceId = `browser_${generateGuid()}`;
221
- localStorage.setItem(INSTANCE_ID_KEY, instanceId);
222
246
  return instanceId;
223
247
  }
224
248
  function getUserId() {
@@ -239,6 +263,7 @@ function clearIds() {
239
263
  }
240
264
  localStorage.removeItem(INSTANCE_ID_KEY);
241
265
  localStorage.removeItem(USER_ID_KEY);
266
+ document.cookie = `${ANON_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
242
267
  }
243
268
 
244
269
  // src/targets.ts
@@ -366,6 +391,19 @@ function normalizeRoute(route) {
366
391
  return path || "/";
367
392
  }
368
393
 
394
+ // src/edge/variantKeys.ts
395
+ function serializeVariantKeys(keys) {
396
+ if (!keys) return "";
397
+ const parts = [];
398
+ const zoneIds = Object.keys(keys).sort();
399
+ for (const zoneId of zoneIds) {
400
+ const variant = keys[zoneId];
401
+ if (!variant || variant === "default") continue;
402
+ parts.push(`${zoneId}:${variant}`);
403
+ }
404
+ return parts.join(",");
405
+ }
406
+
369
407
  // src/edge/resolve.ts
370
408
  var EdgeResolveError = class extends Error {
371
409
  constructor(message, status, body) {
@@ -383,12 +421,23 @@ async function fetchEdgeResolve(options) {
383
421
  trackingId: options.trackingId
384
422
  });
385
423
  if (options.siteId) params.set("site", options.siteId);
386
- if (options.anonId) params.set("anonId", options.anonId);
424
+ const variantParam = serializeVariantKeys(options.variantKeys);
425
+ if (variantParam) {
426
+ params.set("variants", variantParam);
427
+ } else if (options.anonId) {
428
+ params.set("anonId", options.anonId);
429
+ }
387
430
  const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
388
- const res = await fetchFn(`${base}/v1/resolve?${params.toString()}`, {
431
+ const init2 = {
389
432
  method: "GET",
390
433
  headers: { Accept: "application/json" }
391
- });
434
+ };
435
+ if (options.revalidate !== void 0) {
436
+ init2.next = { revalidate: options.revalidate };
437
+ } else {
438
+ init2.cache = "no-store";
439
+ }
440
+ const res = await fetchFn(`${base}/v1/resolve?${params.toString()}`, init2);
392
441
  const body = await res.json().catch(() => ({}));
393
442
  if (!res.ok) {
394
443
  throw new EdgeResolveError(
@@ -477,9 +526,11 @@ async function fetchContent(options) {
477
526
  edgeUrl: options.edgeUrl,
478
527
  trackingId: options.trackingId,
479
528
  route: options.route,
480
- anonId: options.anonId,
529
+ anonId: options.variantKeys ? void 0 : options.anonId,
481
530
  siteId: options.siteId,
482
- fetch: options.fetchFn
531
+ fetch: options.fetchFn,
532
+ revalidate: options.revalidate,
533
+ variantKeys: options.variantKeys
483
534
  });
484
535
  const pageConfig = mapResolvedToPageConfig(payload);
485
536
  const degraded = collectDegraded(payload);
@@ -517,6 +568,145 @@ async function fetchContent(options) {
517
568
  }
518
569
  }
519
570
 
571
+ // src/edge/select.ts
572
+ var SelectVariantsError = class extends Error {
573
+ constructor(message, status, body) {
574
+ super(message);
575
+ this.name = "SelectVariantsError";
576
+ this.status = status;
577
+ this.body = body;
578
+ }
579
+ };
580
+ async function fetchEdgeSelect(options) {
581
+ const base = options.edgeUrl.replace(/\/+$/, "");
582
+ const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
583
+ if (!options.trackingId && !options.site) {
584
+ throw new SelectVariantsError("trackingId or site is required", 400);
585
+ }
586
+ const res = await fetchFn(`${base}/v1/select`, {
587
+ method: "POST",
588
+ headers: {
589
+ Accept: "application/json",
590
+ "Content-Type": "application/json"
591
+ },
592
+ cache: "no-store",
593
+ signal: options.signal,
594
+ body: JSON.stringify({
595
+ view: normalizeRoute(options.view),
596
+ ...options.trackingId ? { trackingId: options.trackingId } : {},
597
+ ...options.site ? { site: options.site } : {},
598
+ anonId: options.anonId,
599
+ ...options.zones?.length ? { zones: options.zones } : {},
600
+ ...options.context ? { context: options.context } : {}
601
+ })
602
+ });
603
+ const body = await res.json().catch(() => ({}));
604
+ if (!res.ok) {
605
+ throw new SelectVariantsError(
606
+ body.error || `Select failed (${res.status})`,
607
+ res.status,
608
+ body
609
+ );
610
+ }
611
+ return {
612
+ siteId: String(body.siteId || ""),
613
+ view: String(body.view || normalizeRoute(options.view)),
614
+ variantKeys: body.variantKeys && typeof body.variantKeys === "object" ? body.variantKeys : {},
615
+ meta: body.meta
616
+ };
617
+ }
618
+ async function selectVariants(options) {
619
+ const target = resolveAmplifyUpTarget({ configTarget: options.target });
620
+ const { edgeUrl } = resolveAmplifyUpUrls(target);
621
+ return fetchEdgeSelect({
622
+ edgeUrl: options.edgeUrl || edgeUrl,
623
+ trackingId: options.trackingId,
624
+ site: options.site,
625
+ view: options.view,
626
+ anonId: options.anonId,
627
+ zones: options.zones,
628
+ context: options.context,
629
+ fetch: options.fetch,
630
+ signal: options.signal
631
+ });
632
+ }
633
+
634
+ // src/edge/query.ts
635
+ var QueryContentError = class extends Error {
636
+ constructor(message, status, body) {
637
+ super(message);
638
+ this.name = "QueryContentError";
639
+ this.status = status;
640
+ this.body = body;
641
+ }
642
+ };
643
+ async function fetchEdgeQuery(options) {
644
+ const base = options.edgeUrl.replace(/\/+$/, "");
645
+ const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
646
+ const limit = Math.max(1, Number(options.spec.limit) || 10);
647
+ const offset = Math.max(0, Number(options.spec.offset) || 0);
648
+ const res = await fetchFn(`${base}/v1/query`, {
649
+ method: "POST",
650
+ headers: {
651
+ Accept: "application/json",
652
+ "Content-Type": "application/json"
653
+ },
654
+ cache: "no-store",
655
+ signal: options.signal,
656
+ body: JSON.stringify({
657
+ route: normalizeRoute(options.route),
658
+ trackingId: options.trackingId,
659
+ ...options.siteId ? { site: options.siteId } : {},
660
+ spec: { ...options.spec, limit, offset }
661
+ })
662
+ });
663
+ const body = await res.json().catch(() => ({}));
664
+ if (!res.ok) {
665
+ throw new QueryContentError(
666
+ body.error || `Content query failed (${res.status})`,
667
+ res.status,
668
+ body
669
+ );
670
+ }
671
+ const results = Array.isArray(body.results) ? body.results : [];
672
+ const resolvedLimit = Number(body.limit) || limit;
673
+ const resolvedOffset = Number.isFinite(Number(body.offset)) ? Number(body.offset) : offset;
674
+ return {
675
+ results,
676
+ count: Number(body.count) || results.length,
677
+ hasMore: !!body.hasMore,
678
+ limit: resolvedLimit,
679
+ offset: resolvedOffset,
680
+ nextOffset: Number(body.nextOffset) || resolvedOffset + resolvedLimit,
681
+ degraded: body.degraded
682
+ };
683
+ }
684
+ async function queryContent(options) {
685
+ const target = resolveAmplifyUpTarget({ configTarget: options.target });
686
+ const { edgeUrl } = resolveAmplifyUpUrls(target);
687
+ return fetchEdgeQuery({
688
+ edgeUrl,
689
+ trackingId: options.trackingId,
690
+ route: options.route,
691
+ spec: options.spec,
692
+ siteId: options.siteId,
693
+ fetch: options.fetch,
694
+ signal: options.signal
695
+ });
696
+ }
697
+ function nextPageSpec(pagination) {
698
+ return { ...pagination.spec, offset: pagination.nextOffset };
699
+ }
700
+ function searchSpec(pagination, field, term) {
701
+ const value = String(term || "").trim();
702
+ const base = { ...pagination.spec, offset: 0 };
703
+ if (!value) return base;
704
+ return {
705
+ ...base,
706
+ filter: [...base.filter || [], { field, op: "contains", value }]
707
+ };
708
+ }
709
+
520
710
  // src/index.ts
521
711
  var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
522
712
  var DWELL_ACTIVITY_IDLE_MS = 2 * 60 * 1e3;
@@ -2847,13 +3037,17 @@ var index_default = getSDK();
2847
3037
  exports.AmplifyUpSDK = AmplifyUpSDK;
2848
3038
  exports.DWELL_ACTIVITY_IDLE_MS = DWELL_ACTIVITY_IDLE_MS;
2849
3039
  exports.EdgeResolveError = EdgeResolveError;
3040
+ exports.QueryContentError = QueryContentError;
2850
3041
  exports.SESSION_IDLE_GAP_MS = SESSION_IDLE_GAP_MS;
3042
+ exports.SelectVariantsError = SelectVariantsError;
2851
3043
  exports.buildLayoutProviderUrl = buildLayoutProviderUrl;
2852
3044
  exports.buildPageRenderUrl = buildPageRenderUrl;
2853
3045
  exports.cleanupEventTracking = cleanupEventTracking;
2854
3046
  exports.default = index_default;
2855
3047
  exports.fetchContent = fetchContent;
3048
+ exports.fetchEdgeQuery = fetchEdgeQuery;
2856
3049
  exports.fetchEdgeResolve = fetchEdgeResolve;
3050
+ exports.fetchEdgeSelect = fetchEdgeSelect;
2857
3051
  exports.fetchPageConfig = fetchPageConfig;
2858
3052
  exports.getDiagnostics = getDiagnostics;
2859
3053
  exports.getInstanceId = getInstanceId2;
@@ -2870,14 +3064,18 @@ exports.isComposerPreview = isComposerPreview;
2870
3064
  exports.isPreviewRequest = isPreviewRequest;
2871
3065
  exports.isWorkingContentRequest = isWorkingContentRequest;
2872
3066
  exports.mapResolvedToPageConfig = mapResolvedToPageConfig;
3067
+ exports.nextPageSpec = nextPageSpec;
2873
3068
  exports.normalizeAmplifyUpTarget = normalizeAmplifyUpTarget;
2874
3069
  exports.normalizeRoute = normalizeRoute;
2875
3070
  exports.page = page;
3071
+ exports.queryContent = queryContent;
2876
3072
  exports.refreshEventTracking = refreshEventTracking;
2877
3073
  exports.reset = reset;
2878
3074
  exports.resolveAmplifyUpTarget = resolveAmplifyUpTarget;
2879
3075
  exports.resolveAmplifyUpUrls = resolveAmplifyUpUrls;
2880
3076
  exports.routeChange = routeChange;
3077
+ exports.searchSpec = searchSpec;
3078
+ exports.selectVariants = selectVariants;
2881
3079
  exports.setProfileFields = setProfileFields;
2882
3080
  exports.setupComponentViewTracking = setupComponentViewTracking;
2883
3081
  exports.setupEventTracking = setupEventTracking;