@amplifyup/sdk 0.1.1 → 0.1.2

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.js CHANGED
@@ -1,3 +1,4 @@
1
+ 'use client';
1
2
  'use strict';
2
3
 
3
4
  var react = require('react');
@@ -220,9 +221,69 @@ function clearIds() {
220
221
  localStorage.removeItem(USER_ID_KEY);
221
222
  }
222
223
 
224
+ // src/targets.ts
225
+ var AWS_DEV_API = "https://api-dev.amplifyup.ai";
226
+ var AWS_PROD_API = "https://api.amplifyup.ai";
227
+ var PROD_ORCHESTRATOR = "https://amplifyup.ai";
228
+ function isAmplifyUpTarget(value) {
229
+ return value === "local" || value === "dev" || value === "production";
230
+ }
231
+ function normalizeAmplifyUpTarget(value) {
232
+ return value === "dev" ? "local" : value;
233
+ }
234
+ function resolveAmplifyUpTarget(options) {
235
+ if (options?.configTarget && isAmplifyUpTarget(options.configTarget)) {
236
+ return normalizeAmplifyUpTarget(options.configTarget);
237
+ }
238
+ if (options?.configDev === true) {
239
+ return "local";
240
+ }
241
+ const envTarget = typeof process !== "undefined" ? process.env.NEXT_PUBLIC_AMPLIFYUP_TARGET : void 0;
242
+ if (isAmplifyUpTarget(envTarget)) {
243
+ return normalizeAmplifyUpTarget(envTarget);
244
+ }
245
+ if (typeof window !== "undefined") {
246
+ const host = window.location.hostname;
247
+ if (host === "localhost" || host === "127.0.0.1") {
248
+ return "local";
249
+ }
250
+ }
251
+ return "production";
252
+ }
253
+ function localOrchestratorUrl() {
254
+ const fromEnv = typeof process !== "undefined" ? process.env.NEXT_PUBLIC_ORCHESTRATOR_URL : void 0;
255
+ return (fromEnv || "http://localhost:5001").replace(/\/$/, "");
256
+ }
257
+ function localAwsApiUrl() {
258
+ const fromEnv = typeof process !== "undefined" ? process.env.NEXT_PUBLIC_AMPLIFYUP_API_URL : void 0;
259
+ return (fromEnv || AWS_DEV_API).replace(/\/$/, "");
260
+ }
261
+ function resolveAmplifyUpUrls(target) {
262
+ if (target === "local") {
263
+ const orchestratorUrl2 = localOrchestratorUrl();
264
+ const apiGatewayUrl = localAwsApiUrl();
265
+ return {
266
+ target: "local",
267
+ // Layouts / site config / composer APIs → local orchestrator + Neon
268
+ orchestratorUrl: orchestratorUrl2,
269
+ // Events + personalize → AWS development stack
270
+ apiGatewayUrl,
271
+ decisionApiUrl: `${apiGatewayUrl}/v1/personalize`
272
+ };
273
+ }
274
+ const resolvedApi = typeof process !== "undefined" && process.env.NEXT_PUBLIC_AMPLIFYUP_API_URL?.replace(/\/$/, "") || AWS_PROD_API;
275
+ const orchestratorFromEnv = typeof process !== "undefined" ? process.env.NEXT_PUBLIC_ORCHESTRATOR_URL : void 0;
276
+ const isLocalOrchestrator = !!orchestratorFromEnv && (orchestratorFromEnv.includes("localhost") || orchestratorFromEnv.includes("127.0.0.1"));
277
+ const orchestratorUrl = isLocalOrchestrator || !orchestratorFromEnv ? PROD_ORCHESTRATOR : orchestratorFromEnv.replace(/\/$/, "");
278
+ return {
279
+ target: "production",
280
+ apiGatewayUrl: resolvedApi,
281
+ orchestratorUrl,
282
+ decisionApiUrl: `${resolvedApi}/v1/personalize`
283
+ };
284
+ }
285
+
223
286
  // src/index.ts
224
- var PROD_API_URL = "https://api.amplifyup.ai" ;
225
- var PROD_ORCHESTRATOR_URL = "https://amplifyup.ai" ;
226
287
  var AmplifyUpSDK = class {
227
288
  constructor(adapter) {
228
289
  this.config = null;
@@ -230,7 +291,12 @@ var AmplifyUpSDK = class {
230
291
  this.apiGatewayUrl = "";
231
292
  this.orchestratorUrl = "";
232
293
  this.decisionApiUrl = "";
294
+ this.target = "local";
233
295
  this.initialized = false;
296
+ /** Prevents overlapping init() from React Strict Mode double-mount */
297
+ this.initPromise = null;
298
+ /** Ensures auto page() on init fires once per page load */
299
+ this.initialPageSent = false;
234
300
  this.highlightingEnabled = false;
235
301
  this.componentRegistry = [];
236
302
  this.messageListener = null;
@@ -313,46 +379,41 @@ var AmplifyUpSDK = class {
313
379
  }
314
380
  return false;
315
381
  }
316
- /**
317
- * Get production URLs
318
- *
319
- * URLs are baked in at build time via tsup.config.ts
320
- *
321
- * API Gateway endpoints:
322
- * - /v1/track - Event tracking (ingestion)
323
- * - /v1/page - Page view tracking (ingestion)
324
- * - /v1/personalize - Decision engine (personalization)
325
- */
326
- getProductionUrls() {
327
- return {
328
- apiGatewayUrl: PROD_API_URL,
329
- orchestratorUrl: PROD_ORCHESTRATOR_URL,
330
- decisionApiUrl: `${PROD_API_URL}/v1/personalize`
331
- };
332
- }
333
382
  /**
334
383
  * Initialize the SDK
335
- * Automatically reads env vars and sets up tracking
384
+ * Resolves local | dev | production targets for ingestion + personalization URLs.
336
385
  */
337
386
  async init(config) {
387
+ if (this.initialized && this.adapter?.isInitialized()) {
388
+ return;
389
+ }
390
+ if (this.initPromise) {
391
+ return this.initPromise;
392
+ }
393
+ this.initPromise = this.performInit(config);
394
+ try {
395
+ await this.initPromise;
396
+ } finally {
397
+ this.initPromise = null;
398
+ }
399
+ }
400
+ async performInit(config) {
338
401
  if (!this.adapter) {
339
402
  this.adapter = new AWSAdapter();
340
403
  }
341
- const isLocalhost = typeof window !== "undefined" && (window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1");
342
- if (isLocalhost || config.dev === true) {
343
- this.apiGatewayUrl = "http://localhost:5001/api";
344
- this.orchestratorUrl = "http://localhost:5001";
345
- this.decisionApiUrl = "http://localhost:5001/api/v1/personalize";
346
- } else {
347
- const urls = this.getProductionUrls();
348
- this.apiGatewayUrl = urls.apiGatewayUrl;
349
- this.orchestratorUrl = urls.orchestratorUrl;
350
- this.decisionApiUrl = urls.decisionApiUrl;
351
- }
404
+ this.target = resolveAmplifyUpTarget({
405
+ configTarget: config.target,
406
+ configDev: config.dev
407
+ });
408
+ const urls = resolveAmplifyUpUrls(this.target);
409
+ this.apiGatewayUrl = urls.apiGatewayUrl;
410
+ this.orchestratorUrl = urls.orchestratorUrl;
411
+ this.decisionApiUrl = urls.decisionApiUrl;
352
412
  this.config = {
353
413
  trackingId: config.trackingId,
354
414
  debug: config.debug ?? true,
355
415
  // Enable debug by default
416
+ target: this.target,
356
417
  dev: config.dev
357
418
  };
358
419
  try {
@@ -369,6 +430,7 @@ var AmplifyUpSDK = class {
369
430
  getInstanceId: () => this.getInstanceId(),
370
431
  getUserId: () => this.getUserId(),
371
432
  getTrackingId: () => this.getTrackingId(),
433
+ getTarget: () => this.getTarget(),
372
434
  getDiagnostics: () => this.getDiagnostics(),
373
435
  getStatus: () => {
374
436
  console.log(this.getStatus());
@@ -389,7 +451,7 @@ var AmplifyUpSDK = class {
389
451
  };
390
452
  if (this.config?.debug) {
391
453
  console.log("AmplifyUp SDK: Diagnostic methods available on window.AmplifyUp");
392
- console.log("Try: AmplifyUp.getStatus() or AmplifyUp.verifyAWSConnection()");
454
+ console.log("Try: AmplifyUp.getStatus() or AmplifyUp.testTracking()");
393
455
  }
394
456
  }
395
457
  this.clearTrackingIntervals();
@@ -401,11 +463,20 @@ var AmplifyUpSDK = class {
401
463
  if (this.isPreviewMode()) {
402
464
  return;
403
465
  }
404
- const url = window.location.pathname;
405
- this.page(url);
466
+ if (!this.initialPageSent) {
467
+ this.initialPageSent = true;
468
+ const url = window.location.pathname;
469
+ this.page(url);
470
+ }
406
471
  }
407
472
  if (this.config?.debug && this.initialized) {
408
- console.log("AmplifyUp SDK: Initialized successfully", { trackingId: config.trackingId });
473
+ console.log("AmplifyUp SDK: Initialized successfully", {
474
+ trackingId: config.trackingId,
475
+ target: this.target,
476
+ apiGatewayUrl: this.apiGatewayUrl,
477
+ orchestratorUrl: this.orchestratorUrl,
478
+ decisionApiUrl: this.decisionApiUrl || "(orchestrator fallback)"
479
+ });
409
480
  }
410
481
  } catch (error) {
411
482
  console.error("AmplifyUp SDK: Initialization failed", error);
@@ -606,6 +677,7 @@ var AmplifyUpSDK = class {
606
677
  instanceId,
607
678
  userId,
608
679
  trackingId: this.config?.trackingId,
680
+ target: this.target,
609
681
  config: {
610
682
  apiGatewayUrl: this.apiGatewayUrl,
611
683
  decisionApiUrl: this.decisionApiUrl,
@@ -614,6 +686,9 @@ var AmplifyUpSDK = class {
614
686
  }
615
687
  };
616
688
  }
689
+ getTarget() {
690
+ return this.target;
691
+ }
617
692
  /**
618
693
  * Test tracking by sending a test event
619
694
  */
@@ -671,6 +746,7 @@ var AmplifyUpSDK = class {
671
746
  AmplifyUp SDK Status:
672
747
  \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
673
748
  Initialized: ${diag.initialized ? "\u2705 Yes" : "\u274C No"}
749
+ Target: ${diag.target}
674
750
  Instance ID: ${diag.instanceId}
675
751
  User ID: ${diag.userId || "Not identified"}
676
752
  Tracking ID: ${diag.trackingId || "Not set"}
@@ -682,11 +758,11 @@ Config:
682
758
  API Gateway: ${this.apiGatewayUrl || "Not set (required)"}
683
759
  Decision API: ${this.decisionApiUrl || "Not set (using Orchestrator fallback)"}
684
760
  Orchestrator: ${this.orchestratorUrl || "Not set"}
685
- Debug: ${diag.config.debug ? "Enabled" : "Disabled"}
686
761
 
687
762
  Available Console Commands:
688
763
  AmplifyUp.getStatus() - Show this status
689
764
  AmplifyUp.getDiagnostics() - Get full diagnostic object
765
+ AmplifyUp.getTarget() - Current environment target
690
766
  AmplifyUp.getInstanceId() - Get instance ID (browser/session ID)
691
767
  AmplifyUp.getUserId() - Get user ID if identified
692
768
  AmplifyUp.testTracking() - Send a test event
@@ -1695,19 +1771,31 @@ function AmplifyRenderer({
1695
1771
  }
1696
1772
 
1697
1773
  // src/server.tsx
1698
- async function fetchPageConfigServer(route, trackingId, preview = false) {
1699
- const sdk = getSDK();
1700
- const orchestratorUrl = typeof process !== "undefined" && process.env.NEXT_PUBLIC_ORCHESTRATOR_URL || "http://localhost:5001";
1701
- const originalConfig = sdk.config;
1702
- sdk.config = {
1703
- trackingId,
1704
- orchestratorUrl
1705
- };
1774
+ async function fetchPageConfigServer(route, trackingId, preview = false, options) {
1775
+ const target = resolveAmplifyUpTarget({
1776
+ configTarget: options?.target
1777
+ });
1778
+ const { orchestratorUrl } = resolveAmplifyUpUrls(target);
1779
+ const params = new URLSearchParams({
1780
+ route,
1781
+ trackingId
1782
+ });
1783
+ if (preview) {
1784
+ params.set("preview", "true");
1785
+ }
1786
+ const url = `${orchestratorUrl}/api/layout/provider?${params.toString()}`;
1706
1787
  try {
1707
- const page = await sdk.fetchPageConfig(route, preview);
1708
- return page;
1709
- } finally {
1710
- sdk.config = originalConfig;
1788
+ const res = await fetch(url, {
1789
+ cache: "no-store",
1790
+ headers: { Accept: "application/json" }
1791
+ });
1792
+ if (!res.ok) {
1793
+ return null;
1794
+ }
1795
+ const data = await res.json();
1796
+ return data.page || null;
1797
+ } catch {
1798
+ return null;
1711
1799
  }
1712
1800
  }
1713
1801
  var ComponentContext = react.createContext(null);
@@ -1895,14 +1983,21 @@ function AmplifyUpProvider({ children, config }) {
1895
1983
  }
1896
1984
  }, [nextPathname]);
1897
1985
  react.useEffect(() => {
1986
+ let cancelled = false;
1898
1987
  const sdk = getSDK();
1899
1988
  sdk.init(config).then(() => {
1900
- setInitialized(true);
1989
+ if (!cancelled) {
1990
+ setInitialized(true);
1991
+ }
1901
1992
  }).catch((err) => {
1993
+ if (cancelled) return;
1902
1994
  console.error("Failed to initialize AmplifyUp SDK:", err);
1903
1995
  setError("Failed to initialize tracking");
1904
1996
  setLoading(false);
1905
1997
  });
1998
+ return () => {
1999
+ cancelled = true;
2000
+ };
1906
2001
  }, []);
1907
2002
  react.useEffect(() => {
1908
2003
  if (!initialized) return;