@amplifyup/sdk 0.1.63 → 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,21 +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
  }
420
- function isEntryRefItem(value) {
421
- if (!value || typeof value !== "object" || Array.isArray(value)) return false;
422
- 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;
430
+ function isIdentifierKey(key) {
431
+ return IDENTIFIER_KEYS.has(key);
424
432
  }
425
- function isRepeaterArray(value) {
433
+ function isRecordList(value) {
426
434
  if (!Array.isArray(value) || value.length === 0) return false;
427
435
  const sample = value.find((item) => item != null);
428
436
  if (sample === void 0) return false;
429
437
  if (typeof sample !== "object" || Array.isArray(sample)) return false;
430
438
  if (isPortableImageShape(sample)) return false;
431
- if (isEntryRefItem(sample)) return false;
432
439
  if (sample.$$typeof) return false;
433
440
  return true;
434
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
+ }
435
494
  function joinPath(prefix, key) {
436
495
  return prefix ? `${prefix}.${key}` : key;
437
496
  }
@@ -439,61 +498,101 @@ function emptyValue(raw) {
439
498
  if (raw === void 0) return null;
440
499
  return raw;
441
500
  }
442
- 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 || {};
443
515
  const input = source && typeof source === "object" && !Array.isArray(source) ? source : {};
444
516
  if (isBrandedFields(input)) {
445
517
  return input;
446
518
  }
447
519
  const out = {};
448
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
+ }
449
525
  const path = joinPath(prefix, key);
450
526
  if (isFieldEnvelope(raw)) {
451
527
  const inner = raw.value;
452
- if (isRepeaterArray(inner)) {
528
+ if (isRecordList(inner)) {
529
+ const owner = ctx.owners?.(key);
453
530
  out[key] = {
454
- value: envelopeRepeaterItems(inner, raw.name || path),
531
+ value: envelopeListRows(inner, owner),
455
532
  name: raw.name || path
456
533
  };
457
534
  continue;
458
535
  }
459
- out[key] = { value: raw.value ?? null, name: raw.name || path };
536
+ out[key] = withLeaf(
537
+ { value: raw.value ?? null, name: raw.name || path },
538
+ raw.ref ? { ref: raw.ref } : ctx
539
+ );
460
540
  continue;
461
541
  }
462
- if (isRepeaterArray(raw)) {
463
- out[key] = { value: envelopeRepeaterItems(raw, path), name: path };
542
+ if (isRecordList(raw)) {
543
+ const owner = ctx.owners?.(key);
544
+ out[key] = { value: envelopeListRows(raw, owner), name: path };
464
545
  continue;
465
546
  }
466
547
  if (raw && typeof raw === "object" && !Array.isArray(raw) && !isPortableImageShape(raw) && !raw.$$typeof) {
467
- out[key] = toFieldEnvelopes(raw, path);
548
+ out[key] = toFieldEnvelopes(raw, path, ctx);
468
549
  continue;
469
550
  }
470
- out[key] = { value: emptyValue(raw), name: path };
551
+ out[key] = withLeaf({ value: emptyValue(raw), name: path }, ctx);
471
552
  }
472
553
  return brandFields(out);
473
554
  }
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);
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
482
568
  });
569
+ const row = fields;
570
+ if (typeof row.id !== "string") {
571
+ row.id = id;
572
+ }
573
+ return row;
483
574
  }
484
- function attachFieldEnvelopes(props) {
575
+ function attachFieldEnvelopes(props, sources) {
485
576
  const raw = props && typeof props === "object" ? { ...props } : {};
486
577
  if (isBrandedFields(raw.fields)) return raw;
487
578
  const { fields: existing, ...content } = raw;
488
579
  const source = existing && typeof existing === "object" && !isFieldEnvelope(existing) ? existing : content;
489
- return { ...raw, fields: toFieldEnvelopes(source) };
580
+ return {
581
+ ...raw,
582
+ fields: toFieldEnvelopes(source, "", {
583
+ owners: (fieldName) => listOwnerForField(fieldName, source, sources)
584
+ })
585
+ };
490
586
  }
491
587
  function walkLayoutNodes(nodes) {
492
588
  if (!Array.isArray(nodes)) return;
493
589
  for (const node of nodes) {
494
590
  if (!node || typeof node !== "object") continue;
495
591
  const rec = node;
496
- rec.props = attachFieldEnvelopes(rec.props);
592
+ rec.props = attachFieldEnvelopes(rec.props, {
593
+ connections: rec.resources,
594
+ reads: rec.reads
595
+ });
497
596
  for (const slot of rec.slots || []) {
498
597
  walkLayoutNodes(slot.children);
499
598
  }
@@ -760,7 +859,10 @@ async function fetchEdgeResolve(options) {
760
859
  return envelopeResolvedPayload(body);
761
860
  }
762
861
  function envelopeResolvedComponent(node) {
763
- node.props = attachFieldEnvelopes(node.props);
862
+ node.props = attachFieldEnvelopes(node.props, {
863
+ connections: node.resources,
864
+ reads: node.reads
865
+ });
764
866
  if (!node.children) return;
765
867
  for (const kids of Object.values(node.children)) {
766
868
  for (const child of kids || []) envelopeResolvedComponent(child);
@@ -1019,7 +1121,7 @@ async function fetchEdgeQuery(options) {
1019
1121
  async function queryContent(options) {
1020
1122
  const target = resolveAmplifyUpTarget({ configTarget: options.target });
1021
1123
  const { edgeUrl } = resolveAmplifyUpUrls(target);
1022
- return fetchEdgeQuery({
1124
+ const raw = await fetchEdgeQuery({
1023
1125
  edgeUrl,
1024
1126
  trackingId: options.trackingId,
1025
1127
  route: options.route,
@@ -1028,6 +1130,13 @@ async function queryContent(options) {
1028
1130
  fetch: options.fetch,
1029
1131
  signal: options.signal
1030
1132
  });
1133
+ return {
1134
+ ...raw,
1135
+ results: envelopeListRows(
1136
+ raw.results,
1137
+ listOwnerFromSpec(options.spec)
1138
+ )
1139
+ };
1031
1140
  }
1032
1141
  function nextPageSpec(pagination) {
1033
1142
  return { ...pagination.spec, offset: pagination.nextOffset };