@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.mjs CHANGED
@@ -1128,6 +1128,100 @@ async function fetchContent(options) {
1128
1128
  }
1129
1129
  }
1130
1130
 
1131
+ // src/consent.ts
1132
+ var STORAGE_KEY = "amplifyup_consent";
1133
+ var current = null;
1134
+ var loaded = false;
1135
+ function readStored() {
1136
+ try {
1137
+ const value = typeof window !== "undefined" ? window.localStorage.getItem(STORAGE_KEY) : null;
1138
+ return value === "granted" || value === "denied" ? value : null;
1139
+ } catch {
1140
+ return null;
1141
+ }
1142
+ }
1143
+ function writeStored(state) {
1144
+ try {
1145
+ if (typeof window === "undefined") return;
1146
+ if (state) window.localStorage.setItem(STORAGE_KEY, state);
1147
+ else window.localStorage.removeItem(STORAGE_KEY);
1148
+ } catch {
1149
+ }
1150
+ }
1151
+ function setConsent(state) {
1152
+ current = state;
1153
+ loaded = true;
1154
+ writeStored(state);
1155
+ }
1156
+ function getConsent() {
1157
+ if (!loaded) {
1158
+ current = readStored();
1159
+ loaded = true;
1160
+ }
1161
+ return current;
1162
+ }
1163
+
1164
+ // src/delivery/forms.ts
1165
+ var DeliveryRequestError = class extends Error {
1166
+ constructor(message, status) {
1167
+ super(message);
1168
+ this.name = "DeliveryRequestError";
1169
+ this.status = status;
1170
+ }
1171
+ };
1172
+ var formUrl = (formId, suffix = "") => {
1173
+ const sdk = getSDK();
1174
+ const edgeUrl = sdk.getEdgeUrl();
1175
+ const trackingId = sdk.getTrackingId();
1176
+ if (!edgeUrl || !trackingId) {
1177
+ throw new DeliveryRequestError("Amplify UP is not set up yet: call init() with your tracking ID first.", 0);
1178
+ }
1179
+ const base = edgeUrl.replace(/\/+$/, "");
1180
+ const query = new URLSearchParams({ trackingId });
1181
+ return `${base}/v1/forms/${encodeURIComponent(formId)}${suffix}?${query.toString()}`;
1182
+ };
1183
+ var errorMessage = async (response, fallback) => {
1184
+ const body = await response.json().catch(() => ({}));
1185
+ return body.error || `${fallback} (${response.status})`;
1186
+ };
1187
+ var getFormDefinition = async (formId, options = {}) => {
1188
+ const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
1189
+ const response = await fetchFn(formUrl(formId), {
1190
+ method: "GET",
1191
+ headers: { Accept: "application/json" }
1192
+ });
1193
+ if (!response.ok) {
1194
+ throw new DeliveryRequestError(await errorMessage(response, "Form failed to load"), response.status);
1195
+ }
1196
+ return await response.json();
1197
+ };
1198
+ var submitForm = async (formId, values, options = {}) => {
1199
+ if (isComposerPreview()) {
1200
+ throw new DeliveryRequestError("Forms do not submit in preview.", 0);
1201
+ }
1202
+ const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
1203
+ const consent = getConsent();
1204
+ const page2 = typeof window !== "undefined" ? window.location.href : void 0;
1205
+ const response = await fetchFn(formUrl(formId, "/submit"), {
1206
+ method: "POST",
1207
+ headers: {
1208
+ Accept: "application/json",
1209
+ "Content-Type": "application/json",
1210
+ ...consent ? { "x-amplifyup-consent": consent } : {}
1211
+ },
1212
+ body: JSON.stringify({ values, page: page2 })
1213
+ });
1214
+ if (response.status === 422) {
1215
+ const body2 = await response.json();
1216
+ return { ok: false, errors: body2.errors };
1217
+ }
1218
+ if (!response.ok) {
1219
+ throw new DeliveryRequestError(await errorMessage(response, "Form failed to submit"), response.status);
1220
+ }
1221
+ const body = await response.json();
1222
+ return { ok: true, onSuccess: body.onSuccess };
1223
+ };
1224
+
1131
1225
  // src/edge/select.ts
1132
1226
  var SelectVariantsError = class extends Error {
1133
1227
  constructor(message, status, body) {
@@ -3570,16 +3664,16 @@ Available Console Commands:
3570
3664
  }
3571
3665
  buildComponentHierarchy(element) {
3572
3666
  const hierarchy = [];
3573
- let current = element;
3667
+ let current2 = element;
3574
3668
  let sawPageSlot = false;
3575
- while (current) {
3576
- 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")) {
3577
- hierarchy.push(current);
3578
- if (current.getAttribute("data-slot-source") === "page") {
3669
+ while (current2) {
3670
+ 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")) {
3671
+ hierarchy.push(current2);
3672
+ if (current2.getAttribute("data-slot-source") === "page") {
3579
3673
  sawPageSlot = true;
3580
3674
  }
3581
3675
  }
3582
- current = current.parentElement;
3676
+ current2 = current2.parentElement;
3583
3677
  }
3584
3678
  const ordered = hierarchy.reverse();
3585
3679
  if (!sawPageSlot) {
@@ -3802,6 +3896,6 @@ var verifyAWSConnection = () => {
3802
3896
  };
3803
3897
  var src_default = getSDK();
3804
3898
 
3805
- export { AMPLIFYUP_EDITOR_ROUTE, ANON_COOKIE_NAME, AmplifyUpSDK, DWELL_ACTIVITY_IDLE_MS, EdgeResolveError, QueryContentError, SESSION_COOKIE_NAME, SESSION_IDLE_GAP_MS, SelectVariantsError, buildLayoutProviderUrl, buildPageRenderUrl, cleanupEventTracking, src_default as default, fetchContent, fetchEdgeQuery, fetchEdgeResolve, fetchEdgeSelect, fetchPageConfig, getDiagnostics, getInstanceId2 as getInstanceId, getOrchestratorUrl, getSDK, getSessionId2 as getSessionId, getStatus, getTarget, getTrackingId, getUserId2 as getUserId, identify, init, isAmplifyUpEditorRoute, isAmplifyUpTarget, isComposerPreview, isPreviewRequest, isWorkingContentRequest, mapResolvedToPageConfig, nextPageSpec, normalizeAmplifyUpTarget, normalizeRoute, page, queryContent, refreshEventTracking, reset, resolveAmplifyUpTarget, resolveAmplifyUpUrls, routeChange, searchSpec, selectVariants, setProfileFields, setupComponentViewTracking, setupEventTracking, testTracking, track, trackClick, trackContentEngagement, trackContentView, trackFormFieldInteraction, trackFormSubmit, verifyAWSConnection };
3899
+ export { AMPLIFYUP_EDITOR_ROUTE, ANON_COOKIE_NAME, AmplifyUpSDK, DWELL_ACTIVITY_IDLE_MS, DeliveryRequestError, EdgeResolveError, QueryContentError, SESSION_COOKIE_NAME, SESSION_IDLE_GAP_MS, SelectVariantsError, buildLayoutProviderUrl, buildPageRenderUrl, cleanupEventTracking, src_default as default, fetchContent, fetchEdgeQuery, fetchEdgeResolve, fetchEdgeSelect, fetchPageConfig, getConsent, getDiagnostics, getFormDefinition, getInstanceId2 as getInstanceId, getOrchestratorUrl, getSDK, getSessionId2 as getSessionId, getStatus, getTarget, getTrackingId, getUserId2 as getUserId, identify, init, isAmplifyUpEditorRoute, isAmplifyUpTarget, isComposerPreview, isPreviewRequest, isWorkingContentRequest, mapResolvedToPageConfig, nextPageSpec, normalizeAmplifyUpTarget, normalizeRoute, page, queryContent, refreshEventTracking, reset, resolveAmplifyUpTarget, resolveAmplifyUpUrls, routeChange, searchSpec, selectVariants, setConsent, setProfileFields, setupComponentViewTracking, setupEventTracking, submitForm, testTracking, track, trackClick, trackContentEngagement, trackContentView, trackFormFieldInteraction, trackFormSubmit, verifyAWSConnection };
3806
3900
  //# sourceMappingURL=index.mjs.map
3807
3901
  //# sourceMappingURL=index.mjs.map