@kameleoon/javascript-sdk-core
Advanced tools
Comparing version 5.2.2 to 5.2.3
# Change Log | ||
## 5.2.3 (2024-12-25) | ||
### Patch Changes | ||
- Enhanced the performance of the [`setLegalConsent()`][setLegalConsent] method. | ||
- Resolved an issue that could lead to uncontrolled memory growth in specific scenarios. | ||
[setLegalConsent]: https://developers.kameleoon.com/feature-management-and-experimentation/web-sdks/js-sdk#setlegalconsent | ||
## 5.2.2 (2024-12-19) | ||
@@ -4,0 +13,0 @@ |
@@ -52,2 +52,3 @@ import { JSONType } from './clientConfiguration'; | ||
protected setUserConsent({ visitorCode, consent, setData, }: SetUserConsentParametersType): void; | ||
private updateConsentData; | ||
private _isConsentProvided; | ||
@@ -54,0 +55,0 @@ private _getFeatureVariables; |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.KameleoonClient=void 0;var _tsRes=require("ts-res"),_cacheManager=require("./cacheManager"),_clientConfiguration=require("./clientConfiguration"),_clientSettings=require("./clientSettings"),_constants=require("./constants"),_kameleoonData=require("./kameleoonData"),_kameleoonError=require("./kameleoonError"),_requester=require("./requester"),_storage=require("./storage"),_types=require("./types"),_utilities=require("./utilities"),_variationConfiguration=require("./variationConfiguration"),_eventSource=require("./eventSource"),_visitorCodeManager=require("./visitorCodeManager"),_eventManager=require("./eventManager"),_tracking=require("./tracking"),_logging=require("./logging");function _defineProperty(a,b,c){return(b=_toPropertyKey(b))in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}function _toPropertyKey(a){var b=_toPrimitive(a,"string");return"symbol"==typeof b?b:b+""}function _toPrimitive(a,b){if("object"!=typeof a||!a)return a;var c=a[Symbol.toPrimitive];if(void 0!==c){var d=c.call(a,b||"default");if("object"!=typeof d)return d;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===b?String:Number)(a)}class KameleoonClient{constructor(a){var b;let{siteCode:c,dependencies:d,configuration:e,internalConfiguration:f}=a;_defineProperty(this,"variationConfiguration",void 0),_defineProperty(this,"clientConfiguration",void 0),_defineProperty(this,"dataManager",void 0),_defineProperty(this,"consentDataStorage",void 0),_defineProperty(this,"trackingCache",void 0),_defineProperty(this,"requester",void 0),_defineProperty(this,"externalPackageInfo",void 0),_defineProperty(this,"initialized",!1),_defineProperty(this,"visitorCodeManager",void 0),_defineProperty(this,"eventManager",void 0),_defineProperty(this,"tracker",void 0),_defineProperty(this,"environment",void 0);const g=null===f||void 0===f?void 0:f.externals.externalLogger;g&&_logging.KameleoonLogger.setLogger(g),_logging.KameleoonLogger.debug`CALL: new KameleoonClient(siteCode: ${c}, dependencies: ${d}, configuration: ${e})`;const{settings:h}=new _clientSettings.ClientSettings(c,e);this.environment=h.environment;const{externalStorage:i,externalEventSource:j,externalPackageInfo:k,externalVisitorCodeManager:l,externalRequester:m,externalPRNG:n}=f.externals,{useAbortController:o}=f.settings,p=new _cacheManager.CacheManager(_constants.CACHE_CLEANUP_TIMEOUT),q=new _storage.DataStorage(i,_storage.KameleoonStorageKey.ClientData),r=new _storage.DataStorage(i,_storage.KameleoonStorageKey.TargetingData),s=new _storage.DataStorage(i,_storage.KameleoonStorageKey.DataInfo),t=new _storage.DataStorage(i,_storage.KameleoonStorageKey.VariationData),u=new _storage.DataStorage(i,_storage.KameleoonStorageKey.ForcedExperimentVariation),v=new _storage.DataStorage(i,_storage.KameleoonStorageKey.ForcedFeatureVariation),w=new _storage.DataStorage(i,_storage.KameleoonStorageKey.ConsentData),x=new _storage.DataStorage(i,_storage.KameleoonStorageKey.TrackingData),y=new _kameleoonData.DataManager({dataStorage:r,infoStorage:s,cleanupInterval:h.cleanupInterval}),z=null!==(b=null===d||void 0===d?void 0:d.urlProvider)&&void 0!==b?b:new _requester.UrlProvider;z.initialize({domain:h.networkDomain,siteCode:h.siteCode,packageInfo:k,environment:h.environment});const A=new _requester.Requester({urlProvider:z,useAbortController:o,packageInfo:k,externalRequester:m,requestTimeout:h.requestTimeout,trackRetryDelay:h.trackRetryDelay}),B=new _eventSource.KameleoonEventSource({siteCode:h.siteCode,externalEventSource:j,urlProvider:z}),C=new _eventManager.EventManager,D=new _clientConfiguration.ClientConfiguration({urlProvider:z,storage:q,updateInterval:h.updateInterval,dataManager:y,eventManager:C,requester:A,eventSource:B,externalVisitorCodeManager:l}),E=new _variationConfiguration.VariationConfiguration(t,u,v,l,D),F=new _tracking.Tracker({dataManager:y,trackingStorage:x,variationConfiguration:E,requester:A,trackingInterval:h.trackingInterval,prng:n});this.tracker=F,this.variationConfiguration=E,this.requester=A,this.trackingCache=p,this.dataManager=y,this.clientConfiguration=D,this.externalPackageInfo=k,this.consentDataStorage=w,this.visitorCodeManager=l,this.eventManager=C,_logging.KameleoonLogger.debug`RETURN: new KameleoonClient(siteCode: ${c}, dependencies: ${d}, configuration: ${e})`}async initialize(){_logging.KameleoonLogger.info("CALL: KameleoonClient.initialize()");const a=await this.clientConfiguration.initialize();return a.throw(),this.initialized=!0,_logging.KameleoonLogger.info`RETURN: KameleoonClient.initialize() -> (result: ${a.ok})`,a.ok}addData(a){for(var b=arguments.length,c=Array(1<b?b-1:0),d=1;d<b;d++)c[d-1]=arguments[d];if(_logging.KameleoonLogger.info`CALL: KameleoonClient.addData(visitorCode: ${a}, data: ${c})`,this.visitorCodeManager.validateVisitorCode(a).throw(),!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);this.dataManager.storeData(a,...c).throw(),_logging.KameleoonLogger.info`RETURN: KameleoonClient.addData(visitorCode: ${a}, data: ${c})`}async getRemoteVisitorData(a){let{visitorCode:b,shouldAddData:c=!0,filters:d=_constants.DEFAULT_VISITOR_DATA_FILTERS}=a;if(_logging.KameleoonLogger.info`CALL: KameleoonClient.getRemoteVisitorData(visitorCode: ${b}, shouldAddData: ${c}, filters: ${d})`,!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);this.visitorCodeManager.validateVisitorCode(b).throw();const{previousVisitAmount:e}=d;if("number"==typeof e&&(1>e||25<e))throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.VisitAmount);const f=this.dataManager.isUniqueIdentifier(b),g=await this.requester.getVisitorData({visitorCode:b,filters:d,isMappingIdentifier:f}),h=g.throw(),{visitorData:i,storageVisitorData:j,visitsData:k}=_utilities.Parser.parseVisitorData({data:h,filters:d,visitorCode:b,dataManager:this.dataManager,variationConfiguration:this.variationConfiguration});for(const d of j)(c||this.dataManager.isPersistentCustomData(d))&&this.addData(b,d);return k&&c&&this.addData(b,k),_logging.KameleoonLogger.info`RETURN: KameleoonClient.getRemoteVisitorData(visitorCode: ${b}, shouldAddData: ${c}, filters: ${d}) -> (visitorData: ${i})`,i}trackConversion(a){let{visitorCode:b,goalId:c,revenue:d}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.trackConversion(visitorCode: ${b}, goalId: ${c}, revenue: ${d})`,this.addData(b,new _kameleoonData.Conversion({goalId:c,revenue:d})),this.flush(b),_logging.KameleoonLogger.info`RETURN: KameleoonClient.trackConversion(visitorCode: ${b}, goalId: ${c}, revenue: ${d})`}flush(a){if(_logging.KameleoonLogger.info`CALL: KameleoonClient.flush(visitorCode: ${a})`,!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);if("string"==typeof a)return this.visitorCodeManager.validateVisitorCode(a).throw(),this.tracker.scheduleVisitor(a,this._isConsentProvided(a)),void _logging.KameleoonLogger.info`RETURN: KameleoonClient.flush(visitorCode: ${a})`;for(const b of this.dataManager.unsentDataVisitors)this.tracker.scheduleVisitor(b,this._isConsentProvided(b));_logging.KameleoonLogger.info`RETURN: KameleoonClient.flush(visitorCode: ${a})`}getFeatureFlags(){if(_logging.KameleoonLogger.info("CALL: KameleoonClient.getFeatureFlags()"),!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);const a=this.clientConfiguration.featureFlags,b=[];return a.forEach(a=>{let{id:c,featureKey:d}=a;b.push({id:c,key:d})}),_logging.KameleoonLogger.info`RETURN: KameleoonClient.getFeatureFlags() -> (FeatureFlags: ${b})`,b}getVisitorFeatureFlags(a){if(_logging.KameleoonLogger.info`CALL: KameleoonClient.getVisitorFeatureFlags(visitorCode: ${a})`,!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);this.visitorCodeManager.validateVisitorCode(a).throw();const b=this._getActiveFeatureVariations(a),c=[];return b.forEach(a=>{let{featureFlagId:b,featureKey:d}=a;c.push({id:b,key:d})}),_logging.KameleoonLogger.info`RETURN: KameleoonClient.getVisitorFeatureFlags(visitorCode: ${a}) -> (FeatureFlags: ${c})`,c}getVariation(a){let{visitorCode:b,featureKey:c,track:d=!0}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.getVariation(visitorCode: ${b}, featureKey: ${c}, track: ${d})`,this.visitorCodeManager.validateVisitorCode(b).throw();const e=this._getFeatureVariation({visitorCode:b,featureKey:c,track:d}).throw();return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getVariation(visitorCode: ${b}, featureKey: ${c}, track: ${d}) -> (variation: ${e})`,e}getVariations(a){let{visitorCode:b,onlyActive:c=!1,track:d=!0}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.getVariations(visitorCode: ${b}, onlyActive: ${c}, track: ${d})`,this.visitorCodeManager.validateVisitorCode(b).throw();const e=new Map,f=this.clientConfiguration.featureFlags;for(const g of f.values()){const a=this._getFeatureVariation({visitorCode:b,featureKey:g.featureKey,track:d});a.ok&&(!c||a.data.key!==_constants.OFF_VARIATION_KEY)&&e.set(g.featureKey,a.data)}return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getVariations(visitorCode: ${b}, onlyActive: ${c}, track: ${d}) -> (variations: ${e})`,e}getActiveFeatureFlags(a){if(_logging.KameleoonLogger.info`CALL: KameleoonClient.getActiveFeatureFlags(visitorCode: ${a})`,!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);this.visitorCodeManager.validateVisitorCode(a).throw();const b=this._getActiveFeatureVariations(a),c=new Map;return b.forEach(b=>{const{variationKey:d,featureKey:e,variationId:f,experimentId:g}=b;let h=[];null!==f&&null!==g&&(h=this._getFeatureVariables({visitorCode:a,featureKey:e,variationKey:d}));const i=h.map(a=>_utilities.Parser.parseFeatureVariable(a).throw());c.set(b.featureKey,{key:d,id:f,experimentId:g,variables:i})}),_logging.KameleoonLogger.info`RETURN: KameleoonClient.getActiveFeatureFlags(visitorCode: ${a}) -> (FeatureFlags: ${c})`,c}getFeatureFlagVariationKey(a,b){_logging.KameleoonLogger.info`CALL: KameleoonClient.getFeatureFlagVariationKey(visitorCode: ${a}, featureKey: ${b})`,this.visitorCodeManager.validateVisitorCode(a).throw();const{key:c}=this._getFeatureVariation({visitorCode:a,featureKey:b,track:!0}).throw();return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getFeatureFlagVariationKey(visitorCode: ${a}, featureKey: ${b}) -> (variationKey: ${c})`,c}getFeatureFlagVariable(a){let{visitorCode:b,featureKey:c,variableKey:d}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.getFeatureFlagVariable(visitorCode: ${b}, featureKey: ${c}, variableKey: ${d})`,this.visitorCodeManager.validateVisitorCode(b).throw();const{key:e}=this._getFeatureVariation({visitorCode:b,featureKey:c,track:!0}).throw(),f=this._getFeatureVariables({visitorCode:b,featureKey:c,variationKey:e}),g=f.find(a=>a.key===d);if(!g)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagVariableNotFound,d,b);const{type:h,value:i}=_utilities.Parser.parseFeatureVariable(g).throw(),j={type:h,value:i};return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getFeatureFlagVariable(visitorCode: ${b}, featureKey: ${c}, variableKey: ${d}) -> (FeatureFlagVariable: ${j})`,j}getFeatureFlagVariables(a,b){_logging.KameleoonLogger.info`CALL: KameleoonClient.getFeatureFlagVariables(visitorCode: ${a}, featureKey: ${b})`,this.visitorCodeManager.validateVisitorCode(a).throw();const{key:c}=this._getFeatureVariation({visitorCode:a,featureKey:b,track:!0}).throw(),d=this._getFeatureVariables({visitorCode:a,featureKey:b,variationKey:c}),e=[];for(const c of d){const a=_utilities.Parser.parseFeatureVariable(c).throw();e.push(a)}return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getFeatureFlagVariables(visitorCode: ${a}, featureKey: ${b}) -> (FeatureFlagVariables: ${e})`,e}isFeatureFlagActive(a,b){let[c,d,e]=["","",!0];"string"==typeof a?(_logging.KameleoonLogger.info`CALL: KameleoonClient.isFeatureFlagActive(visitorCode: ${c}, featureKey: ${d})`,c=a,d=b):(_logging.KameleoonLogger.info`CALL: KameleoonClient.isFeatureFlagActive(visitorCode: ${c}, featureKey: ${d}, track ${e})`,[c,d]=[a.visitorCode,a.featureKey],a.track!==void 0&&(e=a.track)),this.visitorCodeManager.validateVisitorCode(c).throw();try{const{key:a}=this._getFeatureVariation({visitorCode:c,featureKey:d,track:e}).throw(),b=a!==_constants.OFF_VARIATION_KEY;return _logging.KameleoonLogger.info`RETURN: KameleoonClient.isFeatureFlagActive(visitorCode: ${c}, featureKey: ${d}, track: ${e}) -> (isActive: ${b})`,b}catch(a){if(a instanceof _kameleoonError.KameleoonError&&a.type===_kameleoonError.KameleoonException.FeatureFlagEnvironmentDisabled)return _logging.KameleoonLogger.info`RETURN: KameleoonClient.isFeatureFlagActive(visitorCode: ${c}, featureKey: ${d}, track: ${e}) -> (isActive: false)`,!1;throw a}}async getRemoteData(a){_logging.KameleoonLogger.info`CALL: KameleoonClient.getRemoteData(key: ${a})`;const b=await this.requester.getRemoteData(a);return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getRemoteData(key: ${a}) -> (remoteData: ${b})`,b.throw()}async getVisitorWarehouseAudience(a){let{visitorCode:b,customDataIndex:c,warehouseKey:d}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.getVisitorWarehouseAudience(visitorCode: ${b}, customDataIndex: ${c}, warehouseKey: ${d})`,this.visitorCodeManager.validateVisitorCode(b).throw();const e=await this.getRemoteData(d||b);if(!e)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.RemoteData,`No data found for ${d||b} key`);if(!e[_constants.WAREHOUSE_AUDIENCE_KEY]||0===Object.keys(e[_constants.WAREHOUSE_AUDIENCE_KEY]).length)return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getVisitorWarehouseAudience(visitorCode: ${b}, customDataIndex: ${c}, warehouseKey: ${d}) -> (customData: null)`,null;const f=new _kameleoonData.CustomData(c,...Object.keys(e[_constants.WAREHOUSE_AUDIENCE_KEY]));return this.dataManager.storeData(b,f),_logging.KameleoonLogger.info`RETURN: KameleoonClient.getVisitorWarehouseAudience(visitorCode: ${b}, customDataIndex: ${c}, warehouseKey: ${d}) -> (customData: ${f})`,f}onEvent(a,b){if(_logging.KameleoonLogger.info`CALL: KameleoonClient.onEvent(event: ${a}, callback)`,!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);this.eventManager.addEventHandler(a,b),_logging.KameleoonLogger.info`RETURN: KameleoonClient.onEvent(event: ${a}, callback)`}getEngineTrackingCode(a){_logging.KameleoonLogger.info`CALL: KameleoonClient.getEngineTrackingCode(visitorCode: ${a})`,this.visitorCodeManager.validateVisitorCode(a).throw();const b=_utilities.Utilities.getTrackingCode(this.trackingCache,a);return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getEngineTrackingCode(visitorCode: ${a}) -> (trackingCode: ${b})`,b}isInitialized(){return _logging.KameleoonLogger.debug`CALL/RETURN: KameleoonClient.isInitialized() -> (initialized: ${this.initialized})`,this.initialized}setLogLevel(a){_logging.KameleoonLogger.setLogLevel(a)}setForcedVariation(a){let{visitorCode:b,experimentId:c,variationKey:d,forceTargeting:e=!0}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.setForcedVariation(visitorCode: '${b}', experimentId: ${c}, variationKey: '${d}', forceTargeting: ${e})`,this.visitorCodeManager.validateVisitorCode(b).throw(),this._setForcedVariation({visitorCode:b,experimentId:c,variationKey:d,forceTargeting:e}).throw(),_logging.KameleoonLogger.info`RETURN: KameleoonClient.setForcedVariation(visitorCode: '${b}', experimentId: ${c}, variationKey: '${d}', forceTargeting: ${e})`}setUserConsent(a){let{visitorCode:b,consent:c,setData:d}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.setUserConsent(visitorCode: ${b}, consent: ${c}, setData: ${d})`,this.visitorCodeManager.validateVisitorCode(b).throw(),this.consentDataStorage.update({[b]:c}),c?d({visitorCode:b,key:_storage.KameleoonStorageKey.VisitorCode,maxAge:_visitorCodeManager.DEFAULT_MAX_AGE,path:_visitorCodeManager.PATH}):this.visitorCodeManager.consentRequired&&d({visitorCode:"",key:_storage.KameleoonStorageKey.VisitorCode,maxAge:_visitorCodeManager.ZERO_MAX_AGE,path:_visitorCodeManager.PATH}),_logging.KameleoonLogger.info`RETURN: KameleoonClient.setUserConsent(visitorCode: ${b}, consent: ${c}, setData: ${d})`}_isConsentProvided(a){_logging.KameleoonLogger.debug`CALL: KameleoonClient._isConsentProvided(visitorCode: ${a})`;const{isConsentRequired:b}=this.clientConfiguration,c=this.consentDataStorage.read(),d=!b||c.ok&&c.data[a];return _logging.KameleoonLogger.debug`RETURN: KameleoonClient._isConsentProvided(visitorCode: ${a}) -> (isConsentProvided: ${d})`,d}_getFeatureVariables(a){let{visitorCode:b,featureKey:c,variationKey:d}=a;_logging.KameleoonLogger.debug`CALL: KameleoonClient._getFeatureVariables(visitorCode: ${b}, featureKey: ${c}, variationKey: ${d})`;const e=this.clientConfiguration.featureFlags,f=e.get(c);if(!f)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagConfigurationNotFound,c);const g=f.variations.find(a=>a.key===d);if(!g)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagVariationNotFound,d,b);return _logging.KameleoonLogger.debug`RETURN: KameleoonClient._getFeatureVariables(visitorCode: ${b}, featureKey: ${c}, variationKey: ${d}) -> (variables: ${g.variables})`,g.variables}_getActiveFeatureVariations(a){_logging.KameleoonLogger.debug`CALL: KameleoonClient._getActiveFeatureVariations(visitorCode: ${a})`;const b=this.clientConfiguration.featureFlags,c=[],d=this.dataManager.getVisitorData(a),e=this.dataManager.getVisitorIdentifier(a);for(const f of b.values()){if(!f.environmentEnabled)continue;const b=this.variationConfiguration.getVariation({visitorCode:a,visitorIdentifier:e,targetingData:d,featureFlag:f,clientConfiguration:this.clientConfiguration,dataManager:this.dataManager,packageInfo:this.externalPackageInfo}).throw();this.eventManager.fireEvent(_eventManager.EventType.Evaluation,{featureKey:f.featureKey,variation:{key:b.variationKey,experimentId:b.experimentId,id:b.variationId}}),b.variationKey!==_constants.OFF_VARIATION_KEY&&c.push(b)}return _logging.KameleoonLogger.debug`RETURN: KameleoonClient._getActiveFeatureVariations(visitorCode: ${a}) -> (activeVariations: ${c})`,c}_getFeatureVariation(a){let{visitorCode:b,featureKey:c,track:d}=a;if(_logging.KameleoonLogger.debug`CALL: KameleoonClient._getFeatureVariation(visitorCode: ${b}, featureKey: ${c}, track: ${d})`,!this.initialized)return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization));const e=this.clientConfiguration.featureFlags,f=e.get(c);if(!f)return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagConfigurationNotFound,c));if(!f.environmentEnabled)return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagEnvironmentDisabled,f.featureKey,this.environment));let g,h=!1,i=!1;const j=this.variationConfiguration.getForcedFeatureVariation(b,c);if(j.ok){const a=j.data;if(a){var k,l;_logging.KameleoonLogger.debug`Forced variation for visitor ${b} and feature ${c} is found: ${a}`,h=!0,i=!0;let d=f.defaultVariationKey;a.varByExp?d=a.varByExp.variationKey:a.rule&&a.rule.type===_clientConfiguration.RuleType.EXPERIMENTATION&&(d=_constants.OFF_VARIATION_KEY),g={variationKey:d,variationId:(null===(k=a.varByExp)||void 0===k?void 0:k.variationId)||null,experimentId:(null===(l=a.rule)||void 0===l?void 0:l.experimentId)||null,rule:a.rule||null}}}if(!g){const a=this.dataManager.getVisitorData(b),c=this.dataManager.getVisitorIdentifier(b);g=this.variationConfiguration.getVariation({visitorCode:b,visitorIdentifier:c,featureFlag:f,track:d,withAssignment:!0,targetingData:a,clientConfiguration:this.clientConfiguration,dataManager:this.dataManager,packageInfo:this.externalPackageInfo}).throw()}h||(i&&null!==g.experimentId&&null!==g.variationId&&this.variationConfiguration.updateStoredVariations(b,{[g.experimentId]:{variationId:g.variationId,isTargetedRule:g.rule.type===_clientConfiguration.RuleType.TARGETED_DELIVERY,status:d?_types.TrackingStatus.Unsent:_types.TrackingStatus.Sent}}),this.eventManager.fireEvent(_eventManager.EventType.Evaluation,{featureKey:c,variation:{key:g.variationKey,experimentId:g.experimentId,id:g.variationId}}));const{experimentId:m,variationId:n,variationKey:o}=g;let p=new Map,q=!1;try{const a=this._getFeatureVariables({visitorCode:b,featureKey:c,variationKey:o});a.forEach(a=>{q||a.type!==_types.VariableType.JS&&a.type!==_types.VariableType.CSS||(q=!0);const b=_utilities.Parser.parseFeatureVariable(a).throw();p.set(a.key,b)})}catch(_){}m&&"number"==typeof n&&!h&&_utilities.Utilities.updateCache({cacheManager:this.trackingCache,hasJsCssVariables:q,visitorCode:b,experimentId:m,variationId:n}),d&&!h&&this.tracker.scheduleVisitor(b,this._isConsentProvided(b));return _logging.KameleoonLogger.debug`RETURN: KameleoonClient._getFeatureVariation(visitorCode: ${b}, featureKey: ${c}, track: ${d}) -> (variation: ${g})`,(0,_tsRes.Ok)({key:o,id:n,experimentId:m,variables:p})}_setForcedVariation(a){let{visitorCode:b,experimentId:c,variationKey:d,forceTargeting:e=!0}=a;if(_logging.KameleoonLogger.debug`CALL: KameleoonClient._setForcedVariation(visitorCode: '${b}', experimentId: ${c}, variationKey: '${d}', forceTargeting: ${e})`,null===d){const a=this.variationConfiguration.updateForcedExperimentVariation(b,c,null);return _logging.KameleoonLogger.debug`RETURN: KameleoonClient._setForcedVariation(visitorCode: '${b}', experimentId: ${c}, variationKey: '${d}', forceTargeting: ${e}) -> (writeResult: ${a})`,a}let f=null,g=null;if(f=this.clientConfiguration.experimentMap.get(c),!f)return _logging.KameleoonLogger.error`Experiment with id ${c} not found for visitor ${b}`,(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagExperimentNotFound,c,b));if(g=f.variations.get(d),void 0===g)return _logging.KameleoonLogger.error`Variation with key ${d} not found in experiment ${f} for visitor ${b}`,(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagVariationNotFound,d,b));f={type:f.type,experimentId:c},g={variationKey:d,variationId:g};const h=this.variationConfiguration.updateForcedExperimentVariation(b,c,{rule:f,varByExp:g,forceTargeting:e});return _logging.KameleoonLogger.debug`RETURN: KameleoonClient._setForcedVariation(visitorCode: '${b}', experimentId: ${c}, variationKey: '${d}', forceTargeting: ${e}) -> (writeResult: ${h})`,h}}exports.KameleoonClient=KameleoonClient; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.KameleoonClient=void 0;var _tsRes=require("ts-res"),_cacheManager=require("./cacheManager"),_clientConfiguration=require("./clientConfiguration"),_clientSettings=require("./clientSettings"),_constants=require("./constants"),_kameleoonData=require("./kameleoonData"),_kameleoonError=require("./kameleoonError"),_requester=require("./requester"),_storage=require("./storage"),_types=require("./types"),_utilities=require("./utilities"),_variationConfiguration=require("./variationConfiguration"),_eventSource=require("./eventSource"),_visitorCodeManager=require("./visitorCodeManager"),_eventManager=require("./eventManager"),_tracking=require("./tracking"),_logging=require("./logging");function _defineProperty(a,b,c){return(b=_toPropertyKey(b))in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}function _toPropertyKey(a){var b=_toPrimitive(a,"string");return"symbol"==typeof b?b:b+""}function _toPrimitive(a,b){if("object"!=typeof a||!a)return a;var c=a[Symbol.toPrimitive];if(void 0!==c){var d=c.call(a,b||"default");if("object"!=typeof d)return d;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===b?String:Number)(a)}class KameleoonClient{constructor(a){var b;let{siteCode:c,dependencies:d,configuration:e,internalConfiguration:f}=a;_defineProperty(this,"variationConfiguration",void 0),_defineProperty(this,"clientConfiguration",void 0),_defineProperty(this,"dataManager",void 0),_defineProperty(this,"consentDataStorage",void 0),_defineProperty(this,"trackingCache",void 0),_defineProperty(this,"requester",void 0),_defineProperty(this,"externalPackageInfo",void 0),_defineProperty(this,"initialized",!1),_defineProperty(this,"visitorCodeManager",void 0),_defineProperty(this,"eventManager",void 0),_defineProperty(this,"tracker",void 0),_defineProperty(this,"environment",void 0);const g=null===f||void 0===f?void 0:f.externals.externalLogger;g&&_logging.KameleoonLogger.setLogger(g),_logging.KameleoonLogger.debug`CALL: new KameleoonClient(siteCode: ${c}, dependencies: ${d}, configuration: ${e})`;const{settings:h}=new _clientSettings.ClientSettings(c,e);this.environment=h.environment;const{externalStorage:i,externalEventSource:j,externalPackageInfo:k,externalVisitorCodeManager:l,externalRequester:m,externalPRNG:n}=f.externals,{useAbortController:o}=f.settings,p=new _cacheManager.CacheManager(_constants.CACHE_CLEANUP_TIMEOUT),q=new _storage.DataStorage(i,_storage.KameleoonStorageKey.ClientData),r=new _storage.DataStorage(i,_storage.KameleoonStorageKey.TargetingData),s=new _storage.DataStorage(i,_storage.KameleoonStorageKey.DataInfo),t=new _storage.DataStorage(i,_storage.KameleoonStorageKey.VariationData),u=new _storage.DataStorage(i,_storage.KameleoonStorageKey.ForcedExperimentVariation),v=new _storage.DataStorage(i,_storage.KameleoonStorageKey.ForcedFeatureVariation),w=new _storage.DataStorage(i,_storage.KameleoonStorageKey.ConsentData),x=new _storage.DataStorage(i,_storage.KameleoonStorageKey.TrackingData),y=new _kameleoonData.DataManager({dataStorage:r,infoStorage:s,cleanupInterval:h.cleanupInterval}),z=null!==(b=null===d||void 0===d?void 0:d.urlProvider)&&void 0!==b?b:new _requester.UrlProvider;z.initialize({domain:h.networkDomain,siteCode:h.siteCode,packageInfo:k,environment:h.environment});const A=new _requester.Requester({urlProvider:z,useAbortController:o,packageInfo:k,externalRequester:m,requestTimeout:h.requestTimeout,trackRetryDelay:h.trackRetryDelay}),B=new _eventSource.KameleoonEventSource({siteCode:h.siteCode,externalEventSource:j,urlProvider:z}),C=new _eventManager.EventManager,D=new _clientConfiguration.ClientConfiguration({urlProvider:z,storage:q,updateInterval:h.updateInterval,dataManager:y,eventManager:C,requester:A,eventSource:B,externalVisitorCodeManager:l}),E=new _variationConfiguration.VariationConfiguration(t,u,v,l,D),F=new _tracking.Tracker({dataManager:y,trackingStorage:x,variationConfiguration:E,requester:A,trackingInterval:h.trackingInterval,prng:n});this.tracker=F,this.variationConfiguration=E,this.requester=A,this.trackingCache=p,this.dataManager=y,this.clientConfiguration=D,this.externalPackageInfo=k,this.consentDataStorage=w,this.visitorCodeManager=l,this.eventManager=C,null!==h.cleanupInterval&&new _storage.StorageCleanupManager(i,h.cleanupInterval),_logging.KameleoonLogger.debug`RETURN: new KameleoonClient(siteCode: ${c}, dependencies: ${d}, configuration: ${e})`}async initialize(){_logging.KameleoonLogger.info("CALL: KameleoonClient.initialize()");const a=await this.clientConfiguration.initialize();return a.throw(),this.initialized=!0,_logging.KameleoonLogger.info`RETURN: KameleoonClient.initialize() -> (result: ${a.ok})`,a.ok}addData(a){for(var b=arguments.length,c=Array(1<b?b-1:0),d=1;d<b;d++)c[d-1]=arguments[d];if(_logging.KameleoonLogger.info`CALL: KameleoonClient.addData(visitorCode: ${a}, data: ${c})`,this.visitorCodeManager.validateVisitorCode(a).throw(),!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);this.dataManager.storeData(a,...c).throw(),_logging.KameleoonLogger.info`RETURN: KameleoonClient.addData(visitorCode: ${a}, data: ${c})`}async getRemoteVisitorData(a){let{visitorCode:b,shouldAddData:c=!0,filters:d=_constants.DEFAULT_VISITOR_DATA_FILTERS}=a;if(_logging.KameleoonLogger.info`CALL: KameleoonClient.getRemoteVisitorData(visitorCode: ${b}, shouldAddData: ${c}, filters: ${d})`,!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);this.visitorCodeManager.validateVisitorCode(b).throw();const{previousVisitAmount:e}=d;if("number"==typeof e&&(1>e||25<e))throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.VisitAmount);const f=this.dataManager.isUniqueIdentifier(b),g=await this.requester.getVisitorData({visitorCode:b,filters:d,isMappingIdentifier:f}),h=g.throw(),{visitorData:i,storageVisitorData:j,visitsData:k}=_utilities.Parser.parseVisitorData({data:h,filters:d,visitorCode:b,dataManager:this.dataManager,variationConfiguration:this.variationConfiguration});for(const d of j)(c||this.dataManager.isPersistentCustomData(d))&&this.addData(b,d);return k&&c&&this.addData(b,k),_logging.KameleoonLogger.info`RETURN: KameleoonClient.getRemoteVisitorData(visitorCode: ${b}, shouldAddData: ${c}, filters: ${d}) -> (visitorData: ${i})`,i}trackConversion(a){let{visitorCode:b,goalId:c,revenue:d}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.trackConversion(visitorCode: ${b}, goalId: ${c}, revenue: ${d})`,this.addData(b,new _kameleoonData.Conversion({goalId:c,revenue:d})),this.flush(b),_logging.KameleoonLogger.info`RETURN: KameleoonClient.trackConversion(visitorCode: ${b}, goalId: ${c}, revenue: ${d})`}flush(a){if(_logging.KameleoonLogger.info`CALL: KameleoonClient.flush(visitorCode: ${a})`,!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);if("string"==typeof a)return this.visitorCodeManager.validateVisitorCode(a).throw(),this.tracker.scheduleVisitor(a,this._isConsentProvided(a)),void _logging.KameleoonLogger.info`RETURN: KameleoonClient.flush(visitorCode: ${a})`;for(const b of this.dataManager.unsentDataVisitors)this.tracker.scheduleVisitor(b,this._isConsentProvided(b));_logging.KameleoonLogger.info`RETURN: KameleoonClient.flush(visitorCode: ${a})`}getFeatureFlags(){if(_logging.KameleoonLogger.info("CALL: KameleoonClient.getFeatureFlags()"),!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);const a=this.clientConfiguration.featureFlags,b=[];return a.forEach(a=>{let{id:c,featureKey:d}=a;b.push({id:c,key:d})}),_logging.KameleoonLogger.info`RETURN: KameleoonClient.getFeatureFlags() -> (FeatureFlags: ${b})`,b}getVisitorFeatureFlags(a){if(_logging.KameleoonLogger.info`CALL: KameleoonClient.getVisitorFeatureFlags(visitorCode: ${a})`,!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);this.visitorCodeManager.validateVisitorCode(a).throw();const b=this._getActiveFeatureVariations(a),c=[];return b.forEach(a=>{let{featureFlagId:b,featureKey:d}=a;c.push({id:b,key:d})}),_logging.KameleoonLogger.info`RETURN: KameleoonClient.getVisitorFeatureFlags(visitorCode: ${a}) -> (FeatureFlags: ${c})`,c}getVariation(a){let{visitorCode:b,featureKey:c,track:d=!0}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.getVariation(visitorCode: ${b}, featureKey: ${c}, track: ${d})`,this.visitorCodeManager.validateVisitorCode(b).throw();const e=this._getFeatureVariation({visitorCode:b,featureKey:c,track:d}).throw();return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getVariation(visitorCode: ${b}, featureKey: ${c}, track: ${d}) -> (variation: ${e})`,e}getVariations(a){let{visitorCode:b,onlyActive:c=!1,track:d=!0}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.getVariations(visitorCode: ${b}, onlyActive: ${c}, track: ${d})`,this.visitorCodeManager.validateVisitorCode(b).throw();const e=new Map,f=this.clientConfiguration.featureFlags;for(const g of f.values()){const a=this._getFeatureVariation({visitorCode:b,featureKey:g.featureKey,track:d});a.ok&&(!c||a.data.key!==_constants.OFF_VARIATION_KEY)&&e.set(g.featureKey,a.data)}return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getVariations(visitorCode: ${b}, onlyActive: ${c}, track: ${d}) -> (variations: ${e})`,e}getActiveFeatureFlags(a){if(_logging.KameleoonLogger.info`CALL: KameleoonClient.getActiveFeatureFlags(visitorCode: ${a})`,!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);this.visitorCodeManager.validateVisitorCode(a).throw();const b=this._getActiveFeatureVariations(a),c=new Map;return b.forEach(b=>{const{variationKey:d,featureKey:e,variationId:f,experimentId:g}=b;let h=[];null!==f&&null!==g&&(h=this._getFeatureVariables({visitorCode:a,featureKey:e,variationKey:d}));const i=h.map(a=>_utilities.Parser.parseFeatureVariable(a).throw());c.set(b.featureKey,{key:d,id:f,experimentId:g,variables:i})}),_logging.KameleoonLogger.info`RETURN: KameleoonClient.getActiveFeatureFlags(visitorCode: ${a}) -> (FeatureFlags: ${c})`,c}getFeatureFlagVariationKey(a,b){_logging.KameleoonLogger.info`CALL: KameleoonClient.getFeatureFlagVariationKey(visitorCode: ${a}, featureKey: ${b})`,this.visitorCodeManager.validateVisitorCode(a).throw();const{key:c}=this._getFeatureVariation({visitorCode:a,featureKey:b,track:!0}).throw();return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getFeatureFlagVariationKey(visitorCode: ${a}, featureKey: ${b}) -> (variationKey: ${c})`,c}getFeatureFlagVariable(a){let{visitorCode:b,featureKey:c,variableKey:d}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.getFeatureFlagVariable(visitorCode: ${b}, featureKey: ${c}, variableKey: ${d})`,this.visitorCodeManager.validateVisitorCode(b).throw();const{key:e}=this._getFeatureVariation({visitorCode:b,featureKey:c,track:!0}).throw(),f=this._getFeatureVariables({visitorCode:b,featureKey:c,variationKey:e}),g=f.find(a=>a.key===d);if(!g)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagVariableNotFound,d,b);const{type:h,value:i}=_utilities.Parser.parseFeatureVariable(g).throw(),j={type:h,value:i};return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getFeatureFlagVariable(visitorCode: ${b}, featureKey: ${c}, variableKey: ${d}) -> (FeatureFlagVariable: ${j})`,j}getFeatureFlagVariables(a,b){_logging.KameleoonLogger.info`CALL: KameleoonClient.getFeatureFlagVariables(visitorCode: ${a}, featureKey: ${b})`,this.visitorCodeManager.validateVisitorCode(a).throw();const{key:c}=this._getFeatureVariation({visitorCode:a,featureKey:b,track:!0}).throw(),d=this._getFeatureVariables({visitorCode:a,featureKey:b,variationKey:c}),e=[];for(const c of d){const a=_utilities.Parser.parseFeatureVariable(c).throw();e.push(a)}return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getFeatureFlagVariables(visitorCode: ${a}, featureKey: ${b}) -> (FeatureFlagVariables: ${e})`,e}isFeatureFlagActive(a,b){let[c,d,e]=["","",!0];"string"==typeof a?(_logging.KameleoonLogger.info`CALL: KameleoonClient.isFeatureFlagActive(visitorCode: ${c}, featureKey: ${d})`,c=a,d=b):(_logging.KameleoonLogger.info`CALL: KameleoonClient.isFeatureFlagActive(visitorCode: ${c}, featureKey: ${d}, track ${e})`,[c,d]=[a.visitorCode,a.featureKey],a.track!==void 0&&(e=a.track)),this.visitorCodeManager.validateVisitorCode(c).throw();try{const{key:a}=this._getFeatureVariation({visitorCode:c,featureKey:d,track:e}).throw(),b=a!==_constants.OFF_VARIATION_KEY;return _logging.KameleoonLogger.info`RETURN: KameleoonClient.isFeatureFlagActive(visitorCode: ${c}, featureKey: ${d}, track: ${e}) -> (isActive: ${b})`,b}catch(a){if(a instanceof _kameleoonError.KameleoonError&&a.type===_kameleoonError.KameleoonException.FeatureFlagEnvironmentDisabled)return _logging.KameleoonLogger.info`RETURN: KameleoonClient.isFeatureFlagActive(visitorCode: ${c}, featureKey: ${d}, track: ${e}) -> (isActive: false)`,!1;throw a}}async getRemoteData(a){_logging.KameleoonLogger.info`CALL: KameleoonClient.getRemoteData(key: ${a})`;const b=await this.requester.getRemoteData(a);return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getRemoteData(key: ${a}) -> (remoteData: ${b})`,b.throw()}async getVisitorWarehouseAudience(a){let{visitorCode:b,customDataIndex:c,warehouseKey:d}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.getVisitorWarehouseAudience(visitorCode: ${b}, customDataIndex: ${c}, warehouseKey: ${d})`,this.visitorCodeManager.validateVisitorCode(b).throw();const e=await this.getRemoteData(d||b);if(!e)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.RemoteData,`No data found for ${d||b} key`);if(!e[_constants.WAREHOUSE_AUDIENCE_KEY]||0===Object.keys(e[_constants.WAREHOUSE_AUDIENCE_KEY]).length)return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getVisitorWarehouseAudience(visitorCode: ${b}, customDataIndex: ${c}, warehouseKey: ${d}) -> (customData: null)`,null;const f=new _kameleoonData.CustomData(c,...Object.keys(e[_constants.WAREHOUSE_AUDIENCE_KEY]));return this.dataManager.storeData(b,f),_logging.KameleoonLogger.info`RETURN: KameleoonClient.getVisitorWarehouseAudience(visitorCode: ${b}, customDataIndex: ${c}, warehouseKey: ${d}) -> (customData: ${f})`,f}onEvent(a,b){if(_logging.KameleoonLogger.info`CALL: KameleoonClient.onEvent(event: ${a}, callback)`,!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);this.eventManager.addEventHandler(a,b),_logging.KameleoonLogger.info`RETURN: KameleoonClient.onEvent(event: ${a}, callback)`}getEngineTrackingCode(a){_logging.KameleoonLogger.info`CALL: KameleoonClient.getEngineTrackingCode(visitorCode: ${a})`,this.visitorCodeManager.validateVisitorCode(a).throw();const b=_utilities.Utilities.getTrackingCode(this.trackingCache,a);return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getEngineTrackingCode(visitorCode: ${a}) -> (trackingCode: ${b})`,b}isInitialized(){return _logging.KameleoonLogger.debug`CALL/RETURN: KameleoonClient.isInitialized() -> (initialized: ${this.initialized})`,this.initialized}setLogLevel(a){_logging.KameleoonLogger.setLogLevel(a)}setForcedVariation(a){let{visitorCode:b,experimentId:c,variationKey:d,forceTargeting:e=!0}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.setForcedVariation(visitorCode: '${b}', experimentId: ${c}, variationKey: '${d}', forceTargeting: ${e})`,this.visitorCodeManager.validateVisitorCode(b).throw(),this._setForcedVariation({visitorCode:b,experimentId:c,variationKey:d,forceTargeting:e}).throw(),_logging.KameleoonLogger.info`RETURN: KameleoonClient.setForcedVariation(visitorCode: '${b}', experimentId: ${c}, variationKey: '${d}', forceTargeting: ${e})`}setUserConsent(a){let{visitorCode:b,consent:c,setData:d}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.setUserConsent(visitorCode: ${b}, consent: ${c}, setData: ${d})`,this.visitorCodeManager.validateVisitorCode(b).throw(),this.updateConsentData(b,c),c?d({visitorCode:b,key:_storage.KameleoonStorageKey.VisitorCode,maxAge:_visitorCodeManager.DEFAULT_MAX_AGE,path:_visitorCodeManager.PATH}):this.visitorCodeManager.consentRequired&&d({visitorCode:"",key:_storage.KameleoonStorageKey.VisitorCode,maxAge:_visitorCodeManager.ZERO_MAX_AGE,path:_visitorCodeManager.PATH}),_logging.KameleoonLogger.info`RETURN: KameleoonClient.setUserConsent(visitorCode: ${b}, consent: ${c}, setData: ${d})`}updateConsentData(a,b){const c=this.consentDataStorage.read();if(!c.ok)return void(c.error.type===_kameleoonError.KameleoonException.StorageEmpty&&this.consentDataStorage.write({[a]:{consent:b}}));const d=c.data;d[a]={consent:b},this.consentDataStorage.write(d)}_isConsentProvided(a){_logging.KameleoonLogger.debug`CALL: KameleoonClient._isConsentProvided(visitorCode: ${a})`;const{isConsentRequired:b}=this.clientConfiguration,c=this.consentDataStorage.read();let d=!1;if(!b)d=!0;else if(c.ok){const b=c.data[a];d="boolean"==typeof b?b:b&&b.consent}return _logging.KameleoonLogger.debug`RETURN: KameleoonClient._isConsentProvided(visitorCode: ${a}) -> (isConsentProvided: ${d})`,d}_getFeatureVariables(a){let{visitorCode:b,featureKey:c,variationKey:d}=a;_logging.KameleoonLogger.debug`CALL: KameleoonClient._getFeatureVariables(visitorCode: ${b}, featureKey: ${c}, variationKey: ${d})`;const e=this.clientConfiguration.featureFlags,f=e.get(c);if(!f)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagConfigurationNotFound,c);const g=f.variations.find(a=>a.key===d);if(!g)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagVariationNotFound,d,b);return _logging.KameleoonLogger.debug`RETURN: KameleoonClient._getFeatureVariables(visitorCode: ${b}, featureKey: ${c}, variationKey: ${d}) -> (variables: ${g.variables})`,g.variables}_getActiveFeatureVariations(a){_logging.KameleoonLogger.debug`CALL: KameleoonClient._getActiveFeatureVariations(visitorCode: ${a})`;const b=this.clientConfiguration.featureFlags,c=[],d=this.dataManager.getVisitorData(a),e=this.dataManager.getVisitorIdentifier(a);for(const f of b.values()){if(!f.environmentEnabled)continue;const b=this.variationConfiguration.getVariation({visitorCode:a,visitorIdentifier:e,targetingData:d,featureFlag:f,clientConfiguration:this.clientConfiguration,dataManager:this.dataManager,packageInfo:this.externalPackageInfo}).throw();this.eventManager.fireEvent(_eventManager.EventType.Evaluation,{featureKey:f.featureKey,variation:{key:b.variationKey,experimentId:b.experimentId,id:b.variationId}}),b.variationKey!==_constants.OFF_VARIATION_KEY&&c.push(b)}return _logging.KameleoonLogger.debug`RETURN: KameleoonClient._getActiveFeatureVariations(visitorCode: ${a}) -> (activeVariations: ${c})`,c}_getFeatureVariation(a){let{visitorCode:b,featureKey:c,track:d}=a;if(_logging.KameleoonLogger.debug`CALL: KameleoonClient._getFeatureVariation(visitorCode: ${b}, featureKey: ${c}, track: ${d})`,!this.initialized)return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization));const e=this.clientConfiguration.featureFlags,f=e.get(c);if(!f)return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagConfigurationNotFound,c));if(!f.environmentEnabled)return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagEnvironmentDisabled,f.featureKey,this.environment));let g,h=!1,i=!1;const j=this.variationConfiguration.getForcedFeatureVariation(b,c);if(j.ok){const a=j.data;if(a){var k,l;_logging.KameleoonLogger.debug`Forced variation for visitor ${b} and feature ${c} is found: ${a}`,h=!0,i=!0;let d=f.defaultVariationKey;a.varByExp?d=a.varByExp.variationKey:a.rule&&a.rule.type===_clientConfiguration.RuleType.EXPERIMENTATION&&(d=_constants.OFF_VARIATION_KEY),g={variationKey:d,variationId:(null===(k=a.varByExp)||void 0===k?void 0:k.variationId)||null,experimentId:(null===(l=a.rule)||void 0===l?void 0:l.experimentId)||null,rule:a.rule||null}}}if(!g){const a=this.dataManager.getVisitorData(b),c=this.dataManager.getVisitorIdentifier(b);g=this.variationConfiguration.getVariation({visitorCode:b,visitorIdentifier:c,featureFlag:f,track:d,withAssignment:!0,targetingData:a,clientConfiguration:this.clientConfiguration,dataManager:this.dataManager,packageInfo:this.externalPackageInfo}).throw()}h||(i&&null!==g.experimentId&&null!==g.variationId&&this.variationConfiguration.updateStoredVariations(b,{[g.experimentId]:{variationId:g.variationId,isTargetedRule:g.rule.type===_clientConfiguration.RuleType.TARGETED_DELIVERY,status:d?_types.TrackingStatus.Unsent:_types.TrackingStatus.Sent}}),this.eventManager.fireEvent(_eventManager.EventType.Evaluation,{featureKey:c,variation:{key:g.variationKey,experimentId:g.experimentId,id:g.variationId}}));const{experimentId:m,variationId:n,variationKey:o}=g;let p=new Map,q=!1;try{const a=this._getFeatureVariables({visitorCode:b,featureKey:c,variationKey:o});a.forEach(a=>{q||a.type!==_types.VariableType.JS&&a.type!==_types.VariableType.CSS||(q=!0);const b=_utilities.Parser.parseFeatureVariable(a).throw();p.set(a.key,b)})}catch(_){}m&&"number"==typeof n&&!h&&_utilities.Utilities.updateCache({cacheManager:this.trackingCache,hasJsCssVariables:q,visitorCode:b,experimentId:m,variationId:n}),d&&!h&&this.tracker.scheduleVisitor(b,this._isConsentProvided(b));return _logging.KameleoonLogger.debug`RETURN: KameleoonClient._getFeatureVariation(visitorCode: ${b}, featureKey: ${c}, track: ${d}) -> (variation: ${g})`,(0,_tsRes.Ok)({key:o,id:n,experimentId:m,variables:p})}_setForcedVariation(a){let{visitorCode:b,experimentId:c,variationKey:d,forceTargeting:e=!0}=a;if(_logging.KameleoonLogger.debug`CALL: KameleoonClient._setForcedVariation(visitorCode: '${b}', experimentId: ${c}, variationKey: '${d}', forceTargeting: ${e})`,null===d){const a=this.variationConfiguration.updateForcedExperimentVariation(b,c,null);return _logging.KameleoonLogger.debug`RETURN: KameleoonClient._setForcedVariation(visitorCode: '${b}', experimentId: ${c}, variationKey: '${d}', forceTargeting: ${e}) -> (writeResult: ${a})`,a}let f=null,g=null;if(f=this.clientConfiguration.experimentMap.get(c),!f)return _logging.KameleoonLogger.error`Experiment with id ${c} not found for visitor ${b}`,(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagExperimentNotFound,c,b));if(g=f.variations.get(d),void 0===g)return _logging.KameleoonLogger.error`Variation with key ${d} not found in experiment ${f} for visitor ${b}`,(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagVariationNotFound,d,b));f={type:f.type,experimentId:c},g={variationKey:d,variationId:g};const h=this.variationConfiguration.updateForcedExperimentVariation(b,c,{rule:f,varByExp:g,forceTargeting:e});return _logging.KameleoonLogger.debug`RETURN: KameleoonClient._setForcedVariation(visitorCode: '${b}', experimentId: ${c}, variationKey: '${d}', forceTargeting: ${e}) -> (writeResult: ${h})`,h}}exports.KameleoonClient=KameleoonClient; |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Requester=void 0;var _tsRes=require("ts-res"),_kameleoonError=require("../kameleoonError"),_constants=require("./constants"),_types=require("./types"),_logging=require("../logging");function ownKeys(a,b){var c=Object.keys(a);if(Object.getOwnPropertySymbols){var d=Object.getOwnPropertySymbols(a);b&&(d=d.filter(function(b){return Object.getOwnPropertyDescriptor(a,b).enumerable})),c.push.apply(c,d)}return c}function _objectSpread(a){for(var b,c=1;c<arguments.length;c++)b=null==arguments[c]?{}:arguments[c],c%2?ownKeys(Object(b),!0).forEach(function(c){_defineProperty(a,c,b[c])}):Object.getOwnPropertyDescriptors?Object.defineProperties(a,Object.getOwnPropertyDescriptors(b)):ownKeys(Object(b)).forEach(function(c){Object.defineProperty(a,c,Object.getOwnPropertyDescriptor(b,c))});return a}function _defineProperty(a,b,c){return(b=_toPropertyKey(b))in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}function _toPropertyKey(a){var b=_toPrimitive(a,"string");return"symbol"==typeof b?b:b+""}function _toPrimitive(a,b){if("object"!=typeof a||!a)return a;var c=a[Symbol.toPrimitive];if(void 0!==c){var d=c.call(a,b||"default");if("object"!=typeof d)return d;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===b?String:Number)(a)}class Requester{constructor(a){let{urlProvider:b,packageInfo:c,externalRequester:d,requestTimeout:e,trackRetryDelay:f,useAbortController:g}=a;_defineProperty(this,"externalRequester",void 0),_defineProperty(this,"urlProvider",void 0),_defineProperty(this,"packageInfo",void 0),_defineProperty(this,"trackRetryDelay",void 0),_defineProperty(this,"useAbortController",void 0),_defineProperty(this,"timeout",void 0),_logging.KameleoonLogger.debug`CALL: new Requester(urlProvider, packageInfo: ${c}, externalRequester, requestTimeout: ${e}, trackRetryDelay: ${f}, useAbortController: ${g})`,this.urlProvider=b,this.useAbortController=g,this.externalRequester=d,this.trackRetryDelay=f,this.packageInfo=c,this.timeout=e,_logging.KameleoonLogger.debug`RETURN: new Requester(urlProvider, packageInfo: ${c}, externalRequester, requestTimeout: ${e}, trackRetryDelay: ${f}, useAbortController: ${g})`}async getClientConfiguration(a){const b=this.urlProvider.getClientConfigurationUrl(a),c={[_constants.Header.SdkType]:this.packageInfo.type.toLowerCase(),[_constants.Header.SdkVersion]:this.packageInfo.version},d=_constants.NUMBER_OF_RETRIES+1;try{let a={message:""};_logging.KameleoonLogger.debug`Running configuration request Method: ${_types.HttpMethod.Get}, Url: ${b}, Headers: ${c}, with retry limit ${d}, retry delay ${this.trackRetryDelay} ms`;for(let e=0;e<d;e++){const d=await this.sendRequest({url:b,requestType:_types.RequestType.Configuration,retryCount:e,parameters:{method:_types.HttpMethod.Get,headers:c}});if(d.ok){const a=await d.json();return _logging.KameleoonLogger.debug`Received configuration response: ${{status:d.status}} for request Method: ${_types.HttpMethod.Get}, Url: ${b}, Headers: ${c}`,(0,_tsRes.Ok)(a)}let f=_logging.LogLevel.ERROR;if(e===_constants.NUMBER_OF_RETRIES){if(d.text){const b=await d.text();a=JSON.parse(b)}f=_logging.LogLevel.WARNING}await this.logRequestError({logLevel:f,message:"Failed to get configuration response",response:d,method:_types.HttpMethod.Get,url:b})}return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.MaximumRetriesReached,a.message))}catch(a){return await this.logRequestError({logLevel:_logging.LogLevel.ERROR,message:"Failed to get configuration response",error:a,method:_types.HttpMethod.Get,url:b,headers:c}),(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.ClientConfiguration,a))}}async getRemoteData(a){const b=this.urlProvider.getRemoteDataUrl(a);_logging.KameleoonLogger.debug`Running remote data request Method: ${_types.HttpMethod.Get}, Url: ${b}`;try{const a=await this.sendRequest({url:b,requestType:_types.RequestType.RemoteData,retryCount:0,parameters:{method:_types.HttpMethod.Get}});if(!a.ok){if(await this.logRequestError({logLevel:_logging.LogLevel.ERROR,message:"Failed to get remote data response",response:a,method:_types.HttpMethod.Get,url:b}),a.text){var c;const b=await a.text(),d=null===(c=JSON.parse(b))||void 0===c?void 0:c.message;return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.RemoteData,d))}return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.RemoteData,"Unknown Reason - no `text()` was found for a response"))}const d=await a.json();return _logging.KameleoonLogger.debug`Received remote data response: ${{status:a.status}} for request Method: ${_types.HttpMethod.Get}, Url: ${b}`,(0,_tsRes.Ok)(d)}catch(a){return await this.logRequestError({logLevel:_logging.LogLevel.ERROR,message:"Failed to get remote data response",error:a,method:_types.HttpMethod.Get,url:b}),(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.RemoteData,a))}}async getVisitorData(a){let{visitorCode:b,filters:c,isMappingIdentifier:d}=a;const e=this.urlProvider.getVisitorDataUrl({visitorCode:b,filters:c,isMappingIdentifier:d});_logging.KameleoonLogger.debug`Running visitor data request Method: ${_types.HttpMethod.Get}, Url: ${e}`;try{const a=await this.sendRequest({url:e,requestType:_types.RequestType.RemoteData,retryCount:0,parameters:{method:_types.HttpMethod.Get}});if(!a.ok){if(await this.logRequestError({logLevel:_logging.LogLevel.ERROR,message:"Failed to get visitor data response",response:a,method:_types.HttpMethod.Get,url:e}),"function"==typeof a.text){const b=await a.text();if(b){var f;const a=null===(f=JSON.parse(b))||void 0===f?void 0:f.message;return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.RemoteData,a))}}return a.status?(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.RemoteData,`No error message. Error status: ${a.status}`)):(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.RemoteData,"Unknown Reason - no text message or error status was found for a response"))}const b=await a.json();return _logging.KameleoonLogger.debug`Received visitor data response: ${{status:a.status}} for request Method: ${_types.HttpMethod.Get}, Url: ${e}`,(0,_tsRes.Ok)(b)}catch(a){return await this.logRequestError({logLevel:_logging.LogLevel.ERROR,message:"Failed to get visitor data response",error:a,method:_types.HttpMethod.Get,url:e}),(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.RemoteData,a))}}async track(a,b){const c=_constants.NUMBER_OF_RETRIES+1,d=this.urlProvider.getTrackingUrl(b);_logging.KameleoonLogger.debug`Running tracking request Method: ${_types.HttpMethod.Post}, Url: ${d}, Body: ${a}, with retry limit ${c}, retry delay ${this.trackRetryDelay} ms`;for(let e=0;e<c;e++){let b;try{if(b=await this.sendRequest({url:d,retryCount:e,requestType:_types.RequestType.Tracking,parameters:{body:a,method:_types.HttpMethod.Post}}),b.ok)return _logging.KameleoonLogger.debug`Received tracking response: ${{status:b.status}} for request Method: ${_types.HttpMethod.Post}, Url: ${d}, Body: ${a}`,(0,_tsRes.Ok)();await this.logRequestError({logLevel:_logging.LogLevel.WARNING,message:"Failed to get tracking response",response:b,method:_types.HttpMethod.Post,url:d})}catch(a){await this.logRequestError({logLevel:_logging.LogLevel.WARNING,message:"Failed to get tracking response",error:a,method:_types.HttpMethod.Post,url:d})}await new Promise(a=>setTimeout(a,this.trackRetryDelay))}return _logging.KameleoonLogger.error`Failed to get tracking response for request Method: ${_types.HttpMethod.Post}, Url: ${d}`,(0,_tsRes.Err)()}async sendRequest(a){let b,{url:c,requestType:d,retryCount:e,parameters:f}=a;if(this.useAbortController){const a=new AbortController,g=setTimeout(()=>a.abort(),this.timeout);b=await this.externalRequester.sendRequest({url:c,retryCount:e,requestType:d,parameters:_objectSpread(_objectSpread({},f),{},{signal:a.signal})}),clearTimeout(g)}else b=await this.externalRequester.sendRequest({url:c,retryCount:e,requestType:d,parameters:f});return b}async logRequestError(a){let{logLevel:b,message:c,error:d,method:e,url:f,response:g,headers:h}=a;if(d){let a="Unknown error";d instanceof Error&&(a=d.message),_logging.KameleoonLogger.error`${c} with error: ${a} for request Method: ${e}, Url: ${f}`}else if(g&&_logging.KameleoonLogger.checkLevel(b)){const a=await this.parseResponse(g);_logging.KameleoonLogger.log({level:b,strings:()=>`${c} response: Status: '${a.status}' Message: '${a.message}' for request Method: ${e}, Url: '${f}'`+(h?`, Headers: ${h}`:"")})}}async parseResponse(a){let b="";if("function"==typeof a.text){const d=await a.text();if(d)try{var c;b=null===(c=JSON.parse(d))||void 0===c?void 0:c.message}catch(a){}}return{status:a.status,message:b}}}exports.Requester=Requester; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Requester=void 0;var _tsRes=require("ts-res"),_kameleoonError=require("../kameleoonError"),_constants=require("./constants"),_types=require("./types"),_logging=require("../logging");function ownKeys(a,b){var c=Object.keys(a);if(Object.getOwnPropertySymbols){var d=Object.getOwnPropertySymbols(a);b&&(d=d.filter(function(b){return Object.getOwnPropertyDescriptor(a,b).enumerable})),c.push.apply(c,d)}return c}function _objectSpread(a){for(var b,c=1;c<arguments.length;c++)b=null==arguments[c]?{}:arguments[c],c%2?ownKeys(Object(b),!0).forEach(function(c){_defineProperty(a,c,b[c])}):Object.getOwnPropertyDescriptors?Object.defineProperties(a,Object.getOwnPropertyDescriptors(b)):ownKeys(Object(b)).forEach(function(c){Object.defineProperty(a,c,Object.getOwnPropertyDescriptor(b,c))});return a}function _defineProperty(a,b,c){return(b=_toPropertyKey(b))in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}function _toPropertyKey(a){var b=_toPrimitive(a,"string");return"symbol"==typeof b?b:b+""}function _toPrimitive(a,b){if("object"!=typeof a||!a)return a;var c=a[Symbol.toPrimitive];if(void 0!==c){var d=c.call(a,b||"default");if("object"!=typeof d)return d;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===b?String:Number)(a)}class Requester{constructor(a){let{urlProvider:b,packageInfo:c,externalRequester:d,requestTimeout:e,trackRetryDelay:f,useAbortController:g}=a;_defineProperty(this,"externalRequester",void 0),_defineProperty(this,"urlProvider",void 0),_defineProperty(this,"packageInfo",void 0),_defineProperty(this,"trackRetryDelay",void 0),_defineProperty(this,"useAbortController",void 0),_defineProperty(this,"timeout",void 0),_logging.KameleoonLogger.debug`CALL: new Requester(urlProvider, packageInfo: ${c}, externalRequester, requestTimeout: ${e}, trackRetryDelay: ${f}, useAbortController: ${g})`,this.urlProvider=b,this.useAbortController=g,this.externalRequester=d,this.trackRetryDelay=f,this.packageInfo=c,this.timeout=e,_logging.KameleoonLogger.debug`RETURN: new Requester(urlProvider, packageInfo: ${c}, externalRequester, requestTimeout: ${e}, trackRetryDelay: ${f}, useAbortController: ${g})`}async getClientConfiguration(a){const b=this.urlProvider.getClientConfigurationUrl(a),c={[_constants.Header.SdkType]:this.packageInfo.type.toLowerCase(),[_constants.Header.SdkVersion]:this.packageInfo.version},d=_constants.NUMBER_OF_RETRIES+1;try{let a={message:""};_logging.KameleoonLogger.debug`Running configuration request Method: ${_types.HttpMethod.Get}, Url: ${b}, Headers: ${c}, with retry limit ${d}, retry delay ${this.trackRetryDelay} ms`;for(let e=0;e<d;e++){const d=await this.sendRequest({url:b,requestType:_types.RequestType.Configuration,retryCount:e,parameters:{method:_types.HttpMethod.Get,headers:c}});if(d.ok){const a=await d.json();return _logging.KameleoonLogger.debug`Received configuration response: ${{status:d.status}} for request Method: ${_types.HttpMethod.Get}, Url: ${b}, Headers: ${c}`,(0,_tsRes.Ok)(a)}if(e===_constants.NUMBER_OF_RETRIES){if(d.text){const b=await d.text();a=JSON.parse(b)}_logging.KameleoonLogger.error`Failed to get configuration response ${{status:d.status,message:a.message}} for request Method: ${_types.HttpMethod.Get}, Url: ${b}, Headers: ${c}`}else await this.logRequestError({logLevel:_logging.LogLevel.WARNING,message:"Failed to get configuration response",response:d,method:_types.HttpMethod.Get,url:b})}return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.MaximumRetriesReached,a.message))}catch(a){return await this.logRequestError({logLevel:_logging.LogLevel.ERROR,message:"Failed to get configuration response",error:a,method:_types.HttpMethod.Get,url:b,headers:c}),(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.ClientConfiguration,a))}}async getRemoteData(a){const b=this.urlProvider.getRemoteDataUrl(a);_logging.KameleoonLogger.debug`Running remote data request Method: ${_types.HttpMethod.Get}, Url: ${b}`;try{const a=await this.sendRequest({url:b,requestType:_types.RequestType.RemoteData,retryCount:0,parameters:{method:_types.HttpMethod.Get}});if(!a.ok){if(await this.logRequestError({logLevel:_logging.LogLevel.ERROR,message:"Failed to get remote data response",response:a,method:_types.HttpMethod.Get,url:b}),a.text){var c;const b=await a.text(),d=null===(c=JSON.parse(b))||void 0===c?void 0:c.message;return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.RemoteData,d))}return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.RemoteData,"Unknown Reason - no `text()` was found for a response"))}const d=await a.json();return _logging.KameleoonLogger.debug`Received remote data response: ${{status:a.status}} for request Method: ${_types.HttpMethod.Get}, Url: ${b}`,(0,_tsRes.Ok)(d)}catch(a){return await this.logRequestError({logLevel:_logging.LogLevel.ERROR,message:"Failed to get remote data response",error:a,method:_types.HttpMethod.Get,url:b}),(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.RemoteData,a))}}async getVisitorData(a){let{visitorCode:b,filters:c,isMappingIdentifier:d}=a;const e=this.urlProvider.getVisitorDataUrl({visitorCode:b,filters:c,isMappingIdentifier:d});_logging.KameleoonLogger.debug`Running visitor data request Method: ${_types.HttpMethod.Get}, Url: ${e}`;try{const a=await this.sendRequest({url:e,requestType:_types.RequestType.RemoteData,retryCount:0,parameters:{method:_types.HttpMethod.Get}});if(!a.ok){if(await this.logRequestError({logLevel:_logging.LogLevel.ERROR,message:"Failed to get visitor data response",response:a,method:_types.HttpMethod.Get,url:e}),"function"==typeof a.text){const b=await a.text();if(b){var f;const a=null===(f=JSON.parse(b))||void 0===f?void 0:f.message;return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.RemoteData,a))}}return a.status?(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.RemoteData,`No error message. Error status: ${a.status}`)):(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.RemoteData,"Unknown Reason - no text message or error status was found for a response"))}const b=await a.json();return _logging.KameleoonLogger.debug`Received visitor data response: ${{status:a.status}} for request Method: ${_types.HttpMethod.Get}, Url: ${e}`,(0,_tsRes.Ok)(b)}catch(a){return await this.logRequestError({logLevel:_logging.LogLevel.ERROR,message:"Failed to get visitor data response",error:a,method:_types.HttpMethod.Get,url:e}),(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.RemoteData,a))}}async track(a,b){const c=_constants.NUMBER_OF_RETRIES+1,d=this.urlProvider.getTrackingUrl(b);_logging.KameleoonLogger.debug`Running tracking request Method: ${_types.HttpMethod.Post}, Url: ${d}, Body: ${a}, with retry limit ${c}, retry delay ${this.trackRetryDelay} ms`;for(let e=0;e<c;e++){let b;try{if(b=await this.sendRequest({url:d,retryCount:e,requestType:_types.RequestType.Tracking,parameters:{body:a,method:_types.HttpMethod.Post}}),b.ok)return _logging.KameleoonLogger.debug`Received tracking response: ${{status:b.status}} for request Method: ${_types.HttpMethod.Post}, Url: ${d}, Body: ${a}`,(0,_tsRes.Ok)();await this.logRequestError({logLevel:_logging.LogLevel.WARNING,message:"Failed to get tracking response",response:b,method:_types.HttpMethod.Post,url:d})}catch(a){await this.logRequestError({logLevel:_logging.LogLevel.WARNING,message:"Failed to get tracking response",error:a,method:_types.HttpMethod.Post,url:d})}await new Promise(a=>setTimeout(a,this.trackRetryDelay))}return _logging.KameleoonLogger.error`Failed to get tracking response for request Method: ${_types.HttpMethod.Post}, Url: ${d}`,(0,_tsRes.Err)()}async sendRequest(a){let b,{url:c,requestType:d,retryCount:e,parameters:f}=a;if(this.useAbortController){const a=new AbortController,g=setTimeout(()=>a.abort(),this.timeout);b=await this.externalRequester.sendRequest({url:c,retryCount:e,requestType:d,parameters:_objectSpread(_objectSpread({},f),{},{signal:a.signal})}),clearTimeout(g)}else b=await this.externalRequester.sendRequest({url:c,retryCount:e,requestType:d,parameters:f});return b}async logRequestError(a){let{logLevel:b,message:c,error:d,method:e,url:f,response:g,headers:h}=a;if(d){let a="Unknown error";d instanceof Error&&(a=d.message),_logging.KameleoonLogger.error`${c} with error: ${a} for request Method: ${e}, Url: ${f}`}else if(g&&_logging.KameleoonLogger.checkLevel(b)){const a=await this.parseResponse(g);_logging.KameleoonLogger.log({level:b,strings:()=>`${c} response: Status: '${a.status}' Message: '${a.message}' for request Method: ${e}, Url: '${f}'`+(h?`, Headers: ${h}`:"")})}}async parseResponse(a){let b="";try{if("function"==typeof a.text){const d=await a.text();if(d){var c;b=null===(c=JSON.parse(d))||void 0===c?void 0:c.message}}}catch(a){}return{status:a.status,message:b}}}exports.Requester=Requester; |
@@ -15,6 +15,6 @@ import { ClientDataType } from './types'; | ||
KameleoonSimulationFFData = "kameleoonSimulationFFData", | ||
ForcedFeatureVariation = "forcedFeatureVariation", | ||
ForcedExperimentVariation = "forcedExperimentVariation" | ||
ForcedFeatureVariation = "kameleoonForcedFeatureVariation", | ||
ForcedExperimentVariation = "kameleoonForcedExperimentVariation" | ||
} | ||
export declare const DEFAULT_CLIENT_CONFIGURATION: ClientDataType; | ||
export declare const VISIT_DURATION: number; |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.VISIT_DURATION=exports.KameleoonStorageKey=exports.DEFAULT_CLIENT_CONFIGURATION=void 0;var _clientConfiguration=require("../clientConfiguration"),_types=require("../types");let KameleoonStorageKey=exports.KameleoonStorageKey=function(a){return a.ClientData="kameleoonClientData",a.TargetingData="kameleoonTargetingData",a.TrackingData="kameleoonTrackingData",a.VariationData="kameleoonVariationData",a.VisitorCode="kameleoonVisitorCode",a.ConsentData="kameleoonConsentData",a.DataInfo="kameleoonDataInfo",a.KameleoonSimulationFFData="kameleoonSimulationFFData",a.ForcedFeatureVariation="forcedFeatureVariation",a.ForcedExperimentVariation="forcedExperimentVariation",a}({});const DEFAULT_CLIENT_CONFIGURATION=exports.DEFAULT_CLIENT_CONFIGURATION={data:{customData:[],featureFlags:[],configuration:{realTimeUpdate:!1,consentType:_clientConfiguration.ConsentType.NotRequired,dataApiDomain:"data.kameleoon.io"}},lastUpdate:""},VISIT_DURATION=exports.VISIT_DURATION=29*_types.Milliseconds.Minute; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.VISIT_DURATION=exports.KameleoonStorageKey=exports.DEFAULT_CLIENT_CONFIGURATION=void 0;var _clientConfiguration=require("../clientConfiguration"),_types=require("../types");let KameleoonStorageKey=exports.KameleoonStorageKey=function(a){return a.ClientData="kameleoonClientData",a.TargetingData="kameleoonTargetingData",a.TrackingData="kameleoonTrackingData",a.VariationData="kameleoonVariationData",a.VisitorCode="kameleoonVisitorCode",a.ConsentData="kameleoonConsentData",a.DataInfo="kameleoonDataInfo",a.KameleoonSimulationFFData="kameleoonSimulationFFData",a.ForcedFeatureVariation="kameleoonForcedFeatureVariation",a.ForcedExperimentVariation="kameleoonForcedExperimentVariation",a}({});const DEFAULT_CLIENT_CONFIGURATION=exports.DEFAULT_CLIENT_CONFIGURATION={data:{customData:[],featureFlags:[],configuration:{realTimeUpdate:!1,consentType:_clientConfiguration.ConsentType.NotRequired,dataApiDomain:"data.kameleoon.io"}},lastUpdate:""},VISIT_DURATION=exports.VISIT_DURATION=29*_types.Milliseconds.Minute; |
export { KameleoonStorageKey, VISIT_DURATION } from './constants'; | ||
export { DataStorage } from './storage'; | ||
export { IntegrityChecker } from './integrityChecker'; | ||
export { StorageCleanupManager } from './storageCleanupManager'; | ||
export { ClientDataType, TrackingDataType, VariationDataType, ConsentDataType, DataInfoType, StorageDataType, FeatureFlagVariationsType, IStorage, IExternalStorage, } from './types'; |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),Object.defineProperty(exports,"ClientDataType",{enumerable:!0,get:function(){return _types.ClientDataType}}),Object.defineProperty(exports,"ConsentDataType",{enumerable:!0,get:function(){return _types.ConsentDataType}}),Object.defineProperty(exports,"DataInfoType",{enumerable:!0,get:function(){return _types.DataInfoType}}),Object.defineProperty(exports,"DataStorage",{enumerable:!0,get:function(){return _storage.DataStorage}}),Object.defineProperty(exports,"FeatureFlagVariationsType",{enumerable:!0,get:function(){return _types.FeatureFlagVariationsType}}),Object.defineProperty(exports,"IExternalStorage",{enumerable:!0,get:function(){return _types.IExternalStorage}}),Object.defineProperty(exports,"IStorage",{enumerable:!0,get:function(){return _types.IStorage}}),Object.defineProperty(exports,"IntegrityChecker",{enumerable:!0,get:function(){return _integrityChecker.IntegrityChecker}}),Object.defineProperty(exports,"KameleoonStorageKey",{enumerable:!0,get:function(){return _constants.KameleoonStorageKey}}),Object.defineProperty(exports,"StorageDataType",{enumerable:!0,get:function(){return _types.StorageDataType}}),Object.defineProperty(exports,"TrackingDataType",{enumerable:!0,get:function(){return _types.TrackingDataType}}),Object.defineProperty(exports,"VISIT_DURATION",{enumerable:!0,get:function(){return _constants.VISIT_DURATION}}),Object.defineProperty(exports,"VariationDataType",{enumerable:!0,get:function(){return _types.VariationDataType}});var _constants=require("./constants"),_storage=require("./storage"),_integrityChecker=require("./integrityChecker"),_types=require("./types"); | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),Object.defineProperty(exports,"ClientDataType",{enumerable:!0,get:function(){return _types.ClientDataType}}),Object.defineProperty(exports,"ConsentDataType",{enumerable:!0,get:function(){return _types.ConsentDataType}}),Object.defineProperty(exports,"DataInfoType",{enumerable:!0,get:function(){return _types.DataInfoType}}),Object.defineProperty(exports,"DataStorage",{enumerable:!0,get:function(){return _storage.DataStorage}}),Object.defineProperty(exports,"FeatureFlagVariationsType",{enumerable:!0,get:function(){return _types.FeatureFlagVariationsType}}),Object.defineProperty(exports,"IExternalStorage",{enumerable:!0,get:function(){return _types.IExternalStorage}}),Object.defineProperty(exports,"IStorage",{enumerable:!0,get:function(){return _types.IStorage}}),Object.defineProperty(exports,"IntegrityChecker",{enumerable:!0,get:function(){return _integrityChecker.IntegrityChecker}}),Object.defineProperty(exports,"KameleoonStorageKey",{enumerable:!0,get:function(){return _constants.KameleoonStorageKey}}),Object.defineProperty(exports,"StorageCleanupManager",{enumerable:!0,get:function(){return _storageCleanupManager.StorageCleanupManager}}),Object.defineProperty(exports,"StorageDataType",{enumerable:!0,get:function(){return _types.StorageDataType}}),Object.defineProperty(exports,"TrackingDataType",{enumerable:!0,get:function(){return _types.TrackingDataType}}),Object.defineProperty(exports,"VISIT_DURATION",{enumerable:!0,get:function(){return _constants.VISIT_DURATION}}),Object.defineProperty(exports,"VariationDataType",{enumerable:!0,get:function(){return _types.VariationDataType}});var _constants=require("./constants"),_storage=require("./storage"),_integrityChecker=require("./integrityChecker"),_storageCleanupManager=require("./storageCleanupManager"),_types=require("./types"); |
@@ -71,3 +71,5 @@ import { Result } from 'ts-res'; | ||
export type ConsentDataType = { | ||
[visitorCode: string]: boolean; | ||
[visitorCode: string]: boolean | { | ||
consent: boolean; | ||
}; | ||
}; | ||
@@ -74,0 +76,0 @@ export type ScheduledVisitorsType = { |
{ | ||
"name": "@kameleoon/javascript-sdk-core", | ||
"version": "5.2.2", | ||
"version": "5.2.3", | ||
"description": "Kameleoon JS SDK Core", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
452315
233
3764
88