@amplifyup/sdk 0.1.62 → 0.1.64

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
  }
@@ -417,6 +427,70 @@ function isPortableImageShape(value) {
417
427
  if (!value || typeof value !== "object" || Array.isArray(value)) return false;
418
428
  return "url" in value;
419
429
  }
430
+ function isIdentifierKey(key) {
431
+ return IDENTIFIER_KEYS.has(key);
432
+ }
433
+ function isRecordList(value) {
434
+ if (!Array.isArray(value) || value.length === 0) return false;
435
+ const sample = value.find((item) => item != null);
436
+ if (sample === void 0) return false;
437
+ if (typeof sample !== "object" || Array.isArray(sample)) return false;
438
+ if (isPortableImageShape(sample)) return false;
439
+ if (sample.$$typeof) return false;
440
+ return true;
441
+ }
442
+ function isFieldRef(value) {
443
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
444
+ const rec = value;
445
+ return typeof rec.providerId === "string" && !!rec.providerId && typeof rec.entity === "string" && !!rec.entity && typeof rec.id === "string" && !!rec.id;
446
+ }
447
+ function isListOwner(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;
451
+ }
452
+ function listOwnerFromConnection(conn) {
453
+ if (!conn || typeof conn !== "object") return void 0;
454
+ if (conn.mode === "query") {
455
+ const providerId = String(conn.spec?.providerId || "").trim();
456
+ const entity = String(conn.spec?.entity || "").trim();
457
+ return providerId && entity ? { providerId, entity } : void 0;
458
+ }
459
+ if (conn.mode === "item" || conn.mode === "items") {
460
+ const providerId = String(conn.providerId || "").trim();
461
+ const entity = String(conn.entity || "").trim();
462
+ return providerId && entity ? { providerId, entity } : void 0;
463
+ }
464
+ return void 0;
465
+ }
466
+ function listOwnerFromSpec(spec) {
467
+ const providerId = String(spec?.providerId || "").trim();
468
+ const entity = String(spec?.entity || "").trim();
469
+ return providerId && entity ? { providerId, entity } : void 0;
470
+ }
471
+ function isEnvelopeSources(value) {
472
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
473
+ const rec = value;
474
+ return rec.connections != null || rec.reads != null;
475
+ }
476
+ function normalizeEnvelopeSources(sources) {
477
+ if (!sources) return {};
478
+ if (isEnvelopeSources(sources) && !("mode" in sources)) {
479
+ return sources;
480
+ }
481
+ return { connections: sources };
482
+ }
483
+ function listOwnerForField(fieldName, props, sources) {
484
+ const { connections, reads } = normalizeEnvelopeSources(sources);
485
+ const resourceName = reads?.[fieldName]?.resource || fieldName;
486
+ const fromConn = listOwnerFromConnection(connections?.[resourceName]);
487
+ if (fromConn) return fromConn;
488
+ const paging = props?.[`${fieldName}Pagination`];
489
+ if (paging && typeof paging === "object" && !Array.isArray(paging)) {
490
+ return listOwnerFromSpec(paging.spec);
491
+ }
492
+ return void 0;
493
+ }
420
494
  function joinPath(prefix, key) {
421
495
  return prefix ? `${prefix}.${key}` : key;
422
496
  }
@@ -424,39 +498,101 @@ function emptyValue(raw) {
424
498
  if (raw === void 0) return null;
425
499
  return raw;
426
500
  }
427
- function toFieldEnvelopes(source, prefix = "") {
501
+ function rowId(raw) {
502
+ const candidates = [raw.id, raw._id, raw.docId, raw.key, raw._key];
503
+ for (const c of candidates) {
504
+ if (typeof c === "string" && c.trim()) return c;
505
+ }
506
+ return "";
507
+ }
508
+ function withLeaf(envelope, scope) {
509
+ if (isFieldRef(scope?.ref)) envelope.ref = scope.ref;
510
+ else if (scope?.readOnly) envelope.readOnly = true;
511
+ return envelope;
512
+ }
513
+ function toFieldEnvelopes(source, prefix = "", scope) {
514
+ const ctx = isFieldRef(scope) ? { ref: scope } : scope || {};
428
515
  const input = source && typeof source === "object" && !Array.isArray(source) ? source : {};
429
516
  if (isBrandedFields(input)) {
430
517
  return input;
431
518
  }
432
519
  const out = {};
433
520
  for (const [key, raw] of Object.entries(input)) {
521
+ if (isIdentifierKey(key) && (typeof raw === "string" || raw == null)) {
522
+ out[key] = raw ?? "";
523
+ continue;
524
+ }
434
525
  const path = joinPath(prefix, key);
435
526
  if (isFieldEnvelope(raw)) {
436
- out[key] = { value: raw.value ?? null, name: raw.name || path };
527
+ const inner = raw.value;
528
+ if (isRecordList(inner)) {
529
+ const owner = ctx.owners?.(key);
530
+ out[key] = {
531
+ value: envelopeListRows(inner, owner),
532
+ name: raw.name || path
533
+ };
534
+ continue;
535
+ }
536
+ out[key] = withLeaf(
537
+ { value: raw.value ?? null, name: raw.name || path },
538
+ raw.ref ? { ref: raw.ref } : ctx
539
+ );
540
+ continue;
541
+ }
542
+ if (isRecordList(raw)) {
543
+ const owner = ctx.owners?.(key);
544
+ out[key] = { value: envelopeListRows(raw, owner), name: path };
437
545
  continue;
438
546
  }
439
547
  if (raw && typeof raw === "object" && !Array.isArray(raw) && !isPortableImageShape(raw) && !raw.$$typeof) {
440
- out[key] = toFieldEnvelopes(raw, path);
548
+ out[key] = toFieldEnvelopes(raw, path, ctx);
441
549
  continue;
442
550
  }
443
- out[key] = { value: emptyValue(raw), name: path };
551
+ out[key] = withLeaf({ value: emptyValue(raw), name: path }, ctx);
444
552
  }
445
553
  return brandFields(out);
446
554
  }
447
- function attachFieldEnvelopes(props) {
555
+ function envelopeListRows(items, owner) {
556
+ return items.map((item) => envelopeListRow(item, owner));
557
+ }
558
+ function envelopeListRow(item, owner) {
559
+ if (isBrandedFields(item) && typeof item.id === "string") {
560
+ return item;
561
+ }
562
+ const raw = item && typeof item === "object" && !Array.isArray(item) ? item : {};
563
+ const id = rowId(raw);
564
+ const ref = isListOwner(owner) && id ? { providerId: owner.providerId, entity: owner.entity, id } : void 0;
565
+ const fields = toFieldEnvelopes(raw, "", {
566
+ ref,
567
+ readOnly: !ref
568
+ });
569
+ const row = fields;
570
+ if (typeof row.id !== "string") {
571
+ row.id = id;
572
+ }
573
+ return row;
574
+ }
575
+ function attachFieldEnvelopes(props, sources) {
448
576
  const raw = props && typeof props === "object" ? { ...props } : {};
449
577
  if (isBrandedFields(raw.fields)) return raw;
450
578
  const { fields: existing, ...content } = raw;
451
579
  const source = existing && typeof existing === "object" && !isFieldEnvelope(existing) ? existing : content;
452
- return { ...raw, fields: toFieldEnvelopes(source) };
580
+ return {
581
+ ...raw,
582
+ fields: toFieldEnvelopes(source, "", {
583
+ owners: (fieldName) => listOwnerForField(fieldName, source, sources)
584
+ })
585
+ };
453
586
  }
454
587
  function walkLayoutNodes(nodes) {
455
588
  if (!Array.isArray(nodes)) return;
456
589
  for (const node of nodes) {
457
590
  if (!node || typeof node !== "object") continue;
458
591
  const rec = node;
459
- rec.props = attachFieldEnvelopes(rec.props);
592
+ rec.props = attachFieldEnvelopes(rec.props, {
593
+ connections: rec.resources,
594
+ reads: rec.reads
595
+ });
460
596
  for (const slot of rec.slots || []) {
461
597
  walkLayoutNodes(slot.children);
462
598
  }
@@ -723,7 +859,10 @@ async function fetchEdgeResolve(options) {
723
859
  return envelopeResolvedPayload(body);
724
860
  }
725
861
  function envelopeResolvedComponent(node) {
726
- node.props = attachFieldEnvelopes(node.props);
862
+ node.props = attachFieldEnvelopes(node.props, {
863
+ connections: node.resources,
864
+ reads: node.reads
865
+ });
727
866
  if (!node.children) return;
728
867
  for (const kids of Object.values(node.children)) {
729
868
  for (const child of kids || []) envelopeResolvedComponent(child);
@@ -982,7 +1121,7 @@ async function fetchEdgeQuery(options) {
982
1121
  async function queryContent(options) {
983
1122
  const target = resolveAmplifyUpTarget({ configTarget: options.target });
984
1123
  const { edgeUrl } = resolveAmplifyUpUrls(target);
985
- return fetchEdgeQuery({
1124
+ const raw = await fetchEdgeQuery({
986
1125
  edgeUrl,
987
1126
  trackingId: options.trackingId,
988
1127
  route: options.route,
@@ -991,6 +1130,13 @@ async function queryContent(options) {
991
1130
  fetch: options.fetch,
992
1131
  signal: options.signal
993
1132
  });
1133
+ return {
1134
+ ...raw,
1135
+ results: envelopeListRows(
1136
+ raw.results,
1137
+ listOwnerFromSpec(options.spec)
1138
+ )
1139
+ };
994
1140
  }
995
1141
  function nextPageSpec(pagination) {
996
1142
  return { ...pagination.spec, offset: pagination.nextOffset };