@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/server.mjs CHANGED
@@ -98,6 +98,16 @@ function isComposerPreview() {
98
98
 
99
99
  // src/field-envelope.ts
100
100
  var FIELDS_BRAND = Symbol("amplifyup.fields");
101
+ var IDENTIFIER_KEYS = /* @__PURE__ */ new Set([
102
+ "id",
103
+ "_id",
104
+ "slug",
105
+ "key",
106
+ "_key",
107
+ "docId",
108
+ "_type",
109
+ "type"
110
+ ]);
101
111
  function isBrandedFields(value) {
102
112
  return !!value && typeof value === "object" && FIELDS_BRAND in value;
103
113
  }
@@ -119,6 +129,70 @@ function isPortableImageShape(value) {
119
129
  if (!value || typeof value !== "object" || Array.isArray(value)) return false;
120
130
  return "url" in value;
121
131
  }
132
+ function isIdentifierKey(key) {
133
+ return IDENTIFIER_KEYS.has(key);
134
+ }
135
+ function isRecordList(value) {
136
+ if (!Array.isArray(value) || value.length === 0) return false;
137
+ const sample = value.find((item) => item != null);
138
+ if (sample === void 0) return false;
139
+ if (typeof sample !== "object" || Array.isArray(sample)) return false;
140
+ if (isPortableImageShape(sample)) return false;
141
+ if (sample.$$typeof) return false;
142
+ return true;
143
+ }
144
+ function isFieldRef(value) {
145
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
146
+ const rec = value;
147
+ return typeof rec.providerId === "string" && !!rec.providerId && typeof rec.entity === "string" && !!rec.entity && typeof rec.id === "string" && !!rec.id;
148
+ }
149
+ function isListOwner(value) {
150
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
151
+ const rec = value;
152
+ return typeof rec.providerId === "string" && !!rec.providerId && typeof rec.entity === "string" && !!rec.entity;
153
+ }
154
+ function listOwnerFromConnection(conn) {
155
+ if (!conn || typeof conn !== "object") return void 0;
156
+ if (conn.mode === "query") {
157
+ const providerId = String(conn.spec?.providerId || "").trim();
158
+ const entity = String(conn.spec?.entity || "").trim();
159
+ return providerId && entity ? { providerId, entity } : void 0;
160
+ }
161
+ if (conn.mode === "item" || conn.mode === "items") {
162
+ const providerId = String(conn.providerId || "").trim();
163
+ const entity = String(conn.entity || "").trim();
164
+ return providerId && entity ? { providerId, entity } : void 0;
165
+ }
166
+ return void 0;
167
+ }
168
+ function listOwnerFromSpec(spec) {
169
+ const providerId = String(spec?.providerId || "").trim();
170
+ const entity = String(spec?.entity || "").trim();
171
+ return providerId && entity ? { providerId, entity } : void 0;
172
+ }
173
+ function isEnvelopeSources(value) {
174
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
175
+ const rec = value;
176
+ return rec.connections != null || rec.reads != null;
177
+ }
178
+ function normalizeEnvelopeSources(sources) {
179
+ if (!sources) return {};
180
+ if (isEnvelopeSources(sources) && !("mode" in sources)) {
181
+ return sources;
182
+ }
183
+ return { connections: sources };
184
+ }
185
+ function listOwnerForField(fieldName, props, sources) {
186
+ const { connections, reads } = normalizeEnvelopeSources(sources);
187
+ const resourceName = reads?.[fieldName]?.resource || fieldName;
188
+ const fromConn = listOwnerFromConnection(connections?.[resourceName]);
189
+ if (fromConn) return fromConn;
190
+ const paging = props?.[`${fieldName}Pagination`];
191
+ if (paging && typeof paging === "object" && !Array.isArray(paging)) {
192
+ return listOwnerFromSpec(paging.spec);
193
+ }
194
+ return void 0;
195
+ }
122
196
  function joinPath(prefix, key) {
123
197
  return prefix ? `${prefix}.${key}` : key;
124
198
  }
@@ -126,39 +200,101 @@ function emptyValue(raw) {
126
200
  if (raw === void 0) return null;
127
201
  return raw;
128
202
  }
129
- function toFieldEnvelopes(source, prefix = "") {
203
+ function rowId(raw) {
204
+ const candidates = [raw.id, raw._id, raw.docId, raw.key, raw._key];
205
+ for (const c of candidates) {
206
+ if (typeof c === "string" && c.trim()) return c;
207
+ }
208
+ return "";
209
+ }
210
+ function withLeaf(envelope, scope) {
211
+ if (isFieldRef(scope?.ref)) envelope.ref = scope.ref;
212
+ else if (scope?.readOnly) envelope.readOnly = true;
213
+ return envelope;
214
+ }
215
+ function toFieldEnvelopes(source, prefix = "", scope) {
216
+ const ctx = isFieldRef(scope) ? { ref: scope } : scope || {};
130
217
  const input = source && typeof source === "object" && !Array.isArray(source) ? source : {};
131
218
  if (isBrandedFields(input)) {
132
219
  return input;
133
220
  }
134
221
  const out = {};
135
222
  for (const [key, raw] of Object.entries(input)) {
223
+ if (isIdentifierKey(key) && (typeof raw === "string" || raw == null)) {
224
+ out[key] = raw ?? "";
225
+ continue;
226
+ }
136
227
  const path = joinPath(prefix, key);
137
228
  if (isFieldEnvelope(raw)) {
138
- out[key] = { value: raw.value ?? null, name: raw.name || path };
229
+ const inner = raw.value;
230
+ if (isRecordList(inner)) {
231
+ const owner = ctx.owners?.(key);
232
+ out[key] = {
233
+ value: envelopeListRows(inner, owner),
234
+ name: raw.name || path
235
+ };
236
+ continue;
237
+ }
238
+ out[key] = withLeaf(
239
+ { value: raw.value ?? null, name: raw.name || path },
240
+ raw.ref ? { ref: raw.ref } : ctx
241
+ );
242
+ continue;
243
+ }
244
+ if (isRecordList(raw)) {
245
+ const owner = ctx.owners?.(key);
246
+ out[key] = { value: envelopeListRows(raw, owner), name: path };
139
247
  continue;
140
248
  }
141
249
  if (raw && typeof raw === "object" && !Array.isArray(raw) && !isPortableImageShape(raw) && !raw.$$typeof) {
142
- out[key] = toFieldEnvelopes(raw, path);
250
+ out[key] = toFieldEnvelopes(raw, path, ctx);
143
251
  continue;
144
252
  }
145
- out[key] = { value: emptyValue(raw), name: path };
253
+ out[key] = withLeaf({ value: emptyValue(raw), name: path }, ctx);
146
254
  }
147
255
  return brandFields(out);
148
256
  }
149
- function attachFieldEnvelopes(props) {
257
+ function envelopeListRows(items, owner) {
258
+ return items.map((item) => envelopeListRow(item, owner));
259
+ }
260
+ function envelopeListRow(item, owner) {
261
+ if (isBrandedFields(item) && typeof item.id === "string") {
262
+ return item;
263
+ }
264
+ const raw = item && typeof item === "object" && !Array.isArray(item) ? item : {};
265
+ const id = rowId(raw);
266
+ const ref = isListOwner(owner) && id ? { providerId: owner.providerId, entity: owner.entity, id } : void 0;
267
+ const fields = toFieldEnvelopes(raw, "", {
268
+ ref,
269
+ readOnly: !ref
270
+ });
271
+ const row = fields;
272
+ if (typeof row.id !== "string") {
273
+ row.id = id;
274
+ }
275
+ return row;
276
+ }
277
+ function attachFieldEnvelopes(props, sources) {
150
278
  const raw = props && typeof props === "object" ? { ...props } : {};
151
279
  if (isBrandedFields(raw.fields)) return raw;
152
280
  const { fields: existing, ...content } = raw;
153
281
  const source = existing && typeof existing === "object" && !isFieldEnvelope(existing) ? existing : content;
154
- return { ...raw, fields: toFieldEnvelopes(source) };
282
+ return {
283
+ ...raw,
284
+ fields: toFieldEnvelopes(source, "", {
285
+ owners: (fieldName) => listOwnerForField(fieldName, source, sources)
286
+ })
287
+ };
155
288
  }
156
289
  function walkLayoutNodes(nodes) {
157
290
  if (!Array.isArray(nodes)) return;
158
291
  for (const node of nodes) {
159
292
  if (!node || typeof node !== "object") continue;
160
293
  const rec = node;
161
- rec.props = attachFieldEnvelopes(rec.props);
294
+ rec.props = attachFieldEnvelopes(rec.props, {
295
+ connections: rec.resources,
296
+ reads: rec.reads
297
+ });
162
298
  for (const slot of rec.slots || []) {
163
299
  walkLayoutNodes(slot.children);
164
300
  }
@@ -275,7 +411,10 @@ async function fetchEdgeResolve(options) {
275
411
  return envelopeResolvedPayload(body);
276
412
  }
277
413
  function envelopeResolvedComponent(node) {
278
- node.props = attachFieldEnvelopes(node.props);
414
+ node.props = attachFieldEnvelopes(node.props, {
415
+ connections: node.resources,
416
+ reads: node.reads
417
+ });
279
418
  if (!node.children) return;
280
419
  for (const kids of Object.values(node.children)) {
281
420
  for (const child of kids || []) envelopeResolvedComponent(child);
@@ -569,7 +708,7 @@ async function fetchEdgeQuery(options) {
569
708
  async function queryContent(options) {
570
709
  const target = resolveAmplifyUpTarget({ configTarget: options.target });
571
710
  const { edgeUrl } = resolveAmplifyUpUrls(target);
572
- return fetchEdgeQuery({
711
+ const raw = await fetchEdgeQuery({
573
712
  edgeUrl,
574
713
  trackingId: options.trackingId,
575
714
  route: options.route,
@@ -578,6 +717,13 @@ async function queryContent(options) {
578
717
  fetch: options.fetch,
579
718
  signal: options.signal
580
719
  });
720
+ return {
721
+ ...raw,
722
+ results: envelopeListRows(
723
+ raw.results,
724
+ listOwnerFromSpec(options.spec)
725
+ )
726
+ };
581
727
  }
582
728
  function nextPageSpec(pagination) {
583
729
  return { ...pagination.spec, offset: pagination.nextOffset };