@amplifyup/sdk 0.1.22 → 0.1.24

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.
@@ -1,6 +1,7 @@
1
1
  'use client';
2
- import { createContext, useState, useEffect, Fragment as Fragment$1, useContext } from 'react';
2
+ import { createContext, useState, useEffect, Fragment as Fragment$1, useContext, Component } from 'react';
3
3
  import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
4
+ import 'marked';
4
5
 
5
6
  /* @amplifyup/sdk - https://amplifyup.ai */
6
7
  var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
@@ -151,17 +152,42 @@ var AWSAdapter = class {
151
152
  };
152
153
  this.sendEvent("/v1/track", eventData);
153
154
  }
154
- identify(userId, traits) {
155
+ identify(userIdOrIdentifiers, traits = {}) {
155
156
  if (!this.initialized) {
156
157
  console.warn("AmplifyUp SDK: Not initialized. Call init() first.");
157
158
  return;
158
159
  }
160
+ const isMap = userIdOrIdentifiers !== null && typeof userIdOrIdentifiers === "object" && !Array.isArray(userIdOrIdentifiers);
161
+ const identifiers = isMap ? userIdOrIdentifiers : traits.identifiers;
162
+ const userId = isMap ? void 0 : String(userIdOrIdentifiers);
159
163
  const eventData = {
160
164
  type: "identify",
161
- userId,
162
165
  anonymousId: traits.instanceId,
163
166
  traits: {
164
- ...traits
167
+ ...traits,
168
+ ...identifiers ? { identifiers } : {}
169
+ },
170
+ ...identifiers ? { identifiers } : {},
171
+ ...userId ? { userId } : {},
172
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
173
+ };
174
+ this.sendEvent("/v1/track", eventData);
175
+ }
176
+ setProfileFields(fields, context = {}) {
177
+ if (!this.initialized) {
178
+ console.warn("AmplifyUp SDK: Not initialized. Call init() first.");
179
+ return;
180
+ }
181
+ const eventData = {
182
+ type: "set_fields",
183
+ anonymousId: context.instanceId,
184
+ fields,
185
+ traits: {
186
+ instanceId: context.instanceId,
187
+ trackingId: context.trackingId
188
+ },
189
+ properties: {
190
+ trackingId: context.trackingId
165
191
  },
166
192
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
167
193
  };
@@ -223,6 +249,7 @@ function clearIds() {
223
249
  var AWS_DEV_API = "https://api-dev.amplifyup.ai";
224
250
  var AWS_PROD_API = "https://api.amplifyup.ai";
225
251
  var PROD_ORCHESTRATOR = "https://amplifyup.ai";
252
+ var PROD_EDGE = "https://edge.amplifyup.ai";
226
253
  function isAmplifyUpTarget(value) {
227
254
  return value === "local" || value === "dev" || value === "production";
228
255
  }
@@ -258,6 +285,10 @@ function localAwsApiUrl() {
258
285
  const fromEnv = typeof process !== "undefined" ? process.env.NEXT_PUBLIC_AMPLIFYUP_API_URL : void 0;
259
286
  return (fromEnv || AWS_DEV_API).replace(/\/$/, "");
260
287
  }
288
+ function localEdgeUrl() {
289
+ const fromEnv = typeof process !== "undefined" ? process.env.NEXT_PUBLIC_EDGE_URL : void 0;
290
+ return (fromEnv || "http://localhost:8787").replace(/\/$/, "");
291
+ }
261
292
  function resolveAmplifyUpUrls(target) {
262
293
  if (target === "local") {
263
294
  const orchestratorUrl2 = localOrchestratorUrl();
@@ -268,18 +299,22 @@ function resolveAmplifyUpUrls(target) {
268
299
  orchestratorUrl: orchestratorUrl2,
269
300
  // Events + personalize → AWS development stack
270
301
  apiGatewayUrl,
271
- decisionApiUrl: `${apiGatewayUrl}/v1/personalize`
302
+ decisionApiUrl: `${apiGatewayUrl}/v1/personalize`,
303
+ edgeUrl: localEdgeUrl()
272
304
  };
273
305
  }
274
306
  const resolvedApi = typeof process !== "undefined" && process.env.NEXT_PUBLIC_AMPLIFYUP_API_URL?.replace(/\/$/, "") || AWS_PROD_API;
275
307
  const orchestratorFromEnv = typeof process !== "undefined" ? process.env.NEXT_PUBLIC_ORCHESTRATOR_URL : void 0;
276
308
  const isLocalOrchestrator = !!orchestratorFromEnv && (orchestratorFromEnv.includes("localhost") || orchestratorFromEnv.includes("127.0.0.1"));
277
309
  const orchestratorUrl = isLocalOrchestrator || !orchestratorFromEnv ? PROD_ORCHESTRATOR : orchestratorFromEnv.replace(/\/$/, "");
310
+ const edgeFromEnv = typeof process !== "undefined" ? process.env.NEXT_PUBLIC_EDGE_URL?.replace(/\/$/, "") : void 0;
311
+ const edgeUrl = edgeFromEnv && !edgeFromEnv.includes("localhost") && !edgeFromEnv.includes("127.0.0.1") ? edgeFromEnv : PROD_EDGE;
278
312
  return {
279
313
  target: "production",
280
314
  apiGatewayUrl: resolvedApi,
281
315
  orchestratorUrl,
282
- decisionApiUrl: `${resolvedApi}/v1/personalize`
316
+ decisionApiUrl: `${resolvedApi}/v1/personalize`,
317
+ edgeUrl
283
318
  };
284
319
  }
285
320
 
@@ -338,6 +373,8 @@ var AmplifyUpSDK = class {
338
373
  this.apiGatewayUrl = "";
339
374
  this.orchestratorUrl = "";
340
375
  this.decisionApiUrl = "";
376
+ this.edgeUrl = "";
377
+ this.siteId = "";
341
378
  this.target = "local";
342
379
  this.initialized = false;
343
380
  /** Prevents overlapping init() from React Strict Mode double-mount */
@@ -494,7 +531,8 @@ var AmplifyUpSDK = class {
494
531
  isInitialized: () => this.isInitialized(),
495
532
  page: (url, properties) => this.page(url, properties),
496
533
  track: (eventName, properties) => this.track(eventName, properties),
497
- identify: (userId, traits) => this.identify(userId, traits)
534
+ identify: (userIdOrIdentifiers, traits) => this.identify(userIdOrIdentifiers, traits),
535
+ setProfileFields: (fields) => this.setProfileFields(fields)
498
536
  };
499
537
  }
500
538
  async performInit(config) {
@@ -509,6 +547,8 @@ var AmplifyUpSDK = class {
509
547
  this.apiGatewayUrl = urls.apiGatewayUrl;
510
548
  this.orchestratorUrl = urls.orchestratorUrl;
511
549
  this.decisionApiUrl = urls.decisionApiUrl;
550
+ this.edgeUrl = urls.edgeUrl;
551
+ this.siteId = config.siteId || "";
512
552
  const previewOverrides = readPreviewQueryOverrides();
513
553
  if (previewOverrides.orchestratorUrl) {
514
554
  this.orchestratorUrl = previewOverrides.orchestratorUrl;
@@ -517,6 +557,7 @@ var AmplifyUpSDK = class {
517
557
  this.target = "production";
518
558
  this.apiGatewayUrl = prodUrls.apiGatewayUrl;
519
559
  this.decisionApiUrl = prodUrls.decisionApiUrl;
560
+ this.edgeUrl = prodUrls.edgeUrl;
520
561
  }
521
562
  }
522
563
  this.config = {
@@ -524,7 +565,8 @@ var AmplifyUpSDK = class {
524
565
  debug: config.debug ?? true,
525
566
  // Enable debug by default
526
567
  target: this.target,
527
- dev: config.dev
568
+ dev: config.dev,
569
+ ...config.siteId ? { siteId: config.siteId } : {}
528
570
  };
529
571
  try {
530
572
  const adapterConfig = {
@@ -922,12 +964,17 @@ var AmplifyUpSDK = class {
922
964
  });
923
965
  }
924
966
  /**
925
- * Identify user (when they sign up or log in)
967
+ * Identify a visitor.
968
+ * - identify('user_123') — bare string; server maps via org default type
969
+ * - identify({ email: '...', crm_id: '...' }) — explicit types
970
+ * - identify('user_123', traits) — legacy form
926
971
  */
927
- identify(userId, traits) {
972
+ identify(userIdOrIdentifiers, traits) {
928
973
  if (this.isPreviewMode()) {
929
974
  if (this.config?.debug) {
930
- console.log("AmplifyUp SDK: Identify tracking disabled in preview mode", { userId });
975
+ console.log("AmplifyUp SDK: Identify tracking disabled in preview mode", {
976
+ userIdOrIdentifiers
977
+ });
931
978
  }
932
979
  return;
933
980
  }
@@ -943,10 +990,45 @@ var AmplifyUpSDK = class {
943
990
  trackingId: this.config?.trackingId,
944
991
  ...traits
945
992
  };
946
- this.adapter.identify(userId, identifyTraits);
947
- setUserId(userId);
993
+ this.adapter.identify(userIdOrIdentifiers, identifyTraits);
994
+ if (typeof userIdOrIdentifiers === "string") {
995
+ setUserId(userIdOrIdentifiers);
996
+ }
997
+ if (this.config?.debug) {
998
+ console.log("AmplifyUp SDK: User identified", {
999
+ userIdOrIdentifiers,
1000
+ instanceId
1001
+ });
1002
+ }
1003
+ }
1004
+ /**
1005
+ * Set profile fields (partial update). Omitted keys are left alone;
1006
+ * an explicit empty value clears the field.
1007
+ */
1008
+ setProfileFields(fields) {
1009
+ if (this.isPreviewMode()) {
1010
+ if (this.config?.debug) {
1011
+ console.log("AmplifyUp SDK: setProfileFields disabled in preview mode", {
1012
+ fields
1013
+ });
1014
+ }
1015
+ return;
1016
+ }
1017
+ if (!this.isInitialized()) {
1018
+ if (this.config?.debug) {
1019
+ console.warn("AmplifyUp SDK: Not initialized. setProfileFields not called.");
1020
+ }
1021
+ return;
1022
+ }
1023
+ const instanceId = getInstanceId();
1024
+ if (typeof this.adapter.setProfileFields === "function") {
1025
+ this.adapter.setProfileFields(fields, {
1026
+ instanceId: instanceId || void 0,
1027
+ trackingId: this.config?.trackingId
1028
+ });
1029
+ }
948
1030
  if (this.config?.debug) {
949
- console.log("AmplifyUp SDK: User identified", { userId, instanceId });
1031
+ console.log("AmplifyUp SDK: Profile fields set", { fields, instanceId });
950
1032
  }
951
1033
  }
952
1034
  /**
@@ -1121,6 +1203,18 @@ Available Console Commands:
1121
1203
  getDecisionApiUrl() {
1122
1204
  return this.decisionApiUrl || void 0;
1123
1205
  }
1206
+ /**
1207
+ * Get Edge Worker URL (production content resolve)
1208
+ */
1209
+ getEdgeUrl() {
1210
+ return this.edgeUrl || void 0;
1211
+ }
1212
+ /**
1213
+ * Optional site id from config (Edge can resolve via trackingId alone).
1214
+ */
1215
+ getSiteId() {
1216
+ return this.siteId || this.config?.siteId || void 0;
1217
+ }
1124
1218
  /**
1125
1219
  * Build API URL for fetching page config from the Layout Provider
1126
1220
  */
@@ -2414,6 +2508,53 @@ function useAmplifyUp() {
2414
2508
  }
2415
2509
  return context;
2416
2510
  }
2511
+ var ComponentRenderBoundary = class extends Component {
2512
+ constructor() {
2513
+ super(...arguments);
2514
+ this.state = { error: null };
2515
+ }
2516
+ static getDerivedStateFromError(error) {
2517
+ return { error };
2518
+ }
2519
+ componentDidCatch(error) {
2520
+ console.error(
2521
+ `[AmplifyRenderer] "${this.props.componentId}" (${this.props.nodeId}) threw while rendering:`,
2522
+ error
2523
+ );
2524
+ }
2525
+ render() {
2526
+ if (this.state.error) {
2527
+ if (process.env.NODE_ENV !== "production") {
2528
+ return /* @__PURE__ */ jsxs(
2529
+ "div",
2530
+ {
2531
+ "data-amplifyup-render-error": "",
2532
+ "data-component-id": this.props.componentId,
2533
+ "data-node-id": this.props.nodeId,
2534
+ style: {
2535
+ margin: "1rem 0",
2536
+ padding: "0.75rem 1rem",
2537
+ border: "1px solid #fca5a5",
2538
+ borderRadius: "0.5rem",
2539
+ background: "#fef2f2",
2540
+ color: "#991b1b",
2541
+ fontSize: "0.875rem"
2542
+ },
2543
+ children: [
2544
+ 'Component "',
2545
+ this.props.componentId,
2546
+ '" failed to render:',
2547
+ " ",
2548
+ this.state.error.message
2549
+ ]
2550
+ }
2551
+ );
2552
+ }
2553
+ return null;
2554
+ }
2555
+ return this.props.children;
2556
+ }
2557
+ };
2417
2558
  function AmplifyRenderer({
2418
2559
  renderComponent,
2419
2560
  pageConfig: serverPageConfig,
@@ -2426,6 +2567,7 @@ function AmplifyRenderer({
2426
2567
  const pageConfig = serverPageConfig || clientHook.pageConfig || null;
2427
2568
  const loading = serverPageConfig ? false : clientHook.loading;
2428
2569
  const error = serverPageConfig ? null : clientHook.error;
2570
+ const contentSource = clientHook.source;
2429
2571
  const [variantCache, setVariantCache] = useState({});
2430
2572
  useEffect(() => {
2431
2573
  if (typeof window !== "undefined") {
@@ -2567,6 +2709,9 @@ function AmplifyRenderer({
2567
2709
  }, [pageConfig]);
2568
2710
  useEffect(() => {
2569
2711
  if (!pageConfig?.layoutTree || !pageConfig.layoutId) return;
2712
+ const roots = pageConfig.layoutTree;
2713
+ const fromEdge = contentSource === "edge" || roots.some((n) => n._contentSource === "edge");
2714
+ if (fromEdge) return;
2570
2715
  const sdk = getSDK();
2571
2716
  const instanceId = sdk.getInstanceId();
2572
2717
  const decisionApiUrl = sdk.getDecisionApiUrl();
@@ -2661,7 +2806,7 @@ function AmplifyRenderer({
2661
2806
  setVariantCache(newCache);
2662
2807
  };
2663
2808
  fetchVariants();
2664
- }, [pageConfig?.layoutTree, pageConfig?.layoutId, pageConfig?.route]);
2809
+ }, [pageConfig?.layoutTree, pageConfig?.layoutId, pageConfig?.route, contentSource]);
2665
2810
  if (loading) {
2666
2811
  return /* @__PURE__ */ jsx(Fragment, { children: loadingComponent || /* @__PURE__ */ jsx("div", { className: "min-h-screen bg-gray-50", children: /* @__PURE__ */ jsx("div", { className: "container mx-auto px-4 py-16", children: /* @__PURE__ */ jsxs("div", { className: "animate-pulse", children: [
2667
2812
  /* @__PURE__ */ jsx("div", { className: "h-8 bg-gray-200 rounded w-1/4 mb-4" }),
@@ -2766,11 +2911,20 @@ function AmplifyRenderer({
2766
2911
  const slotsToPass = Object.keys(slotChildren).length > 0 ? slotChildren : void 0;
2767
2912
  console.log(`[AmplifyRenderer] Calling renderComponent for ${node.componentId}`, {
2768
2913
  hasSlots: !!slotsToPass,
2769
- slotKeys: slotsToPass ? Object.keys(slotsToPass) : []
2914
+ slotKeys: slotsToPass ? Object.keys(slotsToPass) : [],
2915
+ propKeys: Object.keys(node.props || {}),
2916
+ props: node.props || {}
2770
2917
  });
2771
- return /* @__PURE__ */ jsx("div", { "data-node-id": node.id, "data-amplifyup-node": "", children: renderComponent(node.componentId, node.props || {}, slotsToPass, {
2772
- layoutNodeId: node.id
2773
- }) });
2918
+ return /* @__PURE__ */ jsx("div", { "data-node-id": node.id, "data-amplifyup-node": "", children: /* @__PURE__ */ jsx(
2919
+ ComponentRenderBoundary,
2920
+ {
2921
+ componentId: node.componentId,
2922
+ nodeId: node.id,
2923
+ children: renderComponent(node.componentId, node.props || {}, slotsToPass, {
2924
+ layoutNodeId: node.id
2925
+ })
2926
+ }
2927
+ ) });
2774
2928
  };
2775
2929
  const rawTree = pageConfig.layoutTree;
2776
2930
  const layoutTree = Array.isArray(rawTree) ? rawTree : rawTree && typeof rawTree === "object" && rawTree.componentId ? [rawTree] : [];