@amplifyup/sdk 0.2.5 → 0.3.1-uat.104.gfd92fe0

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.js CHANGED
@@ -1132,6 +1132,100 @@ async function fetchContent(options) {
1132
1132
  }
1133
1133
  }
1134
1134
 
1135
+ // src/consent.ts
1136
+ var STORAGE_KEY = "amplifyup_consent";
1137
+ var current = null;
1138
+ var loaded = false;
1139
+ function readStored() {
1140
+ try {
1141
+ const value = typeof window !== "undefined" ? window.localStorage.getItem(STORAGE_KEY) : null;
1142
+ return value === "granted" || value === "denied" ? value : null;
1143
+ } catch {
1144
+ return null;
1145
+ }
1146
+ }
1147
+ function writeStored(state) {
1148
+ try {
1149
+ if (typeof window === "undefined") return;
1150
+ if (state) window.localStorage.setItem(STORAGE_KEY, state);
1151
+ else window.localStorage.removeItem(STORAGE_KEY);
1152
+ } catch {
1153
+ }
1154
+ }
1155
+ function setConsent(state) {
1156
+ current = state;
1157
+ loaded = true;
1158
+ writeStored(state);
1159
+ }
1160
+ function getConsent() {
1161
+ if (!loaded) {
1162
+ current = readStored();
1163
+ loaded = true;
1164
+ }
1165
+ return current;
1166
+ }
1167
+
1168
+ // src/delivery/forms.ts
1169
+ var DeliveryRequestError = class extends Error {
1170
+ constructor(message, status) {
1171
+ super(message);
1172
+ this.name = "DeliveryRequestError";
1173
+ this.status = status;
1174
+ }
1175
+ };
1176
+ var formUrl = (formId, suffix = "") => {
1177
+ const sdk = getSDK();
1178
+ const edgeUrl = sdk.getEdgeUrl();
1179
+ const trackingId = sdk.getTrackingId();
1180
+ if (!edgeUrl || !trackingId) {
1181
+ throw new DeliveryRequestError("Amplify UP is not set up yet: call init() with your tracking ID first.", 0);
1182
+ }
1183
+ const base = edgeUrl.replace(/\/+$/, "");
1184
+ const query = new URLSearchParams({ trackingId });
1185
+ return `${base}/v1/forms/${encodeURIComponent(formId)}${suffix}?${query.toString()}`;
1186
+ };
1187
+ var errorMessage = async (response, fallback) => {
1188
+ const body = await response.json().catch(() => ({}));
1189
+ return body.error || `${fallback} (${response.status})`;
1190
+ };
1191
+ var getFormDefinition = async (formId, options = {}) => {
1192
+ const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
1193
+ const response = await fetchFn(formUrl(formId), {
1194
+ method: "GET",
1195
+ headers: { Accept: "application/json" }
1196
+ });
1197
+ if (!response.ok) {
1198
+ throw new DeliveryRequestError(await errorMessage(response, "Form failed to load"), response.status);
1199
+ }
1200
+ return await response.json();
1201
+ };
1202
+ var submitForm = async (formId, values, options = {}) => {
1203
+ if (isComposerPreview()) {
1204
+ throw new DeliveryRequestError("Forms do not submit in preview.", 0);
1205
+ }
1206
+ const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
1207
+ const consent = getConsent();
1208
+ const page2 = typeof window !== "undefined" ? window.location.href : void 0;
1209
+ const response = await fetchFn(formUrl(formId, "/submit"), {
1210
+ method: "POST",
1211
+ headers: {
1212
+ Accept: "application/json",
1213
+ "Content-Type": "application/json",
1214
+ ...consent ? { "x-amplifyup-consent": consent } : {}
1215
+ },
1216
+ body: JSON.stringify({ values, page: page2 })
1217
+ });
1218
+ if (response.status === 422) {
1219
+ const body2 = await response.json();
1220
+ return { ok: false, errors: body2.errors };
1221
+ }
1222
+ if (!response.ok) {
1223
+ throw new DeliveryRequestError(await errorMessage(response, "Form failed to submit"), response.status);
1224
+ }
1225
+ const body = await response.json();
1226
+ return { ok: true, onSuccess: body.onSuccess };
1227
+ };
1228
+
1135
1229
  // src/edge/select.ts
1136
1230
  var SelectVariantsError = class extends Error {
1137
1231
  constructor(message, status, body) {
@@ -3574,16 +3668,16 @@ Available Console Commands:
3574
3668
  }
3575
3669
  buildComponentHierarchy(element) {
3576
3670
  const hierarchy = [];
3577
- let current = element;
3671
+ let current2 = element;
3578
3672
  let sawPageSlot = false;
3579
- while (current) {
3580
- if (current.hasAttribute("data-amplifyup-component") || current.hasAttribute("data-component-id") || current.hasAttribute("data-amplifyup-slot") || current.hasAttribute("data-slot-id") || current.hasAttribute("data-node-id") || current.hasAttribute("data-amplifyup-node")) {
3581
- hierarchy.push(current);
3582
- if (current.getAttribute("data-slot-source") === "page") {
3673
+ while (current2) {
3674
+ if (current2.hasAttribute("data-amplifyup-component") || current2.hasAttribute("data-component-id") || current2.hasAttribute("data-amplifyup-slot") || current2.hasAttribute("data-slot-id") || current2.hasAttribute("data-node-id") || current2.hasAttribute("data-amplifyup-node")) {
3675
+ hierarchy.push(current2);
3676
+ if (current2.getAttribute("data-slot-source") === "page") {
3583
3677
  sawPageSlot = true;
3584
3678
  }
3585
3679
  }
3586
- current = current.parentElement;
3680
+ current2 = current2.parentElement;
3587
3681
  }
3588
3682
  const ordered = hierarchy.reverse();
3589
3683
  if (!sawPageSlot) {
@@ -3810,6 +3904,7 @@ exports.AMPLIFYUP_EDITOR_ROUTE = AMPLIFYUP_EDITOR_ROUTE;
3810
3904
  exports.ANON_COOKIE_NAME = ANON_COOKIE_NAME;
3811
3905
  exports.AmplifyUpSDK = AmplifyUpSDK;
3812
3906
  exports.DWELL_ACTIVITY_IDLE_MS = DWELL_ACTIVITY_IDLE_MS;
3907
+ exports.DeliveryRequestError = DeliveryRequestError;
3813
3908
  exports.EdgeResolveError = EdgeResolveError;
3814
3909
  exports.QueryContentError = QueryContentError;
3815
3910
  exports.SESSION_COOKIE_NAME = SESSION_COOKIE_NAME;
@@ -3824,7 +3919,9 @@ exports.fetchEdgeQuery = fetchEdgeQuery;
3824
3919
  exports.fetchEdgeResolve = fetchEdgeResolve;
3825
3920
  exports.fetchEdgeSelect = fetchEdgeSelect;
3826
3921
  exports.fetchPageConfig = fetchPageConfig;
3922
+ exports.getConsent = getConsent;
3827
3923
  exports.getDiagnostics = getDiagnostics;
3924
+ exports.getFormDefinition = getFormDefinition;
3828
3925
  exports.getInstanceId = getInstanceId2;
3829
3926
  exports.getOrchestratorUrl = getOrchestratorUrl;
3830
3927
  exports.getSDK = getSDK;
@@ -3853,9 +3950,11 @@ exports.resolveAmplifyUpUrls = resolveAmplifyUpUrls;
3853
3950
  exports.routeChange = routeChange;
3854
3951
  exports.searchSpec = searchSpec;
3855
3952
  exports.selectVariants = selectVariants;
3953
+ exports.setConsent = setConsent;
3856
3954
  exports.setProfileFields = setProfileFields;
3857
3955
  exports.setupComponentViewTracking = setupComponentViewTracking;
3858
3956
  exports.setupEventTracking = setupEventTracking;
3957
+ exports.submitForm = submitForm;
3859
3958
  exports.testTracking = testTracking;
3860
3959
  exports.track = track;
3861
3960
  exports.trackClick = trackClick;