@azure/app-configuration
Version:
An isomorphic client library for the Azure App Configuration service.
74 lines • 3.32 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { logger } from "./logger.js";
/**
* The prefix for feature flags.
*/
export const featureFlagPrefix = ".appconfig.featureflag/";
/**
* The content type for a FeatureFlag
*/
export const featureFlagContentType = "application/vnd.microsoft.appconfig.ff+json;charset=utf-8";
/**
* @internal
*/
export const FeatureFlagHelper = {
/**
* Takes the FeatureFlag (JSON) and returns a ConfigurationSetting (with the props encodeed in the value).
*/
toConfigurationSettingParam: (featureFlag) => {
var _a;
logger.info("Encoding FeatureFlag value in a ConfigurationSetting:", featureFlag);
if (!featureFlag.value) {
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(featureFlagPrefix)) {
key = featureFlagPrefix + featureFlag.key;
}
const jsonFeatureFlagValue = {
id: (_a = featureFlag.value.id) !== null && _a !== void 0 ? _a : key.replace(featureFlagPrefix, ""),
enabled: featureFlag.value.enabled,
description: featureFlag.value.description,
conditions: {
client_filters: featureFlag.value.conditions.clientFilters,
},
display_name: featureFlag.value.displayName,
};
const configSetting = Object.assign(Object.assign({}, featureFlag), { key, value: JSON.stringify(jsonFeatureFlagValue) });
return configSetting;
},
};
/**
* Takes the ConfigurationSetting as input and returns the ConfigurationSetting<FeatureFlagValue> by parsing the value string.
*/
export function parseFeatureFlag(setting) {
logger.info("Parsing the value to return the FeatureFlagValue", setting);
if (!isFeatureFlag(setting)) {
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(featureFlagPrefix)) {
key = featureFlagPrefix + setting.key;
}
const featureflag = Object.assign(Object.assign({}, setting), { value: {
id: jsonFeatureFlagValue.id,
enabled: jsonFeatureFlagValue.enabled,
description: jsonFeatureFlagValue.description,
displayName: jsonFeatureFlagValue.display_name,
conditions: { clientFilters: jsonFeatureFlagValue.conditions.client_filters },
}, key, contentType: 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"`]
*/
export function isFeatureFlag(setting) {
return (setting && setting.contentType === featureFlagContentType && typeof setting.value === "string");
}
//# sourceMappingURL=featureFlag.js.map