@amplifyup/sdk 0.1.75 → 0.1.77

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.
@@ -13,7 +13,121 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
13
13
  throw Error('Dynamic require of "' + x + '" is not supported');
14
14
  });
15
15
 
16
+ // src/storage.ts
17
+ var INSTANCE_ID_KEY = "amplifyup_instance_id";
18
+ var USER_ID_KEY = "amplifyup_user_id";
19
+ var SESSION_ID_KEY = "amplifyup_session_id";
20
+ var SESSION_ACTIVITY_KEY = "amplifyup_session_activity";
21
+ var VISITOR_FIRST_SEEN_KEY = "amplifyup_visitor_first_seen";
22
+ var ANON_COOKIE_NAME = "amplifyup_anon_id";
23
+ var SESSION_COOKIE_NAME = "amplifyup_session_id";
24
+ var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
25
+ function generateGuid() {
26
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
27
+ return crypto.randomUUID();
28
+ }
29
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
30
+ const r = Math.random() * 16 | 0;
31
+ const v = c === "x" ? r : r & 3 | 8;
32
+ return v.toString(16);
33
+ });
34
+ }
35
+ function readCookie(name) {
36
+ if (typeof document === "undefined") return null;
37
+ const parts = document.cookie.split(";");
38
+ for (const part of parts) {
39
+ const idx = part.indexOf("=");
40
+ if (idx === -1) continue;
41
+ if (part.slice(0, idx).trim() !== name) continue;
42
+ const value = part.slice(idx + 1).trim();
43
+ try {
44
+ return value ? decodeURIComponent(value) : null;
45
+ } catch {
46
+ return value || null;
47
+ }
48
+ }
49
+ return null;
50
+ }
51
+ function writeCookie(name, value, maxAgeSeconds = 31536e3) {
52
+ if (typeof document === "undefined") return;
53
+ document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=${maxAgeSeconds}; SameSite=Lax`;
54
+ }
55
+ function getInstanceId() {
56
+ if (typeof window === "undefined") {
57
+ return "";
58
+ }
59
+ const fromCookie = readCookie(ANON_COOKIE_NAME);
60
+ const fromLs = localStorage.getItem(INSTANCE_ID_KEY);
61
+ const instanceId = fromLs || fromCookie || `browser_${generateGuid()}`;
62
+ if (fromLs !== instanceId) {
63
+ localStorage.setItem(INSTANCE_ID_KEY, instanceId);
64
+ }
65
+ if (fromCookie !== instanceId) {
66
+ writeCookie(ANON_COOKIE_NAME, instanceId);
67
+ }
68
+ return instanceId;
69
+ }
70
+ function getUserId() {
71
+ if (typeof window === "undefined") {
72
+ return null;
73
+ }
74
+ return localStorage.getItem(USER_ID_KEY);
75
+ }
76
+ function getSessionId() {
77
+ if (typeof window === "undefined") {
78
+ return "";
79
+ }
80
+ const now = Date.now();
81
+ const lastActivity = Number(localStorage.getItem(SESSION_ACTIVITY_KEY) || 0);
82
+ const fromCookie = readCookie(SESSION_COOKIE_NAME);
83
+ const fromLs = localStorage.getItem(SESSION_ID_KEY);
84
+ let sessionId = fromLs || fromCookie || "";
85
+ if (!sessionId || lastActivity > 0 && now - lastActivity >= SESSION_IDLE_GAP_MS) {
86
+ sessionId = generateGuid();
87
+ }
88
+ localStorage.setItem(SESSION_ID_KEY, sessionId);
89
+ localStorage.setItem(SESSION_ACTIVITY_KEY, String(now));
90
+ writeCookie(SESSION_COOKIE_NAME, sessionId, Math.floor(SESSION_IDLE_GAP_MS / 1e3));
91
+ return sessionId;
92
+ }
93
+ function getVisitorFirstSeenAt() {
94
+ if (typeof window === "undefined") {
95
+ return null;
96
+ }
97
+ try {
98
+ const existing = localStorage.getItem(VISITOR_FIRST_SEEN_KEY);
99
+ if (existing) return existing;
100
+ const firstSeenAt = (/* @__PURE__ */ new Date()).toISOString();
101
+ localStorage.setItem(VISITOR_FIRST_SEEN_KEY, firstSeenAt);
102
+ return firstSeenAt;
103
+ } catch {
104
+ return null;
105
+ }
106
+ }
107
+ function setUserId(userId) {
108
+ if (typeof window === "undefined") {
109
+ return;
110
+ }
111
+ localStorage.setItem(USER_ID_KEY, userId);
112
+ }
113
+ function clearIds() {
114
+ if (typeof window === "undefined") {
115
+ return;
116
+ }
117
+ localStorage.removeItem(INSTANCE_ID_KEY);
118
+ localStorage.removeItem(USER_ID_KEY);
119
+ localStorage.removeItem(SESSION_ID_KEY);
120
+ localStorage.removeItem(SESSION_ACTIVITY_KEY);
121
+ localStorage.removeItem(VISITOR_FIRST_SEEN_KEY);
122
+ document.cookie = `${ANON_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
123
+ document.cookie = `${SESSION_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
124
+ }
125
+
16
126
  // src/adapters/aws.ts
127
+ function visitorContext() {
128
+ const firstSeenAt = getVisitorFirstSeenAt();
129
+ return firstSeenAt ? { firstSeenAt } : void 0;
130
+ }
17
131
  var AWSAdapter = class {
18
132
  constructor() {
19
133
  this.initialized = false;
@@ -53,7 +167,9 @@ var AWSAdapter = class {
53
167
  console.warn("AmplifyUp SDK: AWS configuration missing");
54
168
  return;
55
169
  }
56
- const url = `${this.apiGatewayUrl}${endpoint}`;
170
+ const trackingId = eventData?.trackingId || this.config?.trackingId;
171
+ const query = trackingId ? `?tid=${encodeURIComponent(trackingId)}` : "";
172
+ const url = `${this.apiGatewayUrl}${endpoint}${query}`;
57
173
  try {
58
174
  const response = await fetch(url, {
59
175
  method: "POST",
@@ -115,7 +231,8 @@ var AWSAdapter = class {
115
231
  page: {
116
232
  path: properties.path || properties.url,
117
233
  referrer: properties.referrer
118
- }
234
+ },
235
+ visitor: visitorContext()
119
236
  },
120
237
  timestamp: properties.timestamp || (/* @__PURE__ */ new Date()).toISOString()
121
238
  };
@@ -153,7 +270,8 @@ var AWSAdapter = class {
153
270
  ...properties
154
271
  },
155
272
  context: {
156
- url: properties.url || (typeof window !== "undefined" ? window.location.href : "")
273
+ url: properties.url || (typeof window !== "undefined" ? window.location.href : ""),
274
+ visitor: visitorContext()
157
275
  },
158
276
  timestamp: properties.timestamp || (/* @__PURE__ */ new Date()).toISOString()
159
277
  };
@@ -209,100 +327,6 @@ var AWSAdapter = class {
209
327
  }
210
328
  };
211
329
 
212
- // src/storage.ts
213
- var INSTANCE_ID_KEY = "amplifyup_instance_id";
214
- var USER_ID_KEY = "amplifyup_user_id";
215
- var SESSION_ID_KEY = "amplifyup_session_id";
216
- var SESSION_ACTIVITY_KEY = "amplifyup_session_activity";
217
- var ANON_COOKIE_NAME = "amplifyup_anon_id";
218
- var SESSION_COOKIE_NAME = "amplifyup_session_id";
219
- var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
220
- function generateGuid() {
221
- if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
222
- return crypto.randomUUID();
223
- }
224
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
225
- const r = Math.random() * 16 | 0;
226
- const v = c === "x" ? r : r & 3 | 8;
227
- return v.toString(16);
228
- });
229
- }
230
- function readCookie(name) {
231
- if (typeof document === "undefined") return null;
232
- const parts = document.cookie.split(";");
233
- for (const part of parts) {
234
- const idx = part.indexOf("=");
235
- if (idx === -1) continue;
236
- if (part.slice(0, idx).trim() !== name) continue;
237
- const value = part.slice(idx + 1).trim();
238
- try {
239
- return value ? decodeURIComponent(value) : null;
240
- } catch {
241
- return value || null;
242
- }
243
- }
244
- return null;
245
- }
246
- function writeCookie(name, value, maxAgeSeconds = 31536e3) {
247
- if (typeof document === "undefined") return;
248
- document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=${maxAgeSeconds}; SameSite=Lax`;
249
- }
250
- function getInstanceId() {
251
- if (typeof window === "undefined") {
252
- return "";
253
- }
254
- const fromCookie = readCookie(ANON_COOKIE_NAME);
255
- const fromLs = localStorage.getItem(INSTANCE_ID_KEY);
256
- const instanceId = fromLs || fromCookie || `browser_${generateGuid()}`;
257
- if (fromLs !== instanceId) {
258
- localStorage.setItem(INSTANCE_ID_KEY, instanceId);
259
- }
260
- if (fromCookie !== instanceId) {
261
- writeCookie(ANON_COOKIE_NAME, instanceId);
262
- }
263
- return instanceId;
264
- }
265
- function getUserId() {
266
- if (typeof window === "undefined") {
267
- return null;
268
- }
269
- return localStorage.getItem(USER_ID_KEY);
270
- }
271
- function getSessionId() {
272
- if (typeof window === "undefined") {
273
- return "";
274
- }
275
- const now = Date.now();
276
- const lastActivity = Number(localStorage.getItem(SESSION_ACTIVITY_KEY) || 0);
277
- const fromCookie = readCookie(SESSION_COOKIE_NAME);
278
- const fromLs = localStorage.getItem(SESSION_ID_KEY);
279
- let sessionId = fromLs || fromCookie || "";
280
- if (!sessionId || lastActivity > 0 && now - lastActivity >= SESSION_IDLE_GAP_MS) {
281
- sessionId = generateGuid();
282
- }
283
- localStorage.setItem(SESSION_ID_KEY, sessionId);
284
- localStorage.setItem(SESSION_ACTIVITY_KEY, String(now));
285
- writeCookie(SESSION_COOKIE_NAME, sessionId, Math.floor(SESSION_IDLE_GAP_MS / 1e3));
286
- return sessionId;
287
- }
288
- function setUserId(userId) {
289
- if (typeof window === "undefined") {
290
- return;
291
- }
292
- localStorage.setItem(USER_ID_KEY, userId);
293
- }
294
- function clearIds() {
295
- if (typeof window === "undefined") {
296
- return;
297
- }
298
- localStorage.removeItem(INSTANCE_ID_KEY);
299
- localStorage.removeItem(USER_ID_KEY);
300
- localStorage.removeItem(SESSION_ID_KEY);
301
- localStorage.removeItem(SESSION_ACTIVITY_KEY);
302
- document.cookie = `${ANON_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
303
- document.cookie = `${SESSION_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
304
- }
305
-
306
330
  // src/targets.ts
307
331
  var AWS_DEV_API = "https://api-dev.amplifyup.ai";
308
332
  var AWS_PROD_API = "https://api.amplifyup.ai";
@@ -3602,14 +3626,16 @@ function applyComposerPatch(store, name, value, ref, layoutNodeId) {
3602
3626
  store.set(composerStoreKey(name, ref, layoutNodeId), value);
3603
3627
  }
3604
3628
  function ComposerProvider({
3605
- children
3629
+ children,
3630
+ enabled = true
3606
3631
  }) {
3607
3632
  const existing = react.useContext(ComposerStoreContext);
3608
3633
  const storeRef = react.useRef(null);
3609
3634
  if (!storeRef.current) storeRef.current = new ComposerFieldStore();
3610
- const store = existing || storeRef.current;
3635
+ const ownsStore = !existing && enabled;
3636
+ const store = existing || (enabled ? storeRef.current : null);
3611
3637
  react.useEffect(() => {
3612
- if (existing) return;
3638
+ if (!ownsStore || !store) return;
3613
3639
  const applyField = (fieldName, value, nodeId, ref) => {
3614
3640
  if (!fieldName || typeof fieldName !== "string") return;
3615
3641
  const fieldRef = isFieldRef(ref) ? ref : void 0;
@@ -3667,8 +3693,7 @@ function ComposerProvider({
3667
3693
  window.removeEventListener("amplifyup:props-patch", onPropsPatch);
3668
3694
  window.removeEventListener("message", onMessage);
3669
3695
  };
3670
- }, [existing, store]);
3671
- if (existing) return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children });
3696
+ }, [ownsStore, store]);
3672
3697
  return /* @__PURE__ */ jsxRuntime.jsx(ComposerStoreContext.Provider, { value: store, children });
3673
3698
  }
3674
3699
  function MaybeComposerProvider({
@@ -3676,9 +3701,7 @@ function MaybeComposerProvider({
3676
3701
  layoutNodeId,
3677
3702
  children
3678
3703
  }) {
3679
- const content = react.useMemo(() => children, [children]);
3680
- if (!enabled) return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: content });
3681
- return /* @__PURE__ */ jsxRuntime.jsx(ComposerProvider, { children: /* @__PURE__ */ jsxRuntime.jsx(ComposerNodeContext.Provider, { value: layoutNodeId, children }) });
3704
+ return /* @__PURE__ */ jsxRuntime.jsx(ComposerProvider, { enabled, children: /* @__PURE__ */ jsxRuntime.jsx(ComposerNodeContext.Provider, { value: enabled ? layoutNodeId : void 0, children }) });
3682
3705
  }
3683
3706
  react.createContext(null);
3684
3707
  var usePathname = null;
@@ -4130,17 +4153,16 @@ function AmplifyRendererInner({
4130
4153
  if (layoutTree.length === 0) {
4131
4154
  return /* @__PURE__ */ jsxRuntime.jsx("main", { children: emptyComponent || /* @__PURE__ */ jsxRuntime.jsx("div", { className: "container mx-auto px-4 py-16", children: /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-muted-foreground", children: "No content configured for this page." }) }) });
4132
4155
  }
4133
- return /* @__PURE__ */ jsxRuntime.jsx(ComposerProvider, { children: /* @__PURE__ */ jsxRuntime.jsx("main", { children: composerPreviewFlag ? /* @__PURE__ */ jsxRuntime.jsx(
4134
- "div",
4156
+ return /* @__PURE__ */ jsxRuntime.jsx(ComposerProvider, { children: /* @__PURE__ */ jsxRuntime.jsx(
4157
+ "main",
4135
4158
  {
4136
- "data-slot-id": "main",
4137
- "data-amplifyup-slot": "main",
4138
- "data-slot-source": "page",
4139
- "data-slot-state": layoutTree.length === 0 ? "empty" : "filled",
4140
- "data-slot-empty": layoutTree.length === 0 ? "true" : void 0,
4159
+ "data-slot-id": composerPreviewFlag ? "main" : void 0,
4160
+ "data-amplifyup-slot": composerPreviewFlag ? "main" : void 0,
4161
+ "data-slot-source": composerPreviewFlag ? "page" : void 0,
4162
+ "data-slot-state": composerPreviewFlag ? "filled" : void 0,
4141
4163
  children: layoutTree.map((node) => /* @__PURE__ */ jsxRuntime.jsx(react.Fragment, { children: renderNode(node) }, node.id))
4142
4164
  }
4143
- ) : layoutTree.map((node) => /* @__PURE__ */ jsxRuntime.jsx(react.Fragment, { children: renderNode(node) }, node.id)) }) });
4165
+ ) });
4144
4166
  }
4145
4167
  var AmplifyRenderer = react.memo(AmplifyRendererInner);
4146
4168