@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/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
  }
@@ -115,25 +125,79 @@ function isFieldEnvelope(value) {
115
125
  const rec = value;
116
126
  return typeof rec.name === "string" && "value" in rec && !("url" in rec);
117
127
  }
118
- function isPortableImageShape(value) {
119
- if (!value || typeof value !== "object" || Array.isArray(value)) return false;
120
- return "url" in value;
128
+ function isPageResourceSentinel(value) {
129
+ return !!value && typeof value === "object" && !Array.isArray(value) && typeof value.__amplifyupPageResource === "string";
121
130
  }
122
- function isEntryRefItem(value) {
131
+ function isPortableImageShape(value) {
123
132
  if (!value || typeof value !== "object" || Array.isArray(value)) return false;
133
+ if ("url" in value) return true;
124
134
  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;
135
+ return rec._type === "image" || rec.asset != null;
126
136
  }
127
- function isRepeaterArray(value) {
137
+ function isIdentifierKey(key) {
138
+ return IDENTIFIER_KEYS.has(key);
139
+ }
140
+ function isRecordList(value) {
128
141
  if (!Array.isArray(value) || value.length === 0) return false;
129
142
  const sample = value.find((item) => item != null);
130
143
  if (sample === void 0) return false;
131
144
  if (typeof sample !== "object" || Array.isArray(sample)) return false;
132
145
  if (isPortableImageShape(sample)) return false;
133
- if (isEntryRefItem(sample)) return false;
134
146
  if (sample.$$typeof) return false;
135
147
  return true;
136
148
  }
149
+ function isFieldRef(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 && typeof rec.id === "string" && !!rec.id;
153
+ }
154
+ function isListOwner(value) {
155
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
156
+ const rec = value;
157
+ return typeof rec.providerId === "string" && !!rec.providerId && typeof rec.entity === "string" && !!rec.entity;
158
+ }
159
+ function listOwnerFromConnection(conn) {
160
+ if (!conn || typeof conn !== "object") return void 0;
161
+ if (conn.mode === "query") {
162
+ const providerId = String(conn.spec?.providerId || "").trim();
163
+ const entity = String(conn.spec?.entity || "").trim();
164
+ return providerId && entity ? { providerId, entity } : void 0;
165
+ }
166
+ if (conn.mode === "item" || conn.mode === "items") {
167
+ const providerId = String(conn.providerId || "").trim();
168
+ const entity = String(conn.entity || "").trim();
169
+ return providerId && entity ? { providerId, entity } : void 0;
170
+ }
171
+ return void 0;
172
+ }
173
+ function listOwnerFromSpec(spec) {
174
+ const providerId = String(spec?.providerId || "").trim();
175
+ const entity = String(spec?.entity || "").trim();
176
+ return providerId && entity ? { providerId, entity } : void 0;
177
+ }
178
+ function isEnvelopeSources(value) {
179
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
180
+ const rec = value;
181
+ return rec.connections != null || rec.reads != null;
182
+ }
183
+ function normalizeEnvelopeSources(sources) {
184
+ if (!sources) return {};
185
+ if (isEnvelopeSources(sources) && !("mode" in sources)) {
186
+ return sources;
187
+ }
188
+ return { connections: sources };
189
+ }
190
+ function listOwnerForField(fieldName, props, sources) {
191
+ const { connections, reads } = normalizeEnvelopeSources(sources);
192
+ const resourceName = reads?.[fieldName]?.resource || fieldName;
193
+ const fromConn = listOwnerFromConnection(connections?.[resourceName]);
194
+ if (fromConn) return fromConn;
195
+ const paging = props?.[`${fieldName}Pagination`];
196
+ if (paging && typeof paging === "object" && !Array.isArray(paging)) {
197
+ return listOwnerFromSpec(paging.spec);
198
+ }
199
+ return void 0;
200
+ }
137
201
  function joinPath(prefix, key) {
138
202
  return prefix ? `${prefix}.${key}` : key;
139
203
  }
@@ -141,61 +205,105 @@ function emptyValue(raw) {
141
205
  if (raw === void 0) return null;
142
206
  return raw;
143
207
  }
144
- function toFieldEnvelopes(source, prefix = "") {
208
+ function rowId(raw) {
209
+ const candidates = [raw.id, raw._id, raw.docId, raw.key, raw._key];
210
+ for (const c of candidates) {
211
+ if (typeof c === "string" && c.trim()) return c;
212
+ }
213
+ return "";
214
+ }
215
+ function withLeaf(envelope, scope) {
216
+ if (isFieldRef(scope?.ref)) envelope.ref = scope.ref;
217
+ else if (scope?.readOnly) envelope.readOnly = true;
218
+ return envelope;
219
+ }
220
+ function toFieldEnvelopes(source, prefix = "", scope) {
221
+ const ctx = isFieldRef(scope) ? { ref: scope } : scope || {};
145
222
  const input = source && typeof source === "object" && !Array.isArray(source) ? source : {};
146
223
  if (isBrandedFields(input)) {
147
224
  return input;
148
225
  }
149
226
  const out = {};
150
227
  for (const [key, raw] of Object.entries(input)) {
228
+ if (isIdentifierKey(key) && (typeof raw === "string" || raw == null)) {
229
+ out[key] = raw ?? "";
230
+ continue;
231
+ }
151
232
  const path = joinPath(prefix, key);
233
+ if (isPageResourceSentinel(raw)) {
234
+ out[key] = withLeaf({ value: raw, name: path }, ctx);
235
+ continue;
236
+ }
152
237
  if (isFieldEnvelope(raw)) {
153
238
  const inner = raw.value;
154
- if (isRepeaterArray(inner)) {
239
+ if (isRecordList(inner)) {
240
+ const owner = ctx.owners?.(key);
155
241
  out[key] = {
156
- value: envelopeRepeaterItems(inner, raw.name || path),
242
+ value: envelopeListRows(inner, owner),
157
243
  name: raw.name || path
158
244
  };
159
245
  continue;
160
246
  }
161
- out[key] = { value: raw.value ?? null, name: raw.name || path };
247
+ out[key] = withLeaf(
248
+ { value: raw.value ?? null, name: raw.name || path },
249
+ raw.ref ? { ref: raw.ref } : ctx
250
+ );
162
251
  continue;
163
252
  }
164
- if (isRepeaterArray(raw)) {
165
- out[key] = { value: envelopeRepeaterItems(raw, path), name: path };
253
+ if (isRecordList(raw)) {
254
+ const owner = ctx.owners?.(key);
255
+ out[key] = { value: envelopeListRows(raw, owner), name: path };
166
256
  continue;
167
257
  }
168
258
  if (raw && typeof raw === "object" && !Array.isArray(raw) && !isPortableImageShape(raw) && !raw.$$typeof) {
169
- out[key] = toFieldEnvelopes(raw, path);
259
+ out[key] = toFieldEnvelopes(raw, path, ctx);
170
260
  continue;
171
261
  }
172
- out[key] = { value: emptyValue(raw), name: path };
262
+ out[key] = withLeaf({ value: emptyValue(raw), name: path }, ctx);
173
263
  }
174
264
  return brandFields(out);
175
265
  }
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);
266
+ function envelopeListRows(items, owner) {
267
+ return items.map((item) => envelopeListRow(item, owner));
268
+ }
269
+ function envelopeListRow(item, owner) {
270
+ if (isBrandedFields(item) && typeof item.id === "string") {
271
+ return item;
272
+ }
273
+ const raw = item && typeof item === "object" && !Array.isArray(item) ? item : {};
274
+ const id = rowId(raw);
275
+ const ref = isListOwner(owner) && id ? { providerId: owner.providerId, entity: owner.entity, id } : void 0;
276
+ const fields = toFieldEnvelopes(raw, "", {
277
+ ref,
278
+ readOnly: !ref
184
279
  });
280
+ const row = fields;
281
+ if (typeof row.id !== "string") {
282
+ row.id = id;
283
+ }
284
+ return row;
185
285
  }
186
- function attachFieldEnvelopes(props) {
286
+ function attachFieldEnvelopes(props, sources) {
187
287
  const raw = props && typeof props === "object" ? { ...props } : {};
188
288
  if (isBrandedFields(raw.fields)) return raw;
189
289
  const { fields: existing, ...content } = raw;
190
290
  const source = existing && typeof existing === "object" && !isFieldEnvelope(existing) ? existing : content;
191
- return { ...raw, fields: toFieldEnvelopes(source) };
291
+ return {
292
+ ...raw,
293
+ fields: toFieldEnvelopes(source, "", {
294
+ owners: (fieldName) => listOwnerForField(fieldName, source, sources)
295
+ })
296
+ };
192
297
  }
193
298
  function walkLayoutNodes(nodes) {
194
299
  if (!Array.isArray(nodes)) return;
195
300
  for (const node of nodes) {
196
301
  if (!node || typeof node !== "object") continue;
197
302
  const rec = node;
198
- rec.props = attachFieldEnvelopes(rec.props);
303
+ rec.props = attachFieldEnvelopes(rec.props, {
304
+ connections: rec.resources,
305
+ reads: rec.reads
306
+ });
199
307
  for (const slot of rec.slots || []) {
200
308
  walkLayoutNodes(slot.children);
201
309
  }
@@ -312,7 +420,10 @@ async function fetchEdgeResolve(options) {
312
420
  return envelopeResolvedPayload(body);
313
421
  }
314
422
  function envelopeResolvedComponent(node) {
315
- node.props = attachFieldEnvelopes(node.props);
423
+ node.props = attachFieldEnvelopes(node.props, {
424
+ connections: node.resources,
425
+ reads: node.reads
426
+ });
316
427
  if (!node.children) return;
317
428
  for (const kids of Object.values(node.children)) {
318
429
  for (const child of kids || []) envelopeResolvedComponent(child);
@@ -606,7 +717,7 @@ async function fetchEdgeQuery(options) {
606
717
  async function queryContent(options) {
607
718
  const target = resolveAmplifyUpTarget({ configTarget: options.target });
608
719
  const { edgeUrl } = resolveAmplifyUpUrls(target);
609
- return fetchEdgeQuery({
720
+ const raw = await fetchEdgeQuery({
610
721
  edgeUrl,
611
722
  trackingId: options.trackingId,
612
723
  route: options.route,
@@ -615,6 +726,13 @@ async function queryContent(options) {
615
726
  fetch: options.fetch,
616
727
  signal: options.signal
617
728
  });
729
+ return {
730
+ ...raw,
731
+ results: envelopeListRows(
732
+ raw.results,
733
+ listOwnerFromSpec(options.spec)
734
+ )
735
+ };
618
736
  }
619
737
  function nextPageSpec(pagination) {
620
738
  return { ...pagination.spec, offset: pagination.nextOffset };