@el7ven/cookie-kit 0.3.1 → 0.3.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.
@@ -0,0 +1,126 @@
1
+ /**
2
+ * Region Rules — GDPR/CCPA region definitions
3
+ * @module RegionRules
4
+ */
5
+
6
+ export const REGION_RULES = {
7
+ // European Union (GDPR)
8
+ EU: {
9
+ name: 'European Union',
10
+ countries: [
11
+ 'AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR',
12
+ 'DE', 'GR', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL',
13
+ 'PL', 'PT', 'RO', 'SK', 'SI', 'ES', 'SE', 'MD'
14
+ ],
15
+ regulation: 'GDPR',
16
+ requiresConsent: true,
17
+ defaultMode: 'opt-in'
18
+ },
19
+
20
+ // European Economic Area (GDPR)
21
+ EEA: {
22
+ name: 'European Economic Area',
23
+ countries: ['IS', 'LI', 'NO'],
24
+ regulation: 'GDPR',
25
+ requiresConsent: true,
26
+ defaultMode: 'opt-in'
27
+ },
28
+
29
+ // United Kingdom (UK GDPR)
30
+ UK: {
31
+ name: 'United Kingdom',
32
+ countries: ['GB'],
33
+ regulation: 'UK GDPR',
34
+ requiresConsent: true,
35
+ defaultMode: 'opt-in'
36
+ },
37
+
38
+ // Switzerland (FADP)
39
+ CH: {
40
+ name: 'Switzerland',
41
+ countries: ['CH'],
42
+ regulation: 'FADP',
43
+ requiresConsent: true,
44
+ defaultMode: 'opt-in'
45
+ },
46
+
47
+ // United States (Federal)
48
+ US: {
49
+ name: 'United States',
50
+ countries: ['US'],
51
+ regulation: 'US Privacy Laws',
52
+ requiresConsent: true,
53
+ defaultMode: 'opt-out'
54
+ },
55
+
56
+ // Brazil (LGPD)
57
+ BR: {
58
+ name: 'Brazil',
59
+ countries: ['BR'],
60
+ regulation: 'LGPD',
61
+ requiresConsent: true,
62
+ defaultMode: 'opt-in'
63
+ },
64
+
65
+ // Canada (PIPEDA)
66
+ CA: {
67
+ name: 'Canada',
68
+ countries: ['CA'],
69
+ regulation: 'PIPEDA',
70
+ requiresConsent: true,
71
+ defaultMode: 'opt-in'
72
+ },
73
+
74
+ // Rest of World
75
+ ROW: {
76
+ name: 'Rest of World',
77
+ countries: [],
78
+ regulation: null,
79
+ requiresConsent: true, // Show essential modal
80
+ defaultMode: 'opt-out',
81
+ essentialOnly: true // Only necessary cookies
82
+ }
83
+ }
84
+
85
+ export function isEU(countryCode) {
86
+ return REGION_RULES.EU.countries.includes(countryCode)
87
+ }
88
+
89
+ export function isEEA(countryCode) {
90
+ return REGION_RULES.EEA.countries.includes(countryCode)
91
+ }
92
+
93
+ export function isUK(countryCode) {
94
+ return countryCode === 'GB'
95
+ }
96
+
97
+ export function requiresGDPR(countryCode) {
98
+ return isEU(countryCode) || isEEA(countryCode) || isUK(countryCode) || countryCode === 'CH'
99
+ }
100
+
101
+ export function getRegion(countryCode, stateCode = null) {
102
+ if (isEU(countryCode)) return 'EU'
103
+ if (isEEA(countryCode)) return 'EEA'
104
+ if (isUK(countryCode)) return 'UK'
105
+ if (countryCode === 'CH') return 'CH'
106
+ if (countryCode === 'BR') return 'BR'
107
+ if (countryCode === 'CA') return 'CA'
108
+ if (countryCode === 'US') return 'US' // All US states now use US region
109
+
110
+ return 'ROW'
111
+ }
112
+
113
+ export function requiresConsentBanner(region) {
114
+ const rule = REGION_RULES[region]
115
+ return rule ? rule.requiresConsent : false
116
+ }
117
+
118
+ export function getRegulation(region) {
119
+ const rule = REGION_RULES[region]
120
+ return rule ? rule.regulation : null
121
+ }
122
+
123
+ export function isEssentialOnly(region) {
124
+ const rule = REGION_RULES[region]
125
+ return rule ? rule.essentialOnly || false : false
126
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Geo Detection System — detect user's geographic region for GDPR/CCPA compliance
3
+ * @module geo
4
+ */
5
+
6
+ export { GeoDetector } from './GeoDetector.js'
7
+ export {
8
+ REGION_RULES,
9
+ isEU,
10
+ isEEA,
11
+ isUK,
12
+ requiresGDPR,
13
+ getRegion,
14
+ requiresConsentBanner,
15
+ getRegulation
16
+ } from './RegionRules.js'
package/src/index.js CHANGED
@@ -1,38 +1,80 @@
1
1
  /**
2
2
  * @el7ven/cookie-kit - Unified Cookie Consent Management
3
3
  * GDPR compliant cookie consent with React and Vue support
4
- * @version 1.0.0-beta.0
4
+ * @version 0.4.0
5
5
  * @author Igor Semionov
6
6
  */
7
7
 
8
- // Core functionality
8
+ // ─── Core (lite) ───
9
9
  export { createCookieKit, CookieKitCore, DEFAULT_CONFIG } from './core/index.js'
10
10
 
11
- // Analytics integration
11
+ // ─── Core (full) ───
12
+ export { ConsentManager } from './core/ConsentManager.js'
13
+ export { StorageAdapter } from './core/StorageAdapter.js'
14
+ export { ConsentMode, CONSENT_MODE_DEFAULTS, CATEGORY_MAPPING } from './core/ConsentMode.js'
15
+ export { FocusTrap } from './core/FocusTrap.js'
16
+ export { ScriptLoader } from './core/ScriptLoader.js'
17
+ export { AnalyticsManager, ANALYTICS_PROVIDERS } from './core/AnalyticsManager.js'
18
+ export { GeoDetector as CoreGeoDetector } from './core/GeoDetector.js'
19
+
20
+ // ─── Analytics helpers ───
12
21
  export { createAnalyticsManager, initGoogleConsentMode, updateGoogleConsent, loadGTM, loadGA4, loadMetaPixel, loadYandexMetrica, blockScriptsBeforeConsent, unblockConsentedScripts, manageScriptBlocking } from './core/analytics.js'
13
22
 
14
- // Import for default export
23
+ // ─── Geo detection ───
24
+ export { GeoDetector } from './geo/GeoDetector.js'
25
+ export { REGION_RULES, isEU, isEEA, isUK, requiresGDPR, getRegion, requiresConsentBanner, getRegulation, isEssentialOnly } from './geo/RegionRules.js'
26
+
27
+ // ─── Locales / i18n ───
28
+ export { locales, getLocale } from './locales/index.js'
29
+
30
+ // ─── Providers ───
31
+ export { GoogleConsentModeProvider } from './providers/GoogleConsentModeProvider.js'
32
+
33
+ // ─── Plugins ───
34
+ export { CMPPlugin } from './plugins/CMPPlugin.js'
35
+ export { PluginManager } from './plugins/PluginManager.js'
36
+
37
+ // ─── Trackers ───
38
+ export { TrackerDetector } from './trackers/TrackerDetector.js'
39
+ export { TrackerRegistry } from './trackers/TrackerRegistry.js'
40
+
41
+ // ─── Script rewriting ───
42
+ export { ScriptRewriter } from './rewriting/ScriptRewriter.js'
43
+
44
+ // ─── Scripts (advanced) ───
45
+ export { ScriptLoader as AdvancedScriptLoader } from './scripts/ScriptLoader.js'
46
+ export { ScriptManager } from './scripts/ScriptManager.js'
47
+ export { ScriptRegistry } from './scripts/ScriptRegistry.js'
48
+ export { ScriptScanner } from './scripts/ScriptScanner.js'
49
+
50
+ // ─── Utils ───
51
+ export { getCookie, setCookie, deleteCookie, getAllCookies } from './utils/cookies.js'
52
+ export { getFocusableElements, trapFocus, lockBodyScroll, unlockBodyScroll, onEscape } from './utils/dom.js'
53
+ export { generateUUID, debounce, throttle, deepMerge, isSSR, isMobile, getRegionFromTimezone } from './utils/helpers.js'
54
+
55
+ // ─── Imports for default export ───
15
56
  import { createCookieKit, CookieKitCore, DEFAULT_CONFIG } from './core/index.js'
16
57
  import { createAnalyticsManager } from './core/analytics.js'
58
+ import { ConsentManager } from './core/ConsentManager.js'
17
59
  import { initCookieKit } from './js/index.js'
18
60
  import { VERSION } from './core/version.js'
19
61
 
20
- // Vanilla JS components
62
+ // ─── Vanilla JS components ───
21
63
  export { initCookieKit, mountCookieConsent } from './js/index.js'
22
64
 
23
- // React components
65
+ // ─── React components ───
24
66
  export * from './react/index.js'
25
67
 
26
- // Vue components
68
+ // ─── Vue components ───
27
69
  export * from './vue/index.js'
28
70
 
29
- // Vue composable
71
+ // ─── Vue composable ───
30
72
  export { useCookieConsent } from './vue/composables/useCookieConsent.js'
31
73
 
32
- // Version info
74
+ // ─── Version info ───
33
75
  export { VERSION, getVersion, getVersionInfo, logVersion } from './core/version.js'
34
76
 
35
- // Unified factory function
77
+ // ─── Unified factory ───
36
78
  export function createCookieConsent(config = {}) {
37
79
  const { framework = 'js', ...restConfig } = config
38
80
 
@@ -46,23 +88,18 @@ export function createCookieConsent(config = {}) {
46
88
  }
47
89
  }
48
90
 
49
- // Auto-detect framework and create appropriate instance
91
+ // Auto-detect framework
50
92
  export function autoCookieConsent(config = {}) {
51
- // Detect React
52
93
  if (typeof window !== 'undefined' && window.React) {
53
94
  return createCookieConsent({ ...config, framework: 'react' })
54
95
  }
55
-
56
- // Detect Vue
57
96
  if (typeof window !== 'undefined' && window.Vue) {
58
97
  return createCookieConsent({ ...config, framework: 'vue' })
59
98
  }
60
-
61
- // Default to vanilla JS
62
99
  return createCookieConsent({ ...config, framework: 'js' })
63
100
  }
64
101
 
65
- // Default export for convenience
102
+ // Default export
66
103
  export default {
67
104
  createCookieKit,
68
105
  createCookieConsent,
@@ -70,6 +107,7 @@ export default {
70
107
  autoCookieConsent,
71
108
  initCookieKit,
72
109
  CookieKitCore,
110
+ ConsentManager,
73
111
  DEFAULT_CONFIG,
74
112
  VERSION
75
113
  }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * English translations
3
+ */
4
+
5
+ export default {
6
+ essential: {
7
+ title: 'Cookies',
8
+ message: 'We use cookies to improve your experience on our site.',
9
+ button: 'I understand'
10
+ },
11
+ gdpr: {
12
+ title: 'Cookie Preferences',
13
+ message: 'We use cookies to provide you with the best experience. Choose which types of cookies you accept.',
14
+ buttons: {
15
+ acceptAll: 'Accept all',
16
+ rejectAll: 'Reject all',
17
+ acceptSelection: 'Accept selection',
18
+ settings: 'Settings'
19
+ }
20
+ },
21
+ settings: {
22
+ title: 'Cookie Settings',
23
+ tabs: {
24
+ privacy: 'Privacy'
25
+ },
26
+ buttons: {
27
+ acceptAll: 'Accept all',
28
+ rejectAll: 'Reject all',
29
+ acceptSelection: 'Save selection'
30
+ },
31
+ links: {
32
+ moreInfo: 'More information',
33
+ cookieDetails: 'Cookie details'
34
+ }
35
+ },
36
+ categories: {
37
+ necessary: {
38
+ label: 'Necessary',
39
+ description: 'Essential cookies for site functionality'
40
+ },
41
+ analytics: {
42
+ label: 'Analytics',
43
+ description: 'Cookies for traffic and user behavior analysis'
44
+ },
45
+ marketing: {
46
+ label: 'Marketing',
47
+ description: 'Cookies for personalized advertising'
48
+ },
49
+ preferences: {
50
+ label: 'Preferences',
51
+ description: 'Cookies for saving user preferences'
52
+ }
53
+ }
54
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * i18n support for cookie consent
3
+ */
4
+
5
+ import en from './en.js'
6
+ import ro from './ro.js'
7
+
8
+ export const locales = {
9
+ en,
10
+ ro
11
+ }
12
+
13
+ export function getLocale(lang = 'ro') {
14
+ return locales[lang] || locales.ro
15
+ }
16
+
17
+ export default {
18
+ locales,
19
+ getLocale
20
+ }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Romanian translations
3
+ */
4
+
5
+ export default {
6
+ essential: {
7
+ title: 'Cookie-uri',
8
+ message: 'Folosim cookie-uri pentru a îmbunătăți experiența ta pe site.',
9
+ button: 'Am înțeles'
10
+ },
11
+ gdpr: {
12
+ title: 'Preferințe Cookie-uri',
13
+ message: 'Folosim cookie-uri pentru a-ți oferi cea mai bună experiență. Alege ce tipuri de cookie-uri accepți.',
14
+ buttons: {
15
+ acceptAll: 'Acceptă toate',
16
+ rejectAll: 'Respinge toate',
17
+ acceptSelection: 'Acceptă selecția',
18
+ settings: 'Setări'
19
+ }
20
+ },
21
+ settings: {
22
+ title: 'Setări Cookie-uri',
23
+ tabs: {
24
+ privacy: 'Confidențialitate'
25
+ },
26
+ buttons: {
27
+ acceptAll: 'Acceptă toate',
28
+ rejectAll: 'Respinge toate',
29
+ acceptSelection: 'Salvează selecția'
30
+ },
31
+ links: {
32
+ moreInfo: 'Mai multe informații',
33
+ cookieDetails: 'Detalii cookie-uri'
34
+ }
35
+ },
36
+ categories: {
37
+ necessary: {
38
+ label: 'Necesare',
39
+ description: 'Cookie-uri esențiale pentru funcționarea site-ului'
40
+ },
41
+ analytics: {
42
+ label: 'Analiză',
43
+ description: 'Cookie-uri pentru analiza traficului și comportamentului utilizatorilor'
44
+ },
45
+ marketing: {
46
+ label: 'Marketing',
47
+ description: 'Cookie-uri pentru publicitate personalizată'
48
+ },
49
+ preferences: {
50
+ label: 'Preferințe',
51
+ description: 'Cookie-uri pentru salvarea preferințelor utilizatorului'
52
+ }
53
+ }
54
+ }
@@ -0,0 +1,187 @@
1
+ /**
2
+ * CMP Plugin Base Class
3
+ * @module CMPPlugin
4
+ *
5
+ * Base class for all CMP plugins
6
+ * Plugins can extend CMP functionality without modifying core
7
+ */
8
+
9
+ export class CMPPlugin {
10
+ constructor(config = {}) {
11
+ this.config = config
12
+ this.cmp = null
13
+ this.isInitialized = false
14
+ }
15
+
16
+ /**
17
+ * Get plugin name
18
+ * @returns {string}
19
+ */
20
+ get name() {
21
+ return this.constructor.name
22
+ }
23
+
24
+ /**
25
+ * Get plugin version
26
+ * @returns {string}
27
+ */
28
+ get version() {
29
+ return '1.0.0'
30
+ }
31
+
32
+ /**
33
+ * Initialize plugin
34
+ * Called when plugin is registered
35
+ * @param {Object} cmpInstance - CMP instance
36
+ */
37
+ init(cmpInstance) {
38
+ this.cmp = cmpInstance
39
+ this.isInitialized = true
40
+ this._debug('Plugin initialized')
41
+ }
42
+
43
+ /**
44
+ * Lifecycle: Before CMP initialization
45
+ */
46
+ beforeInit() {
47
+ // Override in subclass
48
+ }
49
+
50
+ /**
51
+ * Lifecycle: After CMP initialization
52
+ */
53
+ afterInit() {
54
+ // Override in subclass
55
+ }
56
+
57
+ /**
58
+ * Lifecycle: Before consent banner shown
59
+ */
60
+ beforeBannerShow() {
61
+ // Override in subclass
62
+ }
63
+
64
+ /**
65
+ * Lifecycle: After consent banner shown
66
+ */
67
+ afterBannerShow() {
68
+ // Override in subclass
69
+ }
70
+
71
+ /**
72
+ * Lifecycle: Before consent saved
73
+ * @param {Object} consent - Consent data
74
+ * @returns {Object} Modified consent (or original)
75
+ */
76
+ beforeConsentSave(consent) {
77
+ // Override in subclass
78
+ return consent
79
+ }
80
+
81
+ /**
82
+ * Lifecycle: After consent saved
83
+ * @param {Object} consent - Saved consent data
84
+ */
85
+ afterConsentSave(consent) {
86
+ // Override in subclass
87
+ }
88
+
89
+ /**
90
+ * Event: Consent changed
91
+ * @param {Object} data - { consent, categories }
92
+ */
93
+ onConsentChange(data) {
94
+ // Override in subclass
95
+ }
96
+
97
+ /**
98
+ * Event: Consent accepted
99
+ * @param {Object} data - { consent, categories }
100
+ */
101
+ onConsentAccepted(data) {
102
+ // Override in subclass
103
+ }
104
+
105
+ /**
106
+ * Event: Consent rejected
107
+ * @param {Object} data - { consent, categories }
108
+ */
109
+ onConsentRejected(data) {
110
+ // Override in subclass
111
+ }
112
+
113
+ /**
114
+ * Event: Consent expired
115
+ */
116
+ onConsentExpired() {
117
+ // Override in subclass
118
+ }
119
+
120
+ /**
121
+ * Event: Script loaded
122
+ * @param {Object} script - Script info
123
+ */
124
+ onScriptLoaded(script) {
125
+ // Override in subclass
126
+ }
127
+
128
+ /**
129
+ * Event: Tracker detected
130
+ * @param {Object} tracker - Tracker info
131
+ */
132
+ onTrackerDetected(tracker) {
133
+ // Override in subclass
134
+ }
135
+
136
+ /**
137
+ * Cleanup plugin
138
+ * Called when plugin is unregistered
139
+ */
140
+ destroy() {
141
+ this.isInitialized = false
142
+ this.cmp = null
143
+ this._debug('Plugin destroyed')
144
+ }
145
+
146
+ /**
147
+ * Get CMP API
148
+ * @returns {Object}
149
+ */
150
+ getAPI() {
151
+ return this.cmp?.api || {}
152
+ }
153
+
154
+ /**
155
+ * Get consent
156
+ * @returns {Object|null}
157
+ */
158
+ getConsent() {
159
+ return this.cmp?.getConsent() || null
160
+ }
161
+
162
+ /**
163
+ * Check category consent
164
+ * @param {string} category
165
+ * @returns {boolean}
166
+ */
167
+ hasCategoryConsent(category) {
168
+ return this.cmp?.hasCategoryConsent(category) || false
169
+ }
170
+
171
+ /**
172
+ * Emit custom event
173
+ * @param {string} eventName
174
+ * @param {Object} data
175
+ */
176
+ emit(eventName, data = {}) {
177
+ if (typeof window !== 'undefined') {
178
+ window.dispatchEvent(new CustomEvent(`cmp:plugin:${eventName}`, {
179
+ detail: { plugin: this.name, ...data }
180
+ }))
181
+ }
182
+ }
183
+
184
+ _debug(...args) {
185
+ // Debug disabled
186
+ }
187
+ }