@redacto.io/consent-sdk-react 3.2.0 → 3.3.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @redacto.io/consent-sdk-react
2
2
 
3
+ ## 3.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - update support for submit API
8
+
3
9
  ## 3.2.0
4
10
 
5
11
  ### Minor Changes
package/dist/index.js CHANGED
@@ -30,6 +30,21 @@ var import_react2 = require("react");
30
30
 
31
31
  // src/RedactoNoticeConsent/api/index.ts
32
32
  var import_jwt_decode = require("jwt-decode");
33
+
34
+ // src/utils/isLegacyApi.ts
35
+ function isLegacyApi(baseUrl) {
36
+ try {
37
+ return new URL(baseUrl).hostname.endsWith(".redacto.io");
38
+ } catch {
39
+ try {
40
+ return new URL(`https://${baseUrl}`).hostname.endsWith(".redacto.io");
41
+ } catch {
42
+ return false;
43
+ }
44
+ }
45
+ }
46
+
47
+ // src/RedactoNoticeConsent/api/index.ts
33
48
  var BASE_URL = "https://api.redacto.io/consent";
34
49
  var apiCache = /* @__PURE__ */ new Map();
35
50
  var CACHE_TTL = 5 * 60 * 1e3;
@@ -54,14 +69,6 @@ function decodeTokenSafely(token) {
54
69
  return null;
55
70
  }
56
71
  }
57
- var storage = {
58
- getItem: (key) => {
59
- if (typeof window !== "undefined") {
60
- return localStorage.getItem(key);
61
- }
62
- return null;
63
- }
64
- };
65
72
  var clearApiCache = () => {
66
73
  apiCache.clear();
67
74
  };
@@ -135,7 +142,6 @@ var fetchConsentContent = async ({
135
142
  }
136
143
  return data;
137
144
  } catch (error) {
138
- console.error("Error fetching consent content:", error instanceof Error ? error.message : error);
139
145
  throw error;
140
146
  }
141
147
  };
@@ -161,42 +167,62 @@ var submitConsentEvent = async ({
161
167
  if (!ORGANISATION_UUID || !WORKSPACE_UUID) {
162
168
  throw new Error("Invalid token: missing organization or workspace UUID");
163
169
  }
164
- const primaryEmail = storage.getItem("userEmail");
165
- const validatedPurposes = purposes.map((purpose) => {
166
- if (!purpose.uuid || !purpose.name) {
167
- throw new Error(`Invalid purpose data: missing uuid or name`);
168
- }
169
- return {
170
- uuid: purpose.uuid,
171
- name: purpose.name,
172
- description: purpose.description || "",
173
- industries: purpose.industries || "",
174
- selected: Boolean(purpose.selected),
175
- data_elements: (purpose.data_elements || []).map((element) => {
176
- if (!element.uuid || !element.name) {
177
- throw new Error(`Invalid data element: missing uuid or name`);
178
- }
179
- return {
180
- uuid: element.uuid,
181
- name: element.name,
182
- enabled: Boolean(element.enabled),
183
- required: Boolean(element.required),
184
- selected: element.required ? true : Boolean(element.selected)
185
- };
186
- })
187
- };
188
- });
189
- const payload = {
190
- primary_email: primaryEmail || void 0,
191
- notice_uuid: noticeUuid,
192
- source: "WEB",
193
- declined: Boolean(declined),
194
- consents: {
195
- purposes: validatedPurposes
196
- },
197
- meta_data: meta_data ? { specific_uuid: meta_data.specific_uuid } : void 0
198
- };
199
170
  const apiBaseUrl = baseUrl || BASE_URL;
171
+ let payload;
172
+ if (isLegacyApi(apiBaseUrl)) {
173
+ const primaryEmail = typeof window !== "undefined" ? localStorage.getItem("userEmail") : null;
174
+ payload = {
175
+ primary_email: primaryEmail || void 0,
176
+ notice_uuid: noticeUuid,
177
+ source: "WEB",
178
+ declined: Boolean(declined),
179
+ consents: {
180
+ purposes: purposes.map((purpose) => {
181
+ if (!purpose.uuid) {
182
+ throw new Error(`Invalid purpose data: missing uuid`);
183
+ }
184
+ return {
185
+ uuid: purpose.uuid,
186
+ name: purpose.name,
187
+ description: purpose.description || "",
188
+ industries: purpose.industries || "",
189
+ selected: Boolean(purpose.selected),
190
+ data_elements: (purpose.data_elements || []).map((element) => ({
191
+ uuid: element.uuid,
192
+ name: element.name,
193
+ enabled: Boolean(element.enabled),
194
+ required: Boolean(element.required),
195
+ selected: element.required ? true : Boolean(element.selected)
196
+ }))
197
+ };
198
+ })
199
+ },
200
+ meta_data: meta_data ? { specific_uuid: meta_data.specific_uuid } : void 0
201
+ };
202
+ } else {
203
+ const validatedPurposes = purposes.map((purpose) => {
204
+ if (!purpose.uuid) {
205
+ throw new Error(`Invalid purpose data: missing uuid`);
206
+ }
207
+ const dataElements = (purpose.data_elements || []).filter((element) => element.enabled).map((element) => ({
208
+ uuid: element.uuid,
209
+ selected: element.required ? true : Boolean(element.selected)
210
+ }));
211
+ return {
212
+ purpose_uuid: purpose.uuid,
213
+ selected: Boolean(purpose.selected),
214
+ data_elements: dataElements.length > 0 ? dataElements : void 0
215
+ };
216
+ });
217
+ payload = {
218
+ notice_uuid: noticeUuid,
219
+ purposes: validatedPurposes,
220
+ select_all_mandatory: false,
221
+ source: "WEB",
222
+ declined: Boolean(declined),
223
+ meta_data: meta_data ? { specific_uuid: meta_data.specific_uuid } : void 0
224
+ };
225
+ }
200
226
  const response = await fetch(
201
227
  `${apiBaseUrl}/public/organisations/${ORGANISATION_UUID}/workspaces/${WORKSPACE_UUID}/submit-consent`,
202
228
  {
@@ -229,7 +255,6 @@ var submitConsentEvent = async ({
229
255
  }
230
256
  apiCache.clear();
231
257
  } catch (error) {
232
- console.error("Error submitting consent event:", error instanceof Error ? error.message : error);
233
258
  throw error;
234
259
  }
235
260
  };
@@ -361,7 +386,6 @@ var fetchTTSAudioUrls = async ({
361
386
  const data = await response.json();
362
387
  return data;
363
388
  } catch (error) {
364
- console.error("Error fetching TTS audio URLs:", error instanceof Error ? error.message : error);
365
389
  throw error;
366
390
  }
367
391
  };
@@ -1852,14 +1876,15 @@ var RedactoNoticeConsent = ({
1852
1876
  validate_against: validateAgainst,
1853
1877
  signal: (_a2 = abortControllerRef.current) == null ? void 0 : _a2.signal
1854
1878
  });
1855
- setContent(consentContentData.detail.active_config);
1856
- if (consentContentData.detail.active_config.default_language) {
1857
- const defaultLang = consentContentData.detail.active_config.default_language;
1879
+ const activeConfig = consentContentData.detail.active_config;
1880
+ setContent(activeConfig);
1881
+ if (activeConfig.default_language) {
1882
+ const defaultLang = activeConfig.default_language;
1858
1883
  setSelectedLanguage(
1859
1884
  defaultLang === "en" || defaultLang === "EN" ? "English" : defaultLang
1860
1885
  );
1861
1886
  }
1862
- const initialCollapsedState = consentContentData.detail.active_config.purposes.reduce(
1887
+ const initialCollapsedState = activeConfig.purposes.reduce(
1863
1888
  (acc, purpose) => ({
1864
1889
  ...acc,
1865
1890
  [purpose.uuid]: true
@@ -1870,13 +1895,13 @@ var RedactoNoticeConsent = ({
1870
1895
  const isReconsentRequired = consentContentData.detail.reconsent_required;
1871
1896
  const purposeSelections = consentContentData.detail.purpose_selections;
1872
1897
  const newCategorizedPurposes = isReconsentRequired ? {
1873
- alreadyConsented: consentContentData.detail.active_config.purposes.filter(
1898
+ alreadyConsented: activeConfig.purposes.filter(
1874
1899
  (purpose) => {
1875
1900
  const selection = purposeSelections == null ? void 0 : purposeSelections[purpose.uuid];
1876
1901
  return selection && (selection.status === "ACTIVE" || selection.status === "EXPIRED") && !selection.needs_reconsent;
1877
1902
  }
1878
1903
  ),
1879
- needsConsent: consentContentData.detail.active_config.purposes.filter(
1904
+ needsConsent: activeConfig.purposes.filter(
1880
1905
  (purpose) => {
1881
1906
  const selection = purposeSelections == null ? void 0 : purposeSelections[purpose.uuid];
1882
1907
  return !selection || selection.status !== "ACTIVE" || selection.needs_reconsent;
@@ -1884,7 +1909,7 @@ var RedactoNoticeConsent = ({
1884
1909
  )
1885
1910
  } : null;
1886
1911
  setCategorizedPurposes(newCategorizedPurposes);
1887
- const initialPurposeState = consentContentData.detail.active_config.purposes.reduce(
1912
+ const initialPurposeState = activeConfig.purposes.reduce(
1888
1913
  (acc, purpose) => {
1889
1914
  if (isReconsentRequired && (purposeSelections == null ? void 0 : purposeSelections[purpose.uuid])) {
1890
1915
  acc[purpose.uuid] = purposeSelections[purpose.uuid].selected;
@@ -1896,7 +1921,7 @@ var RedactoNoticeConsent = ({
1896
1921
  {}
1897
1922
  );
1898
1923
  setSelectedPurposes(initialPurposeState);
1899
- const initialDataElementState = consentContentData.detail.active_config.purposes.reduce(
1924
+ const initialDataElementState = activeConfig.purposes.reduce(
1900
1925
  (acc, purpose) => {
1901
1926
  purpose.data_elements.forEach((element) => {
1902
1927
  var _a3, _b2;
@@ -1937,7 +1962,6 @@ var RedactoNoticeConsent = ({
1937
1962
  if (err instanceof Error && err.name === "AbortError") {
1938
1963
  return;
1939
1964
  }
1940
- console.error(err);
1941
1965
  const error = err;
1942
1966
  setFetchError(error);
1943
1967
  if (error.status === 409) {
@@ -2072,13 +2096,9 @@ var RedactoNoticeConsent = ({
2072
2096
  if (error instanceof Error && error.name === "AbortError") {
2073
2097
  return;
2074
2098
  }
2075
- console.error("Error submitting consent:", error);
2076
2099
  const err = error;
2077
- if (err.status === 500) {
2078
- setErrorMessage(
2079
- "An error occurred while submitting your consent. Please try again later."
2080
- );
2081
- }
2100
+ const message = err.status === 500 ? "An error occurred while submitting your consent. Please try again later." : err.message || "Failed to submit consent. Please try again.";
2101
+ setErrorMessage(message);
2082
2102
  onError == null ? void 0 : onError(error);
2083
2103
  } finally {
2084
2104
  setIsSubmittingAccept(false);
@@ -2460,7 +2480,6 @@ var RedactoNoticeConsent = ({
2460
2480
  setIsTTSAvailable(true);
2461
2481
  }).catch((error) => {
2462
2482
  if (error instanceof Error && error.name !== "AbortError") {
2463
- console.warn("Failed to fetch TTS audio URLs:", error.message);
2464
2483
  setIsTTSAvailable(false);
2465
2484
  setTtsAudioUrls(null);
2466
2485
  }
@@ -3226,7 +3245,6 @@ var fetchConsentContent2 = async ({
3226
3245
  const data = await response.json();
3227
3246
  return data;
3228
3247
  } catch (error) {
3229
- console.error("Error fetching consent content:", error);
3230
3248
  throw error;
3231
3249
  }
3232
3250
  };
@@ -3242,31 +3260,61 @@ var submitConsentEvent2 = async ({
3242
3260
  }) => {
3243
3261
  var _a, _b;
3244
3262
  try {
3245
- const primaryEmail = localStorage.getItem("userEmail");
3246
- const payload = {
3247
- primary_email: primaryEmail || void 0,
3248
- notice_uuid: noticeUuid,
3249
- source: "WEB",
3250
- declined,
3251
- consents: {
3252
- purposes: purposes.map((purpose) => ({
3253
- uuid: purpose.uuid,
3254
- name: purpose.name,
3255
- description: purpose.description,
3256
- industries: purpose.industries || "",
3257
- selected: purpose.selected,
3258
- data_elements: purpose.data_elements.map((element) => ({
3259
- uuid: element.uuid,
3260
- name: element.name,
3261
- enabled: element.enabled,
3262
- required: element.required,
3263
- selected: element.required ? true : element.selected
3264
- }))
3265
- }))
3266
- },
3267
- meta_data
3268
- };
3269
3263
  const apiBaseUrl = baseUrl || BASE_URL2;
3264
+ let payload;
3265
+ if (isLegacyApi(apiBaseUrl)) {
3266
+ const primaryEmail = typeof window !== "undefined" ? localStorage.getItem("userEmail") : null;
3267
+ payload = {
3268
+ primary_email: primaryEmail || void 0,
3269
+ notice_uuid: noticeUuid,
3270
+ source: "WEB",
3271
+ declined: Boolean(declined),
3272
+ consents: {
3273
+ purposes: purposes.map((purpose) => {
3274
+ if (!purpose.uuid) {
3275
+ throw new Error(`Invalid purpose data: missing uuid`);
3276
+ }
3277
+ return {
3278
+ uuid: purpose.uuid,
3279
+ name: purpose.name,
3280
+ description: purpose.description || "",
3281
+ industries: purpose.industries || "",
3282
+ selected: Boolean(purpose.selected),
3283
+ data_elements: (purpose.data_elements || []).map((element) => ({
3284
+ uuid: element.uuid,
3285
+ name: element.name,
3286
+ enabled: Boolean(element.enabled),
3287
+ required: Boolean(element.required),
3288
+ selected: element.required ? true : Boolean(element.selected)
3289
+ }))
3290
+ };
3291
+ })
3292
+ },
3293
+ meta_data: meta_data ? { specific_uuid: meta_data.specific_uuid } : void 0
3294
+ };
3295
+ } else {
3296
+ payload = {
3297
+ notice_uuid: noticeUuid,
3298
+ purposes: purposes.map((purpose) => {
3299
+ if (!purpose.uuid) {
3300
+ throw new Error(`Invalid purpose data: missing uuid`);
3301
+ }
3302
+ const dataElements = purpose.data_elements.filter((element) => element.enabled).map((element) => ({
3303
+ uuid: element.uuid,
3304
+ selected: element.required ? true : Boolean(element.selected)
3305
+ }));
3306
+ return {
3307
+ purpose_uuid: purpose.uuid,
3308
+ selected: Boolean(purpose.selected),
3309
+ data_elements: dataElements.length > 0 ? dataElements : void 0
3310
+ };
3311
+ }),
3312
+ select_all_mandatory: false,
3313
+ source: "WEB",
3314
+ declined,
3315
+ meta_data
3316
+ };
3317
+ }
3270
3318
  const response = await fetch(
3271
3319
  `${apiBaseUrl}/public/organisations/${org_uuid}/workspaces/${workspace_uuid}/submit-consent`,
3272
3320
  {
@@ -3303,7 +3351,6 @@ var submitConsentEvent2 = async ({
3303
3351
  throw error;
3304
3352
  }
3305
3353
  } catch (error) {
3306
- console.error("Error submitting consent event:", error);
3307
3354
  throw error;
3308
3355
  }
3309
3356
  };
@@ -3821,13 +3868,12 @@ var RedactoConsentInlineComponent = ({
3821
3868
  language,
3822
3869
  specific_uuid: applicationId
3823
3870
  });
3824
- setContent(consentContentData.detail.active_config);
3825
- if (consentContentData.detail.active_config.default_language) {
3826
- setSelectedLanguage(
3827
- consentContentData.detail.active_config.default_language
3828
- );
3871
+ const activeConfig = consentContentData.detail.active_config;
3872
+ setContent(activeConfig);
3873
+ if (activeConfig.default_language) {
3874
+ setSelectedLanguage(activeConfig.default_language);
3829
3875
  }
3830
- const initialCollapsedState = consentContentData.detail.active_config.purposes.reduce(
3876
+ const initialCollapsedState = activeConfig.purposes.reduce(
3831
3877
  (acc, purpose) => ({
3832
3878
  ...acc,
3833
3879
  [purpose.uuid]: true
@@ -3835,7 +3881,7 @@ var RedactoConsentInlineComponent = ({
3835
3881
  {}
3836
3882
  );
3837
3883
  setCollapsedPurposes(initialCollapsedState);
3838
- const initialDataElementState = consentContentData.detail.active_config.purposes.reduce(
3884
+ const initialDataElementState = activeConfig.purposes.reduce(
3839
3885
  (acc, purpose) => {
3840
3886
  purpose.data_elements.forEach((element) => {
3841
3887
  const combinedId = `${purpose.uuid}-${element.uuid}`;
@@ -3846,7 +3892,7 @@ var RedactoConsentInlineComponent = ({
3846
3892
  {}
3847
3893
  );
3848
3894
  setSelectedDataElements(initialDataElementState);
3849
- const initialPurposeState = consentContentData.detail.active_config.purposes.reduce(
3895
+ const initialPurposeState = activeConfig.purposes.reduce(
3850
3896
  (acc, purpose) => {
3851
3897
  return {
3852
3898
  ...acc,
@@ -3857,7 +3903,6 @@ var RedactoConsentInlineComponent = ({
3857
3903
  );
3858
3904
  setSelectedPurposes(initialPurposeState);
3859
3905
  } catch (err) {
3860
- console.error(err);
3861
3906
  const error = err;
3862
3907
  setFetchError(error);
3863
3908
  if (error.status === 409) {
@@ -3972,13 +4017,9 @@ var RedactoConsentInlineComponent = ({
3972
4017
  onAccept == null ? void 0 : onAccept();
3973
4018
  hasSubmittedRef.current = false;
3974
4019
  } catch (error) {
3975
- console.error("Error submitting consent:", error);
3976
4020
  const err = error;
3977
- if (err.status === 500) {
3978
- setErrorMessage(
3979
- "An error occurred while submitting your consent. Please try again later."
3980
- );
3981
- }
4021
+ const message = err.status === 500 ? "An error occurred while submitting your consent. Please try again later." : err.message || "Failed to submit consent. Please try again.";
4022
+ setErrorMessage(message);
3982
4023
  hasSubmittedRef.current = false;
3983
4024
  throw error;
3984
4025
  } finally {
@@ -4003,7 +4044,6 @@ var RedactoConsentInlineComponent = ({
4003
4044
  try {
4004
4045
  await handleAcceptWithToken(accessToken);
4005
4046
  } catch (err) {
4006
- console.error("Failed to auto-submit consent:", err);
4007
4047
  hasSubmittedRef.current = false;
4008
4048
  onError == null ? void 0 : onError(err);
4009
4049
  }