@amplifyup/sdk 0.1.34 → 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/react.mjs CHANGED
@@ -575,6 +575,145 @@ async function fetchContent(options) {
575
575
  }
576
576
  }
577
577
 
578
+ // src/edge/select.ts
579
+ var SelectVariantsError = class extends Error {
580
+ constructor(message, status, body) {
581
+ super(message);
582
+ this.name = "SelectVariantsError";
583
+ this.status = status;
584
+ this.body = body;
585
+ }
586
+ };
587
+ async function fetchEdgeSelect(options) {
588
+ const base = options.edgeUrl.replace(/\/+$/, "");
589
+ const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
590
+ if (!options.trackingId && !options.site) {
591
+ throw new SelectVariantsError("trackingId or site is required", 400);
592
+ }
593
+ const res = await fetchFn(`${base}/v1/select`, {
594
+ method: "POST",
595
+ headers: {
596
+ Accept: "application/json",
597
+ "Content-Type": "application/json"
598
+ },
599
+ cache: "no-store",
600
+ signal: options.signal,
601
+ body: JSON.stringify({
602
+ view: normalizeRoute(options.view),
603
+ ...options.trackingId ? { trackingId: options.trackingId } : {},
604
+ ...options.site ? { site: options.site } : {},
605
+ anonId: options.anonId,
606
+ ...options.zones?.length ? { zones: options.zones } : {},
607
+ ...options.context ? { context: options.context } : {}
608
+ })
609
+ });
610
+ const body = await res.json().catch(() => ({}));
611
+ if (!res.ok) {
612
+ throw new SelectVariantsError(
613
+ body.error || `Select failed (${res.status})`,
614
+ res.status,
615
+ body
616
+ );
617
+ }
618
+ return {
619
+ siteId: String(body.siteId || ""),
620
+ view: String(body.view || normalizeRoute(options.view)),
621
+ variantKeys: body.variantKeys && typeof body.variantKeys === "object" ? body.variantKeys : {},
622
+ meta: body.meta
623
+ };
624
+ }
625
+ async function selectVariants(options) {
626
+ const target = resolveAmplifyUpTarget({ configTarget: options.target });
627
+ const { edgeUrl } = resolveAmplifyUpUrls(target);
628
+ return fetchEdgeSelect({
629
+ edgeUrl: options.edgeUrl || edgeUrl,
630
+ trackingId: options.trackingId,
631
+ site: options.site,
632
+ view: options.view,
633
+ anonId: options.anonId,
634
+ zones: options.zones,
635
+ context: options.context,
636
+ fetch: options.fetch,
637
+ signal: options.signal
638
+ });
639
+ }
640
+
641
+ // src/edge/query.ts
642
+ var QueryContentError = class extends Error {
643
+ constructor(message, status, body) {
644
+ super(message);
645
+ this.name = "QueryContentError";
646
+ this.status = status;
647
+ this.body = body;
648
+ }
649
+ };
650
+ async function fetchEdgeQuery(options) {
651
+ const base = options.edgeUrl.replace(/\/+$/, "");
652
+ const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
653
+ const limit = Math.max(1, Number(options.spec.limit) || 10);
654
+ const offset = Math.max(0, Number(options.spec.offset) || 0);
655
+ const res = await fetchFn(`${base}/v1/query`, {
656
+ method: "POST",
657
+ headers: {
658
+ Accept: "application/json",
659
+ "Content-Type": "application/json"
660
+ },
661
+ cache: "no-store",
662
+ signal: options.signal,
663
+ body: JSON.stringify({
664
+ route: normalizeRoute(options.route),
665
+ trackingId: options.trackingId,
666
+ ...options.siteId ? { site: options.siteId } : {},
667
+ spec: { ...options.spec, limit, offset }
668
+ })
669
+ });
670
+ const body = await res.json().catch(() => ({}));
671
+ if (!res.ok) {
672
+ throw new QueryContentError(
673
+ body.error || `Content query failed (${res.status})`,
674
+ res.status,
675
+ body
676
+ );
677
+ }
678
+ const results = Array.isArray(body.results) ? body.results : [];
679
+ const resolvedLimit = Number(body.limit) || limit;
680
+ const resolvedOffset = Number.isFinite(Number(body.offset)) ? Number(body.offset) : offset;
681
+ return {
682
+ results,
683
+ count: Number(body.count) || results.length,
684
+ hasMore: !!body.hasMore,
685
+ limit: resolvedLimit,
686
+ offset: resolvedOffset,
687
+ nextOffset: Number(body.nextOffset) || resolvedOffset + resolvedLimit,
688
+ degraded: body.degraded
689
+ };
690
+ }
691
+ async function queryContent(options) {
692
+ const target = resolveAmplifyUpTarget({ configTarget: options.target });
693
+ const { edgeUrl } = resolveAmplifyUpUrls(target);
694
+ return fetchEdgeQuery({
695
+ edgeUrl,
696
+ trackingId: options.trackingId,
697
+ route: options.route,
698
+ spec: options.spec,
699
+ siteId: options.siteId,
700
+ fetch: options.fetch,
701
+ signal: options.signal
702
+ });
703
+ }
704
+ function nextPageSpec(pagination) {
705
+ return { ...pagination.spec, offset: pagination.nextOffset };
706
+ }
707
+ function searchSpec(pagination, field, term) {
708
+ const value = String(term || "").trim();
709
+ const base = { ...pagination.spec, offset: 0 };
710
+ if (!value) return base;
711
+ return {
712
+ ...base,
713
+ filter: [...base.filter || [], { field, op: "contains", value }]
714
+ };
715
+ }
716
+
578
717
  // src/index.ts
579
718
  var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
580
719
  var DWELL_ACTIVITY_IDLE_MS = 2 * 60 * 1e3;
@@ -4059,14 +4198,31 @@ function AmplifyUpProvider({ children, config }) {
4059
4198
  setLoading(false);
4060
4199
  return;
4061
4200
  }
4201
+ let variantKeys;
4202
+ const anonId = sdk.getInstanceId();
4203
+ if (!preview && anonId) {
4204
+ try {
4205
+ const selected = await selectVariants({
4206
+ trackingId,
4207
+ view: actualRoute,
4208
+ anonId,
4209
+ site: sdk.getSiteId() || void 0,
4210
+ edgeUrl
4211
+ });
4212
+ variantKeys = selected.variantKeys;
4213
+ } catch (err) {
4214
+ console.error("AmplifyUp selectVariants failed; overlay will use visitor id", err);
4215
+ }
4216
+ }
4062
4217
  const result = await fetchContent({
4063
4218
  route: actualRoute,
4064
4219
  trackingId,
4065
- anonId: sdk.getInstanceId(),
4220
+ anonId: variantKeys ? void 0 : anonId,
4066
4221
  siteId: sdk.getSiteId() || null,
4067
4222
  preview,
4068
4223
  orchestratorUrl,
4069
4224
  edgeUrl,
4225
+ variantKeys,
4070
4226
  fetchOrchestratorPage: (route, isPreview) => sdk.fetchPageConfig(route, isPreview)
4071
4227
  });
4072
4228
  if (cancelled) return;
@@ -4115,6 +4271,6 @@ function useAmplifyUp() {
4115
4271
  return context;
4116
4272
  }
4117
4273
 
4118
- export { AmplifyPageContent, AmplifyRenderer, AmplifyUpProvider, ComponentContextProvider, Field, Image, RichText, Slot, fetchPageConfigServer, isComposerPreview, isPreviewRequest, isWorkingContentRequest, useAmplifyUp, useComponentProps, useLiveComposerProps };
4274
+ export { AmplifyPageContent, AmplifyRenderer, AmplifyUpProvider, ComponentContextProvider, Field, Image, QueryContentError, RichText, SelectVariantsError, Slot, fetchPageConfigServer, isComposerPreview, isPreviewRequest, isWorkingContentRequest, nextPageSpec, queryContent, searchSpec, selectVariants, useAmplifyUp, useComponentProps, useLiveComposerProps };
4119
4275
  //# sourceMappingURL=react.mjs.map
4120
4276
  //# sourceMappingURL=react.mjs.map