@omniconvert/server-side-testing-sdk
Version:
TypeScript SDK for server-side A/B testing and experimentation
137 lines • 3.45 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Decision = void 0;
const Experiment_1 = require("./Experiment");
/**
* Decision entity representing the result of experiment assignment
* Contains the experiment, variation, and when the decision was made
*/
class Decision {
constructor(experiment, variation) {
this.experiment = experiment;
this.variation = variation;
this.madeAt = new Date();
}
/**
* Get the experiment
*/
getExperiment() {
return this.experiment;
}
/**
* Get the experiment key (slug)
*/
getExperimentKey() {
return this.experiment.slug;
}
/**
* Get the experiment ID
*/
getExperimentId() {
return this.experiment.id;
}
/**
* Get the variation
*/
getVariation() {
return this.variation;
}
/**
* Get the variation key (slug)
*/
getVariationKey() {
return this.variation.slug;
}
/**
* Get the variation ID
*/
getVariationId() {
return this.variation.id;
}
/**
* Get all variation variables
*/
getVariationVariables() {
return this.variation.variables || [];
}
/**
* Get a specific variation variable by slug
*/
getVariationVariable(slug) {
const variable = this.variation.variables?.find(v => v.slug === slug);
return variable?.value ?? null;
}
/**
* Get variation variable with type safety
*/
getVariationVariableAs(slug) {
const value = this.getVariationVariable(slug);
return value !== null ? value : null;
}
/**
* Check if a variation variable exists
*/
hasVariationVariable(slug) {
return this.variation.variables?.some(v => v.slug === slug) ?? false;
}
/**
* Get the timestamp when the decision was made
*/
getMadeAt() {
return this.madeAt;
}
/**
* Get variation JavaScript code
*/
getVariationJavascript() {
return this.variation.javascript;
}
/**
* Get variation CSS code
*/
getVariationCss() {
return this.variation.css;
}
/**
* Get variation traffic allocation
*/
getVariationTrafficAllocation() {
return this.variation.traffic_allocation || 0;
}
/**
* Convert to plain object for serialization
*/
toObject() {
return {
experiment: this.experiment.toObject(),
variation: this.variation,
madeAt: this.madeAt.toISOString(),
};
}
/**
* Create decision from plain object (for deserialization)
*/
static fromObject(obj) {
const experiment = Experiment_1.Experiment.fromObject(obj.experiment);
const variation = obj.variation;
const decision = new Decision(experiment, variation);
if (obj.madeAt) {
decision.madeAt = new Date(obj.madeAt);
}
return decision;
}
/**
* Convert to JSON
*/
toJSON() {
return this.toObject();
}
/**
* Get a summary of the decision for logging/debugging
*/
getSummary() {
return `Experiment: ${this.getExperimentKey()} (${this.getExperimentId()}), Variation: ${this.getVariationKey()} (${this.getVariationId()})`;
}
}
exports.Decision = Decision;
//# sourceMappingURL=Decision.js.map