@eppo/js-client-sdk-common
Advanced tools
Comparing version 1.4.1 to 1.5.0
@@ -35,3 +35,3 @@ import { IAssignmentHooks } from '../assignment-hooks'; | ||
getNumericAssignment(subjectKey: string, flagKey: string, subjectAttributes?: Record<string, any>, assignmentHooks?: IAssignmentHooks): number | null; | ||
getJSONAssignment(subjectKey: string, flagKey: string, subjectAttributes?: Record<string, any>, assignmentHooks?: IAssignmentHooks): string | null; | ||
getJSONStringAssignment(subjectKey: string, flagKey: string, subjectAttributes?: Record<string, any>, assignmentHooks?: IAssignmentHooks): string | null; | ||
} | ||
@@ -47,3 +47,4 @@ export default class EppoClient implements IEppoClient { | ||
getNumericAssignment(subjectKey: string, flagKey: string, subjectAttributes?: Record<string, EppoValue>, assignmentHooks?: IAssignmentHooks | undefined): number | null; | ||
getJSONAssignment(subjectKey: string, flagKey: string, subjectAttributes?: Record<string, EppoValue>, assignmentHooks?: IAssignmentHooks | undefined): string | null; | ||
getJSONStringAssignment(subjectKey: string, flagKey: string, subjectAttributes?: Record<string, EppoValue>, assignmentHooks?: IAssignmentHooks | undefined): string | null; | ||
getParsedJSONAssignment(subjectKey: string, flagKey: string, subjectAttributes?: Record<string, EppoValue>, assignmentHooks?: IAssignmentHooks | undefined): object | null; | ||
private getAssignmentVariation; | ||
@@ -50,0 +51,0 @@ private getAssignmentInternal; |
@@ -14,24 +14,30 @@ "use strict"; | ||
} | ||
// @deprecated getAssignment is deprecated in favor of the typed get<Type>Assignment methods | ||
getAssignment(subjectKey, flagKey, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
subjectAttributes = {}, assignmentHooks) { | ||
var _a, _b; | ||
return ((_b = (_a = this.getAssignmentVariation(subjectKey, flagKey, subjectAttributes, assignmentHooks, eppo_value_1.ValueType.StringType)) === null || _a === void 0 ? void 0 : _a.stringValue) !== null && _b !== void 0 ? _b : null); | ||
var _a; | ||
return ((_a = this.getAssignmentVariation(subjectKey, flagKey, subjectAttributes, assignmentHooks) | ||
.stringValue) !== null && _a !== void 0 ? _a : null); | ||
} | ||
getStringAssignment(subjectKey, flagKey, subjectAttributes = {}, assignmentHooks) { | ||
var _a, _b; | ||
return ((_b = (_a = this.getAssignmentVariation(subjectKey, flagKey, subjectAttributes, assignmentHooks, eppo_value_1.ValueType.StringType)) === null || _a === void 0 ? void 0 : _a.stringValue) !== null && _b !== void 0 ? _b : null); | ||
var _a; | ||
return ((_a = this.getAssignmentVariation(subjectKey, flagKey, subjectAttributes, assignmentHooks, eppo_value_1.ValueType.StringType).stringValue) !== null && _a !== void 0 ? _a : null); | ||
} | ||
getBoolAssignment(subjectKey, flagKey, subjectAttributes = {}, assignmentHooks) { | ||
var _a, _b; | ||
return ((_b = (_a = this.getAssignmentVariation(subjectKey, flagKey, subjectAttributes, assignmentHooks, eppo_value_1.ValueType.BoolType)) === null || _a === void 0 ? void 0 : _a.boolValue) !== null && _b !== void 0 ? _b : null); | ||
var _a; | ||
return ((_a = this.getAssignmentVariation(subjectKey, flagKey, subjectAttributes, assignmentHooks, eppo_value_1.ValueType.BoolType).boolValue) !== null && _a !== void 0 ? _a : null); | ||
} | ||
getNumericAssignment(subjectKey, flagKey, subjectAttributes, assignmentHooks) { | ||
var _a, _b; | ||
return ((_b = (_a = this.getAssignmentVariation(subjectKey, flagKey, subjectAttributes, assignmentHooks, eppo_value_1.ValueType.NumericType)) === null || _a === void 0 ? void 0 : _a.numericValue) !== null && _b !== void 0 ? _b : null); | ||
var _a; | ||
return ((_a = this.getAssignmentVariation(subjectKey, flagKey, subjectAttributes, assignmentHooks, eppo_value_1.ValueType.NumericType).numericValue) !== null && _a !== void 0 ? _a : null); | ||
} | ||
getJSONAssignment(subjectKey, flagKey, subjectAttributes = {}, assignmentHooks) { | ||
var _a, _b; | ||
return ((_b = (_a = this.getAssignmentVariation(subjectKey, flagKey, subjectAttributes, assignmentHooks, eppo_value_1.ValueType.StringType)) === null || _a === void 0 ? void 0 : _a.stringValue) !== null && _b !== void 0 ? _b : null); | ||
getJSONStringAssignment(subjectKey, flagKey, subjectAttributes = {}, assignmentHooks) { | ||
var _a; | ||
return ((_a = this.getAssignmentVariation(subjectKey, flagKey, subjectAttributes, assignmentHooks, eppo_value_1.ValueType.JSONType).stringValue) !== null && _a !== void 0 ? _a : null); | ||
} | ||
getParsedJSONAssignment(subjectKey, flagKey, subjectAttributes = {}, assignmentHooks) { | ||
var _a; | ||
return ((_a = this.getAssignmentVariation(subjectKey, flagKey, subjectAttributes, assignmentHooks, eppo_value_1.ValueType.JSONType).objectValue) !== null && _a !== void 0 ? _a : null); | ||
} | ||
getAssignmentVariation(subjectKey, flagKey, subjectAttributes = {}, assignmentHooks, valueType) { | ||
@@ -44,4 +50,3 @@ const { allocationKey, assignment } = this.getAssignmentInternal(subjectKey, flagKey, subjectAttributes, assignmentHooks, valueType); | ||
} | ||
getAssignmentInternal(subjectKey, flagKey, subjectAttributes = {}, assignmentHooks, valueType) { | ||
var _a; | ||
getAssignmentInternal(subjectKey, flagKey, subjectAttributes = {}, assignmentHooks, expectedValueType) { | ||
(0, validation_1.validateNotBlank)(subjectKey, 'Invalid argument: subjectKey cannot be blank'); | ||
@@ -51,6 +56,7 @@ (0, validation_1.validateNotBlank)(flagKey, 'Invalid argument: flagKey cannot be blank'); | ||
const experimentConfig = this.configurationStore.get(flagKey); | ||
const allowListOverride = this.getSubjectVariationOverride(subjectKey, experimentConfig, valueType); | ||
if (allowListOverride) { | ||
if (!allowListOverride.isExpectedType()) | ||
const allowListOverride = this.getSubjectVariationOverride(subjectKey, experimentConfig, expectedValueType); | ||
if (!allowListOverride.isNullType()) { | ||
if (!allowListOverride.isExpectedType()) { | ||
return nullAssignment; | ||
} | ||
return Object.assign(Object.assign({}, nullAssignment), { assignment: allowListOverride }); | ||
@@ -80,20 +86,7 @@ } | ||
const shard = (0, shard_1.getShard)(`assignment-${subjectKey}-${flagKey}`, subjectShards); | ||
const assignedVariation = (_a = variations.find((variation) => (0, shard_1.isShardInRange)(shard, variation.shardRange))) === null || _a === void 0 ? void 0 : _a.typedValue; | ||
const assignedVariation = variations.find((variation) => (0, shard_1.isShardInRange)(shard, variation.shardRange)); | ||
const internalAssignment = { | ||
allocationKey: matchedRule.allocationKey, | ||
assignment: eppo_value_1.EppoValue.Null(), | ||
assignment: eppo_value_1.EppoValue.generateEppoValue(expectedValueType, assignedVariation === null || assignedVariation === void 0 ? void 0 : assignedVariation.value, assignedVariation === null || assignedVariation === void 0 ? void 0 : assignedVariation.typedValue), | ||
}; | ||
switch (valueType) { | ||
case eppo_value_1.ValueType.BoolType: | ||
internalAssignment['assignment'] = eppo_value_1.EppoValue.Bool(assignedVariation); | ||
break; | ||
case eppo_value_1.ValueType.NumericType: | ||
internalAssignment['assignment'] = eppo_value_1.EppoValue.Numeric(assignedVariation); | ||
break; | ||
case eppo_value_1.ValueType.StringType: | ||
internalAssignment['assignment'] = eppo_value_1.EppoValue.String(assignedVariation); | ||
break; | ||
default: | ||
return nullAssignment; | ||
} | ||
return internalAssignment.assignment.isExpectedType() ? internalAssignment : nullAssignment; | ||
@@ -140,18 +133,7 @@ } | ||
} | ||
getSubjectVariationOverride(subjectKey, experimentConfig, valueType) { | ||
getSubjectVariationOverride(subjectKey, experimentConfig, expectedValueType) { | ||
const subjectHash = md5(subjectKey); | ||
const overridden = (experimentConfig === null || experimentConfig === void 0 ? void 0 : experimentConfig.typedOverrides) && experimentConfig.typedOverrides[subjectHash]; | ||
if (overridden) { | ||
switch (valueType) { | ||
case eppo_value_1.ValueType.BoolType: | ||
return eppo_value_1.EppoValue.Bool(overridden); | ||
case eppo_value_1.ValueType.NumericType: | ||
return eppo_value_1.EppoValue.Numeric(overridden); | ||
case eppo_value_1.ValueType.StringType: | ||
return eppo_value_1.EppoValue.String(overridden); | ||
default: | ||
return null; | ||
} | ||
} | ||
return null; | ||
const override = (experimentConfig === null || experimentConfig === void 0 ? void 0 : experimentConfig.overrides) && experimentConfig.overrides[subjectHash]; | ||
const typedOverride = (experimentConfig === null || experimentConfig === void 0 ? void 0 : experimentConfig.typedOverrides) && experimentConfig.typedOverrides[subjectHash]; | ||
return eppo_value_1.EppoValue.generateEppoValue(expectedValueType, override, typedOverride); | ||
} | ||
@@ -158,0 +140,0 @@ /** |
@@ -7,3 +7,4 @@ import { IAllocation } from './allocation-dto'; | ||
subjectShards: number; | ||
typedOverrides: Record<string, string>; | ||
overrides: Record<string, string>; | ||
typedOverrides: Record<string, number | boolean | string | object>; | ||
allocations: Record<string, IAllocation>; | ||
@@ -10,0 +11,0 @@ rules: IRule[]; |
@@ -8,2 +8,3 @@ import { IValue } from '../eppo_value'; | ||
name: string; | ||
value: string; | ||
typedValue: IValue; | ||
@@ -10,0 +11,0 @@ shardRange: IShardRange; |
@@ -5,3 +5,4 @@ export declare enum ValueType { | ||
NumericType = 2, | ||
StringType = 3 | ||
StringType = 3, | ||
JSONType = 4 | ||
} | ||
@@ -14,3 +15,5 @@ export declare type IValue = boolean | number | string | undefined; | ||
stringValue: string | undefined; | ||
objectValue: object | undefined; | ||
private constructor(); | ||
static generateEppoValue(expectedValueType?: ValueType, value?: string, typedValue?: boolean | number | string | object): EppoValue; | ||
toString(): string; | ||
@@ -22,4 +25,5 @@ isExpectedType(): boolean; | ||
static String(value: string): EppoValue; | ||
static JSON(value: string, typedValue: object): EppoValue; | ||
static Null(): EppoValue; | ||
} | ||
//# sourceMappingURL=eppo_value.d.ts.map |
@@ -10,5 +10,6 @@ "use strict"; | ||
ValueType[ValueType["StringType"] = 3] = "StringType"; | ||
ValueType[ValueType["JSONType"] = 4] = "JSONType"; | ||
})(ValueType = exports.ValueType || (exports.ValueType = {})); | ||
class EppoValue { | ||
constructor(valueType, boolValue, numericValue, stringValue) { | ||
constructor(valueType, boolValue, numericValue, stringValue, objectValue) { | ||
this.valueType = valueType; | ||
@@ -18,5 +19,23 @@ this.boolValue = boolValue; | ||
this.stringValue = stringValue; | ||
this.objectValue = objectValue; | ||
} | ||
static generateEppoValue(expectedValueType, value, typedValue) { | ||
if (value != null && typedValue != null) { | ||
switch (expectedValueType) { | ||
case ValueType.BoolType: | ||
return EppoValue.Bool(typedValue); | ||
case ValueType.NumericType: | ||
return EppoValue.Numeric(typedValue); | ||
case ValueType.StringType: | ||
return EppoValue.String(typedValue); | ||
case ValueType.JSONType: | ||
return EppoValue.JSON(value, typedValue); | ||
default: | ||
return EppoValue.String(value); | ||
} | ||
} | ||
return EppoValue.Null(); | ||
} | ||
toString() { | ||
var _a; | ||
var _a, _b, _c; | ||
switch (this.valueType) { | ||
@@ -31,2 +50,9 @@ case ValueType.NullType: | ||
return (_a = this.stringValue) !== null && _a !== void 0 ? _a : ''; | ||
case ValueType.JSONType: | ||
try { | ||
return (_b = JSON.stringify(this.objectValue)) !== null && _b !== void 0 ? _b : ''; | ||
} | ||
catch (_d) { | ||
return (_c = this.stringValue) !== null && _c !== void 0 ? _c : ''; | ||
} | ||
} | ||
@@ -42,4 +68,13 @@ } | ||
return typeof this.stringValue === 'string'; | ||
default: | ||
return true; | ||
case ValueType.JSONType: | ||
try { | ||
return (typeof this.objectValue === 'object' && | ||
typeof this.stringValue === 'string' && | ||
JSON.stringify(JSON.parse(this.stringValue)) === JSON.stringify(this.objectValue)); | ||
} | ||
catch (_a) { | ||
return false; | ||
} | ||
case ValueType.NullType: | ||
return false; | ||
} | ||
@@ -51,12 +86,15 @@ } | ||
static Bool(value) { | ||
return new EppoValue(ValueType.BoolType, value, undefined, undefined); | ||
return new EppoValue(ValueType.BoolType, value, undefined, undefined, undefined); | ||
} | ||
static Numeric(value) { | ||
return new EppoValue(ValueType.NumericType, undefined, value, undefined); | ||
return new EppoValue(ValueType.NumericType, undefined, value, undefined, undefined); | ||
} | ||
static String(value) { | ||
return new EppoValue(ValueType.StringType, undefined, undefined, value); | ||
return new EppoValue(ValueType.StringType, undefined, undefined, value, undefined); | ||
} | ||
static JSON(value, typedValue) { | ||
return new EppoValue(ValueType.JSONType, undefined, undefined, value, typedValue); | ||
} | ||
static Null() { | ||
return new EppoValue(ValueType.NullType, undefined, undefined, undefined); | ||
return new EppoValue(ValueType.NullType, undefined, undefined, undefined, undefined); | ||
} | ||
@@ -63,0 +101,0 @@ } |
{ | ||
"name": "@eppo/js-client-sdk-common", | ||
"version": "1.4.1", | ||
"version": "1.5.0", | ||
"description": "Eppo SDK for client-side JavaScript applications (base for both web and react native)", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
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
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
146944
739