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