UNPKG

@splitsoftware/splitio-commons

Version:
119 lines (118 loc) 6.17 kB
import { AttributesCacheInMemory } from '../storages/inMemory/AttributesCacheInMemory'; import { validateAttributesDeep } from '../utils/inputValidation/attributes'; import { objectAssign } from '../utils/lang/objectAssign'; /** * Add in memory attributes storage methods and combine them with any attribute received from the getTreatment/s call */ export function clientAttributesDecoration(log, client) { var attributeStorage = new 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 ? objectAssign({}, storedAttributes, maybeAttributes) : maybeAttributes; } return 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 (!validateAttributesDeep(log, attribute, 'setAttribute')) return false; log.debug("stored " + attributeValue + " for attribute " + 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 " + 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 (!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 " + attributeName); return attributeStorage.removeAttribute(attributeName + ''); }, /** * Remove all the stored attributes in the client's in memory attribute storage */ clearAttributes: function () { return attributeStorage.clear(); } }); }