@amplifyup/sdk 0.1.34 → 0.1.36

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.mjs CHANGED
@@ -564,6 +564,145 @@ async function fetchContent(options) {
564
564
  }
565
565
  }
566
566
 
567
+ // src/edge/select.ts
568
+ var SelectVariantsError = class extends Error {
569
+ constructor(message, status, body) {
570
+ super(message);
571
+ this.name = "SelectVariantsError";
572
+ this.status = status;
573
+ this.body = body;
574
+ }
575
+ };
576
+ async function fetchEdgeSelect(options) {
577
+ const base = options.edgeUrl.replace(/\/+$/, "");
578
+ const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
579
+ if (!options.trackingId && !options.site) {
580
+ throw new SelectVariantsError("trackingId or site is required", 400);
581
+ }
582
+ const res = await fetchFn(`${base}/v1/select`, {
583
+ method: "POST",
584
+ headers: {
585
+ Accept: "application/json",
586
+ "Content-Type": "application/json"
587
+ },
588
+ cache: "no-store",
589
+ signal: options.signal,
590
+ body: JSON.stringify({
591
+ view: normalizeRoute(options.view),
592
+ ...options.trackingId ? { trackingId: options.trackingId } : {},
593
+ ...options.site ? { site: options.site } : {},
594
+ anonId: options.anonId,
595
+ ...options.zones?.length ? { zones: options.zones } : {},
596
+ ...options.context ? { context: options.context } : {}
597
+ })
598
+ });
599
+ const body = await res.json().catch(() => ({}));
600
+ if (!res.ok) {
601
+ throw new SelectVariantsError(
602
+ body.error || `Select failed (${res.status})`,
603
+ res.status,
604
+ body
605
+ );
606
+ }
607
+ return {
608
+ siteId: String(body.siteId || ""),
609
+ view: String(body.view || normalizeRoute(options.view)),
610
+ variantKeys: body.variantKeys && typeof body.variantKeys === "object" ? body.variantKeys : {},
611
+ meta: body.meta
612
+ };
613
+ }
614
+ async function selectVariants(options) {
615
+ const target = resolveAmplifyUpTarget({ configTarget: options.target });
616
+ const { edgeUrl } = resolveAmplifyUpUrls(target);
617
+ return fetchEdgeSelect({
618
+ edgeUrl: options.edgeUrl || edgeUrl,
619
+ trackingId: options.trackingId,
620
+ site: options.site,
621
+ view: options.view,
622
+ anonId: options.anonId,
623
+ zones: options.zones,
624
+ context: options.context,
625
+ fetch: options.fetch,
626
+ signal: options.signal
627
+ });
628
+ }
629
+
630
+ // src/edge/query.ts
631
+ var QueryContentError = class extends Error {
632
+ constructor(message, status, body) {
633
+ super(message);
634
+ this.name = "QueryContentError";
635
+ this.status = status;
636
+ this.body = body;
637
+ }
638
+ };
639
+ async function fetchEdgeQuery(options) {
640
+ const base = options.edgeUrl.replace(/\/+$/, "");
641
+ const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
642
+ const limit = Math.max(1, Number(options.spec.limit) || 10);
643
+ const offset = Math.max(0, Number(options.spec.offset) || 0);
644
+ const res = await fetchFn(`${base}/v1/query`, {
645
+ method: "POST",
646
+ headers: {
647
+ Accept: "application/json",
648
+ "Content-Type": "application/json"
649
+ },
650
+ cache: "no-store",
651
+ signal: options.signal,
652
+ body: JSON.stringify({
653
+ route: normalizeRoute(options.route),
654
+ trackingId: options.trackingId,
655
+ ...options.siteId ? { site: options.siteId } : {},
656
+ spec: { ...options.spec, limit, offset }
657
+ })
658
+ });
659
+ const body = await res.json().catch(() => ({}));
660
+ if (!res.ok) {
661
+ throw new QueryContentError(
662
+ body.error || `Content query failed (${res.status})`,
663
+ res.status,
664
+ body
665
+ );
666
+ }
667
+ const results = Array.isArray(body.results) ? body.results : [];
668
+ const resolvedLimit = Number(body.limit) || limit;
669
+ const resolvedOffset = Number.isFinite(Number(body.offset)) ? Number(body.offset) : offset;
670
+ return {
671
+ results,
672
+ count: Number(body.count) || results.length,
673
+ hasMore: !!body.hasMore,
674
+ limit: resolvedLimit,
675
+ offset: resolvedOffset,
676
+ nextOffset: Number(body.nextOffset) || resolvedOffset + resolvedLimit,
677
+ degraded: body.degraded
678
+ };
679
+ }
680
+ async function queryContent(options) {
681
+ const target = resolveAmplifyUpTarget({ configTarget: options.target });
682
+ const { edgeUrl } = resolveAmplifyUpUrls(target);
683
+ return fetchEdgeQuery({
684
+ edgeUrl,
685
+ trackingId: options.trackingId,
686
+ route: options.route,
687
+ spec: options.spec,
688
+ siteId: options.siteId,
689
+ fetch: options.fetch,
690
+ signal: options.signal
691
+ });
692
+ }
693
+ function nextPageSpec(pagination) {
694
+ return { ...pagination.spec, offset: pagination.nextOffset };
695
+ }
696
+ function searchSpec(pagination, field, term) {
697
+ const value = String(term || "").trim();
698
+ const base = { ...pagination.spec, offset: 0 };
699
+ if (!value) return base;
700
+ return {
701
+ ...base,
702
+ filter: [...base.filter || [], { field, op: "contains", value }]
703
+ };
704
+ }
705
+
567
706
  // src/index.ts
568
707
  var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
569
708
  var DWELL_ACTIVITY_IDLE_MS = 2 * 60 * 1e3;
@@ -2891,6 +3030,6 @@ var verifyAWSConnection = () => {
2891
3030
  };
2892
3031
  var index_default = getSDK();
2893
3032
 
2894
- export { AmplifyUpSDK, DWELL_ACTIVITY_IDLE_MS, EdgeResolveError, SESSION_IDLE_GAP_MS, buildLayoutProviderUrl, buildPageRenderUrl, cleanupEventTracking, index_default as default, fetchContent, fetchEdgeResolve, fetchPageConfig, getDiagnostics, getInstanceId2 as getInstanceId, getOrchestratorUrl, getSDK, getStatus, getTarget, getTrackingId, getUserId2 as getUserId, identify, init, isAmplifyUpTarget, isComposerPreview, isPreviewRequest, isWorkingContentRequest, mapResolvedToPageConfig, normalizeAmplifyUpTarget, normalizeRoute, page, refreshEventTracking, reset, resolveAmplifyUpTarget, resolveAmplifyUpUrls, routeChange, setProfileFields, setupComponentViewTracking, setupEventTracking, testTracking, track, trackClick, trackContentEngagement, trackContentView, trackFormFieldInteraction, trackFormSubmit, verifyAWSConnection };
3033
+ export { AmplifyUpSDK, DWELL_ACTIVITY_IDLE_MS, EdgeResolveError, QueryContentError, SESSION_IDLE_GAP_MS, SelectVariantsError, buildLayoutProviderUrl, buildPageRenderUrl, cleanupEventTracking, index_default as default, fetchContent, fetchEdgeQuery, fetchEdgeResolve, fetchEdgeSelect, fetchPageConfig, getDiagnostics, getInstanceId2 as getInstanceId, getOrchestratorUrl, getSDK, getStatus, getTarget, getTrackingId, getUserId2 as getUserId, identify, init, isAmplifyUpTarget, isComposerPreview, isPreviewRequest, isWorkingContentRequest, mapResolvedToPageConfig, nextPageSpec, normalizeAmplifyUpTarget, normalizeRoute, page, queryContent, refreshEventTracking, reset, resolveAmplifyUpTarget, resolveAmplifyUpUrls, routeChange, searchSpec, selectVariants, setProfileFields, setupComponentViewTracking, setupEventTracking, testTracking, track, trackClick, trackContentEngagement, trackContentView, trackFormFieldInteraction, trackFormSubmit, verifyAWSConnection };
2895
3034
  //# sourceMappingURL=index.mjs.map
2896
3035
  //# sourceMappingURL=index.mjs.map