@solvenza/cookies-compliance 1.0.0 → 1.2.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.
@@ -457,14 +457,34 @@ var CookieStore = class {
457
457
  const isHttps = typeof window !== "undefined" && window.location.protocol === "https:";
458
458
  const secure = (_b = options.secure) != null ? _b : isHttps;
459
459
  let cookieStr = `${encodeURIComponent(name)}=${encodeURIComponent(value)}; Path=${path}; Max-Age=${maxAgeSeconds}; SameSite=${sameSite}`;
460
+ if (options.domain) {
461
+ cookieStr += `; Domain=${options.domain}`;
462
+ }
460
463
  if (secure) {
461
464
  cookieStr += "; Secure";
462
465
  }
463
466
  document.cookie = cookieStr;
464
467
  }
465
- static remove(name, path = "/") {
468
+ static remove(name, path = "/", domain) {
466
469
  if (typeof document === "undefined") return;
467
- document.cookie = `${encodeURIComponent(name)}=; Path=${path}; Max-Age=0; SameSite=Lax`;
470
+ let cookieStr = `${encodeURIComponent(name)}=; Path=${path}; Max-Age=0; SameSite=Lax`;
471
+ if (domain) {
472
+ cookieStr += `; Domain=${domain}`;
473
+ }
474
+ document.cookie = cookieStr;
475
+ }
476
+ static clearServiceCookies(config, revokedCategory) {
477
+ var _a, _b;
478
+ if (typeof document === "undefined" || !config.services) return;
479
+ const path = ((_a = config.storage) == null ? void 0 : _a.path) || "/";
480
+ const domain = (_b = config.storage) == null ? void 0 : _b.domain;
481
+ for (const service of Object.values(config.services)) {
482
+ if (service.category === revokedCategory && service.cookies) {
483
+ for (const cookieItem of service.cookies) {
484
+ this.remove(cookieItem.name, path, domain || cookieItem.domain);
485
+ }
486
+ }
487
+ }
468
488
  }
469
489
  };
470
490
 
@@ -1625,6 +1645,7 @@ var ConsentEngine = class {
1625
1645
  GoogleConsentAdapter.initDefault();
1626
1646
  if (evalResult.isValid) {
1627
1647
  GoogleConsentAdapter.update(evalResult.choices);
1648
+ this.dispatchDomEvent("solvenza:restored", { state: this.getConsent() });
1628
1649
  }
1629
1650
  this.blockerRegistry.init(
1630
1651
  (cat) => this.has(cat),
@@ -1711,7 +1732,7 @@ var ConsentEngine = class {
1711
1732
  this.preferencesModal.close();
1712
1733
  }
1713
1734
  withdraw() {
1714
- var _a, _b;
1735
+ var _a, _b, _c, _d;
1715
1736
  const previousChoices = this.stateManager.getChoices();
1716
1737
  const config = this.stateManager.getConfig();
1717
1738
  if (!config) return;
@@ -1719,11 +1740,17 @@ var ConsentEngine = class {
1719
1740
  if (((_b = config.storage) == null ? void 0 : _b.type) === "memory") {
1720
1741
  MemoryStore.remove(cookieName);
1721
1742
  } else {
1722
- CookieStore.remove(cookieName);
1743
+ CookieStore.remove(cookieName, ((_c = config.storage) == null ? void 0 : _c.path) || "/", (_d = config.storage) == null ? void 0 : _d.domain);
1744
+ }
1745
+ for (const catId of Object.keys(config.categories)) {
1746
+ if (catId !== "necessary") {
1747
+ CookieStore.clearServiceCookies(config, catId);
1748
+ }
1723
1749
  }
1724
1750
  this.stateManager.clearChoices();
1725
1751
  GoogleConsentAdapter.update(this.stateManager.getChoices());
1726
1752
  this.eventBus.emit("consent:withdrawn", { previousChoices });
1753
+ this.dispatchDomEvent("solvenza:updated", { choices: this.stateManager.getChoices() });
1727
1754
  this.showBanner();
1728
1755
  }
1729
1756
  when(categoryOrService, callback) {
@@ -1758,7 +1785,7 @@ var ConsentEngine = class {
1758
1785
  }
1759
1786
  }
1760
1787
  saveChoices(choices, source) {
1761
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1788
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1762
1789
  const config = this.stateManager.getConfig();
1763
1790
  if (!config) return;
1764
1791
  const receipt = createReceipt(
@@ -1769,6 +1796,11 @@ var ConsentEngine = class {
1769
1796
  (_a = config.security) == null ? void 0 : _a.secretKey
1770
1797
  );
1771
1798
  this.stateManager.updateChoices(receipt);
1799
+ for (const [catId, isAllowed] of Object.entries(choices)) {
1800
+ if (!isAllowed) {
1801
+ CookieStore.clearServiceCookies(config, catId);
1802
+ }
1803
+ }
1772
1804
  const cookieName = ((_b = config.storage) == null ? void 0 : _b.name) || "site_consent";
1773
1805
  const receiptJson = JSON.stringify(receipt);
1774
1806
  if (((_c = config.storage) == null ? void 0 : _c.type) === "memory") {
@@ -1778,7 +1810,8 @@ var ConsentEngine = class {
1778
1810
  path: ((_d = config.storage) == null ? void 0 : _d.path) || "/",
1779
1811
  maxAgeDays: (_f = (_e = config.consent) == null ? void 0 : _e.maxAgeDays) != null ? _f : 365,
1780
1812
  sameSite: ((_g = config.storage) == null ? void 0 : _g.sameSite) || "Lax",
1781
- secure: (_h = config.storage) == null ? void 0 : _h.secure
1813
+ secure: (_h = config.storage) == null ? void 0 : _h.secure,
1814
+ domain: (_i = config.storage) == null ? void 0 : _i.domain
1782
1815
  });
1783
1816
  }
1784
1817
  GoogleConsentAdapter.update(choices);
@@ -1786,7 +1819,7 @@ var ConsentEngine = class {
1786
1819
  (cat) => this.has(cat),
1787
1820
  (srv) => this.hasService(srv)
1788
1821
  );
1789
- if (((_i = config.logging) == null ? void 0 : _i.enabled) && config.logging.endpoint) {
1822
+ if (((_j = config.logging) == null ? void 0 : _j.enabled) && config.logging.endpoint) {
1790
1823
  try {
1791
1824
  fetch(config.logging.endpoint, {
1792
1825
  method: "POST",
@@ -1799,6 +1832,7 @@ var ConsentEngine = class {
1799
1832
  }
1800
1833
  this.banner.remove();
1801
1834
  this.eventBus.emit("consent:changed", { choices, receipt });
1835
+ this.dispatchDomEvent("solvenza:updated", { choices, receipt });
1802
1836
  }
1803
1837
  showBanner() {
1804
1838
  const config = this.stateManager.getConfig();
@@ -1809,6 +1843,7 @@ var ConsentEngine = class {
1809
1843
  onConfigure: () => this.openPreferences()
1810
1844
  });
1811
1845
  this.eventBus.emit("banner:shown", void 0);
1846
+ this.dispatchDomEvent("solvenza:show", void 0);
1812
1847
  }
1813
1848
  setupGlobalRevocationTrigger() {
1814
1849
  if (typeof document === "undefined") return;
@@ -1819,6 +1854,20 @@ var ConsentEngine = class {
1819
1854
  this.openPreferences();
1820
1855
  }
1821
1856
  });
1857
+ document.addEventListener("solvenza:show", () => {
1858
+ this.showBanner();
1859
+ });
1860
+ document.addEventListener("solvenza:preferences", () => {
1861
+ this.openPreferences();
1862
+ });
1863
+ }
1864
+ dispatchDomEvent(name, detail) {
1865
+ if (typeof document === "undefined") return;
1866
+ try {
1867
+ const customEvent = new CustomEvent(name, { detail, bubbles: true });
1868
+ document.dispatchEvent(customEvent);
1869
+ } catch (e) {
1870
+ }
1822
1871
  }
1823
1872
  };
1824
1873
  var globalScope = typeof window !== "undefined" ? window : globalThis;