@kameleoon/javascript-sdk-core 2.9.0 → 2.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"variationConfiguration.js","names":["VariationConfiguration","a","experiments","externalStorage","d","_classCallCheck","_defineProperty","visitorCode","experimentId","experiment","find","id","assignVariation","Err","KameleoonError","KameleoonException","ExperimentConfigurationNotFound","storage","_createClass","key","value","getAssignedVariations","result","read","ok","data","Ok","StorageRead","getFeatureFlagVariation","featureFlag","targetingData","packageInfo","campaignConfiguration","g","withAssignment","validateResult","Utilities","validateVisitorCode","error","i","rules","featureKey","featureFlagId","defaultVariationKey","n","_createForOfIteratorHelper","s","done","rule","segment","exposition","respoolTime","variationByExposition","targetingResult","checkTargeting","variationConfiguration","ruleHash","Hasher","obtainFeatureFlagHashDouble","campaignId","x","totalDeviation","variationHash","A","variation","type","RuleType","EXPERIMENTATION","updateStorageData","variationId","restParameters","_objectWithoutProperties","_excluded","_objectSpread","e","f","TARGETED_DELIVERY","variationKey","getExperimentVariation","c","deviations","hashDouble","obtainExperimentHashDouble","isUnallocated","resultVariationId","k","deviation","NotAllocated","variationResult","StorageParse","updatedVariationData","updatedVariation","assignmentDate","Date","toString","write","exports"],"sources":["../../src/variationConfiguration/variationConfiguration.ts"],"sourcesContent":["import { Err, Ok, Result } from 'ts-res';\nimport { ExperimentItemType } from 'src/campaignConfiguration';\nimport { Hasher } from 'src/hasher';\nimport { KameleoonError, KameleoonException } from 'src/kameleoonError';\nimport {\n IStorage,\n VariationDataType,\n ExperimentVariationsType,\n} from 'src/storage';\nimport { Utilities } from 'src/utilities';\nimport { RuleType } from 'src/campaignConfiguration/types';\nimport {\n FeatureFlagVariationType,\n GetFeatureFlagVariationParametersType,\n UpdateStorageParamsType,\n} from './types';\n\ninterface IVariationConfiguration {\n /**\n * @method getAssociatedVariationId - gets the id of a variation assigned to a visitor with `visitorCode` from the storage. If the visitor doesn't have associated variation yet, assigns a new one, stores it and returns it's id.\n */\n getAssociatedVariationId: (\n visitorCode: string,\n experimentId: number,\n ) => Result<number, KameleoonError>;\n /**\n * @method getAssignedVariations - obtains experiments with according variations that are *already assigned* and *stored* in the `variationStorage`. If `getAssociatedVariationId` had not been called before, there will be no variations to get.\n */\n getAssignedVariations(\n visitorCode: string,\n ): Result<ExperimentVariationsType, KameleoonError>;\n /**\n * @method getFeatureFlagVariation - obtains variation for the rule of the feature flag which is both targeted and has a variation, if no such rule was found, returns empty rule with default `variationKey`, given an option `withAssignment` of `true` will also assign a new variation and save it in the storage\n */\n getFeatureFlagVariation: (\n params: GetFeatureFlagVariationParametersType,\n ) => Result<FeatureFlagVariationType, KameleoonError>;\n /**\n * @method getExperimentVariation - obtains variation for the experiment by calculating the variation for the visitor *without assigning* it to a visitor\n */\n getExperimentVariation: (\n visitorCode: string,\n experiment: ExperimentItemType,\n ) => Result<number, KameleoonError>;\n}\n\nexport class VariationConfiguration implements IVariationConfiguration {\n private experiments: ExperimentItemType[];\n private storage: IStorage<VariationDataType>;\n\n constructor(\n experiments: ExperimentItemType[],\n externalStorage: IStorage<VariationDataType>,\n ) {\n this.experiments = experiments;\n this.storage = externalStorage;\n }\n\n public getAssociatedVariationId = (\n visitorCode: string,\n experimentId: number,\n ): Result<number, KameleoonError> => {\n // TODO:\n // --- Variation from storage is not checked anymore\n // --- If nothing changes the commented code should be deleted\n // along with the `Variation` class and according tests/types\n // --- Mind `todo` in `getAssociatedVariationId.spec.ts`\n //\n // let associatedVariation: VariationType | undefined;\n\n // this.storage.read().and((data) => {\n // associatedVariation = data?.[visitorCode]?.[experimentId];\n // });\n\n const experiment = this.experiments.find(\n (experiment) => experiment.id === String(experimentId),\n );\n\n if (!experiment) {\n return Err(\n new KameleoonError(\n KameleoonException.ExperimentConfigurationNotFound,\n experimentId,\n visitorCode,\n ),\n );\n }\n\n // const variation = new Variation(associatedVariation);\n\n // if (variation.isValid(experiment?.respoolTime)) {\n // return Ok(variation.id);\n // }\n\n return this.assignVariation(visitorCode, experiment);\n };\n\n public getAssignedVariations(\n visitorCode: string,\n ): Result<ExperimentVariationsType, KameleoonError> {\n const result = this.storage.read();\n\n if (!result.ok) {\n return result;\n }\n\n const experiments = result.data[visitorCode];\n\n if (!experiments) {\n return Err(\n new KameleoonError(KameleoonException.StorageRead, visitorCode),\n );\n }\n\n return Ok(experiments);\n }\n\n public getFeatureFlagVariation({\n visitorCode,\n featureFlag,\n targetingData,\n packageInfo,\n campaignConfiguration,\n withAssignment = false,\n }: GetFeatureFlagVariationParametersType): Result<\n FeatureFlagVariationType,\n KameleoonError\n > {\n const validateResult = Utilities.validateVisitorCode(visitorCode);\n\n if (!validateResult.ok) {\n return Err(validateResult.error);\n }\n\n const {\n rules,\n featureKey,\n id: featureFlagId,\n defaultVariationKey,\n } = featureFlag;\n\n for (const rule of rules) {\n const {\n segment,\n experimentId,\n id,\n exposition,\n respoolTime,\n variationByExposition,\n } = rule;\n\n // --- Check targeting ---\n const targetingResult = Utilities.checkTargeting({\n segment,\n visitorCode,\n experimentId,\n targetingData,\n packageInfo,\n campaignConfiguration,\n variationConfiguration: this,\n });\n\n if (!targetingResult.ok) {\n return targetingResult;\n }\n\n if (!targetingResult.data) {\n continue;\n }\n\n // --- Note ---\n // Calculate new variation (and assign it if `withAssignment` is `true`)\n // First the the exposed rule is calculated and then the variation inside of it\n const ruleHash = Hasher.obtainFeatureFlagHashDouble({\n visitorCode,\n respoolTime,\n campaignId: String(id),\n });\n\n if (ruleHash <= exposition) {\n let totalDeviation = 0;\n\n const variationHash = Hasher.obtainFeatureFlagHashDouble({\n visitorCode,\n respoolTime,\n campaignId: String(experimentId),\n });\n\n for (const variation of variationByExposition) {\n totalDeviation += variation.exposition;\n\n if (variationHash <= totalDeviation) {\n // --- Note ---\n // For the `RuleType.EXPERIMENTATION` variation assignment must happen without\n // calculating experiment deviation\n if (rule.type === RuleType.EXPERIMENTATION && withAssignment) {\n this.updateStorageData({\n visitorCode,\n experimentId: String(experimentId),\n // --- Note ---\n // For `RuleType.EXPERIMENTATION` `variationId` can not be null\n variationId: variation.variationId as number,\n });\n }\n\n const { exposition, ...restParameters } = variation;\n\n return Ok({\n ...restParameters,\n rule,\n featureFlagId,\n featureKey,\n experimentId,\n });\n }\n }\n }\n\n if (rule.type === RuleType.TARGETED_DELIVERY) {\n break;\n }\n }\n\n return Ok({\n featureKey,\n featureFlagId,\n experimentId: null,\n variationKey: defaultVariationKey,\n variationId: null,\n rule: null,\n });\n }\n\n public getExperimentVariation(\n visitorCode: string,\n experiment: ExperimentItemType,\n ): Result<number, KameleoonError> {\n const { id, respoolTime, deviations } = experiment;\n\n const hashDouble = Hasher.obtainExperimentHashDouble({\n visitorCode,\n campaignId: id,\n respoolTime,\n });\n\n let isUnallocated = true;\n let totalDeviation = 0;\n let resultVariationId = 0;\n\n for (const deviation of deviations) {\n totalDeviation += deviation.value;\n\n if (totalDeviation >= hashDouble) {\n // --- Note ---\n // If sum of all variations is less than 1 (100%), then there is a chance\n // user will end up outside of any variation\n isUnallocated = false;\n // --- Note ---\n // 'origin' is the default `variationId`, it must return id of 0\n if (deviation.variationId !== 'origin') {\n resultVariationId = Number(deviation.variationId);\n }\n\n break;\n }\n }\n\n if (isUnallocated) {\n return Err(\n new KameleoonError(KameleoonException.NotAllocated, visitorCode),\n );\n }\n\n return Ok(resultVariationId);\n }\n\n private assignVariation(\n visitorCode: string,\n experiment: ExperimentItemType,\n ): Result<number, KameleoonError> {\n const variationResult = this.getExperimentVariation(\n visitorCode,\n experiment,\n );\n\n if (!variationResult.ok) {\n return variationResult;\n }\n\n const result = this.updateStorageData({\n visitorCode,\n experimentId: experiment.id,\n variationId: variationResult.data,\n });\n\n if (!result.ok) {\n return result;\n }\n\n return Ok(variationResult.data);\n }\n\n private updateStorageData({\n visitorCode,\n experimentId,\n variationId,\n }: UpdateStorageParamsType): Result<void, KameleoonError> {\n const result = this.storage.read();\n\n if (!result.ok) {\n // --- Note ---\n // If there is a parsing error we return error\n // But if there is `LocalStorageRead` error it means that\n // data was read successfully and it's empty, which is not problem\n // cause that might be initial `read`\n if (result.error.type === KameleoonException.StorageParse) {\n return result;\n }\n }\n\n const updatedVariationData: VariationDataType = result.ok\n ? result.data\n : {};\n\n const updatedVariation = {\n variationId,\n assignmentDate: new Date().toString(),\n };\n\n if (updatedVariationData[visitorCode]) {\n updatedVariationData[visitorCode][experimentId] = updatedVariation;\n } else {\n updatedVariationData[visitorCode] = {\n [experimentId]: updatedVariation,\n };\n }\n\n return this.storage.write(updatedVariationData);\n }\n\n // --- TODO: commented out in case of future logic rework ---\n // --- Mind: `variationId` could cause the `undefined` issue ---\n // --- Reference: https://development.kameleoon.net/sdk/javascript-sdk-packages/-/merge_requests/89 ---\n // private getAssignedRuleVariation(\n // visitorCode: string,\n // rule: RuleItemType,\n // ): Result<GetAssignedRuleVariationResultType, KameleoonError> {\n // const emptyVariation: Omit<VariationByExpositionType, 'exposition'> = {\n // variationId: null,\n // variationKey: '',\n // };\n // const { experimentId, variationByExposition } = rule;\n\n // const variationsResult = this.getAssignedVariations(visitorCode);\n\n // // --- Note ---\n // // If there is an error, we just want to skip seeking\n // // for variation in the storage, not throw an error\n // if (!variationsResult.ok) {\n // return Ok(emptyVariation);\n // }\n\n // const variations = variationsResult.data;\n\n // if (\n // experimentId &&\n // typeof variations[experimentId].variationId !== undefined\n // ) {\n // const id = variations[experimentId].variationId;\n // const featureFlagVariation = variationByExposition.find(\n // (item) => item.variationId == id,\n // );\n\n // if (!featureFlagVariation) {\n // // --- Note ---\n // // This error is not described for public methods as\n // // it serves internal purpose of checking whether\n // // the persistent variation has been updated and should be re-allocated\n // return Err(new KameleoonError(KameleoonException.DataInconsistency));\n // }\n\n // const { exposition, ...restParameters } = featureFlagVariation;\n\n // return Ok({ ...restParameters });\n // }\n\n // return Ok(emptyVariation);\n // }\n}\n"],"mappings":"iuHA8Ca,CAAAA,sBAAsB,yBAIjC,SAAAC,EACEC,CAAiC,CACjCC,CAA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AAkB,CAAC0E,YAAY,CALzD;AACA;AACA;AACA;AACA;AAEE,MAAO,CAAAnE,CAAM,CAEhB,GAEK,CAAAoE,CAAuC,CAAGpE,CAAM,CAACE,EAAE,CACrDF,CAAM,CAACG,IAAI,CACX,CAAC,CAAC,CAEAkE,CAAgB,CAAG,CACvBtB,WAAW,CAAXA,CAAW,CACXuB,cAAc,CAAE,GAAI,CAAAC,IAAI,EAAE,CAACC,QAAQ,EACrC,CAAC,CAUD,MARI,CAAAJ,CAAoB,CAACnF,CAAW,CAAC,CACnCmF,CAAoB,CAACnF,CAAW,CAAC,CAACC,CAAY,CAAC,CAAGmF,CAAgB,CAElED,CAAoB,CAACnF,CAAW,CAAC,CAAAD,eAAA,IAC9BE,CAAY,CAAGmF,CAAgB,CACjC,CAGI,IAAI,CAAC1E,OAAO,CAAC8E,KAAK,CAACL,CAAoB,CAChD,CAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AAEA;AACA;AACA;AACA;AACA;AACA;AAEA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AAEA;AACA;AAEA;AACA;AAAA,IAAAzF,CAAA,IAAA+F,OAAA,CAAAhG,sBAAA,CAAAA,sBAAA"}
1
+ {"version":3,"file":"variationConfiguration.js","names":["VariationConfiguration","a","experiments","externalStorage","d","_classCallCheck","_defineProperty","visitorCode","experimentId","experiment","find","id","assignVariation","Err","KameleoonError","KameleoonException","ExperimentConfigurationNotFound","storage","_createClass","key","value","getAssignedVariations","result","read","ok","data","Ok","StorageRead","getFeatureFlagVariation","featureFlag","targetingData","packageInfo","campaignConfiguration","g","withAssignment","validateResult","Utilities","validateVisitorCode","error","i","rules","featureKey","featureFlagId","defaultVariationKey","n","_createForOfIteratorHelper","s","done","rule","segment","exposition","respoolTime","variationByExposition","targetingResult","checkTargeting","variationConfiguration","ruleHash","Hasher","obtainFeatureFlagHashDouble","campaignId","x","totalDeviation","variationHash","A","variation","type","RuleType","EXPERIMENTATION","updateStorageData","variationId","restParameters","_objectWithoutProperties","_excluded","_objectSpread","e","f","TARGETED_DELIVERY","variationKey","getExperimentVariation","c","deviations","hashDouble","obtainExperimentHashDouble","isUnallocated","resultVariationId","k","deviation","NotAllocated","variationResult","StorageParse","updatedVariationData","updatedVariation","assignmentDate","Date","toString","write","exports"],"sources":["../../src/variationConfiguration/variationConfiguration.ts"],"sourcesContent":["import { Err, Ok, Result } from 'ts-res';\nimport { ExperimentItemType } from 'src/campaignConfiguration';\nimport { Hasher } from 'src/hasher';\nimport { KameleoonError, KameleoonException } from 'src/kameleoonError';\nimport {\n IExternalStorage,\n VariationDataType,\n ExperimentVariationsType,\n} from 'src/storage';\nimport { Utilities } from 'src/utilities';\nimport { RuleType } from 'src/campaignConfiguration/types';\nimport {\n FeatureFlagVariationType,\n GetFeatureFlagVariationParametersType,\n UpdateStorageParamsType,\n} from './types';\n\ninterface IVariationConfiguration {\n /**\n * @method getAssociatedVariationId - gets the id of a variation assigned to a visitor with `visitorCode` from the storage. If the visitor doesn't have associated variation yet, assigns a new one, stores it and returns it's id.\n */\n getAssociatedVariationId: (\n visitorCode: string,\n experimentId: number,\n ) => Result<number, KameleoonError>;\n /**\n * @method getAssignedVariations - obtains experiments with according variations that are *already assigned* and *stored* in the `variationStorage`. If `getAssociatedVariationId` had not been called before, there will be no variations to get.\n */\n getAssignedVariations(\n visitorCode: string,\n ): Result<ExperimentVariationsType, KameleoonError>;\n /**\n * @method getFeatureFlagVariation - obtains variation for the rule of the feature flag which is both targeted and has a variation, if no such rule was found, returns empty rule with default `variationKey`, given an option `withAssignment` of `true` will also assign a new variation and save it in the storage\n */\n getFeatureFlagVariation: (\n params: GetFeatureFlagVariationParametersType,\n ) => Result<FeatureFlagVariationType, KameleoonError>;\n /**\n * @method getExperimentVariation - obtains variation for the experiment by calculating the variation for the visitor *without assigning* it to a visitor\n */\n getExperimentVariation: (\n visitorCode: string,\n experiment: ExperimentItemType,\n ) => Result<number, KameleoonError>;\n}\n\nexport class VariationConfiguration implements IVariationConfiguration {\n private experiments: ExperimentItemType[];\n private storage: IExternalStorage<VariationDataType>;\n\n constructor(\n experiments: ExperimentItemType[],\n externalStorage: IExternalStorage<VariationDataType>,\n ) {\n this.experiments = experiments;\n this.storage = externalStorage;\n }\n\n public getAssociatedVariationId = (\n visitorCode: string,\n experimentId: number,\n ): Result<number, KameleoonError> => {\n // TODO:\n // --- Variation from storage is not checked anymore\n // --- If nothing changes the commented code should be deleted\n // along with the `Variation` class and according tests/types\n // --- Mind `todo` in `getAssociatedVariationId.spec.ts`\n //\n // let associatedVariation: VariationType | undefined;\n\n // this.storage.read().and((data) => {\n // associatedVariation = data?.[visitorCode]?.[experimentId];\n // });\n\n const experiment = this.experiments.find(\n (experiment) => experiment.id === String(experimentId),\n );\n\n if (!experiment) {\n return Err(\n new KameleoonError(\n KameleoonException.ExperimentConfigurationNotFound,\n experimentId,\n visitorCode,\n ),\n );\n }\n\n // const variation = new Variation(associatedVariation);\n\n // if (variation.isValid(experiment?.respoolTime)) {\n // return Ok(variation.id);\n // }\n\n return this.assignVariation(visitorCode, experiment);\n };\n\n public getAssignedVariations(\n visitorCode: string,\n ): Result<ExperimentVariationsType, KameleoonError> {\n const result = this.storage.read();\n\n if (!result.ok) {\n return result;\n }\n\n const experiments = result.data[visitorCode];\n\n if (!experiments) {\n return Err(\n new KameleoonError(KameleoonException.StorageRead, visitorCode),\n );\n }\n\n return Ok(experiments);\n }\n\n public getFeatureFlagVariation({\n visitorCode,\n featureFlag,\n targetingData,\n packageInfo,\n campaignConfiguration,\n withAssignment = false,\n }: GetFeatureFlagVariationParametersType): Result<\n FeatureFlagVariationType,\n KameleoonError\n > {\n const validateResult = Utilities.validateVisitorCode(visitorCode);\n\n if (!validateResult.ok) {\n return Err(validateResult.error);\n }\n\n const {\n rules,\n featureKey,\n id: featureFlagId,\n defaultVariationKey,\n } = featureFlag;\n\n for (const rule of rules) {\n const {\n segment,\n experimentId,\n id,\n exposition,\n respoolTime,\n variationByExposition,\n } = rule;\n\n // --- Check targeting ---\n const targetingResult = Utilities.checkTargeting({\n segment,\n visitorCode,\n experimentId,\n targetingData,\n packageInfo,\n campaignConfiguration,\n variationConfiguration: this,\n });\n\n if (!targetingResult.ok) {\n return targetingResult;\n }\n\n if (!targetingResult.data) {\n continue;\n }\n\n // --- Note ---\n // Calculate new variation (and assign it if `withAssignment` is `true`)\n // First the the exposed rule is calculated and then the variation inside of it\n const ruleHash = Hasher.obtainFeatureFlagHashDouble({\n visitorCode,\n respoolTime,\n campaignId: String(id),\n });\n\n if (ruleHash <= exposition) {\n let totalDeviation = 0;\n\n const variationHash = Hasher.obtainFeatureFlagHashDouble({\n visitorCode,\n respoolTime,\n campaignId: String(experimentId),\n });\n\n for (const variation of variationByExposition) {\n totalDeviation += variation.exposition;\n\n if (variationHash <= totalDeviation) {\n // --- Note ---\n // For the `RuleType.EXPERIMENTATION` variation assignment must happen without\n // calculating experiment deviation\n if (rule.type === RuleType.EXPERIMENTATION && withAssignment) {\n this.updateStorageData({\n visitorCode,\n experimentId: String(experimentId),\n // --- Note ---\n // For `RuleType.EXPERIMENTATION` `variationId` can not be null\n variationId: variation.variationId as number,\n });\n }\n\n const { exposition, ...restParameters } = variation;\n\n return Ok({\n ...restParameters,\n rule,\n featureFlagId,\n featureKey,\n experimentId,\n });\n }\n }\n }\n\n if (rule.type === RuleType.TARGETED_DELIVERY) {\n break;\n }\n }\n\n return Ok({\n featureKey,\n featureFlagId,\n experimentId: null,\n variationKey: defaultVariationKey,\n variationId: null,\n rule: null,\n });\n }\n\n public getExperimentVariation(\n visitorCode: string,\n experiment: ExperimentItemType,\n ): Result<number, KameleoonError> {\n const { id, respoolTime, deviations } = experiment;\n\n const hashDouble = Hasher.obtainExperimentHashDouble({\n visitorCode,\n campaignId: id,\n respoolTime,\n });\n\n let isUnallocated = true;\n let totalDeviation = 0;\n let resultVariationId = 0;\n\n for (const deviation of deviations) {\n totalDeviation += deviation.value;\n\n if (totalDeviation >= hashDouble) {\n // --- Note ---\n // If sum of all variations is less than 1 (100%), then there is a chance\n // user will end up outside of any variation\n isUnallocated = false;\n // --- Note ---\n // 'origin' is the default `variationId`, it must return id of 0\n if (deviation.variationId !== 'origin') {\n resultVariationId = Number(deviation.variationId);\n }\n\n break;\n }\n }\n\n if (isUnallocated) {\n return Err(\n new KameleoonError(KameleoonException.NotAllocated, visitorCode),\n );\n }\n\n return Ok(resultVariationId);\n }\n\n private assignVariation(\n visitorCode: string,\n experiment: ExperimentItemType,\n ): Result<number, KameleoonError> {\n const variationResult = this.getExperimentVariation(\n visitorCode,\n experiment,\n );\n\n if (!variationResult.ok) {\n return variationResult;\n }\n\n const result = this.updateStorageData({\n visitorCode,\n experimentId: experiment.id,\n variationId: variationResult.data,\n });\n\n if (!result.ok) {\n return result;\n }\n\n return Ok(variationResult.data);\n }\n\n private updateStorageData({\n visitorCode,\n experimentId,\n variationId,\n }: UpdateStorageParamsType): Result<void, KameleoonError> {\n const result = this.storage.read();\n\n if (!result.ok) {\n // --- Note ---\n // If there is a parsing error we return error\n // But if there is `LocalStorageRead` error it means that\n // data was read successfully and it's empty, which is not problem\n // cause that might be initial `read`\n if (result.error.type === KameleoonException.StorageParse) {\n return result;\n }\n }\n\n const updatedVariationData: VariationDataType = result.ok\n ? result.data\n : {};\n\n const updatedVariation = {\n variationId,\n assignmentDate: new Date().toString(),\n };\n\n if (updatedVariationData[visitorCode]) {\n updatedVariationData[visitorCode][experimentId] = updatedVariation;\n } else {\n updatedVariationData[visitorCode] = {\n [experimentId]: updatedVariation,\n };\n }\n\n return this.storage.write(updatedVariationData);\n }\n\n // --- TODO: commented out in case of future logic rework ---\n // --- Mind: `variationId` could cause the `undefined` issue ---\n // --- Reference: https://development.kameleoon.net/sdk/javascript-sdk-packages/-/merge_requests/89 ---\n // private getAssignedRuleVariation(\n // visitorCode: string,\n // rule: RuleItemType,\n // ): Result<GetAssignedRuleVariationResultType, KameleoonError> {\n // const emptyVariation: Omit<VariationByExpositionType, 'exposition'> = {\n // variationId: null,\n // variationKey: '',\n // };\n // const { experimentId, variationByExposition } = rule;\n\n // const variationsResult = this.getAssignedVariations(visitorCode);\n\n // // --- Note ---\n // // If there is an error, we just want to skip seeking\n // // for variation in the storage, not throw an error\n // if (!variationsResult.ok) {\n // return Ok(emptyVariation);\n // }\n\n // const variations = variationsResult.data;\n\n // if (\n // experimentId &&\n // typeof variations[experimentId].variationId !== undefined\n // ) {\n // const id = variations[experimentId].variationId;\n // const featureFlagVariation = variationByExposition.find(\n // (item) => item.variationId == id,\n // );\n\n // if (!featureFlagVariation) {\n // // --- Note ---\n // // This error is not described for public methods as\n // // it serves internal purpose of checking whether\n // // the persistent variation has been updated and should be re-allocated\n // return Err(new KameleoonError(KameleoonException.DataInconsistency));\n // }\n\n // const { exposition, ...restParameters } = featureFlagVariation;\n\n // return Ok({ ...restParameters });\n // }\n\n // return Ok(emptyVariation);\n // }\n}\n"],"mappings":"iuHA8Ca,CAAAA,sBAAsB,yBAIjC,SAAAC,EACEC,CAAiC,CACjCC,CAAoD,CACpD,KAAAC,CAAA,MAAAC,eAAA,MAAAJ,CAAA,EAAAK,eAAA,4BAAAA,eAAA,wBAAAA,eAAA,iCAKgC,SAChCC,CAAmB,CACnBC,CAAoB,CACe,CACnC;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AAEA,GAAM,CAAAC,CAAU,CAAGL,CAAI,CAACF,WAAW,CAACQ,IAAI,CACtC,SAACD,CAAU,QAAK,CAAAA,CAAU,CAACE,EAAE,GAAYH,CAAY,GAAC,EACvD,CAAC,MAEG,CAAAC,CAAU,CAgBRL,CAAI,CAACQ,eAAe,CAACL,CAAW,CAAEE,CAAU,CAAC,CAf3C,GAAAI,UAAG,EACR,GAAI,CAAAC,8BAAc,CAChBC,kCAAkB,CAACC,+BAA+B,CAClDR,CAAY,CACZD,CAAW,CACZ,CACF,CAGH;AAEA;AACA;AAC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AChD,CAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AAEA;AACA;AACA;AACA;AACA;AACA;AAEA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AAEA;AACA;AAEA;AACA;AAAA,IAAAzF,CAAA,IAAA+F,OAAA,CAAAhG,sBAAA,CAAAA,sBAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kameleoon/javascript-sdk-core",
3
- "version": "2.9.0",
3
+ "version": "2.9.1",
4
4
  "description": "Kameleoon JS SDK Core",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -50,4 +50,4 @@
50
50
  "ts-jest": "^27.0.7",
51
51
  "tsc-alias": "^1.8.7"
52
52
  }
53
- }
53
+ }
@@ -1,11 +0,0 @@
1
- import { Result } from 'ts-res';
2
- import { KameleoonError } from '../kameleoonError';
3
- import { IExternalStorage, IStorage, StorageDataType } from './types';
4
- export declare class ExternalStorage<T extends StorageDataType> implements IStorage<T> {
5
- private key;
6
- private storage;
7
- constructor(externalStorage: IExternalStorage);
8
- read(): Result<T, KameleoonError>;
9
- write(data: T): Result<void, KameleoonError>;
10
- private parse;
11
- }
@@ -1,2 +0,0 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ExternalStorage=void 0;var _tsRes=require("ts-res"),_kameleoonError=require("../kameleoonError");function _typeof(a){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,_toPropertyKey(c.key),c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),Object.defineProperty(a,"prototype",{writable:!1}),a}function _defineProperty(a,b,c){return b=_toPropertyKey(b),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)||null===a)return a;var c=a[Symbol.toPrimitive];if(c!==void 0){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)}var ExternalStorage=/*#__PURE__*/function(){function a(b){_classCallCheck(this,a),_defineProperty(this,"key",void 0),_defineProperty(this,"storage",void 0),this.key=b.key,this.storage=b}return _createClass(a,[{key:"read",value:function read(){var a=this.storage.read();return a.ok?this.parse(a.data):a}},{key:"write",value:function write(a){return this.storage.write(JSON.stringify(a)),(0,_tsRes.Ok)()}},{key:"parse",value:function parse(a){var b;try{b=JSON.parse(a)}catch(a){throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.StorageParse,a,this.key)}return(0,_tsRes.Ok)(b)}}]),a}();exports.ExternalStorage=ExternalStorage;
2
- //# sourceMappingURL=externalStorage.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"externalStorage.js","names":["ExternalStorage","a","externalStorage","_classCallCheck","_defineProperty","key","storage","_createClass","value","read","result","ok","parse","data","write","JSON","stringify","Ok","err","KameleoonError","KameleoonException","StorageParse","exports"],"sources":["../../src/storage/externalStorage.ts"],"sourcesContent":["import { Ok, Result } from 'ts-res';\nimport { KameleoonError, KameleoonException } from 'src/kameleoonError';\nimport { IExternalStorage, IStorage, StorageDataType } from './types';\nimport { KameleoonStorageKey } from './constants';\n\nexport class ExternalStorage<T extends StorageDataType> implements IStorage<T> {\n private key: KameleoonStorageKey;\n private storage: IExternalStorage;\n\n constructor(externalStorage: IExternalStorage) {\n this.key = externalStorage.key;\n this.storage = externalStorage;\n }\n\n public read(): Result<T, KameleoonError> {\n const result = this.storage.read();\n\n if (!result.ok) {\n return result;\n }\n\n return this.parse(result.data);\n }\n\n public write(data: T): Result<void, KameleoonError> {\n this.storage.write(JSON.stringify(data));\n\n return Ok();\n }\n\n private parse(data: string): Result<T, KameleoonError> {\n let result: T;\n\n try {\n result = JSON.parse(data);\n } catch (err) {\n throw new KameleoonError(KameleoonException.StorageParse, err, this.key);\n }\n\n return Ok(result);\n }\n}\n"],"mappings":"y5CAKa,CAAAA,eAAe,yBAI1B,SAAAC,EAAYC,CAAiC,CAAE,CAAAC,eAAA,MAAAF,CAAA,EAAAG,eAAA,oBAAAA,eAAA,wBAC7C,IAAI,CAACC,GAAG,CAAGH,CAAe,CAACG,GAAG,CAC9B,IAAI,CAACC,OAAO,CAAGJ,CACjB,CAAC,OAAAK,YAAA,CAAAN,CAAA,GAAAI,GAAA,QAAAG,KAAA,CAED,SAAAC,KAAA,CAAyC,CACvC,GAAM,CAAAC,CAAM,CAAG,IAAI,CAACJ,OAAO,CAACG,IAAI,EAAE,CAAC,MAE9B,CAAAC,CAAM,CAACC,EAAE,CAIP,IAAI,CAACC,KAAK,CAACF,CAAM,CAACG,IAAI,CAAC,CAHrBH,CAIX,CAAC,GAAAL,GAAA,SAAAG,KAAA,CAED,SAAAM,MAAaD,CAAO,CAAgC,CAGlD,MAFA,KAAI,CAACP,OAAO,CAACQ,KAAK,CAACC,IAAI,CAACC,SAAS,CAACH,CAAI,CAAC,CAAC,CAEjC,GAAAI,SAAE,GACX,CAAC,GAAAZ,GAAA,SAAAG,KAAA,CAED,SAAAI,MAAcC,CAAY,CAA6B,CACrD,GAAI,CAAAH,CAAS,CAEb,GAAI,CACFA,CAAM,CAAGK,IAAI,CAACH,KAAK,CAACC,CAAI,CAC1B,CAAE,MAAOK,CAAG,CAAE,CACZ,KAAM,IAAI,CAAAC,8BAAc,CAACC,kCAAkB,CAACC,YAAY,CAAEH,CAAG,CAAE,IAAI,CAACb,GAAG,CACzE,CAEA,MAAO,GAAAY,SAAE,EAACP,CAAM,CAClB,CAAC,IAAAT,CAAA,IAAAqB,OAAA,CAAAtB,eAAA,CAAAA,eAAA"}