@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/index.mjs CHANGED
@@ -230,14 +230,16 @@ function resolveAmplifyUpTarget(options) {
230
230
  if (isAmplifyUpTarget(envTarget)) {
231
231
  return normalizeAmplifyUpTarget(envTarget);
232
232
  }
233
- if (typeof window !== "undefined") {
234
- const host = window.location.hostname;
235
- if (host === "localhost" || host === "127.0.0.1") {
236
- return "local";
237
- }
238
- }
239
233
  return "production";
240
234
  }
235
+ function isRemoteOrchestratorUrl(orchestratorUrl) {
236
+ try {
237
+ const host = new URL(orchestratorUrl).hostname.toLowerCase();
238
+ return host !== "localhost" && host !== "127.0.0.1" && host !== "::1";
239
+ } catch {
240
+ return false;
241
+ }
242
+ }
241
243
  function localOrchestratorUrl() {
242
244
  const fromEnv = typeof process !== "undefined" ? process.env.NEXT_PUBLIC_ORCHESTRATOR_URL : void 0;
243
245
  return (fromEnv || "http://localhost:5001").replace(/\/$/, "");
@@ -271,6 +273,32 @@ function resolveAmplifyUpUrls(target) {
271
273
  };
272
274
  }
273
275
 
276
+ // src/preview.ts
277
+ function isPreviewRequest(searchParams) {
278
+ if (searchParams instanceof URLSearchParams) {
279
+ return searchParams.get("preview") === "true" || searchParams.get("visualEditor") === "true";
280
+ }
281
+ const preview = searchParams.preview;
282
+ const visualEditor = searchParams.visualEditor;
283
+ return preview === "true" || visualEditor === "true";
284
+ }
285
+ function isComposerPreview() {
286
+ if (typeof window === "undefined") return false;
287
+ if (window.__AMPLIFYUP_PREVIEW_MODE__ === true) return true;
288
+ const params = new URLSearchParams(window.location.search);
289
+ if (isPreviewRequest(params)) return true;
290
+ if (window.self !== window.top && document.referrer) {
291
+ try {
292
+ const refUrl = new URL(document.referrer);
293
+ 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/")) {
294
+ return true;
295
+ }
296
+ } catch {
297
+ }
298
+ }
299
+ return false;
300
+ }
301
+
274
302
  // src/index.ts
275
303
  function readPreviewQueryOverrides() {
276
304
  if (typeof window === "undefined") return {};
@@ -393,6 +421,7 @@ var AmplifyUpSDK = class {
393
421
  */
394
422
  async init(config) {
395
423
  if (this.initialized && this.adapter?.isInitialized()) {
424
+ this.ensureWindowBridge();
396
425
  return;
397
426
  }
398
427
  if (this.initPromise) {
@@ -405,6 +434,32 @@ var AmplifyUpSDK = class {
405
434
  this.initPromise = null;
406
435
  }
407
436
  }
437
+ /** Attach console helpers to window (idempotent; survives Fast Refresh). */
438
+ ensureWindowBridge() {
439
+ if (typeof window === "undefined") return;
440
+ window.AmplifyUp = {
441
+ getInstanceId: () => this.getInstanceId(),
442
+ getUserId: () => this.getUserId(),
443
+ getTrackingId: () => this.getTrackingId(),
444
+ getTarget: () => this.getTarget(),
445
+ getDiagnostics: () => this.getDiagnostics(),
446
+ getStatus: () => {
447
+ console.log(this.getStatus());
448
+ return this.getDiagnostics();
449
+ },
450
+ testTracking: () => this.testTracking(),
451
+ verifyAWSConnection: () => {
452
+ const result = this.verifyAWSConnection();
453
+ console.log("AWS Connection Verification:", result.message);
454
+ console.log("Full details:", result);
455
+ return result;
456
+ },
457
+ isInitialized: () => this.isInitialized(),
458
+ page: (url, properties) => this.page(url, properties),
459
+ track: (eventName, properties) => this.track(eventName, properties),
460
+ identify: (userId, traits) => this.identify(userId, traits)
461
+ };
462
+ }
408
463
  async performInit(config) {
409
464
  if (!this.adapter) {
410
465
  this.adapter = new AWSAdapter();
@@ -420,6 +475,12 @@ var AmplifyUpSDK = class {
420
475
  const previewOverrides = readPreviewQueryOverrides();
421
476
  if (previewOverrides.orchestratorUrl) {
422
477
  this.orchestratorUrl = previewOverrides.orchestratorUrl;
478
+ if (isRemoteOrchestratorUrl(previewOverrides.orchestratorUrl)) {
479
+ const prodUrls = resolveAmplifyUpUrls("production");
480
+ this.target = "production";
481
+ this.apiGatewayUrl = prodUrls.apiGatewayUrl;
482
+ this.decisionApiUrl = prodUrls.decisionApiUrl;
483
+ }
423
484
  }
424
485
  this.config = {
425
486
  trackingId: previewOverrides.trackingId || config.trackingId,
@@ -437,34 +498,10 @@ var AmplifyUpSDK = class {
437
498
  };
438
499
  await this.adapter.init(adapterConfig);
439
500
  this.initialized = this.adapter.isInitialized();
440
- if (typeof window !== "undefined") {
441
- window.AmplifyUp = {
442
- getInstanceId: () => this.getInstanceId(),
443
- getUserId: () => this.getUserId(),
444
- getTrackingId: () => this.getTrackingId(),
445
- getTarget: () => this.getTarget(),
446
- getDiagnostics: () => this.getDiagnostics(),
447
- getStatus: () => {
448
- console.log(this.getStatus());
449
- return this.getDiagnostics();
450
- },
451
- testTracking: () => this.testTracking(),
452
- verifyAWSConnection: () => {
453
- const result = this.verifyAWSConnection();
454
- console.log("AWS Connection Verification:", result.message);
455
- console.log("Full details:", result);
456
- return result;
457
- },
458
- isInitialized: () => this.isInitialized(),
459
- // Re-export main tracking methods for convenience
460
- page: (url, properties) => this.page(url, properties),
461
- track: (eventName, properties) => this.track(eventName, properties),
462
- identify: (userId, traits) => this.identify(userId, traits)
463
- };
464
- if (this.config?.debug) {
465
- console.log("AmplifyUp SDK: Diagnostic methods available on window.AmplifyUp");
466
- console.log("Try: AmplifyUp.getStatus() or AmplifyUp.testTracking()");
467
- }
501
+ this.ensureWindowBridge();
502
+ if (this.config?.debug) {
503
+ console.log("AmplifyUp SDK: Diagnostic methods available on window.AmplifyUp");
504
+ console.log("Try: AmplifyUp.getStatus() or AmplifyUp.testTracking()");
468
505
  }
469
506
  this.clearTrackingIntervals();
470
507
  if (this.isPreviewMode()) {
@@ -1475,10 +1512,17 @@ Available Console Commands:
1475
1512
  }
1476
1513
  };
1477
1514
  var sdkInstance = null;
1515
+ if (typeof window !== "undefined" && window.__AMPLIFYUP_SDK__) {
1516
+ sdkInstance = window.__AMPLIFYUP_SDK__;
1517
+ }
1478
1518
  function getSDK(adapter) {
1479
1519
  if (!sdkInstance) {
1480
1520
  sdkInstance = new AmplifyUpSDK(adapter);
1521
+ if (typeof window !== "undefined") {
1522
+ window.__AMPLIFYUP_SDK__ = sdkInstance;
1523
+ }
1481
1524
  }
1525
+ sdkInstance.ensureWindowBridge();
1482
1526
  return sdkInstance;
1483
1527
  }
1484
1528
  async function init(config) {
@@ -1520,6 +1564,6 @@ var verifyAWSConnection = () => {
1520
1564
  };
1521
1565
  var index_default = getSDK();
1522
1566
 
1523
- export { AmplifyUpSDK, buildLayoutProviderUrl, buildPageRenderUrl, cleanupEventTracking, index_default as default, fetchPageConfig, getDiagnostics, getInstanceId2 as getInstanceId, getOrchestratorUrl, getSDK, getStatus, getTarget, getTrackingId, getUserId2 as getUserId, identify, init, isAmplifyUpTarget, normalizeAmplifyUpTarget, page, refreshEventTracking, reset, resolveAmplifyUpTarget, resolveAmplifyUpUrls, setupEventTracking, testTracking, track, trackClick, trackContentEngagement, trackContentView, trackFormFieldInteraction, trackFormSubmit, verifyAWSConnection };
1567
+ export { AmplifyUpSDK, buildLayoutProviderUrl, buildPageRenderUrl, cleanupEventTracking, index_default as default, fetchPageConfig, getDiagnostics, getInstanceId2 as getInstanceId, getOrchestratorUrl, getSDK, getStatus, getTarget, getTrackingId, getUserId2 as getUserId, identify, init, isAmplifyUpTarget, isComposerPreview, isPreviewRequest, normalizeAmplifyUpTarget, page, refreshEventTracking, reset, resolveAmplifyUpTarget, resolveAmplifyUpUrls, setupEventTracking, testTracking, track, trackClick, trackContentEngagement, trackContentView, trackFormFieldInteraction, trackFormSubmit, verifyAWSConnection };
1524
1568
  //# sourceMappingURL=index.mjs.map
1525
1569
  //# sourceMappingURL=index.mjs.map