@consentry/core
Version:
Core consent management logic for Consentry SDK — handles preferences, script filtering, and integration with analytics tools.
1 lines • 4.03 kB
Source Map (JSON)
{"version":3,"sources":["../src/storage.ts","../src/defaults.ts","../src/updateConsentSettings.ts","../src/getAllowedScripts.ts"],"sourcesContent":["import Cookies from \"js-cookie\";\nimport { CookiePreferences } from \"./types\";\n\nconst STORAGE_KEY = \"cookiePreferences\";\n\nexport const getConsentPreferences = (): CookiePreferences | null => {\n if (typeof window === \"undefined\") return null;\n\n try {\n const raw = localStorage.getItem(STORAGE_KEY) || Cookies.get(STORAGE_KEY) || \"\";\n if (!raw) return null;\n return JSON.parse(raw);\n } catch (e) {\n console.warn(\"Failed to parse consent preferences:\", e);\n return null;\n }\n};\n\nexport const setConsentPreferences = (prefs: CookiePreferences) => {\n if (typeof window === \"undefined\") return;\n\n const json = JSON.stringify(prefs);\n\n try {\n localStorage.setItem(STORAGE_KEY, json);\n } catch (e) {\n console.warn(\"localStorage set failed, falling back to cookies\", e);\n Cookies.set(STORAGE_KEY, json, { expires: 365 });\n }\n};\n","import { CookiePreferences } from \"./types\";\n\nexport const fallbackDefaults: CookiePreferences = {\n functional: true,\n performance: false,\n advertising: false,\n social: false,\n};\n","declare global {\n interface Window {\n gtag?: (...args: any[]) => void;\n }\n}\n\nexport interface ConsentSettings {\n analytics_storage?: \"granted\" | \"denied\";\n ad_storage?: \"granted\" | \"denied\";\n ad_user_data?: \"granted\" | \"denied\";\n ad_personalization?: \"granted\" | \"denied\";\n}\n\nexport const updateConsentSettings = (type: \"default\" | \"update\", settings: ConsentSettings) => {\n if (typeof window !== \"undefined\" && typeof window.gtag === \"function\") {\n window.gtag(\"consent\", type, settings);\n } else {\n console.warn(\"Google Analytics is not loaded yet.\");\n }\n};\n","import type { ConsentConfig, ConsentScript, CookiePreferences } from \"./types\";\n\n/**\n * Filters scripts that should be loaded based on current user preferences.\n *\n * @param config - Consent configuration object\n * @param prefs - Current cookie preferences\n * @param debug - Enable logging\n * @returns Filtered array of allowed scripts\n */\nexport function getAllowedScripts(\n config: ConsentConfig,\n prefs: CookiePreferences,\n debug: boolean = false\n): ConsentScript[] {\n return config.scripts.filter(script => {\n const requiresConsent = script.consentRequired ?? true;\n const userConsented = prefs[script.category];\n\n const shouldLoad = requiresConsent ? !!userConsented : true;\n\n if (debug || config.debug) {\n console.debug(\n `[consentry] Script \"${script.id}\" (${script.vendor || \"no vendor\"}) - category \"${script.category}\" → ${\n shouldLoad ? \"✅ ALLOWED\" : \"🚫 BLOCKED\"\n }`\n );\n }\n\n return shouldLoad;\n });\n}\n"],"mappings":";AAAA,OAAO,aAAa;AAGpB,IAAM,cAAc;AAEb,IAAM,wBAAwB,MAAgC;AACnE,MAAI,OAAO,WAAW,YAAa,QAAO;AAE1C,MAAI;AACF,UAAM,MAAM,aAAa,QAAQ,WAAW,KAAK,QAAQ,IAAI,WAAW,KAAK;AAC7E,QAAI,CAAC,IAAK,QAAO;AACjB,WAAO,KAAK,MAAM,GAAG;AAAA,EACvB,SAAS,GAAG;AACV,YAAQ,KAAK,wCAAwC,CAAC;AACtD,WAAO;AAAA,EACT;AACF;AAEO,IAAM,wBAAwB,CAAC,UAA6B;AACjE,MAAI,OAAO,WAAW,YAAa;AAEnC,QAAM,OAAO,KAAK,UAAU,KAAK;AAEjC,MAAI;AACF,iBAAa,QAAQ,aAAa,IAAI;AAAA,EACxC,SAAS,GAAG;AACV,YAAQ,KAAK,oDAAoD,CAAC;AAClE,YAAQ,IAAI,aAAa,MAAM,EAAE,SAAS,IAAI,CAAC;AAAA,EACjD;AACF;;;AC3BO,IAAM,mBAAsC;AAAA,EACjD,YAAY;AAAA,EACZ,aAAa;AAAA,EACb,aAAa;AAAA,EACb,QAAQ;AACV;;;ACMO,IAAM,wBAAwB,CAAC,MAA4B,aAA8B;AAC9F,MAAI,OAAO,WAAW,eAAe,OAAO,OAAO,SAAS,YAAY;AACtE,WAAO,KAAK,WAAW,MAAM,QAAQ;AAAA,EACvC,OAAO;AACL,YAAQ,KA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