@amplifyup/sdk 0.1.0 → 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/index.mjs CHANGED
@@ -167,6 +167,16 @@ var AWSAdapter = class {
167
167
  // src/storage.ts
168
168
  var INSTANCE_ID_KEY = "amplifyup_instance_id";
169
169
  var USER_ID_KEY = "amplifyup_user_id";
170
+ function generateGuid() {
171
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
172
+ return crypto.randomUUID();
173
+ }
174
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
175
+ const r = Math.random() * 16 | 0;
176
+ const v = c === "x" ? r : r & 3 | 8;
177
+ return v.toString(16);
178
+ });
179
+ }
170
180
  function getInstanceId() {
171
181
  if (typeof window === "undefined") {
172
182
  return "";
@@ -175,7 +185,7 @@ function getInstanceId() {
175
185
  if (stored) {
176
186
  return stored;
177
187
  }
178
- const instanceId = `anon_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
188
+ const instanceId = `browser_${generateGuid()}`;
179
189
  localStorage.setItem(INSTANCE_ID_KEY, instanceId);
180
190
  return instanceId;
181
191
  }
@@ -199,9 +209,69 @@ function clearIds() {
199
209
  localStorage.removeItem(USER_ID_KEY);
200
210
  }
201
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
+
202
274
  // src/index.ts
203
- var PROD_API_URL = "https://api.amplifyup.ai" ;
204
- var PROD_ORCHESTRATOR_URL = "https://amplifyup.ai" ;
205
275
  var AmplifyUpSDK = class {
206
276
  constructor(adapter) {
207
277
  this.config = null;
@@ -209,7 +279,12 @@ var AmplifyUpSDK = class {
209
279
  this.apiGatewayUrl = "";
210
280
  this.orchestratorUrl = "";
211
281
  this.decisionApiUrl = "";
282
+ this.target = "local";
212
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;
213
288
  this.highlightingEnabled = false;
214
289
  this.componentRegistry = [];
215
290
  this.messageListener = null;
@@ -292,46 +367,41 @@ var AmplifyUpSDK = class {
292
367
  }
293
368
  return false;
294
369
  }
295
- /**
296
- * Get production URLs
297
- *
298
- * URLs are baked in at build time via tsup.config.ts
299
- *
300
- * API Gateway endpoints:
301
- * - /v1/track - Event tracking (ingestion)
302
- * - /v1/page - Page view tracking (ingestion)
303
- * - /v1/personalize - Decision engine (personalization)
304
- */
305
- getProductionUrls() {
306
- return {
307
- apiGatewayUrl: PROD_API_URL,
308
- orchestratorUrl: PROD_ORCHESTRATOR_URL,
309
- decisionApiUrl: `${PROD_API_URL}/v1/personalize`
310
- };
311
- }
312
370
  /**
313
371
  * Initialize the SDK
314
- * Automatically reads env vars and sets up tracking
372
+ * Resolves local | dev | production targets for ingestion + personalization URLs.
315
373
  */
316
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) {
317
389
  if (!this.adapter) {
318
390
  this.adapter = new AWSAdapter();
319
391
  }
320
- const isLocalhost = typeof window !== "undefined" && (window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1");
321
- if (isLocalhost || config.dev === true) {
322
- this.apiGatewayUrl = "http://localhost:5001/api";
323
- this.orchestratorUrl = "http://localhost:5001";
324
- this.decisionApiUrl = "http://localhost:5001/api/v1/personalize";
325
- } else {
326
- const urls = this.getProductionUrls();
327
- this.apiGatewayUrl = urls.apiGatewayUrl;
328
- this.orchestratorUrl = urls.orchestratorUrl;
329
- this.decisionApiUrl = urls.decisionApiUrl;
330
- }
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;
331
400
  this.config = {
332
401
  trackingId: config.trackingId,
333
402
  debug: config.debug ?? true,
334
403
  // Enable debug by default
404
+ target: this.target,
335
405
  dev: config.dev
336
406
  };
337
407
  try {
@@ -348,6 +418,7 @@ var AmplifyUpSDK = class {
348
418
  getInstanceId: () => this.getInstanceId(),
349
419
  getUserId: () => this.getUserId(),
350
420
  getTrackingId: () => this.getTrackingId(),
421
+ getTarget: () => this.getTarget(),
351
422
  getDiagnostics: () => this.getDiagnostics(),
352
423
  getStatus: () => {
353
424
  console.log(this.getStatus());
@@ -368,7 +439,7 @@ var AmplifyUpSDK = class {
368
439
  };
369
440
  if (this.config?.debug) {
370
441
  console.log("AmplifyUp SDK: Diagnostic methods available on window.AmplifyUp");
371
- console.log("Try: AmplifyUp.getStatus() or AmplifyUp.verifyAWSConnection()");
442
+ console.log("Try: AmplifyUp.getStatus() or AmplifyUp.testTracking()");
372
443
  }
373
444
  }
374
445
  this.clearTrackingIntervals();
@@ -380,11 +451,20 @@ var AmplifyUpSDK = class {
380
451
  if (this.isPreviewMode()) {
381
452
  return;
382
453
  }
383
- const url = window.location.pathname;
384
- this.page(url);
454
+ if (!this.initialPageSent) {
455
+ this.initialPageSent = true;
456
+ const url = window.location.pathname;
457
+ this.page(url);
458
+ }
385
459
  }
386
460
  if (this.config?.debug && this.initialized) {
387
- 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
+ });
388
468
  }
389
469
  } catch (error) {
390
470
  console.error("AmplifyUp SDK: Initialization failed", error);
@@ -585,6 +665,7 @@ var AmplifyUpSDK = class {
585
665
  instanceId,
586
666
  userId,
587
667
  trackingId: this.config?.trackingId,
668
+ target: this.target,
588
669
  config: {
589
670
  apiGatewayUrl: this.apiGatewayUrl,
590
671
  decisionApiUrl: this.decisionApiUrl,
@@ -593,6 +674,9 @@ var AmplifyUpSDK = class {
593
674
  }
594
675
  };
595
676
  }
677
+ getTarget() {
678
+ return this.target;
679
+ }
596
680
  /**
597
681
  * Test tracking by sending a test event
598
682
  */
@@ -650,6 +734,7 @@ var AmplifyUpSDK = class {
650
734
  AmplifyUp SDK Status:
651
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
652
736
  Initialized: ${diag.initialized ? "\u2705 Yes" : "\u274C No"}
737
+ Target: ${diag.target}
653
738
  Instance ID: ${diag.instanceId}
654
739
  User ID: ${diag.userId || "Not identified"}
655
740
  Tracking ID: ${diag.trackingId || "Not set"}
@@ -661,11 +746,11 @@ Config:
661
746
  API Gateway: ${this.apiGatewayUrl || "Not set (required)"}
662
747
  Decision API: ${this.decisionApiUrl || "Not set (using Orchestrator fallback)"}
663
748
  Orchestrator: ${this.orchestratorUrl || "Not set"}
664
- Debug: ${diag.config.debug ? "Enabled" : "Disabled"}
665
749
 
666
750
  Available Console Commands:
667
751
  AmplifyUp.getStatus() - Show this status
668
752
  AmplifyUp.getDiagnostics() - Get full diagnostic object
753
+ AmplifyUp.getTarget() - Current environment target
669
754
  AmplifyUp.getInstanceId() - Get instance ID (browser/session ID)
670
755
  AmplifyUp.getUserId() - Get user ID if identified
671
756
  AmplifyUp.testTracking() - Send a test event
@@ -1388,6 +1473,7 @@ var reset = () => getSDK().reset();
1388
1473
  var getInstanceId2 = () => getSDK().getInstanceId();
1389
1474
  var getUserId2 = () => getSDK().getUserId();
1390
1475
  var getTrackingId = () => getSDK().getTrackingId();
1476
+ var getTarget = () => getSDK().getTarget();
1391
1477
  var getOrchestratorUrl = () => getSDK().getOrchestratorUrl();
1392
1478
  var buildLayoutProviderUrl = (route, preview) => getSDK().buildLayoutProviderUrl(route, preview);
1393
1479
  var buildPageRenderUrl = (route, preview) => getSDK().buildLayoutProviderUrl(route, preview);
@@ -1410,6 +1496,6 @@ var verifyAWSConnection = () => {
1410
1496
  };
1411
1497
  var index_default = getSDK();
1412
1498
 
1413
- 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 };
1414
1500
  //# sourceMappingURL=index.mjs.map
1415
1501
  //# sourceMappingURL=index.mjs.map