UNPKG

@didomi/iabtcf-cmpapi

Version:

Ensures other in-page digital marketing technologies have access to CMP transparency and consent information for the iab. Transparency and Consent Framework (TCF).

108 lines (107 loc) 4.53 kB
import { CmpApiModel } from '../CmpApiModel.js'; import { Response } from './Response.js'; export class TCData extends Response { tcString; listenerId; eventStatus; cmpStatus; isServiceSpecific; useNonStandardTexts; publisherCC; purposeOneTreatment; outOfBand; purpose; vendor; specialFeatureOptins; publisher; /** * Constructor to create a TCData object from a TCModel * @param {number[]} vendorIds - if not undefined, will be used to filter vendor ids * @param {number} listenerId - if there is a listenerId to add */ constructor(vendorIds, listenerId) { super(); this.eventStatus = CmpApiModel.eventStatus; this.cmpStatus = CmpApiModel.cmpStatus; this.listenerId = listenerId; const bucket = CmpApiModel.restrictionsCache.getBucket(this.constructor.name); if (CmpApiModel.gdprApplies) { const tcModel = CmpApiModel.tcModel; this.tcString = CmpApiModel.tcString; this.isServiceSpecific = tcModel.isServiceSpecific; this.useNonStandardTexts = tcModel.useNonStandardTexts; this.purposeOneTreatment = tcModel.purposeOneTreatment; this.publisherCC = tcModel.publisherCountryCode; const restrictions = bucket.get(this.tcString, this.createRestrictions.bind(this), tcModel.publisherRestrictions); if (this.isServiceSpecific === false) { this.outOfBand = { allowedVendors: this.createVectorField(tcModel.vendorsAllowed, vendorIds), disclosedVendors: this.createVectorField(tcModel.vendorsDisclosed, vendorIds), }; } this.purpose = { consents: this.createVectorField(tcModel.purposeConsents), legitimateInterests: this.createVectorField(tcModel.purposeLegitimateInterests), }; this.vendor = { consents: this.createVectorField(tcModel.vendorConsents, vendorIds), legitimateInterests: this.createVectorField(tcModel.vendorLegitimateInterests, vendorIds), disclosedVendors: this.createVectorField(tcModel.vendorsDisclosed, vendorIds), }; this.specialFeatureOptins = this.createVectorField(tcModel.specialFeatureOptins); this.publisher = { consents: this.createVectorField(tcModel.publisherConsents), legitimateInterests: this.createVectorField(tcModel.publisherLegitimateInterests), customPurpose: { consents: this.createVectorField(tcModel.publisherCustomConsents), legitimateInterests: this.createVectorField(tcModel.publisherCustomLegitimateInterests), }, restrictions, }; } } /** * Creates a restrictions object given a PurposeRestrictionVector * @param {PurposeRestrictionVector} purpRestrictions * @return {Restrictions} */ createRestrictions(purpRestrictions) { const retr = {}; if (purpRestrictions.numRestrictions > 0) { const max = purpRestrictions.getMaxVendorId(); for (let vendorId = 1; vendorId <= max; vendorId++) { const strVendorId = vendorId.toString(); // vendors restrictions purpRestrictions.getRestrictions(vendorId).forEach((pRestrict) => { const strPurpId = pRestrict.purposeId.toString(); if (!retr[strPurpId]) { retr[strPurpId] = {}; } retr[strPurpId][strVendorId] = pRestrict.restrictionType; }); } } return retr; } ; /** * Creates a string bit field with a value for each id where each value is * '1' if its id is in the passed in vector Can be overwritten to return a * string * @param {Vector} vector * @param {number[]} ids filter * @return {BooleanVector | string} */ createVectorField(vector, ids) { if (ids) { return ids.reduce((booleanVector, obj) => { booleanVector[String(obj)] = vector.has(Number(obj)); return booleanVector; }, {}); } return [...vector].reduce((booleanVector, keys) => { booleanVector[keys[0].toString(10)] = keys[1]; return booleanVector; }, {}); } }