UNPKG

@azure/app-configuration

Version:
93 lines 3.94 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. Object.defineProperty(exports, "__esModule", { value: true }); exports.FeatureFlagHelper = exports.featureFlagContentType = exports.featureFlagPrefix = void 0; exports.parseFeatureFlag = parseFeatureFlag; exports.isFeatureFlag = isFeatureFlag; const logger_js_1 = require("./logger.js"); /** * The prefix for feature flags. */ exports.featureFlagPrefix = ".appconfig.featureflag/"; /** * The content type for a FeatureFlag */ exports.featureFlagContentType = "application/vnd.microsoft.appconfig.ff+json;charset=utf-8"; /** * @internal */ exports.FeatureFlagHelper = { /** * Takes the FeatureFlag (JSON) and returns a ConfigurationSetting (with the props encodeed in the value). */ toConfigurationSettingParam: (featureFlag) => { logger_js_1.logger.info("Encoding FeatureFlag value in a ConfigurationSetting:", featureFlag); if (!featureFlag.value) { logger_js_1.logger.error("FeatureFlag has an unexpected value", featureFlag); throw new TypeError(`FeatureFlag has an unexpected value - ${featureFlag.value}`); } let key = featureFlag.key; if (typeof featureFlag.key === "string" && !featureFlag.key.startsWith(exports.featureFlagPrefix)) { key = exports.featureFlagPrefix + featureFlag.key; } const jsonFeatureFlagValue = { id: featureFlag.value.id ?? key.replace(exports.featureFlagPrefix, ""), enabled: featureFlag.value.enabled, description: featureFlag.value.description, conditions: { client_filters: featureFlag.value.conditions.clientFilters, requirement_type: featureFlag.value.conditions.requirementType ?? "Any", }, display_name: featureFlag.value.displayName, }; const configSetting = { ...featureFlag, key, value: JSON.stringify(jsonFeatureFlagValue), }; return configSetting; }, }; /** * Takes the ConfigurationSetting as input and returns the ConfigurationSetting<FeatureFlagValue> by parsing the value string. */ function parseFeatureFlag(setting) { logger_js_1.logger.info("Parsing the value to return the FeatureFlagValue", setting); if (!isFeatureFlag(setting)) { logger_js_1.logger.error("Invalid FeatureFlag input", setting); throw TypeError(`Setting with key ${setting.key} is not a valid FeatureFlag, make sure to have the correct content-type and a valid non-null value.`); } const jsonFeatureFlagValue = JSON.parse(setting.value); let key = setting.key; if (typeof setting.key === "string" && !setting.key.startsWith(exports.featureFlagPrefix)) { key = exports.featureFlagPrefix + setting.key; } const featureflag = { ...setting, value: { id: jsonFeatureFlagValue.id, enabled: jsonFeatureFlagValue.enabled ?? false, description: jsonFeatureFlagValue.description, displayName: jsonFeatureFlagValue.display_name, conditions: jsonFeatureFlagValue.conditions ? { clientFilters: jsonFeatureFlagValue.conditions.client_filters, requirementType: jsonFeatureFlagValue.conditions.requirement_type, } : { clientFilters: [] }, }, key, contentType: exports.featureFlagContentType, }; return featureflag; } /** * Lets you know if the ConfigurationSetting is a feature flag. * * [Checks if the content type is featureFlagContentType `"application/vnd.microsoft.appconfig.ff+json;charset=utf-8"`] */ function isFeatureFlag(setting) { return (setting && setting.contentType === exports.featureFlagContentType && typeof setting.value === "string"); } //# sourceMappingURL=featureFlag.js.map