@amplifyup/sdk 0.1.4 → 0.1.6

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/react.mjs CHANGED
@@ -240,14 +240,16 @@ function resolveAmplifyUpTarget(options) {
240
240
  if (isAmplifyUpTarget(envTarget)) {
241
241
  return normalizeAmplifyUpTarget(envTarget);
242
242
  }
243
- if (typeof window !== "undefined") {
244
- const host = window.location.hostname;
245
- if (host === "localhost" || host === "127.0.0.1") {
246
- return "local";
247
- }
248
- }
249
243
  return "production";
250
244
  }
245
+ function isRemoteOrchestratorUrl(orchestratorUrl) {
246
+ try {
247
+ const host = new URL(orchestratorUrl).hostname.toLowerCase();
248
+ return host !== "localhost" && host !== "127.0.0.1" && host !== "::1";
249
+ } catch {
250
+ return false;
251
+ }
252
+ }
251
253
  function localOrchestratorUrl() {
252
254
  const fromEnv = typeof process !== "undefined" ? process.env.NEXT_PUBLIC_ORCHESTRATOR_URL : void 0;
253
255
  return (fromEnv || "http://localhost:5001").replace(/\/$/, "");
@@ -281,6 +283,32 @@ function resolveAmplifyUpUrls(target) {
281
283
  };
282
284
  }
283
285
 
286
+ // src/preview.ts
287
+ function isPreviewRequest(searchParams) {
288
+ if (searchParams instanceof URLSearchParams) {
289
+ return searchParams.get("preview") === "true" || searchParams.get("visualEditor") === "true";
290
+ }
291
+ const preview = searchParams.preview;
292
+ const visualEditor = searchParams.visualEditor;
293
+ return preview === "true" || visualEditor === "true";
294
+ }
295
+ function isComposerPreview() {
296
+ if (typeof window === "undefined") return false;
297
+ if (window.__AMPLIFYUP_PREVIEW_MODE__ === true) return true;
298
+ const params = new URLSearchParams(window.location.search);
299
+ if (isPreviewRequest(params)) return true;
300
+ if (window.self !== window.top && document.referrer) {
301
+ try {
302
+ const refUrl = new URL(document.referrer);
303
+ if (refUrl.hostname === "amplifyup.ai" || refUrl.hostname.endsWith(".amplifyup.ai") || refUrl.hostname === "localhost" && refUrl.port === "5001" || refUrl.pathname.includes("/dashboard/composer") || refUrl.pathname.includes("/dashboard/routes/")) {
304
+ return true;
305
+ }
306
+ } catch {
307
+ }
308
+ }
309
+ return false;
310
+ }
311
+
284
312
  // src/index.ts
285
313
  function readPreviewQueryOverrides() {
286
314
  if (typeof window === "undefined") return {};
@@ -403,6 +431,7 @@ var AmplifyUpSDK = class {
403
431
  */
404
432
  async init(config) {
405
433
  if (this.initialized && this.adapter?.isInitialized()) {
434
+ this.ensureWindowBridge();
406
435
  return;
407
436
  }
408
437
  if (this.initPromise) {
@@ -415,6 +444,32 @@ var AmplifyUpSDK = class {
415
444
  this.initPromise = null;
416
445
  }
417
446
  }
447
+ /** Attach console helpers to window (idempotent; survives Fast Refresh). */
448
+ ensureWindowBridge() {
449
+ if (typeof window === "undefined") return;
450
+ window.AmplifyUp = {
451
+ getInstanceId: () => this.getInstanceId(),
452
+ getUserId: () => this.getUserId(),
453
+ getTrackingId: () => this.getTrackingId(),
454
+ getTarget: () => this.getTarget(),
455
+ getDiagnostics: () => this.getDiagnostics(),
456
+ getStatus: () => {
457
+ console.log(this.getStatus());
458
+ return this.getDiagnostics();
459
+ },
460
+ testTracking: () => this.testTracking(),
461
+ verifyAWSConnection: () => {
462
+ const result = this.verifyAWSConnection();
463
+ console.log("AWS Connection Verification:", result.message);
464
+ console.log("Full details:", result);
465
+ return result;
466
+ },
467
+ isInitialized: () => this.isInitialized(),
468
+ page: (url, properties) => this.page(url, properties),
469
+ track: (eventName, properties) => this.track(eventName, properties),
470
+ identify: (userId, traits) => this.identify(userId, traits)
471
+ };
472
+ }
418
473
  async performInit(config) {
419
474
  if (!this.adapter) {
420
475
  this.adapter = new AWSAdapter();
@@ -430,6 +485,12 @@ var AmplifyUpSDK = class {
430
485
  const previewOverrides = readPreviewQueryOverrides();
431
486
  if (previewOverrides.orchestratorUrl) {
432
487
  this.orchestratorUrl = previewOverrides.orchestratorUrl;
488
+ if (isRemoteOrchestratorUrl(previewOverrides.orchestratorUrl)) {
489
+ const prodUrls = resolveAmplifyUpUrls("production");
490
+ this.target = "production";
491
+ this.apiGatewayUrl = prodUrls.apiGatewayUrl;
492
+ this.decisionApiUrl = prodUrls.decisionApiUrl;
493
+ }
433
494
  }
434
495
  this.config = {
435
496
  trackingId: previewOverrides.trackingId || config.trackingId,
@@ -447,34 +508,10 @@ var AmplifyUpSDK = class {
447
508
  };
448
509
  await this.adapter.init(adapterConfig);
449
510
  this.initialized = this.adapter.isInitialized();
450
- if (typeof window !== "undefined") {
451
- window.AmplifyUp = {
452
- getInstanceId: () => this.getInstanceId(),
453
- getUserId: () => this.getUserId(),
454
- getTrackingId: () => this.getTrackingId(),
455
- getTarget: () => this.getTarget(),
456
- getDiagnostics: () => this.getDiagnostics(),
457
- getStatus: () => {
458
- console.log(this.getStatus());
459
- return this.getDiagnostics();
460
- },
461
- testTracking: () => this.testTracking(),
462
- verifyAWSConnection: () => {
463
- const result = this.verifyAWSConnection();
464
- console.log("AWS Connection Verification:", result.message);
465
- console.log("Full details:", result);
466
- return result;
467
- },
468
- isInitialized: () => this.isInitialized(),
469
- // Re-export main tracking methods for convenience
470
- page: (url, properties) => this.page(url, properties),
471
- track: (eventName, properties) => this.track(eventName, properties),
472
- identify: (userId, traits) => this.identify(userId, traits)
473
- };
474
- if (this.config?.debug) {
475
- console.log("AmplifyUp SDK: Diagnostic methods available on window.AmplifyUp");
476
- console.log("Try: AmplifyUp.getStatus() or AmplifyUp.testTracking()");
477
- }
511
+ this.ensureWindowBridge();
512
+ if (this.config?.debug) {
513
+ console.log("AmplifyUp SDK: Diagnostic methods available on window.AmplifyUp");
514
+ console.log("Try: AmplifyUp.getStatus() or AmplifyUp.testTracking()");
478
515
  }
479
516
  this.clearTrackingIntervals();
480
517
  if (this.isPreviewMode()) {
@@ -1485,10 +1522,17 @@ Available Console Commands:
1485
1522
  }
1486
1523
  };
1487
1524
  var sdkInstance = null;
1525
+ if (typeof window !== "undefined" && window.__AMPLIFYUP_SDK__) {
1526
+ sdkInstance = window.__AMPLIFYUP_SDK__;
1527
+ }
1488
1528
  function getSDK(adapter) {
1489
1529
  if (!sdkInstance) {
1490
1530
  sdkInstance = new AmplifyUpSDK(adapter);
1531
+ if (typeof window !== "undefined") {
1532
+ window.__AMPLIFYUP_SDK__ = sdkInstance;
1533
+ }
1491
1534
  }
1535
+ sdkInstance.ensureWindowBridge();
1492
1536
  return sdkInstance;
1493
1537
  }
1494
1538
  var trackContentView = (contentId, contentType, properties) => getSDK().trackContentView(contentId, contentType, properties);
@@ -1791,6 +1835,62 @@ function AmplifyRenderer({
1791
1835
  }
1792
1836
  return /* @__PURE__ */ jsx("main", { children: layoutTree.map((node) => /* @__PURE__ */ jsx("div", { children: renderNode(node) }, node.id)) });
1793
1837
  }
1838
+ function defaultPreviewCanvas() {
1839
+ return /* @__PURE__ */ jsx("main", { className: "min-h-[50vh]", "data-amplifyup-composer-canvas": true });
1840
+ }
1841
+ function hasAmplifyLayout(pageConfig) {
1842
+ return !!(pageConfig && pageConfig.id !== "default" && Array.isArray(pageConfig.layoutTree) && pageConfig.layoutTree.length > 0);
1843
+ }
1844
+ function AmplifyPageContent({
1845
+ fallback,
1846
+ renderComponent,
1847
+ pageConfig: pageConfigProp,
1848
+ previewLoadingComponent,
1849
+ previewEmptyComponent,
1850
+ loadingComponent,
1851
+ emptyComponent,
1852
+ errorComponent
1853
+ }) {
1854
+ const client = useAmplifyUp();
1855
+ const preview = isComposerPreview();
1856
+ const pageConfig = pageConfigProp ?? client.pageConfig;
1857
+ const loading = pageConfigProp ? false : client.loading;
1858
+ const error = pageConfigProp ? null : client.error;
1859
+ const previewCanvas = previewEmptyComponent ?? defaultPreviewCanvas();
1860
+ const previewLoading = previewLoadingComponent ?? previewCanvas;
1861
+ if (preview) {
1862
+ return /* @__PURE__ */ jsx(
1863
+ AmplifyRenderer,
1864
+ {
1865
+ pageConfig: pageConfig ?? void 0,
1866
+ renderComponent,
1867
+ loadingComponent: loadingComponent ?? previewLoading,
1868
+ emptyComponent: emptyComponent ?? previewCanvas,
1869
+ errorComponent: errorComponent ?? (() => previewCanvas)
1870
+ }
1871
+ );
1872
+ }
1873
+ if (loading) {
1874
+ return loadingComponent ? /* @__PURE__ */ jsx(Fragment, { children: loadingComponent }) : null;
1875
+ }
1876
+ if (error) {
1877
+ if (errorComponent) return /* @__PURE__ */ jsx(Fragment, { children: errorComponent(error) });
1878
+ return fallback ? /* @__PURE__ */ jsx(Fragment, { children: fallback }) : null;
1879
+ }
1880
+ if (!hasAmplifyLayout(pageConfig)) {
1881
+ return fallback ? /* @__PURE__ */ jsx(Fragment, { children: fallback }) : null;
1882
+ }
1883
+ return /* @__PURE__ */ jsx(
1884
+ AmplifyRenderer,
1885
+ {
1886
+ pageConfig: pageConfig ?? void 0,
1887
+ renderComponent,
1888
+ loadingComponent,
1889
+ emptyComponent,
1890
+ errorComponent
1891
+ }
1892
+ );
1893
+ }
1794
1894
 
1795
1895
  // src/server.tsx
1796
1896
  async function fetchPageConfigServer(route, trackingId, preview = false, options) {
@@ -2020,7 +2120,7 @@ function AmplifyUpProvider({ children, config }) {
2020
2120
  return () => {
2021
2121
  cancelled = true;
2022
2122
  };
2023
- }, []);
2123
+ }, [config.trackingId, config.target, config.debug, config.dev]);
2024
2124
  useEffect(() => {
2025
2125
  if (!initialized) return;
2026
2126
  const fetchConfig = async () => {
@@ -2033,24 +2133,7 @@ function AmplifyUpProvider({ children, config }) {
2033
2133
  return;
2034
2134
  }
2035
2135
  const sdk = getSDK();
2036
- const isPreviewContext = (() => {
2037
- if (typeof window === "undefined") return false;
2038
- if (window.__AMPLIFYUP_PREVIEW_MODE__ === true) return true;
2039
- const params = new URLSearchParams(window.location.search);
2040
- if (params.get("preview") === "true" || params.get("visualEditor") === "true") return true;
2041
- if (window.self !== window.top) {
2042
- if (document.referrer) {
2043
- try {
2044
- const refUrl = new URL(document.referrer);
2045
- if (refUrl.hostname === "localhost" && refUrl.port === "5001" || refUrl.pathname.includes("/dashboard/composer") || refUrl.pathname.includes("/dashboard/routes/")) {
2046
- return true;
2047
- }
2048
- } catch {
2049
- }
2050
- }
2051
- }
2052
- return false;
2053
- })();
2136
+ const isPreviewContext = isComposerPreview();
2054
2137
  const page = await sdk.fetchPageConfig(actualRoute, isPreviewContext);
2055
2138
  if (!page) {
2056
2139
  setPageConfig({
@@ -2082,6 +2165,6 @@ function useAmplifyUp() {
2082
2165
  return context;
2083
2166
  }
2084
2167
 
2085
- export { AmplifyRenderer, AmplifyUpProvider, ComponentContextProvider, Field, Slot, fetchPageConfigServer, useAmplifyUp };
2168
+ export { AmplifyPageContent, AmplifyRenderer, AmplifyUpProvider, ComponentContextProvider, Field, Slot, fetchPageConfigServer, isComposerPreview, isPreviewRequest, useAmplifyUp };
2086
2169
  //# sourceMappingURL=react.mjs.map
2087
2170
  //# sourceMappingURL=react.mjs.map