@amplifyup/sdk 0.1.33 → 0.1.35

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.d.mts CHANGED
@@ -1,13 +1,24 @@
1
- import { f as AmplifyUpTarget, P as PageConfig, A as AmplifyContentMeta } from './types-npF_lNoq.mjs';
2
- export { r as resolveAmplifyUpTarget, g as resolveAmplifyUpUrls } from './types-npF_lNoq.mjs';
1
+ import { f as AmplifyUpTarget, P as PageConfig, A as AmplifyContentMeta } from './types-CBoVIFI8.mjs';
2
+ export { r as resolveAmplifyUpTarget, g as resolveAmplifyUpUrls } from './types-CBoVIFI8.mjs';
3
+ export { g as FetchEdgeQueryOptions, F as FetchEdgeSelectOptions, Q as QueryContentError, e as QueryContentOptions, k as QueryFilter, l as QueryFilterOp, i as QueryPagination, h as QueryResult, m as QuerySort, j as QuerySpec, S as SelectVariantsError, a as SelectVariantsOptions, b as SelectVariantsResult, c as fetchEdgeQuery, f as fetchEdgeSelect, n as nextPageSpec, q as queryContent, d as searchSpec, s as selectVariants } from './query-6BmINKLR.mjs';
4
+
5
+ /**
6
+ * Compact variant-key codec for URLs and Edge `variants` query params.
7
+ * Default variants are omitted so equivalent assignments share one cache entry.
8
+ */
9
+ declare function serializeVariantKeys(keys: Record<string, string> | null | undefined): string;
10
+ declare function parseVariantKeys(raw: string | null | undefined): Record<string, string>;
11
+ declare function isDefaultVariantKeys(keys: Record<string, string> | null | undefined): boolean;
3
12
 
4
13
  /**
5
14
  * Server-side utilities for AmplifyUp SDK
6
15
  *
7
- * Provides functions for fetching page config on the server
8
- * for SSR/SSG support (Composer preview → orchestrator; production → Edge).
16
+ * Production: SSG at build (Edge /v1/routes + /v1/resolve), then ISR for
17
+ * unknown routes (SSR once → static). Composer preview stays dynamic.
9
18
  */
10
19
 
20
+ /** Default ISR window for published Edge pages (seconds). */
21
+ declare const AMPLIFY_DEFAULT_REVALIDATE = 60;
11
22
  /**
12
23
  * Fetch page configuration on the server.
13
24
  * Production visitors resolve via Edge; Composer preview uses the orchestrator.
@@ -23,6 +34,10 @@ declare function fetchPageConfigServer(route: string, trackingId: string, previe
23
34
  siteId?: string | null;
24
35
  /** Raw search params — used to detect Composer preview when preview flag omitted. */
25
36
  searchParams?: URLSearchParams | Record<string, string | string[] | undefined>;
37
+ /** Next.js ISR for Edge resolve. Ignored in preview. Default 60s. */
38
+ revalidate?: number | false;
39
+ /** Cacheable variant combo (personalized ISR). Do not pass anonId with this. */
40
+ variantKeys?: Record<string, string> | null;
26
41
  }): Promise<PageConfig | null>;
27
42
  /**
28
43
  * Same as fetchPageConfigServer but also returns content source meta
@@ -34,10 +49,32 @@ declare function fetchContentServer(route: string, trackingId: string, options?:
34
49
  anonId?: string | null;
35
50
  siteId?: string | null;
36
51
  searchParams?: URLSearchParams | Record<string, string | string[] | undefined>;
52
+ revalidate?: number | false;
53
+ variantKeys?: Record<string, string> | null;
37
54
  }): Promise<{
38
55
  pageConfig: PageConfig | null;
39
56
  meta: AmplifyContentMeta | null;
40
57
  error: string | null;
41
58
  }>;
59
+ /**
60
+ * Published routes for a tracking id (Edge KV). Soft-fails to [] if Edge is down.
61
+ */
62
+ declare function listPublishedRoutes(trackingId: string, options?: {
63
+ target?: AmplifyUpTarget;
64
+ siteId?: string | null;
65
+ }): Promise<string[]>;
66
+ /**
67
+ * Use from Next.js `generateStaticParams` on a catch-all page.
68
+ * Always includes `/`. Soft-fails to home-only if Edge is unreachable at build.
69
+ *
70
+ * Skips the Edge route list during `next dev` unless `generateStaticPaths: true`.
71
+ */
72
+ declare function generateAmplifyStaticParams(trackingId: string, options?: {
73
+ target?: AmplifyUpTarget;
74
+ siteId?: string | null;
75
+ generateStaticPaths?: boolean;
76
+ }): Promise<{
77
+ slug: string[];
78
+ }[]>;
42
79
 
43
- export { fetchContentServer, fetchPageConfigServer };
80
+ export { AMPLIFY_DEFAULT_REVALIDATE, fetchContentServer, fetchPageConfigServer, generateAmplifyStaticParams, isDefaultVariantKeys, listPublishedRoutes, parseVariantKeys, serializeVariantKeys };
package/dist/server.d.ts CHANGED
@@ -1,13 +1,24 @@
1
- import { f as AmplifyUpTarget, P as PageConfig, A as AmplifyContentMeta } from './types-npF_lNoq.js';
2
- export { r as resolveAmplifyUpTarget, g as resolveAmplifyUpUrls } from './types-npF_lNoq.js';
1
+ import { f as AmplifyUpTarget, P as PageConfig, A as AmplifyContentMeta } from './types-CBoVIFI8.js';
2
+ export { r as resolveAmplifyUpTarget, g as resolveAmplifyUpUrls } from './types-CBoVIFI8.js';
3
+ export { g as FetchEdgeQueryOptions, F as FetchEdgeSelectOptions, Q as QueryContentError, e as QueryContentOptions, k as QueryFilter, l as QueryFilterOp, i as QueryPagination, h as QueryResult, m as QuerySort, j as QuerySpec, S as SelectVariantsError, a as SelectVariantsOptions, b as SelectVariantsResult, c as fetchEdgeQuery, f as fetchEdgeSelect, n as nextPageSpec, q as queryContent, d as searchSpec, s as selectVariants } from './query-DjIuJ71C.js';
4
+
5
+ /**
6
+ * Compact variant-key codec for URLs and Edge `variants` query params.
7
+ * Default variants are omitted so equivalent assignments share one cache entry.
8
+ */
9
+ declare function serializeVariantKeys(keys: Record<string, string> | null | undefined): string;
10
+ declare function parseVariantKeys(raw: string | null | undefined): Record<string, string>;
11
+ declare function isDefaultVariantKeys(keys: Record<string, string> | null | undefined): boolean;
3
12
 
4
13
  /**
5
14
  * Server-side utilities for AmplifyUp SDK
6
15
  *
7
- * Provides functions for fetching page config on the server
8
- * for SSR/SSG support (Composer preview → orchestrator; production → Edge).
16
+ * Production: SSG at build (Edge /v1/routes + /v1/resolve), then ISR for
17
+ * unknown routes (SSR once → static). Composer preview stays dynamic.
9
18
  */
10
19
 
20
+ /** Default ISR window for published Edge pages (seconds). */
21
+ declare const AMPLIFY_DEFAULT_REVALIDATE = 60;
11
22
  /**
12
23
  * Fetch page configuration on the server.
13
24
  * Production visitors resolve via Edge; Composer preview uses the orchestrator.
@@ -23,6 +34,10 @@ declare function fetchPageConfigServer(route: string, trackingId: string, previe
23
34
  siteId?: string | null;
24
35
  /** Raw search params — used to detect Composer preview when preview flag omitted. */
25
36
  searchParams?: URLSearchParams | Record<string, string | string[] | undefined>;
37
+ /** Next.js ISR for Edge resolve. Ignored in preview. Default 60s. */
38
+ revalidate?: number | false;
39
+ /** Cacheable variant combo (personalized ISR). Do not pass anonId with this. */
40
+ variantKeys?: Record<string, string> | null;
26
41
  }): Promise<PageConfig | null>;
27
42
  /**
28
43
  * Same as fetchPageConfigServer but also returns content source meta
@@ -34,10 +49,32 @@ declare function fetchContentServer(route: string, trackingId: string, options?:
34
49
  anonId?: string | null;
35
50
  siteId?: string | null;
36
51
  searchParams?: URLSearchParams | Record<string, string | string[] | undefined>;
52
+ revalidate?: number | false;
53
+ variantKeys?: Record<string, string> | null;
37
54
  }): Promise<{
38
55
  pageConfig: PageConfig | null;
39
56
  meta: AmplifyContentMeta | null;
40
57
  error: string | null;
41
58
  }>;
59
+ /**
60
+ * Published routes for a tracking id (Edge KV). Soft-fails to [] if Edge is down.
61
+ */
62
+ declare function listPublishedRoutes(trackingId: string, options?: {
63
+ target?: AmplifyUpTarget;
64
+ siteId?: string | null;
65
+ }): Promise<string[]>;
66
+ /**
67
+ * Use from Next.js `generateStaticParams` on a catch-all page.
68
+ * Always includes `/`. Soft-fails to home-only if Edge is unreachable at build.
69
+ *
70
+ * Skips the Edge route list during `next dev` unless `generateStaticPaths: true`.
71
+ */
72
+ declare function generateAmplifyStaticParams(trackingId: string, options?: {
73
+ target?: AmplifyUpTarget;
74
+ siteId?: string | null;
75
+ generateStaticPaths?: boolean;
76
+ }): Promise<{
77
+ slug: string[];
78
+ }[]>;
42
79
 
43
- export { fetchContentServer, fetchPageConfigServer };
80
+ export { AMPLIFY_DEFAULT_REVALIDATE, fetchContentServer, fetchPageConfigServer, generateAmplifyStaticParams, isDefaultVariantKeys, listPublishedRoutes, parseVariantKeys, serializeVariantKeys };
package/dist/server.js CHANGED
@@ -119,6 +119,43 @@ function normalizeRoute(route) {
119
119
  return path || "/";
120
120
  }
121
121
 
122
+ // src/edge/variantKeys.ts
123
+ function serializeVariantKeys(keys) {
124
+ if (!keys) return "";
125
+ const parts = [];
126
+ const zoneIds = Object.keys(keys).sort();
127
+ for (const zoneId of zoneIds) {
128
+ const variant = keys[zoneId];
129
+ if (!variant || variant === "default") continue;
130
+ parts.push(`${zoneId}:${variant}`);
131
+ }
132
+ return parts.join(",");
133
+ }
134
+ function parseVariantKeys(raw) {
135
+ const out = {};
136
+ const text = (raw || "").trim();
137
+ if (!text) return out;
138
+ const decoded = safeDecode(text);
139
+ for (const part of decoded.split(",")) {
140
+ const i = part.indexOf(":");
141
+ if (i <= 0) continue;
142
+ const zoneId = part.slice(0, i).trim();
143
+ const variant = part.slice(i + 1).trim();
144
+ if (zoneId && variant) out[zoneId] = variant;
145
+ }
146
+ return out;
147
+ }
148
+ function isDefaultVariantKeys(keys) {
149
+ return serializeVariantKeys(keys) === "";
150
+ }
151
+ function safeDecode(value) {
152
+ try {
153
+ return decodeURIComponent(value);
154
+ } catch {
155
+ return value;
156
+ }
157
+ }
158
+
122
159
  // src/edge/resolve.ts
123
160
  var EdgeResolveError = class extends Error {
124
161
  constructor(message, status, body) {
@@ -136,12 +173,23 @@ async function fetchEdgeResolve(options) {
136
173
  trackingId: options.trackingId
137
174
  });
138
175
  if (options.siteId) params.set("site", options.siteId);
139
- if (options.anonId) params.set("anonId", options.anonId);
176
+ const variantParam = serializeVariantKeys(options.variantKeys);
177
+ if (variantParam) {
178
+ params.set("variants", variantParam);
179
+ } else if (options.anonId) {
180
+ params.set("anonId", options.anonId);
181
+ }
140
182
  const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
141
- const res = await fetchFn(`${base}/v1/resolve?${params.toString()}`, {
183
+ const init = {
142
184
  method: "GET",
143
185
  headers: { Accept: "application/json" }
144
- });
186
+ };
187
+ if (options.revalidate !== void 0) {
188
+ init.next = { revalidate: options.revalidate };
189
+ } else {
190
+ init.cache = "no-store";
191
+ }
192
+ const res = await fetchFn(`${base}/v1/resolve?${params.toString()}`, init);
145
193
  const body = await res.json().catch(() => ({}));
146
194
  if (!res.ok) {
147
195
  throw new EdgeResolveError(
@@ -230,9 +278,11 @@ async function fetchContent(options) {
230
278
  edgeUrl: options.edgeUrl,
231
279
  trackingId: options.trackingId,
232
280
  route: options.route,
233
- anonId: options.anonId,
281
+ anonId: options.variantKeys ? void 0 : options.anonId,
234
282
  siteId: options.siteId,
235
- fetch: options.fetchFn
283
+ fetch: options.fetchFn,
284
+ revalidate: options.revalidate,
285
+ variantKeys: options.variantKeys
236
286
  });
237
287
  const pageConfig = mapResolvedToPageConfig(payload);
238
288
  const degraded = collectDegraded(payload);
@@ -270,7 +320,176 @@ async function fetchContent(options) {
270
320
  }
271
321
  }
272
322
 
323
+ // src/edge/routes.ts
324
+ async function fetchEdgeRoutes(options) {
325
+ const base = options.edgeUrl.replace(/\/+$/, "");
326
+ const params = new URLSearchParams();
327
+ if (options.trackingId) params.set("trackingId", options.trackingId);
328
+ if (options.siteId) params.set("site", options.siteId);
329
+ const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
330
+ const controller = new AbortController();
331
+ const timer = setTimeout(() => controller.abort(), 8e3);
332
+ try {
333
+ const res = await fetchFn(`${base}/v1/routes?${params.toString()}`, {
334
+ method: "GET",
335
+ headers: { Accept: "application/json" },
336
+ cache: "no-store",
337
+ signal: controller.signal
338
+ });
339
+ const body = await res.json().catch(() => ({}));
340
+ if (!res.ok) {
341
+ throw new Error(body.error || `Edge routes failed (${res.status})`);
342
+ }
343
+ return {
344
+ siteId: body.siteId || "",
345
+ routes: Array.isArray(body.routes) ? body.routes : []
346
+ };
347
+ } finally {
348
+ clearTimeout(timer);
349
+ }
350
+ }
351
+
352
+ // src/edge/select.ts
353
+ var SelectVariantsError = class extends Error {
354
+ constructor(message, status, body) {
355
+ super(message);
356
+ this.name = "SelectVariantsError";
357
+ this.status = status;
358
+ this.body = body;
359
+ }
360
+ };
361
+ async function fetchEdgeSelect(options) {
362
+ const base = options.edgeUrl.replace(/\/+$/, "");
363
+ const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
364
+ if (!options.trackingId && !options.site) {
365
+ throw new SelectVariantsError("trackingId or site is required", 400);
366
+ }
367
+ const res = await fetchFn(`${base}/v1/select`, {
368
+ method: "POST",
369
+ headers: {
370
+ Accept: "application/json",
371
+ "Content-Type": "application/json"
372
+ },
373
+ cache: "no-store",
374
+ signal: options.signal,
375
+ body: JSON.stringify({
376
+ view: normalizeRoute(options.view),
377
+ ...options.trackingId ? { trackingId: options.trackingId } : {},
378
+ ...options.site ? { site: options.site } : {},
379
+ anonId: options.anonId,
380
+ ...options.zones?.length ? { zones: options.zones } : {},
381
+ ...options.context ? { context: options.context } : {}
382
+ })
383
+ });
384
+ const body = await res.json().catch(() => ({}));
385
+ if (!res.ok) {
386
+ throw new SelectVariantsError(
387
+ body.error || `Select failed (${res.status})`,
388
+ res.status,
389
+ body
390
+ );
391
+ }
392
+ return {
393
+ siteId: String(body.siteId || ""),
394
+ view: String(body.view || normalizeRoute(options.view)),
395
+ variantKeys: body.variantKeys && typeof body.variantKeys === "object" ? body.variantKeys : {},
396
+ meta: body.meta
397
+ };
398
+ }
399
+ async function selectVariants(options) {
400
+ const target = resolveAmplifyUpTarget({ configTarget: options.target });
401
+ const { edgeUrl } = resolveAmplifyUpUrls(target);
402
+ return fetchEdgeSelect({
403
+ edgeUrl: options.edgeUrl || edgeUrl,
404
+ trackingId: options.trackingId,
405
+ site: options.site,
406
+ view: options.view,
407
+ anonId: options.anonId,
408
+ zones: options.zones,
409
+ context: options.context,
410
+ fetch: options.fetch,
411
+ signal: options.signal
412
+ });
413
+ }
414
+
415
+ // src/edge/query.ts
416
+ var QueryContentError = class extends Error {
417
+ constructor(message, status, body) {
418
+ super(message);
419
+ this.name = "QueryContentError";
420
+ this.status = status;
421
+ this.body = body;
422
+ }
423
+ };
424
+ async function fetchEdgeQuery(options) {
425
+ const base = options.edgeUrl.replace(/\/+$/, "");
426
+ const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
427
+ const limit = Math.max(1, Number(options.spec.limit) || 10);
428
+ const offset = Math.max(0, Number(options.spec.offset) || 0);
429
+ const res = await fetchFn(`${base}/v1/query`, {
430
+ method: "POST",
431
+ headers: {
432
+ Accept: "application/json",
433
+ "Content-Type": "application/json"
434
+ },
435
+ cache: "no-store",
436
+ signal: options.signal,
437
+ body: JSON.stringify({
438
+ route: normalizeRoute(options.route),
439
+ trackingId: options.trackingId,
440
+ ...options.siteId ? { site: options.siteId } : {},
441
+ spec: { ...options.spec, limit, offset }
442
+ })
443
+ });
444
+ const body = await res.json().catch(() => ({}));
445
+ if (!res.ok) {
446
+ throw new QueryContentError(
447
+ body.error || `Content query failed (${res.status})`,
448
+ res.status,
449
+ body
450
+ );
451
+ }
452
+ const results = Array.isArray(body.results) ? body.results : [];
453
+ const resolvedLimit = Number(body.limit) || limit;
454
+ const resolvedOffset = Number.isFinite(Number(body.offset)) ? Number(body.offset) : offset;
455
+ return {
456
+ results,
457
+ count: Number(body.count) || results.length,
458
+ hasMore: !!body.hasMore,
459
+ limit: resolvedLimit,
460
+ offset: resolvedOffset,
461
+ nextOffset: Number(body.nextOffset) || resolvedOffset + resolvedLimit,
462
+ degraded: body.degraded
463
+ };
464
+ }
465
+ async function queryContent(options) {
466
+ const target = resolveAmplifyUpTarget({ configTarget: options.target });
467
+ const { edgeUrl } = resolveAmplifyUpUrls(target);
468
+ return fetchEdgeQuery({
469
+ edgeUrl,
470
+ trackingId: options.trackingId,
471
+ route: options.route,
472
+ spec: options.spec,
473
+ siteId: options.siteId,
474
+ fetch: options.fetch,
475
+ signal: options.signal
476
+ });
477
+ }
478
+ function nextPageSpec(pagination) {
479
+ return { ...pagination.spec, offset: pagination.nextOffset };
480
+ }
481
+ function searchSpec(pagination, field, term) {
482
+ const value = String(term || "").trim();
483
+ const base = { ...pagination.spec, offset: 0 };
484
+ if (!value) return base;
485
+ return {
486
+ ...base,
487
+ filter: [...base.filter || [], { field, op: "contains", value }]
488
+ };
489
+ }
490
+
273
491
  // src/server.tsx
492
+ var AMPLIFY_DEFAULT_REVALIDATE = 60;
274
493
  async function fetchPageConfigServer(route, trackingId, preview = false, options) {
275
494
  const target = resolveAmplifyUpTarget({
276
495
  configTarget: options?.target
@@ -306,7 +525,9 @@ async function fetchPageConfigServer(route, trackingId, preview = false, options
306
525
  preview: previewFlag,
307
526
  orchestratorUrl,
308
527
  edgeUrl,
309
- fetchOrchestratorPage
528
+ fetchOrchestratorPage,
529
+ revalidate: previewFlag ? void 0 : options?.revalidate ?? AMPLIFY_DEFAULT_REVALIDATE,
530
+ variantKeys: previewFlag ? void 0 : options?.variantKeys
310
531
  });
311
532
  return result.pageConfig;
312
533
  }
@@ -344,7 +565,9 @@ async function fetchContentServer(route, trackingId, options) {
344
565
  } catch {
345
566
  return null;
346
567
  }
347
- }
568
+ },
569
+ revalidate: previewFlag ? void 0 : options?.revalidate ?? AMPLIFY_DEFAULT_REVALIDATE,
570
+ variantKeys: previewFlag ? void 0 : options?.variantKeys
348
571
  });
349
572
  return {
350
573
  pageConfig: result.pageConfig,
@@ -352,10 +575,58 @@ async function fetchContentServer(route, trackingId, options) {
352
575
  error: result.error
353
576
  };
354
577
  }
578
+ async function listPublishedRoutes(trackingId, options) {
579
+ if (!trackingId && !options?.siteId) return [];
580
+ const target = resolveAmplifyUpTarget({
581
+ configTarget: options?.target
582
+ });
583
+ const { edgeUrl } = resolveAmplifyUpUrls(target);
584
+ try {
585
+ const listed = await fetchEdgeRoutes({
586
+ edgeUrl,
587
+ trackingId: trackingId || void 0,
588
+ siteId: options?.siteId
589
+ });
590
+ return listed.routes;
591
+ } catch {
592
+ return [];
593
+ }
594
+ }
595
+ function routeToStaticParam(route) {
596
+ const normalized = normalizeRoute(route);
597
+ if (normalized === "/") return { slug: [] };
598
+ return { slug: normalized.replace(/^\//, "").split("/") };
599
+ }
600
+ async function generateAmplifyStaticParams(trackingId, options) {
601
+ const generateStaticPaths = options?.generateStaticPaths ?? process.env.NODE_ENV !== "development";
602
+ if (!generateStaticPaths) {
603
+ return [{ slug: [] }];
604
+ }
605
+ const routes = await listPublishedRoutes(trackingId, options);
606
+ const params = routes.map(routeToStaticParam);
607
+ if (!params.some((p) => p.slug.length === 0)) {
608
+ params.unshift({ slug: [] });
609
+ }
610
+ return params;
611
+ }
355
612
 
613
+ exports.AMPLIFY_DEFAULT_REVALIDATE = AMPLIFY_DEFAULT_REVALIDATE;
614
+ exports.QueryContentError = QueryContentError;
615
+ exports.SelectVariantsError = SelectVariantsError;
356
616
  exports.fetchContentServer = fetchContentServer;
617
+ exports.fetchEdgeQuery = fetchEdgeQuery;
618
+ exports.fetchEdgeSelect = fetchEdgeSelect;
357
619
  exports.fetchPageConfigServer = fetchPageConfigServer;
620
+ exports.generateAmplifyStaticParams = generateAmplifyStaticParams;
621
+ exports.isDefaultVariantKeys = isDefaultVariantKeys;
622
+ exports.listPublishedRoutes = listPublishedRoutes;
623
+ exports.nextPageSpec = nextPageSpec;
624
+ exports.parseVariantKeys = parseVariantKeys;
625
+ exports.queryContent = queryContent;
358
626
  exports.resolveAmplifyUpTarget = resolveAmplifyUpTarget;
359
627
  exports.resolveAmplifyUpUrls = resolveAmplifyUpUrls;
628
+ exports.searchSpec = searchSpec;
629
+ exports.selectVariants = selectVariants;
630
+ exports.serializeVariantKeys = serializeVariantKeys;
360
631
  //# sourceMappingURL=server.js.map
361
632
  //# sourceMappingURL=server.js.map