@consentry/core
Version:
Core consent management logic for Consentry SDK — handles preferences, script filtering, and integration with analytics tools.
105 lines (100 loc) • 3.8 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
fallbackDefaults: () => fallbackDefaults,
getAllowedScripts: () => getAllowedScripts,
getConsentPreferences: () => getConsentPreferences,
setConsentPreferences: () => setConsentPreferences,
updateConsentSettings: () => updateConsentSettings
});
module.exports = __toCommonJS(index_exports);
// src/storage.ts
var import_js_cookie = __toESM(require("js-cookie"));
var STORAGE_KEY = "cookiePreferences";
var getConsentPreferences = () => {
if (typeof window === "undefined") return null;
try {
const raw = localStorage.getItem(STORAGE_KEY) || import_js_cookie.default.get(STORAGE_KEY) || "";
if (!raw) return null;
return JSON.parse(raw);
} catch (e) {
console.warn("Failed to parse consent preferences:", e);
return null;
}
};
var setConsentPreferences = (prefs) => {
if (typeof window === "undefined") return;
const json = JSON.stringify(prefs);
try {
localStorage.setItem(STORAGE_KEY, json);
} catch (e) {
console.warn("localStorage set failed, falling back to cookies", e);
import_js_cookie.default.set(STORAGE_KEY, json, { expires: 365 });
}
};
// src/defaults.ts
var fallbackDefaults = {
functional: true,
performance: false,
advertising: false,
social: false
};
// src/updateConsentSettings.ts
var updateConsentSettings = (type, settings) => {
if (typeof window !== "undefined" && typeof window.gtag === "function") {
window.gtag("consent", type, settings);
} else {
console.warn("Google Analytics is not loaded yet.");
}
};
// src/getAllowedScripts.ts
function getAllowedScripts(config, prefs, debug = false) {
return config.scripts.filter((script) => {
const requiresConsent = script.consentRequired ?? true;
const userConsented = prefs[script.category];
const shouldLoad = requiresConsent ? !!userConsented : true;
if (debug || config.debug) {
console.debug(
`[consentry] Script "${script.id}" (${script.vendor || "no vendor"}) - category "${script.category}" \u2192 ${shouldLoad ? "\u2705 ALLOWED" : "\u{1F6AB} BLOCKED"}`
);
}
return shouldLoad;
});
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
fallbackDefaults,
getAllowedScripts,
getConsentPreferences,
setConsentPreferences,
updateConsentSettings
});
//# sourceMappingURL=index.js.map