@amplifyup/sdk 0.1.63 → 0.1.65

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
@@ -396,6 +396,16 @@ function isComposerPreview() {
396
396
 
397
397
  // src/field-envelope.ts
398
398
  var FIELDS_BRAND = Symbol("amplifyup.fields");
399
+ var IDENTIFIER_KEYS = /* @__PURE__ */ new Set([
400
+ "id",
401
+ "_id",
402
+ "slug",
403
+ "key",
404
+ "_key",
405
+ "docId",
406
+ "_type",
407
+ "type"
408
+ ]);
399
409
  function isBrandedFields(value) {
400
410
  return !!value && typeof value === "object" && FIELDS_BRAND in value;
401
411
  }
@@ -413,25 +423,79 @@ function isFieldEnvelope(value) {
413
423
  const rec = value;
414
424
  return typeof rec.name === "string" && "value" in rec && !("url" in rec);
415
425
  }
416
- function isPortableImageShape(value) {
417
- if (!value || typeof value !== "object" || Array.isArray(value)) return false;
418
- return "url" in value;
426
+ function isPageResourceSentinel(value) {
427
+ return !!value && typeof value === "object" && !Array.isArray(value) && typeof value.__amplifyupPageResource === "string";
419
428
  }
420
- function isEntryRefItem(value) {
429
+ function isPortableImageShape(value) {
421
430
  if (!value || typeof value !== "object" || Array.isArray(value)) return false;
431
+ if ("url" in value) return true;
422
432
  const rec = value;
423
- return typeof rec.id === "string" && rec.id.trim().length > 0 || typeof rec._id === "string" && rec._id.trim().length > 0 || typeof rec.docId === "string" && rec.docId.trim().length > 0;
433
+ return rec._type === "image" || rec.asset != null;
424
434
  }
425
- function isRepeaterArray(value) {
435
+ function isIdentifierKey(key) {
436
+ return IDENTIFIER_KEYS.has(key);
437
+ }
438
+ function isRecordList(value) {
426
439
  if (!Array.isArray(value) || value.length === 0) return false;
427
440
  const sample = value.find((item) => item != null);
428
441
  if (sample === void 0) return false;
429
442
  if (typeof sample !== "object" || Array.isArray(sample)) return false;
430
443
  if (isPortableImageShape(sample)) return false;
431
- if (isEntryRefItem(sample)) return false;
432
444
  if (sample.$$typeof) return false;
433
445
  return true;
434
446
  }
447
+ function isFieldRef(value) {
448
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
449
+ const rec = value;
450
+ return typeof rec.providerId === "string" && !!rec.providerId && typeof rec.entity === "string" && !!rec.entity && typeof rec.id === "string" && !!rec.id;
451
+ }
452
+ function isListOwner(value) {
453
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
454
+ const rec = value;
455
+ return typeof rec.providerId === "string" && !!rec.providerId && typeof rec.entity === "string" && !!rec.entity;
456
+ }
457
+ function listOwnerFromConnection(conn) {
458
+ if (!conn || typeof conn !== "object") return void 0;
459
+ if (conn.mode === "query") {
460
+ const providerId = String(conn.spec?.providerId || "").trim();
461
+ const entity = String(conn.spec?.entity || "").trim();
462
+ return providerId && entity ? { providerId, entity } : void 0;
463
+ }
464
+ if (conn.mode === "item" || conn.mode === "items") {
465
+ const providerId = String(conn.providerId || "").trim();
466
+ const entity = String(conn.entity || "").trim();
467
+ return providerId && entity ? { providerId, entity } : void 0;
468
+ }
469
+ return void 0;
470
+ }
471
+ function listOwnerFromSpec(spec) {
472
+ const providerId = String(spec?.providerId || "").trim();
473
+ const entity = String(spec?.entity || "").trim();
474
+ return providerId && entity ? { providerId, entity } : void 0;
475
+ }
476
+ function isEnvelopeSources(value) {
477
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
478
+ const rec = value;
479
+ return rec.connections != null || rec.reads != null;
480
+ }
481
+ function normalizeEnvelopeSources(sources) {
482
+ if (!sources) return {};
483
+ if (isEnvelopeSources(sources) && !("mode" in sources)) {
484
+ return sources;
485
+ }
486
+ return { connections: sources };
487
+ }
488
+ function listOwnerForField(fieldName, props, sources) {
489
+ const { connections, reads } = normalizeEnvelopeSources(sources);
490
+ const resourceName = reads?.[fieldName]?.resource || fieldName;
491
+ const fromConn = listOwnerFromConnection(connections?.[resourceName]);
492
+ if (fromConn) return fromConn;
493
+ const paging = props?.[`${fieldName}Pagination`];
494
+ if (paging && typeof paging === "object" && !Array.isArray(paging)) {
495
+ return listOwnerFromSpec(paging.spec);
496
+ }
497
+ return void 0;
498
+ }
435
499
  function joinPath(prefix, key) {
436
500
  return prefix ? `${prefix}.${key}` : key;
437
501
  }
@@ -439,61 +503,105 @@ function emptyValue(raw) {
439
503
  if (raw === void 0) return null;
440
504
  return raw;
441
505
  }
442
- function toFieldEnvelopes(source, prefix = "") {
506
+ function rowId(raw) {
507
+ const candidates = [raw.id, raw._id, raw.docId, raw.key, raw._key];
508
+ for (const c of candidates) {
509
+ if (typeof c === "string" && c.trim()) return c;
510
+ }
511
+ return "";
512
+ }
513
+ function withLeaf(envelope, scope) {
514
+ if (isFieldRef(scope?.ref)) envelope.ref = scope.ref;
515
+ else if (scope?.readOnly) envelope.readOnly = true;
516
+ return envelope;
517
+ }
518
+ function toFieldEnvelopes(source, prefix = "", scope) {
519
+ const ctx = isFieldRef(scope) ? { ref: scope } : scope || {};
443
520
  const input = source && typeof source === "object" && !Array.isArray(source) ? source : {};
444
521
  if (isBrandedFields(input)) {
445
522
  return input;
446
523
  }
447
524
  const out = {};
448
525
  for (const [key, raw] of Object.entries(input)) {
526
+ if (isIdentifierKey(key) && (typeof raw === "string" || raw == null)) {
527
+ out[key] = raw ?? "";
528
+ continue;
529
+ }
449
530
  const path = joinPath(prefix, key);
531
+ if (isPageResourceSentinel(raw)) {
532
+ out[key] = withLeaf({ value: raw, name: path }, ctx);
533
+ continue;
534
+ }
450
535
  if (isFieldEnvelope(raw)) {
451
536
  const inner = raw.value;
452
- if (isRepeaterArray(inner)) {
537
+ if (isRecordList(inner)) {
538
+ const owner = ctx.owners?.(key);
453
539
  out[key] = {
454
- value: envelopeRepeaterItems(inner, raw.name || path),
540
+ value: envelopeListRows(inner, owner),
455
541
  name: raw.name || path
456
542
  };
457
543
  continue;
458
544
  }
459
- out[key] = { value: raw.value ?? null, name: raw.name || path };
545
+ out[key] = withLeaf(
546
+ { value: raw.value ?? null, name: raw.name || path },
547
+ raw.ref ? { ref: raw.ref } : ctx
548
+ );
460
549
  continue;
461
550
  }
462
- if (isRepeaterArray(raw)) {
463
- out[key] = { value: envelopeRepeaterItems(raw, path), name: path };
551
+ if (isRecordList(raw)) {
552
+ const owner = ctx.owners?.(key);
553
+ out[key] = { value: envelopeListRows(raw, owner), name: path };
464
554
  continue;
465
555
  }
466
556
  if (raw && typeof raw === "object" && !Array.isArray(raw) && !isPortableImageShape(raw) && !raw.$$typeof) {
467
- out[key] = toFieldEnvelopes(raw, path);
557
+ out[key] = toFieldEnvelopes(raw, path, ctx);
468
558
  continue;
469
559
  }
470
- out[key] = { value: emptyValue(raw), name: path };
560
+ out[key] = withLeaf({ value: emptyValue(raw), name: path }, ctx);
471
561
  }
472
562
  return brandFields(out);
473
563
  }
474
- function envelopeRepeaterItems(items, prefix) {
475
- return items.map((item, i) => {
476
- const path = joinPath(prefix, String(i));
477
- if (isBrandedFields(item)) return item;
478
- if (item && typeof item === "object" && !Array.isArray(item)) {
479
- return toFieldEnvelopes(item, path);
480
- }
481
- return toFieldEnvelopes({}, path);
564
+ function envelopeListRows(items, owner) {
565
+ return items.map((item) => envelopeListRow(item, owner));
566
+ }
567
+ function envelopeListRow(item, owner) {
568
+ if (isBrandedFields(item) && typeof item.id === "string") {
569
+ return item;
570
+ }
571
+ const raw = item && typeof item === "object" && !Array.isArray(item) ? item : {};
572
+ const id = rowId(raw);
573
+ const ref = isListOwner(owner) && id ? { providerId: owner.providerId, entity: owner.entity, id } : void 0;
574
+ const fields = toFieldEnvelopes(raw, "", {
575
+ ref,
576
+ readOnly: !ref
482
577
  });
578
+ const row = fields;
579
+ if (typeof row.id !== "string") {
580
+ row.id = id;
581
+ }
582
+ return row;
483
583
  }
484
- function attachFieldEnvelopes(props) {
584
+ function attachFieldEnvelopes(props, sources) {
485
585
  const raw = props && typeof props === "object" ? { ...props } : {};
486
586
  if (isBrandedFields(raw.fields)) return raw;
487
587
  const { fields: existing, ...content } = raw;
488
588
  const source = existing && typeof existing === "object" && !isFieldEnvelope(existing) ? existing : content;
489
- return { ...raw, fields: toFieldEnvelopes(source) };
589
+ return {
590
+ ...raw,
591
+ fields: toFieldEnvelopes(source, "", {
592
+ owners: (fieldName) => listOwnerForField(fieldName, source, sources)
593
+ })
594
+ };
490
595
  }
491
596
  function walkLayoutNodes(nodes) {
492
597
  if (!Array.isArray(nodes)) return;
493
598
  for (const node of nodes) {
494
599
  if (!node || typeof node !== "object") continue;
495
600
  const rec = node;
496
- rec.props = attachFieldEnvelopes(rec.props);
601
+ rec.props = attachFieldEnvelopes(rec.props, {
602
+ connections: rec.resources,
603
+ reads: rec.reads
604
+ });
497
605
  for (const slot of rec.slots || []) {
498
606
  walkLayoutNodes(slot.children);
499
607
  }
@@ -760,7 +868,10 @@ async function fetchEdgeResolve(options) {
760
868
  return envelopeResolvedPayload(body);
761
869
  }
762
870
  function envelopeResolvedComponent(node) {
763
- node.props = attachFieldEnvelopes(node.props);
871
+ node.props = attachFieldEnvelopes(node.props, {
872
+ connections: node.resources,
873
+ reads: node.reads
874
+ });
764
875
  if (!node.children) return;
765
876
  for (const kids of Object.values(node.children)) {
766
877
  for (const child of kids || []) envelopeResolvedComponent(child);
@@ -1019,7 +1130,7 @@ async function fetchEdgeQuery(options) {
1019
1130
  async function queryContent(options) {
1020
1131
  const target = resolveAmplifyUpTarget({ configTarget: options.target });
1021
1132
  const { edgeUrl } = resolveAmplifyUpUrls(target);
1022
- return fetchEdgeQuery({
1133
+ const raw = await fetchEdgeQuery({
1023
1134
  edgeUrl,
1024
1135
  trackingId: options.trackingId,
1025
1136
  route: options.route,
@@ -1028,6 +1139,13 @@ async function queryContent(options) {
1028
1139
  fetch: options.fetch,
1029
1140
  signal: options.signal
1030
1141
  });
1142
+ return {
1143
+ ...raw,
1144
+ results: envelopeListRows(
1145
+ raw.results,
1146
+ listOwnerFromSpec(options.spec)
1147
+ )
1148
+ };
1031
1149
  }
1032
1150
  function nextPageSpec(pagination) {
1033
1151
  return { ...pagination.spec, offset: pagination.nextOffset };