@amplifyup/sdk 0.1.67 → 0.1.68

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
@@ -423,6 +423,13 @@ function isFieldEnvelope(value) {
423
423
  const rec = value;
424
424
  return typeof rec.name === "string" && "value" in rec && !("url" in rec);
425
425
  }
426
+ var OBJECT_FIELD_KINDS = /* @__PURE__ */ new Set(["object", "array", "list"]);
427
+ function isLeafFieldKind(kind) {
428
+ if (typeof kind !== "string") return false;
429
+ const normalized = kind.trim().toLowerCase();
430
+ if (!normalized) return false;
431
+ return !OBJECT_FIELD_KINDS.has(normalized);
432
+ }
426
433
  function isPageResourceSentinel(value) {
427
434
  return !!value && typeof value === "object" && !Array.isArray(value) && typeof value.__amplifyupPageResource === "string";
428
435
  }
@@ -553,6 +560,10 @@ function toFieldEnvelopes(source, prefix = "", scope) {
553
560
  out[key] = { value: envelopeListRows(raw, owner), name: path };
554
561
  continue;
555
562
  }
563
+ if (ctx.leaf?.(path)) {
564
+ out[key] = withLeaf({ value: emptyValue(raw), name: path }, ctx);
565
+ continue;
566
+ }
556
567
  if (raw && typeof raw === "object" && !Array.isArray(raw) && !isPortableImageShape(raw) && !raw.$$typeof) {
557
568
  out[key] = toFieldEnvelopes(raw, path, ctx);
558
569
  continue;
@@ -581,15 +592,33 @@ function envelopeListRow(item, owner) {
581
592
  }
582
593
  return row;
583
594
  }
595
+ var warnedMissingFieldKind = /* @__PURE__ */ new Set();
596
+ function warnMissingFieldKind(fieldName) {
597
+ if (process.env.NODE_ENV === "production") return;
598
+ if (warnedMissingFieldKind.has(fieldName)) return;
599
+ warnedMissingFieldKind.add(fieldName);
600
+ console.warn(
601
+ `[AmplifyUp SDK] Field "${fieldName}" has no schema kind; using value-shape detection. Republish the component definition so its fields carry a type.`
602
+ );
603
+ }
584
604
  function attachFieldEnvelopes(props, sources) {
585
605
  const raw = props && typeof props === "object" ? { ...props } : {};
586
606
  if (isBrandedFields(raw.fields)) return raw;
587
607
  const { fields: existing, ...content } = raw;
588
608
  const source = existing && typeof existing === "object" && !isFieldEnvelope(existing) ? existing : content;
609
+ const { reads } = normalizeEnvelopeSources(sources);
610
+ if (reads) {
611
+ for (const [fieldName, read] of Object.entries(reads)) {
612
+ if (!read?.kind) warnMissingFieldKind(fieldName);
613
+ }
614
+ }
589
615
  return {
590
616
  ...raw,
591
617
  fields: toFieldEnvelopes(source, "", {
592
- owners: (fieldName) => listOwnerForField(fieldName, source, sources)
618
+ owners: (fieldName) => listOwnerForField(fieldName, source, sources),
619
+ // Top-level field paths equal field names; nested paths have no schema
620
+ // entry and fall back to shape-based handling.
621
+ leaf: (path) => isLeafFieldKind(reads?.[path]?.kind)
593
622
  })
594
623
  };
595
624
  }
@@ -779,7 +808,58 @@ function readAmplifyUpDebugEnv() {
779
808
  return false;
780
809
  }
781
810
  }
811
+ var configDebug;
782
812
  function setAmplifyUpDebug(enabled) {
813
+ configDebug = enabled;
814
+ }
815
+ function isAmplifyUpDebug() {
816
+ if (configDebug === true) return true;
817
+ if (configDebug === false) return false;
818
+ return readAmplifyUpDebugEnv();
819
+ }
820
+ function sdkDebug(...args) {
821
+ if (!isAmplifyUpDebug()) return;
822
+ console.log(...args);
823
+ }
824
+
825
+ // src/content/layoutProviderCache.ts
826
+ var SETTLED_TTL_MS = 5e3;
827
+ var inflight = /* @__PURE__ */ new Map();
828
+ var settled = /* @__PURE__ */ new Map();
829
+ function layoutProviderCacheKey(opts) {
830
+ const route = (opts.route || "/").split("?")[0] || "/";
831
+ return [
832
+ route,
833
+ opts.trackingId || "",
834
+ opts.instanceId || "",
835
+ opts.preview ? "1" : "0",
836
+ opts.working ? "1" : "0",
837
+ opts.zoneId || "",
838
+ opts.compositionId || ""
839
+ ].join("|");
840
+ }
841
+ function getSettledLayoutProvider(key) {
842
+ const entry = settled.get(key);
843
+ if (!entry) return void 0;
844
+ if (Date.now() - entry.at > SETTLED_TTL_MS) {
845
+ settled.delete(key);
846
+ return void 0;
847
+ }
848
+ return entry.value;
849
+ }
850
+ function dedupeLayoutProviderFetch(key, fetchFn) {
851
+ const cached = getSettledLayoutProvider(key);
852
+ if (cached !== void 0) return Promise.resolve(cached);
853
+ const existing = inflight.get(key);
854
+ if (existing) return existing;
855
+ const pending = fetchFn().then((value) => {
856
+ settled.set(key, { value, at: Date.now() });
857
+ return value;
858
+ }).finally(() => {
859
+ inflight.delete(key);
860
+ });
861
+ inflight.set(key, pending);
862
+ return pending;
783
863
  }
784
864
 
785
865
  // src/reservedRoutes.ts
@@ -2200,6 +2280,32 @@ Available Console Commands:
2200
2280
  */
2201
2281
  async fetchPageConfig(route, preview = false) {
2202
2282
  const url = this.buildLayoutProviderUrl(route, preview);
2283
+ const pageSearch = typeof window !== "undefined" ? new URLSearchParams(window.location.search) : null;
2284
+ const cacheKey = layoutProviderCacheKey({
2285
+ route,
2286
+ trackingId: this.getTrackingId(),
2287
+ instanceId: getInstanceId(),
2288
+ preview,
2289
+ working: pageSearch ? isWorkingContentRequest(pageSearch) : false,
2290
+ zoneId: pageSearch?.get("zoneId"),
2291
+ compositionId: pageSearch?.get("compositionId")
2292
+ });
2293
+ return dedupeLayoutProviderFetch(cacheKey, async () => {
2294
+ const startedAt = Date.now();
2295
+ sdkDebug("[AmplifyUp SDK] layout/provider request", {
2296
+ route,
2297
+ at: new Date(startedAt).toISOString()
2298
+ });
2299
+ const page2 = await this.fetchPageConfigUncached(url);
2300
+ sdkDebug("[AmplifyUp SDK] layout/provider response", {
2301
+ route,
2302
+ durationMs: Date.now() - startedAt,
2303
+ hasPage: !!page2
2304
+ });
2305
+ return page2;
2306
+ });
2307
+ }
2308
+ async fetchPageConfigUncached(url) {
2203
2309
  const response = await fetch(url);
2204
2310
  if (!response.ok) {
2205
2311
  if (response.status === 404) {
@@ -2558,6 +2664,12 @@ Available Console Commands:
2558
2664
  );
2559
2665
  return;
2560
2666
  }
2667
+ if (event.data?.type === "AMPLIFYUP_LAYOUT_PATCH") {
2668
+ window.dispatchEvent(
2669
+ new CustomEvent("amplifyup:layout-patch", { detail: event.data })
2670
+ );
2671
+ return;
2672
+ }
2561
2673
  if (event.data?.type === "COMPONENT_DELETABLE_STATUS") {
2562
2674
  const { componentId, deletable, nodeId } = event.data;
2563
2675
  const element = document.querySelector(`[data-component-id="${componentId}"]`);