UNPKG

@consentry/core

Version:

Core consent management logic for Consentry SDK — handles preferences, script filtering, and integration with analytics tools.

107 lines (101 loc) 3.48 kB
/** * All consent categories supported by the SDK. * Can be extended later if you support TCF or custom categories. */ type ConsentCategory = "functional" | "performance" | "advertising" | "social"; /** * User preferences mapped to each cookie category. */ type CookiePreferences = Record<ConsentCategory, boolean>; /** * Individual script definition for injection and control. */ type ConsentScript = { /** * Unique ID for your script tag (used for hydration and deduplication). */ id: string; /** * Cookie category this script belongs to. * Determines whether the script loads based on consent. */ category: ConsentCategory; /** * If true (or undefined), the script will wait for consent. * If false, it will load regardless of user's consent. */ consentRequired?: boolean; /** * Script loading strategy (from next/script). * Defaults to "afterInteractive". */ strategy?: "afterInteractive" | "lazyOnload" | "beforeInteractive"; /** * External script URL. Mutually exclusive with `content`, unless content includes a <script> tag. */ src?: string; /** * Inline JavaScript OR raw <script> HTML (e.g. what devs copy-paste from vendors). * Will be parsed if it starts with a <script> tag. */ content?: string; /** * Optional <noscript> fallback HTML for non-JS environments. */ noscript?: string; /** * Optional display label for developer tools or UI. */ vendor?: string; /** * Sets whether this script's category should be accepted by default, * before the user has made a choice. Overridden by stored preferences. */ default?: boolean; /** * Optional extra attributes for the <script> tag, e.g. { async: "true", "data-key": "abc" }. */ attributes?: Record<string, string>; }; /** * Overall configuration passed to the Consent Manager. */ type ConsentConfig = { /** * If true, enables detailed console logs. */ debug?: boolean; /** * Fallback default values per category, used before user gives explicit consent. */ defaults: CookiePreferences; /** * All scripts managed by the SDK. */ scripts: ConsentScript[]; }; declare const getConsentPreferences: () => CookiePreferences | null; declare const setConsentPreferences: (prefs: CookiePreferences) => void; declare const fallbackDefaults: CookiePreferences; declare global { interface Window { gtag?: (...args: any[]) => void; } } interface ConsentSettings { analytics_storage?: "granted" | "denied"; ad_storage?: "granted" | "denied"; ad_user_data?: "granted" | "denied"; ad_personalization?: "granted" | "denied"; } declare const updateConsentSettings: (type: "default" | "update", settings: ConsentSettings) => void; /** * Filters scripts that should be loaded based on current user preferences. * * @param config - Consent configuration object * @param prefs - Current cookie preferences * @param debug - Enable logging * @returns Filtered array of allowed scripts */ declare function getAllowedScripts(config: ConsentConfig, prefs: CookiePreferences, debug?: boolean): ConsentScript[]; export { type ConsentCategory, type ConsentConfig, type ConsentScript, type ConsentSettings, type CookiePreferences, fallbackDefaults, getAllowedScripts, getConsentPreferences, setConsentPreferences, updateConsentSettings };