UNPKG

@splitsoftware/splitio-commons

Version:
123 lines (122 loc) 6.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.clientAttributesDecoration = void 0; var AttributesCacheInMemory_1 = require("../storages/inMemory/AttributesCacheInMemory"); var attributes_1 = require("../utils/inputValidation/attributes"); var objectAssign_1 = require("../utils/lang/objectAssign"); /** * Add in memory attributes storage methods and combine them with any attribute received from the getTreatment/s call */ function clientAttributesDecoration(log, client) { var attributeStorage = new AttributesCacheInMemory_1.AttributesCacheInMemory(); // Keep a reference to the original methods var clientGetTreatment = client.getTreatment; var clientGetTreatmentWithConfig = client.getTreatmentWithConfig; var clientGetTreatments = client.getTreatments; var clientGetTreatmentsWithConfig = client.getTreatmentsWithConfig; var clientGetTreatmentsByFlagSets = client.getTreatmentsByFlagSets; var clientGetTreatmentsWithConfigByFlagSets = client.getTreatmentsWithConfigByFlagSets; var clientGetTreatmentsByFlagSet = client.getTreatmentsByFlagSet; var clientGetTreatmentsWithConfigByFlagSet = client.getTreatmentsWithConfigByFlagSet; function getTreatment(maybeKey, maybeFeatureFlagName, maybeAttributes, maybeOptions) { return clientGetTreatment(maybeKey, maybeFeatureFlagName, combineAttributes(maybeAttributes), maybeOptions); } function getTreatmentWithConfig(maybeKey, maybeFeatureFlagName, maybeAttributes, maybeOptions) { return clientGetTreatmentWithConfig(maybeKey, maybeFeatureFlagName, combineAttributes(maybeAttributes), maybeOptions); } function getTreatments(maybeKey, maybeFeatureFlagNames, maybeAttributes, maybeOptions) { return clientGetTreatments(maybeKey, maybeFeatureFlagNames, combineAttributes(maybeAttributes), maybeOptions); } function getTreatmentsWithConfig(maybeKey, maybeFeatureFlagNames, maybeAttributes, maybeOptions) { return clientGetTreatmentsWithConfig(maybeKey, maybeFeatureFlagNames, combineAttributes(maybeAttributes), maybeOptions); } function getTreatmentsByFlagSets(maybeKey, maybeFlagSets, maybeAttributes, maybeOptions) { return clientGetTreatmentsByFlagSets(maybeKey, maybeFlagSets, combineAttributes(maybeAttributes), maybeOptions); } function getTreatmentsWithConfigByFlagSets(maybeKey, maybeFlagSets, maybeAttributes, maybeOptions) { return clientGetTreatmentsWithConfigByFlagSets(maybeKey, maybeFlagSets, combineAttributes(maybeAttributes), maybeOptions); } function getTreatmentsByFlagSet(maybeKey, maybeFlagSet, maybeAttributes, maybeOptions) { return clientGetTreatmentsByFlagSet(maybeKey, maybeFlagSet, combineAttributes(maybeAttributes), maybeOptions); } function getTreatmentsWithConfigByFlagSet(maybeKey, maybeFlagSet, maybeAttributes, maybeOptions) { return clientGetTreatmentsWithConfigByFlagSet(maybeKey, maybeFlagSet, combineAttributes(maybeAttributes), maybeOptions); } function combineAttributes(maybeAttributes) { var storedAttributes = attributeStorage.getAll(); return Object.keys(storedAttributes).length > 0 ? (0, objectAssign_1.objectAssign)({}, storedAttributes, maybeAttributes) : maybeAttributes; } return (0, objectAssign_1.objectAssign)(client, { getTreatment: getTreatment, getTreatmentWithConfig: getTreatmentWithConfig, getTreatments: getTreatments, getTreatmentsWithConfig: getTreatmentsWithConfig, getTreatmentsByFlagSets: getTreatmentsByFlagSets, getTreatmentsWithConfigByFlagSets: getTreatmentsWithConfigByFlagSets, getTreatmentsByFlagSet: getTreatmentsByFlagSet, getTreatmentsWithConfigByFlagSet: getTreatmentsWithConfigByFlagSet, /** * Add an attribute to client's in memory attributes storage * * @param attributeName - Attribute name * @param attributeValue - Attribute value * @returns true if the attribute was stored and false otherways */ setAttribute: function (attributeName, attributeValue) { var attribute = {}; attribute[attributeName] = attributeValue; if (!(0, attributes_1.validateAttributesDeep)(log, attribute, 'setAttribute')) return false; log.debug("stored ".concat(attributeValue, " for attribute ").concat(attributeName)); return attributeStorage.setAttribute(attributeName, attributeValue); }, /** * Returns the attribute with the given name * * @param attributeName - Attribute name * @returns Attribute with the given name */ getAttribute: function (attributeName) { log.debug("retrieved attribute ".concat(attributeName)); return attributeStorage.getAttribute(attributeName + ''); }, /** * Add to client's in memory attributes storage the attributes in 'attributes' * * @param attributes - Object with attributes to store * @returns true if attributes were stored an false otherways */ setAttributes: function (attributes) { if (!(0, attributes_1.validateAttributesDeep)(log, attributes, 'setAttributes')) return false; return attributeStorage.setAttributes(attributes); }, /** * Return all the attributes stored in client's in memory attributes storage * * @returns returns all the stored attributes */ getAttributes: function () { return attributeStorage.getAll(); }, /** * Removes from client's in memory attributes storage the attribute with the given name * * @param attributeName - Attribute name * @returns true if attribute was removed and false otherways */ removeAttribute: function (attributeName) { log.debug("removed attribute ".concat(attributeName)); return attributeStorage.removeAttribute(attributeName + ''); }, /** * Remove all the stored attributes in the client's in memory attribute storage */ clearAttributes: function () { return attributeStorage.clear(); } }); } exports.clientAttributesDecoration = clientAttributesDecoration;