lineas-romero-cookies-consent 1.0.0 → 1.1.2

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/README.md CHANGED
@@ -3,3 +3,52 @@ Gestor en vanilla JS + CSS para granular las cookies de las páginas de LR, Apar
3
3
 
4
4
 
5
5
  En este readme se detallará la manera de añadir o quitar cookies según dominio querido...
6
+
7
+ ## API (sin UI)
8
+
9
+ La librería expone un core **sin interfaz**. Esto permite usarla tanto en Next.js como en WordPress sin imponer un banner/modals concreto.
10
+
11
+ - `initCookieConsent(config)` inicializa el gestor.
12
+ - Persiste preferencias en la cookie `CookieConsent` (por defecto).
13
+ - Si `autoClear: true`, intenta borrar cookies propias que coincidan con `cookiePatterns` cuando una categoría está desactivada.
14
+
15
+ Ejemplo mínimo:
16
+
17
+ ```js
18
+ import { initCookieConsent, nextJsConfig } from 'lineas-romero-cookies-consent';
19
+
20
+ const consent = initCookieConsent(nextJsConfig);
21
+
22
+ // Ejemplos de acciones que dispararías desde tu UI
23
+ // consent.acceptAll();
24
+ // consent.rejectAll();
25
+ // consent.setPreferences({ analytics: true, marketing: false });
26
+
27
+ // Para leer el estado:
28
+ console.log(consent.getPreferences());
29
+ ```
30
+
31
+ ## Presets listos
32
+
33
+ La librería exporta dos objetos `config` listos para usar:
34
+
35
+ - `nextJsConfig` (Fareharbor, Google Analytics, Metricool, Facebook Pixel)
36
+ - `wpApartamentosConfig` (WordPress/Polylang + Google Ads/Maps)
37
+
38
+ Puedes importarlos así:
39
+
40
+ ```js
41
+ import { initCookieConsent, wpApartamentosConfig } from 'lineas-romero-cookies-consent';
42
+
43
+ const consent = initCookieConsent(wpApartamentosConfig);
44
+ ```
45
+
46
+ ## Nota importante: cookies de terceros (Google / DoubleClick)
47
+
48
+ Cookies como `IDE`, `SOCS`, `__Secure-ENID` pertenecen a dominios de terceros como `.google.com` o `.doubleclick.net`.
49
+
50
+ El navegador no permite que un script corriendo en tu dominio borre cookies de terceros. Por eso:
51
+
52
+ - `autoClear` solo es una “limpieza de cortesía” para cookies **propias**.
53
+ - La protección real consiste en **no cargar** los scripts/iframes (Maps/Ads) si el usuario rechaza esa categoría.
54
+
@@ -1 +1,264 @@
1
-
1
+ const y = "CookieConsent";
2
+ function C(o) {
3
+ if (!o || typeof o != "object")
4
+ throw new Error("initCookieConsent(config): config is required");
5
+ const e = Array.isArray(o.categories) ? o.categories : [];
6
+ if (e.length === 0)
7
+ throw new Error("initCookieConsent(config): config.categories is required");
8
+ const t = e.map((r) => {
9
+ if (!r || typeof r != "object")
10
+ throw new Error("initCookieConsent(config): invalid category");
11
+ if (!r.id)
12
+ throw new Error("initCookieConsent(config): category.id is required");
13
+ return {
14
+ id: String(r.id),
15
+ label: r.label ? String(r.label) : String(r.id),
16
+ description: r.description ? String(r.description) : "",
17
+ enabled: !!r.enabled,
18
+ readonly: !!r.readonly,
19
+ cookiePatterns: Array.isArray(r.cookiePatterns) ? r.cookiePatterns.map(String) : []
20
+ };
21
+ }), n = o.cookieName ? String(o.cookieName) : y, i = Number.isFinite(o.cookieExpiresDays) ? Number(o.cookieExpiresDays) : 180;
22
+ return {
23
+ siteName: o.siteName ? String(o.siteName) : "",
24
+ autoClear: !!o.autoClear,
25
+ cookieName: n,
26
+ cookieExpiresDays: i,
27
+ categories: t,
28
+ onAccept: typeof o.onAccept == "function" ? o.onAccept : null
29
+ };
30
+ }
31
+ function A(o) {
32
+ const e = {};
33
+ for (const t of o)
34
+ e[t.id] = t.readonly ? !0 : !!t.enabled;
35
+ return e;
36
+ }
37
+ function S(o) {
38
+ try {
39
+ return JSON.parse(decodeURIComponent(o));
40
+ } catch {
41
+ return null;
42
+ }
43
+ }
44
+ function b(o) {
45
+ if (typeof document > "u") return null;
46
+ const e = document.cookie ? document.cookie.split("; ") : [];
47
+ for (const t of e) {
48
+ const n = t.indexOf("=");
49
+ if ((n >= 0 ? t.slice(0, n) : t) === o)
50
+ return n >= 0 ? t.slice(n + 1) : "";
51
+ }
52
+ return null;
53
+ }
54
+ function N(o, e, { expiresDays: t }) {
55
+ if (typeof document > "u") return;
56
+ const n = /* @__PURE__ */ new Date();
57
+ n.setTime(n.getTime() + t * 24 * 60 * 60 * 1e3);
58
+ const i = encodeURIComponent(JSON.stringify(e));
59
+ document.cookie = `${o}=${i}; Expires=${n.toUTCString()}; Path=/; SameSite=Lax`;
60
+ }
61
+ function f(o, e) {
62
+ return e ? o === e ? !0 : o.startsWith(e) : !1;
63
+ }
64
+ function p() {
65
+ return typeof document > "u" ? [] : (document.cookie ? document.cookie.split("; ") : []).map((e) => {
66
+ const t = e.indexOf("=");
67
+ return t >= 0 ? e.slice(0, t) : e;
68
+ }).filter(Boolean);
69
+ }
70
+ function P(o) {
71
+ const e = /* @__PURE__ */ new Set();
72
+ if (!o) return Array.from(e);
73
+ e.add(o), o.includes(".") && e.add(`.${o}`);
74
+ const t = o.split(".").filter(Boolean);
75
+ for (let n = 1; n < t.length - 1; n += 1) {
76
+ const i = t.slice(n).join(".");
77
+ e.add(i), e.add(`.${i}`);
78
+ }
79
+ return Array.from(e);
80
+ }
81
+ function m(o) {
82
+ if (typeof document > "u") return;
83
+ const e = "Thu, 01 Jan 1970 00:00:00 GMT", t = typeof location < "u" ? location.hostname : "", n = P(t);
84
+ document.cookie = `${o}=; Expires=${e}; Path=/; SameSite=Lax`;
85
+ for (const i of n)
86
+ document.cookie = `${o}=; Expires=${e}; Path=/; Domain=${i}; SameSite=Lax`;
87
+ }
88
+ function E(o, e) {
89
+ return o.find((t) => t.id === e) || null;
90
+ }
91
+ function _(o, e) {
92
+ const t = E(o, e);
93
+ return t ? t.cookiePatterns : [];
94
+ }
95
+ function x({ categories: o, allowedPreferences: e }) {
96
+ const t = p();
97
+ for (const n of o)
98
+ if (!e[n.id])
99
+ for (const r of t)
100
+ n.cookiePatterns.some((l) => f(r, l)) && m(r);
101
+ }
102
+ function u(o, e) {
103
+ const t = {};
104
+ for (const n of o) {
105
+ const i = e && Object.prototype.hasOwnProperty.call(e, n.id) ? !!e[n.id] : !!n.enabled;
106
+ t[n.id] = n.readonly ? !0 : i;
107
+ }
108
+ return t;
109
+ }
110
+ function h(o) {
111
+ const e = C(o), t = A(e.categories), n = b(e.cookieName), i = n ? S(n) : null;
112
+ let a = i && i.preferences ? u(e.categories, i.preferences) : t;
113
+ i && i.preferences && e.onAccept && e.onAccept({ ...a });
114
+ function l(s) {
115
+ return a = u(e.categories, s), N(
116
+ e.cookieName,
117
+ {
118
+ preferences: a,
119
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
120
+ siteName: e.siteName
121
+ },
122
+ { expiresDays: e.cookieExpiresDays }
123
+ ), e.autoClear && x({ categories: e.categories, allowedPreferences: a }), e.onAccept && e.onAccept({ ...a }), { ...a };
124
+ }
125
+ return {
126
+ getConfig() {
127
+ return {
128
+ siteName: e.siteName,
129
+ autoClear: e.autoClear,
130
+ cookieName: e.cookieName,
131
+ cookieExpiresDays: e.cookieExpiresDays,
132
+ categories: e.categories.map((s) => ({ ...s }))
133
+ };
134
+ },
135
+ hasStoredConsent() {
136
+ return !!(i && i.preferences);
137
+ },
138
+ getPreferences() {
139
+ return { ...a };
140
+ },
141
+ acceptAll() {
142
+ const s = {};
143
+ for (const c of e.categories)
144
+ s[c.id] = !0;
145
+ return l(s);
146
+ },
147
+ rejectAll() {
148
+ const s = {};
149
+ for (const c of e.categories)
150
+ s[c.id] = !!c.readonly;
151
+ return l(s);
152
+ },
153
+ setPreferences(s) {
154
+ return l({ ...a, ...s || {} });
155
+ },
156
+ clearCategoryCookies(s) {
157
+ const c = _(e.categories, s), g = p();
158
+ for (const d of g)
159
+ c.some((k) => f(d, k)) && m(d);
160
+ }
161
+ };
162
+ }
163
+ const w = {
164
+ siteName: "Lineas Romero NextJS",
165
+ autoClear: !0,
166
+ categories: [
167
+ {
168
+ id: "necessary",
169
+ label: "Strictly Necessary",
170
+ description: "Essential for booking and site functionality.",
171
+ enabled: !0,
172
+ readonly: !0,
173
+ cookiePatterns: ["fh_", "fareharbor", "next-", "__Host-", "connect.sid"]
174
+ },
175
+ {
176
+ id: "analytics",
177
+ label: "Analytics",
178
+ description: "Metrics to improve our service.",
179
+ enabled: !1,
180
+ cookiePatterns: ["_ga", "_gid", "_gat", "mc_", "metricool"]
181
+ },
182
+ {
183
+ id: "marketing",
184
+ label: "Marketing",
185
+ description: "Personalized advertising.",
186
+ enabled: !1,
187
+ cookiePatterns: ["_fbp", "fr", "tr"]
188
+ }
189
+ ],
190
+ /* aqui es donde voy a inicializar los scripts de analytics y marketing, y cualquiera que sea */
191
+ onAccept: (o) => {
192
+ o.analytics, o.marketing;
193
+ }
194
+ }, B = {
195
+ siteName: "Apartamentos Lineas Romero",
196
+ autoClear: !0,
197
+ categories: [
198
+ {
199
+ id: "necessary",
200
+ label: "Strictly Necessary",
201
+ description: "Language selection and basic functions.",
202
+ enabled: !0,
203
+ readonly: !0,
204
+ cookiePatterns: [
205
+ "wp-settings",
206
+ "wordpress_",
207
+ "pll_",
208
+ "CookieConsent",
209
+ "AEC",
210
+ "__Secure-BUCKET"
211
+ ]
212
+ },
213
+ {
214
+ id: "marketing",
215
+ label: "Marketing & Ads",
216
+ description: "Google Maps and Advertising integration.",
217
+ enabled: !1,
218
+ cookiePatterns: ["IDE", "_gcl", "ar_debug", "SOCS", "__Secure-ENID", "NID", "1P_JAR"]
219
+ }
220
+ ],
221
+ onAccept: (o) => {
222
+ o.marketing;
223
+ }
224
+ }, I = {
225
+ siteName: "Visit La Graciosa",
226
+ autoClear: !0,
227
+ categories: [
228
+ {
229
+ id: "necessary",
230
+ label: "Strictly Necessary",
231
+ description: "Essential for language preferences and site functionality.",
232
+ enabled: !0,
233
+ readonly: !0,
234
+ cookiePatterns: [
235
+ "pll_",
236
+ "session_id",
237
+ "PHPSESSID"
238
+ ]
239
+ },
240
+ {
241
+ id: "analytics",
242
+ label: "Analytics",
243
+ description: "Anonymous usage statistics.",
244
+ enabled: !1,
245
+ cookiePatterns: ["_ga", "_gid"]
246
+ },
247
+ {
248
+ id: "marketing",
249
+ label: "Marketing",
250
+ description: "Advertising and conversion tracking.",
251
+ enabled: !1,
252
+ cookiePatterns: ["_gcl", "_gac"]
253
+ }
254
+ ],
255
+ onAccept: (o) => {
256
+ o.marketing;
257
+ }
258
+ };
259
+ export {
260
+ h as initCookieConsent,
261
+ w as nextJsConfig,
262
+ I as visitLaGraciosaConfig,
263
+ B as wpApartamentosConfig
264
+ };
@@ -1 +1 @@
1
- (function(n){typeof define=="function"&&define.amd?define(n):n()})((function(){"use strict"}));
1
+ (function(a,d){typeof exports=="object"&&typeof module<"u"?d(exports):typeof define=="function"&&define.amd?define(["exports"],d):(a=typeof globalThis<"u"?globalThis:a||self,d(a.LineasRomeroCookies={}))})(this,(function(a){"use strict";const d="CookieConsent";function y(o){if(!o||typeof o!="object")throw new Error("initCookieConsent(config): config is required");const e=Array.isArray(o.categories)?o.categories:[];if(e.length===0)throw new Error("initCookieConsent(config): config.categories is required");const t=e.map(r=>{if(!r||typeof r!="object")throw new Error("initCookieConsent(config): invalid category");if(!r.id)throw new Error("initCookieConsent(config): category.id is required");return{id:String(r.id),label:r.label?String(r.label):String(r.id),description:r.description?String(r.description):"",enabled:!!r.enabled,readonly:!!r.readonly,cookiePatterns:Array.isArray(r.cookiePatterns)?r.cookiePatterns.map(String):[]}}),n=o.cookieName?String(o.cookieName):d,i=Number.isFinite(o.cookieExpiresDays)?Number(o.cookieExpiresDays):180;return{siteName:o.siteName?String(o.siteName):"",autoClear:!!o.autoClear,cookieName:n,cookieExpiresDays:i,categories:t,onAccept:typeof o.onAccept=="function"?o.onAccept:null}}function C(o){const e={};for(const t of o)e[t.id]=t.readonly?!0:!!t.enabled;return e}function A(o){try{return JSON.parse(decodeURIComponent(o))}catch{return null}}function b(o){if(typeof document>"u")return null;const e=document.cookie?document.cookie.split("; "):[];for(const t of e){const n=t.indexOf("=");if((n>=0?t.slice(0,n):t)===o)return n>=0?t.slice(n+1):""}return null}function S(o,e,{expiresDays:t}){if(typeof document>"u")return;const n=new Date;n.setTime(n.getTime()+t*24*60*60*1e3);const i=encodeURIComponent(JSON.stringify(e));document.cookie=`${o}=${i}; Expires=${n.toUTCString()}; Path=/; SameSite=Lax`}function u(o,e){return e?o===e?!0:o.startsWith(e):!1}function p(){return typeof document>"u"?[]:(document.cookie?document.cookie.split("; "):[]).map(e=>{const t=e.indexOf("=");return t>=0?e.slice(0,t):e}).filter(Boolean)}function N(o){const e=new Set;if(!o)return Array.from(e);e.add(o),o.includes(".")&&e.add(`.${o}`);const t=o.split(".").filter(Boolean);for(let n=1;n<t.length-1;n+=1){const i=t.slice(n).join(".");e.add(i),e.add(`.${i}`)}return Array.from(e)}function m(o){if(typeof document>"u")return;const e="Thu, 01 Jan 1970 00:00:00 GMT",t=typeof location<"u"?location.hostname:"",n=N(t);document.cookie=`${o}=; Expires=${e}; Path=/; SameSite=Lax`;for(const i of n)document.cookie=`${o}=; Expires=${e}; Path=/; Domain=${i}; SameSite=Lax`}function P(o,e){return o.find(t=>t.id===e)||null}function E(o,e){const t=P(o,e);return t?t.cookiePatterns:[]}function _({categories:o,allowedPreferences:e}){const t=p();for(const n of o)if(!e[n.id])for(const r of t)n.cookiePatterns.some(f=>u(r,f))&&m(r)}function g(o,e){const t={};for(const n of o){const i=e&&Object.prototype.hasOwnProperty.call(e,n.id)?!!e[n.id]:!!n.enabled;t[n.id]=n.readonly?!0:i}return t}function h(o){const e=y(o),t=C(e.categories),n=b(e.cookieName),i=n?A(n):null;let c=i&&i.preferences?g(e.categories,i.preferences):t;i&&i.preferences&&e.onAccept&&e.onAccept({...c});function f(s){return c=g(e.categories,s),S(e.cookieName,{preferences:c,updatedAt:new Date().toISOString(),siteName:e.siteName},{expiresDays:e.cookieExpiresDays}),e.autoClear&&_({categories:e.categories,allowedPreferences:c}),e.onAccept&&e.onAccept({...c}),{...c}}return{getConfig(){return{siteName:e.siteName,autoClear:e.autoClear,cookieName:e.cookieName,cookieExpiresDays:e.cookieExpiresDays,categories:e.categories.map(s=>({...s}))}},hasStoredConsent(){return!!(i&&i.preferences)},getPreferences(){return{...c}},acceptAll(){const s={};for(const l of e.categories)s[l.id]=!0;return f(s)},rejectAll(){const s={};for(const l of e.categories)s[l.id]=!!l.readonly;return f(s)},setPreferences(s){return f({...c,...s||{}})},clearCategoryCookies(s){const l=E(e.categories,s),L=p();for(const k of L)l.some(B=>u(k,B))&&m(k)}}}const w={siteName:"Lineas Romero NextJS",autoClear:!0,categories:[{id:"necessary",label:"Strictly Necessary",description:"Essential for booking and site functionality.",enabled:!0,readonly:!0,cookiePatterns:["fh_","fareharbor","next-","__Host-","connect.sid"]},{id:"analytics",label:"Analytics",description:"Metrics to improve our service.",enabled:!1,cookiePatterns:["_ga","_gid","_gat","mc_","metricool"]},{id:"marketing",label:"Marketing",description:"Personalized advertising.",enabled:!1,cookiePatterns:["_fbp","fr","tr"]}],onAccept:o=>{o.analytics,o.marketing}},D={siteName:"Apartamentos Lineas Romero",autoClear:!0,categories:[{id:"necessary",label:"Strictly Necessary",description:"Language selection and basic functions.",enabled:!0,readonly:!0,cookiePatterns:["wp-settings","wordpress_","pll_","CookieConsent","AEC","__Secure-BUCKET"]},{id:"marketing",label:"Marketing & Ads",description:"Google Maps and Advertising integration.",enabled:!1,cookiePatterns:["IDE","_gcl","ar_debug","SOCS","__Secure-ENID","NID","1P_JAR"]}],onAccept:o=>{o.marketing}},x={siteName:"Visit La Graciosa",autoClear:!0,categories:[{id:"necessary",label:"Strictly Necessary",description:"Essential for language preferences and site functionality.",enabled:!0,readonly:!0,cookiePatterns:["pll_","session_id","PHPSESSID"]},{id:"analytics",label:"Analytics",description:"Anonymous usage statistics.",enabled:!1,cookiePatterns:["_ga","_gid"]},{id:"marketing",label:"Marketing",description:"Advertising and conversion tracking.",enabled:!1,cookiePatterns:["_gcl","_gac"]}],onAccept:o=>{o.marketing}};a.initCookieConsent=h,a.nextJsConfig=w,a.visitLaGraciosaConfig=x,a.wpApartamentosConfig=D,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})}));
package/index.d.ts ADDED
@@ -0,0 +1,41 @@
1
+ // index.d.ts
2
+
3
+ export interface CookieCategory {
4
+ id: string;
5
+ label?: string;
6
+ description?: string;
7
+ enabled?: boolean;
8
+ readonly?: boolean;
9
+ cookiePatterns?: string[];
10
+ }
11
+
12
+ export interface UserPreferences {
13
+ [categoryId: string]: boolean;
14
+ }
15
+
16
+ export interface CookieConsentConfig {
17
+ siteName?: string;
18
+ cookieName?: string;
19
+ cookieExpiresDays?: number;
20
+ autoClear?: boolean;
21
+ categories: CookieCategory[];
22
+ onAccept?: (preferences: UserPreferences) => void;
23
+ }
24
+
25
+ export interface ConsentAPI {
26
+ getConfig(): CookieConsentConfig;
27
+ hasStoredConsent(): boolean;
28
+ getPreferences(): UserPreferences;
29
+ acceptAll(): UserPreferences;
30
+ rejectAll(): UserPreferences;
31
+ setPreferences(preferences: UserPreferences): UserPreferences;
32
+ clearCategoryCookies(categoryId: string): void;
33
+ }
34
+
35
+ // Función principal
36
+ export function initCookieConsent(config: CookieConsentConfig): ConsentAPI;
37
+
38
+ // Presets exportados
39
+ export const nextJsConfig: CookieConsentConfig;
40
+ export const wpApartamentosConfig: CookieConsentConfig;
41
+ export const visitLaGraciosaConfig: CookieConsentConfig;
package/package.json CHANGED
@@ -1,15 +1,18 @@
1
1
  {
2
2
  "name": "lineas-romero-cookies-consent",
3
- "version": "1.0.0",
3
+ "version": "1.1.2",
4
4
  "description": "Gestor en vanilla JS + CSS para granular las cookies de las páginas de LR, Apartamentos y Visit La Graciosa de manera manual.",
5
5
  "type": "module",
6
6
  "files": [
7
- "dist"
7
+ "dist",
8
+ "index.d.ts"
8
9
  ],
10
+ "types": "./index.d.ts",
9
11
  "main": "./dist/lineas-romero-cookies-consent.umd.cjs",
10
12
  "module": "./dist/lineas-romero-cookies-consent.js",
11
13
  "exports": {
12
14
  ".": {
15
+ "types": "./index.d.ts",
13
16
  "import": "./dist/lineas-romero-cookies-consent.js",
14
17
  "require": "./dist/lineas-romero-cookies-consent.umd.cjs"
15
18
  },