@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/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,21 +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
  }
122
- function isEntryRefItem(value) {
123
- if (!value || typeof value !== "object" || Array.isArray(value)) return false;
124
- const rec = value;
125
- 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;
132
+ function isIdentifierKey(key) {
133
+ return IDENTIFIER_KEYS.has(key);
126
134
  }
127
- function isRepeaterArray(value) {
135
+ function isRecordList(value) {
128
136
  if (!Array.isArray(value) || value.length === 0) return false;
129
137
  const sample = value.find((item) => item != null);
130
138
  if (sample === void 0) return false;
131
139
  if (typeof sample !== "object" || Array.isArray(sample)) return false;
132
140
  if (isPortableImageShape(sample)) return false;
133
- if (isEntryRefItem(sample)) return false;
134
141
  if (sample.$$typeof) return false;
135
142
  return true;
136
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
+ }
137
196
  function joinPath(prefix, key) {
138
197
  return prefix ? `${prefix}.${key}` : key;
139
198
  }
@@ -141,61 +200,101 @@ function emptyValue(raw) {
141
200
  if (raw === void 0) return null;
142
201
  return raw;
143
202
  }
144
- 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 || {};
145
217
  const input = source && typeof source === "object" && !Array.isArray(source) ? source : {};
146
218
  if (isBrandedFields(input)) {
147
219
  return input;
148
220
  }
149
221
  const out = {};
150
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
+ }
151
227
  const path = joinPath(prefix, key);
152
228
  if (isFieldEnvelope(raw)) {
153
229
  const inner = raw.value;
154
- if (isRepeaterArray(inner)) {
230
+ if (isRecordList(inner)) {
231
+ const owner = ctx.owners?.(key);
155
232
  out[key] = {
156
- value: envelopeRepeaterItems(inner, raw.name || path),
233
+ value: envelopeListRows(inner, owner),
157
234
  name: raw.name || path
158
235
  };
159
236
  continue;
160
237
  }
161
- out[key] = { value: raw.value ?? null, name: raw.name || path };
238
+ out[key] = withLeaf(
239
+ { value: raw.value ?? null, name: raw.name || path },
240
+ raw.ref ? { ref: raw.ref } : ctx
241
+ );
162
242
  continue;
163
243
  }
164
- if (isRepeaterArray(raw)) {
165
- out[key] = { value: envelopeRepeaterItems(raw, path), name: path };
244
+ if (isRecordList(raw)) {
245
+ const owner = ctx.owners?.(key);
246
+ out[key] = { value: envelopeListRows(raw, owner), name: path };
166
247
  continue;
167
248
  }
168
249
  if (raw && typeof raw === "object" && !Array.isArray(raw) && !isPortableImageShape(raw) && !raw.$$typeof) {
169
- out[key] = toFieldEnvelopes(raw, path);
250
+ out[key] = toFieldEnvelopes(raw, path, ctx);
170
251
  continue;
171
252
  }
172
- out[key] = { value: emptyValue(raw), name: path };
253
+ out[key] = withLeaf({ value: emptyValue(raw), name: path }, ctx);
173
254
  }
174
255
  return brandFields(out);
175
256
  }
176
- function envelopeRepeaterItems(items, prefix) {
177
- return items.map((item, i) => {
178
- const path = joinPath(prefix, String(i));
179
- if (isBrandedFields(item)) return item;
180
- if (item && typeof item === "object" && !Array.isArray(item)) {
181
- return toFieldEnvelopes(item, path);
182
- }
183
- return toFieldEnvelopes({}, path);
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
184
270
  });
271
+ const row = fields;
272
+ if (typeof row.id !== "string") {
273
+ row.id = id;
274
+ }
275
+ return row;
185
276
  }
186
- function attachFieldEnvelopes(props) {
277
+ function attachFieldEnvelopes(props, sources) {
187
278
  const raw = props && typeof props === "object" ? { ...props } : {};
188
279
  if (isBrandedFields(raw.fields)) return raw;
189
280
  const { fields: existing, ...content } = raw;
190
281
  const source = existing && typeof existing === "object" && !isFieldEnvelope(existing) ? existing : content;
191
- return { ...raw, fields: toFieldEnvelopes(source) };
282
+ return {
283
+ ...raw,
284
+ fields: toFieldEnvelopes(source, "", {
285
+ owners: (fieldName) => listOwnerForField(fieldName, source, sources)
286
+ })
287
+ };
192
288
  }
193
289
  function walkLayoutNodes(nodes) {
194
290
  if (!Array.isArray(nodes)) return;
195
291
  for (const node of nodes) {
196
292
  if (!node || typeof node !== "object") continue;
197
293
  const rec = node;
198
- rec.props = attachFieldEnvelopes(rec.props);
294
+ rec.props = attachFieldEnvelopes(rec.props, {
295
+ connections: rec.resources,
296
+ reads: rec.reads
297
+ });
199
298
  for (const slot of rec.slots || []) {
200
299
  walkLayoutNodes(slot.children);
201
300
  }
@@ -312,7 +411,10 @@ async function fetchEdgeResolve(options) {
312
411
  return envelopeResolvedPayload(body);
313
412
  }
314
413
  function envelopeResolvedComponent(node) {
315
- node.props = attachFieldEnvelopes(node.props);
414
+ node.props = attachFieldEnvelopes(node.props, {
415
+ connections: node.resources,
416
+ reads: node.reads
417
+ });
316
418
  if (!node.children) return;
317
419
  for (const kids of Object.values(node.children)) {
318
420
  for (const child of kids || []) envelopeResolvedComponent(child);
@@ -606,7 +708,7 @@ async function fetchEdgeQuery(options) {
606
708
  async function queryContent(options) {
607
709
  const target = resolveAmplifyUpTarget({ configTarget: options.target });
608
710
  const { edgeUrl } = resolveAmplifyUpUrls(target);
609
- return fetchEdgeQuery({
711
+ const raw = await fetchEdgeQuery({
610
712
  edgeUrl,
611
713
  trackingId: options.trackingId,
612
714
  route: options.route,
@@ -615,6 +717,13 @@ async function queryContent(options) {
615
717
  fetch: options.fetch,
616
718
  signal: options.signal
617
719
  });
720
+ return {
721
+ ...raw,
722
+ results: envelopeListRows(
723
+ raw.results,
724
+ listOwnerFromSpec(options.spec)
725
+ )
726
+ };
618
727
  }
619
728
  function nextPageSpec(pagination) {
620
729
  return { ...pagination.spec, offset: pagination.nextOffset };