@amplifyup/sdk 0.1.1 → 0.1.3

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
@@ -209,9 +209,69 @@ function clearIds() {
209
209
  localStorage.removeItem(USER_ID_KEY);
210
210
  }
211
211
 
212
+ // src/targets.ts
213
+ var AWS_DEV_API = "https://api-dev.amplifyup.ai";
214
+ var AWS_PROD_API = "https://api.amplifyup.ai";
215
+ var PROD_ORCHESTRATOR = "https://amplifyup.ai";
216
+ function isAmplifyUpTarget(value) {
217
+ return value === "local" || value === "dev" || value === "production";
218
+ }
219
+ function normalizeAmplifyUpTarget(value) {
220
+ return value === "dev" ? "local" : value;
221
+ }
222
+ function resolveAmplifyUpTarget(options) {
223
+ if (options?.configTarget && isAmplifyUpTarget(options.configTarget)) {
224
+ return normalizeAmplifyUpTarget(options.configTarget);
225
+ }
226
+ if (options?.configDev === true) {
227
+ return "local";
228
+ }
229
+ const envTarget = typeof process !== "undefined" ? process.env.NEXT_PUBLIC_AMPLIFYUP_TARGET : void 0;
230
+ if (isAmplifyUpTarget(envTarget)) {
231
+ return normalizeAmplifyUpTarget(envTarget);
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
+ return "production";
240
+ }
241
+ function localOrchestratorUrl() {
242
+ const fromEnv = typeof process !== "undefined" ? process.env.NEXT_PUBLIC_ORCHESTRATOR_URL : void 0;
243
+ return (fromEnv || "http://localhost:5001").replace(/\/$/, "");
244
+ }
245
+ function localAwsApiUrl() {
246
+ const fromEnv = typeof process !== "undefined" ? process.env.NEXT_PUBLIC_AMPLIFYUP_API_URL : void 0;
247
+ return (fromEnv || AWS_DEV_API).replace(/\/$/, "");
248
+ }
249
+ function resolveAmplifyUpUrls(target) {
250
+ if (target === "local") {
251
+ const orchestratorUrl2 = localOrchestratorUrl();
252
+ const apiGatewayUrl = localAwsApiUrl();
253
+ return {
254
+ target: "local",
255
+ // Layouts / site config / composer APIs → local orchestrator + Neon
256
+ orchestratorUrl: orchestratorUrl2,
257
+ // Events + personalize → AWS development stack
258
+ apiGatewayUrl,
259
+ decisionApiUrl: `${apiGatewayUrl}/v1/personalize`
260
+ };
261
+ }
262
+ const resolvedApi = typeof process !== "undefined" && process.env.NEXT_PUBLIC_AMPLIFYUP_API_URL?.replace(/\/$/, "") || AWS_PROD_API;
263
+ const orchestratorFromEnv = typeof process !== "undefined" ? process.env.NEXT_PUBLIC_ORCHESTRATOR_URL : void 0;
264
+ const isLocalOrchestrator = !!orchestratorFromEnv && (orchestratorFromEnv.includes("localhost") || orchestratorFromEnv.includes("127.0.0.1"));
265
+ const orchestratorUrl = isLocalOrchestrator || !orchestratorFromEnv ? PROD_ORCHESTRATOR : orchestratorFromEnv.replace(/\/$/, "");
266
+ return {
267
+ target: "production",
268
+ apiGatewayUrl: resolvedApi,
269
+ orchestratorUrl,
270
+ decisionApiUrl: `${resolvedApi}/v1/personalize`
271
+ };
272
+ }
273
+
212
274
  // src/index.ts
213
- var PROD_API_URL = "https://api.amplifyup.ai" ;
214
- var PROD_ORCHESTRATOR_URL = "https://amplifyup.ai" ;
215
275
  var AmplifyUpSDK = class {
216
276
  constructor(adapter) {
217
277
  this.config = null;
@@ -219,7 +279,12 @@ var AmplifyUpSDK = class {
219
279
  this.apiGatewayUrl = "";
220
280
  this.orchestratorUrl = "";
221
281
  this.decisionApiUrl = "";
282
+ this.target = "local";
222
283
  this.initialized = false;
284
+ /** Prevents overlapping init() from React Strict Mode double-mount */
285
+ this.initPromise = null;
286
+ /** Ensures auto page() on init fires once per page load */
287
+ this.initialPageSent = false;
223
288
  this.highlightingEnabled = false;
224
289
  this.componentRegistry = [];
225
290
  this.messageListener = null;
@@ -302,46 +367,41 @@ var AmplifyUpSDK = class {
302
367
  }
303
368
  return false;
304
369
  }
305
- /**
306
- * Get production URLs
307
- *
308
- * URLs are baked in at build time via tsup.config.ts
309
- *
310
- * API Gateway endpoints:
311
- * - /v1/track - Event tracking (ingestion)
312
- * - /v1/page - Page view tracking (ingestion)
313
- * - /v1/personalize - Decision engine (personalization)
314
- */
315
- getProductionUrls() {
316
- return {
317
- apiGatewayUrl: PROD_API_URL,
318
- orchestratorUrl: PROD_ORCHESTRATOR_URL,
319
- decisionApiUrl: `${PROD_API_URL}/v1/personalize`
320
- };
321
- }
322
370
  /**
323
371
  * Initialize the SDK
324
- * Automatically reads env vars and sets up tracking
372
+ * Resolves local | dev | production targets for ingestion + personalization URLs.
325
373
  */
326
374
  async init(config) {
375
+ if (this.initialized && this.adapter?.isInitialized()) {
376
+ return;
377
+ }
378
+ if (this.initPromise) {
379
+ return this.initPromise;
380
+ }
381
+ this.initPromise = this.performInit(config);
382
+ try {
383
+ await this.initPromise;
384
+ } finally {
385
+ this.initPromise = null;
386
+ }
387
+ }
388
+ async performInit(config) {
327
389
  if (!this.adapter) {
328
390
  this.adapter = new AWSAdapter();
329
391
  }
330
- const isLocalhost = typeof window !== "undefined" && (window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1");
331
- if (isLocalhost || config.dev === true) {
332
- this.apiGatewayUrl = "http://localhost:5001/api";
333
- this.orchestratorUrl = "http://localhost:5001";
334
- this.decisionApiUrl = "http://localhost:5001/api/v1/personalize";
335
- } else {
336
- const urls = this.getProductionUrls();
337
- this.apiGatewayUrl = urls.apiGatewayUrl;
338
- this.orchestratorUrl = urls.orchestratorUrl;
339
- this.decisionApiUrl = urls.decisionApiUrl;
340
- }
392
+ this.target = resolveAmplifyUpTarget({
393
+ configTarget: config.target,
394
+ configDev: config.dev
395
+ });
396
+ const urls = resolveAmplifyUpUrls(this.target);
397
+ this.apiGatewayUrl = urls.apiGatewayUrl;
398
+ this.orchestratorUrl = urls.orchestratorUrl;
399
+ this.decisionApiUrl = urls.decisionApiUrl;
341
400
  this.config = {
342
401
  trackingId: config.trackingId,
343
402
  debug: config.debug ?? true,
344
403
  // Enable debug by default
404
+ target: this.target,
345
405
  dev: config.dev
346
406
  };
347
407
  try {
@@ -358,6 +418,7 @@ var AmplifyUpSDK = class {
358
418
  getInstanceId: () => this.getInstanceId(),
359
419
  getUserId: () => this.getUserId(),
360
420
  getTrackingId: () => this.getTrackingId(),
421
+ getTarget: () => this.getTarget(),
361
422
  getDiagnostics: () => this.getDiagnostics(),
362
423
  getStatus: () => {
363
424
  console.log(this.getStatus());
@@ -378,7 +439,7 @@ var AmplifyUpSDK = class {
378
439
  };
379
440
  if (this.config?.debug) {
380
441
  console.log("AmplifyUp SDK: Diagnostic methods available on window.AmplifyUp");
381
- console.log("Try: AmplifyUp.getStatus() or AmplifyUp.verifyAWSConnection()");
442
+ console.log("Try: AmplifyUp.getStatus() or AmplifyUp.testTracking()");
382
443
  }
383
444
  }
384
445
  this.clearTrackingIntervals();
@@ -390,11 +451,20 @@ var AmplifyUpSDK = class {
390
451
  if (this.isPreviewMode()) {
391
452
  return;
392
453
  }
393
- const url = window.location.pathname;
394
- this.page(url);
454
+ if (!this.initialPageSent) {
455
+ this.initialPageSent = true;
456
+ const url = window.location.pathname;
457
+ this.page(url);
458
+ }
395
459
  }
396
460
  if (this.config?.debug && this.initialized) {
397
- console.log("AmplifyUp SDK: Initialized successfully", { trackingId: config.trackingId });
461
+ console.log("AmplifyUp SDK: Initialized successfully", {
462
+ trackingId: config.trackingId,
463
+ target: this.target,
464
+ apiGatewayUrl: this.apiGatewayUrl,
465
+ orchestratorUrl: this.orchestratorUrl,
466
+ decisionApiUrl: this.decisionApiUrl || "(orchestrator fallback)"
467
+ });
398
468
  }
399
469
  } catch (error) {
400
470
  console.error("AmplifyUp SDK: Initialization failed", error);
@@ -595,6 +665,7 @@ var AmplifyUpSDK = class {
595
665
  instanceId,
596
666
  userId,
597
667
  trackingId: this.config?.trackingId,
668
+ target: this.target,
598
669
  config: {
599
670
  apiGatewayUrl: this.apiGatewayUrl,
600
671
  decisionApiUrl: this.decisionApiUrl,
@@ -603,6 +674,9 @@ var AmplifyUpSDK = class {
603
674
  }
604
675
  };
605
676
  }
677
+ getTarget() {
678
+ return this.target;
679
+ }
606
680
  /**
607
681
  * Test tracking by sending a test event
608
682
  */
@@ -660,6 +734,7 @@ var AmplifyUpSDK = class {
660
734
  AmplifyUp SDK Status:
661
735
  \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
662
736
  Initialized: ${diag.initialized ? "\u2705 Yes" : "\u274C No"}
737
+ Target: ${diag.target}
663
738
  Instance ID: ${diag.instanceId}
664
739
  User ID: ${diag.userId || "Not identified"}
665
740
  Tracking ID: ${diag.trackingId || "Not set"}
@@ -671,11 +746,11 @@ Config:
671
746
  API Gateway: ${this.apiGatewayUrl || "Not set (required)"}
672
747
  Decision API: ${this.decisionApiUrl || "Not set (using Orchestrator fallback)"}
673
748
  Orchestrator: ${this.orchestratorUrl || "Not set"}
674
- Debug: ${diag.config.debug ? "Enabled" : "Disabled"}
675
749
 
676
750
  Available Console Commands:
677
751
  AmplifyUp.getStatus() - Show this status
678
752
  AmplifyUp.getDiagnostics() - Get full diagnostic object
753
+ AmplifyUp.getTarget() - Current environment target
679
754
  AmplifyUp.getInstanceId() - Get instance ID (browser/session ID)
680
755
  AmplifyUp.getUserId() - Get user ID if identified
681
756
  AmplifyUp.testTracking() - Send a test event
@@ -1398,6 +1473,7 @@ var reset = () => getSDK().reset();
1398
1473
  var getInstanceId2 = () => getSDK().getInstanceId();
1399
1474
  var getUserId2 = () => getSDK().getUserId();
1400
1475
  var getTrackingId = () => getSDK().getTrackingId();
1476
+ var getTarget = () => getSDK().getTarget();
1401
1477
  var getOrchestratorUrl = () => getSDK().getOrchestratorUrl();
1402
1478
  var buildLayoutProviderUrl = (route, preview) => getSDK().buildLayoutProviderUrl(route, preview);
1403
1479
  var buildPageRenderUrl = (route, preview) => getSDK().buildLayoutProviderUrl(route, preview);
@@ -1420,6 +1496,6 @@ var verifyAWSConnection = () => {
1420
1496
  };
1421
1497
  var index_default = getSDK();
1422
1498
 
1423
- export { AmplifyUpSDK, buildLayoutProviderUrl, buildPageRenderUrl, cleanupEventTracking, index_default as default, fetchPageConfig, getDiagnostics, getInstanceId2 as getInstanceId, getOrchestratorUrl, getSDK, getStatus, getTrackingId, getUserId2 as getUserId, identify, init, page, refreshEventTracking, reset, setupEventTracking, testTracking, track, trackClick, trackContentEngagement, trackContentView, trackFormFieldInteraction, trackFormSubmit, verifyAWSConnection };
1499
+ 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 };
1424
1500
  //# sourceMappingURL=index.mjs.map
1425
1501
  //# sourceMappingURL=index.mjs.map