@splitsoftware/splitio-commons
Version:
Split JavaScript SDK common components
97 lines (96 loc) • 5.01 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.sdkManagerFactory = void 0;
var objectAssign_1 = require("../utils/lang/objectAssign");
var thenable_1 = require("../utils/promise/thenable");
var lang_1 = require("../utils/lang");
var inputValidation_1 = require("../utils/inputValidation");
var mode_1 = require("../utils/settingsValidation/mode");
var constants_1 = require("../utils/constants");
function collectTreatments(splitObject) {
var conditions = splitObject.conditions;
// Rollout conditions are supposed to have the entire partitions list, so we find the first one.
var allTreatmentsCondition = (0, lang_1.find)(conditions, function (cond) { return cond.conditionType === 'ROLLOUT'; });
// Localstorage mode could fall into a no rollout conditions state. Take the first condition in that case.
if (!allTreatmentsCondition)
allTreatmentsCondition = conditions[0];
// Then extract the treatments from the partitions
return allTreatmentsCondition ? allTreatmentsCondition.partitions.map(function (v) { return v.treatment; }) : [];
}
function objectToView(splitObject) {
if (!splitObject)
return null;
return {
name: splitObject.name,
trafficType: splitObject.trafficTypeName,
killed: splitObject.killed,
changeNumber: splitObject.changeNumber || 0,
treatments: collectTreatments(splitObject),
configs: splitObject.configurations || {},
sets: splitObject.sets || [],
defaultTreatment: splitObject.defaultTreatment,
impressionsDisabled: splitObject.impressionsDisabled === true,
prerequisites: (splitObject.prerequisites || []).map(function (p) { return ({ flagName: p.n, treatments: p.ts }); }),
};
}
function objectsToViews(splitObjects) {
var views = [];
splitObjects.forEach(function (split) {
var view = objectToView(split);
if (view)
views.push(view);
});
return views;
}
function sdkManagerFactory(settings, splits, _a) {
var readinessManager = _a.readinessManager, sdkStatus = _a.sdkStatus;
var log = settings.log, mode = settings.mode;
var isAsync = (0, mode_1.isConsumerMode)(mode);
return (0, objectAssign_1.objectAssign)(
// Proto-linkage of the readiness Event Emitter
Object.create(sdkStatus), {
/**
* Get the feature flag object corresponding to the given feature flag name if valid
*/
split: function (featureFlagName) {
var splitName = (0, inputValidation_1.validateSplit)(log, featureFlagName, constants_1.SPLIT_FN_LABEL);
if (!(0, inputValidation_1.validateIfNotDestroyed)(log, readinessManager, constants_1.SPLIT_FN_LABEL) || !(0, inputValidation_1.validateIfOperational)(log, readinessManager, constants_1.SPLIT_FN_LABEL) || !splitName) {
return isAsync ? Promise.resolve(null) : null;
}
var split = splits.getSplit(splitName);
if ((0, thenable_1.thenable)(split)) {
return split.catch(function () { return null; }).then(function (result) {
(0, inputValidation_1.validateSplitExistence)(log, readinessManager, splitName, result, constants_1.SPLIT_FN_LABEL);
return objectToView(result);
});
}
(0, inputValidation_1.validateSplitExistence)(log, readinessManager, splitName, split, constants_1.SPLIT_FN_LABEL);
return objectToView(split);
},
/**
* Get the feature flag objects present on the factory storage
*/
splits: function () {
if (!(0, inputValidation_1.validateIfNotDestroyed)(log, readinessManager, constants_1.SPLITS_FN_LABEL) || !(0, inputValidation_1.validateIfOperational)(log, readinessManager, constants_1.SPLITS_FN_LABEL)) {
return isAsync ? Promise.resolve([]) : [];
}
var currentSplits = splits.getAll();
return (0, thenable_1.thenable)(currentSplits) ?
currentSplits.catch(function () { return []; }).then(objectsToViews) : // handle possible rejections when using pluggable storage
objectsToViews(currentSplits);
},
/**
* Get the feature flag names present on the factory storage
*/
names: function () {
if (!(0, inputValidation_1.validateIfNotDestroyed)(log, readinessManager, constants_1.NAMES_FN_LABEL) || !(0, inputValidation_1.validateIfOperational)(log, readinessManager, constants_1.NAMES_FN_LABEL)) {
return isAsync ? Promise.resolve([]) : [];
}
var splitNames = splits.getSplitNames();
return (0, thenable_1.thenable)(splitNames) ?
splitNames.catch(function () { return []; }) : // handle possible rejections when using pluggable storage
splitNames;
}
});
}
exports.sdkManagerFactory = sdkManagerFactory;
;