diff --git a/.gitignore b/.gitignore index 5fd795e..250bd47 100644 --- a/.gitignore +++ b/.gitignore @@ -409,7 +409,7 @@ types/ *.tgz # examples -examples/package-lock.json +examples/**/**/package-lock.json # playwright test result test-results diff --git a/README.md b/README.md index 54aa97a..ffeff87 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ The App Configuration JavaScript provider provides feature flags in as a `Map` o A builtin `ConfigurationMapFeatureFlagProvider` helps to load feature flags in this case. ```js -const appConfig = load(connectionString, {featureFlagOptions}); // load feature flags from Azure App Configuration service +const appConfig = await load(connectionString, {featureFlagOptions}); // load feature flags from Azure App Configuration service const featureProvider = new ConfigurationMapFeatureFlagProvider(appConfig); const featureManager = new FeatureManager(featureProvider); const isAlphaEnabled = await featureManager.isEnabled("Alpha"); diff --git a/examples/console-app/README.md b/examples/console-app/README.md new file mode 100644 index 0000000..1aa576e --- /dev/null +++ b/examples/console-app/README.md @@ -0,0 +1,23 @@ +# Examples for Microsoft Feature Management for JavaScript + +These examples show how to use the Microsoft Feature Management in some common scenarios. + +## Prerequisites + +The examples are compatible with [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule). + +Some examples use `@azure/app-configuration-provider` to load feature flags from the [Azure App Configuration](https://learn.microsoft.com/azure/azure-app-configuration/overview). Azure App Configuration provides a service to centrally manage application settings and feature flags. + +## Setup & Run + +1. Install the dependencies using `npm`: + + ``` bash + npm install + ``` + +1. Run the examples: + + ``` bash + node featureFlagSample.mjs + ``` diff --git a/examples/console-app/config.json b/examples/console-app/config.json new file mode 100644 index 0000000..702abcd --- /dev/null +++ b/examples/console-app/config.json @@ -0,0 +1,62 @@ +{ + "feature_management": { + "feature_flags": [ + { + "id": "FeatureX", + "enabled": true + }, + { + "id": "FeatureY", + "enabled": false + }, + { + "id": "FeatureFlagWithTimeWindowFilter", + "enabled": true, + "conditions": { + "client_filters": [ + { + "name": "Microsoft.TimeWindow", + "parameters": { + "Start": "Thu, 15 Aug 2024 00:00:00 GMT", + "End": "Mon, 19 Aug 2024 00:00:00 GMT" + } + } + ] + } + }, + { + "id": "FeatureFlagWithTargetingFilter", + "enabled": true, + "conditions": { + "client_filters": [ + { + "name": "Microsoft.Targeting", + "parameters": { + "Audience": { + "Users": [ + "Jeff" + ], + "Groups": [ + { + "Name": "Admin", + "RolloutPercentage": 100 + } + ], + "DefaultRolloutPercentage": 40, + "Exclusion": { + "Users": [ + "Anne" + ], + "Groups": [ + "Guest" + ] + } + } + } + } + ] + } + } + ] + } +} \ No newline at end of file diff --git a/examples/console-app/featureFlagSample.mjs b/examples/console-app/featureFlagSample.mjs new file mode 100644 index 0000000..3de5e9e --- /dev/null +++ b/examples/console-app/featureFlagSample.mjs @@ -0,0 +1,29 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +import fs from 'node:fs/promises'; +import { ConfigurationObjectFeatureFlagProvider, FeatureManager } from "@microsoft/feature-management"; + +const config = JSON.parse(await fs.readFile("config.json")); +const featureProvider = new ConfigurationObjectFeatureFlagProvider(config); +const featureManager = new FeatureManager(featureProvider); + +console.log("FeatureX is:", await featureManager.isEnabled("FeatureX")); +console.log("FeatureY is:", await featureManager.isEnabled("FeatureY")); + +// Is true between 2024-8-15 ~ 2024-8-19 +console.log("Feature flag with Time WindoW Filter is:", await featureManager.isEnabled("FeatureFlagWithTimeWindowFilter")); + +// Targeted by Users +console.log("Feature flag with Targeting Filter is:", await featureManager.isEnabled("FeatureFlagWithTargetingFilter", {userId: "Jeff"})) +// Excluded by Users +console.log("Feature flag with Targeting Filter is:", await featureManager.isEnabled("FeatureFlagWithTargetingFilter", {userId: "Anne"})) +// Targeted by Groups Admin +console.log("Feature flag with Targeting Filter is:", await featureManager.isEnabled("FeatureFlagWithTargetingFilter", {userId: "Admin1", groups: ["Admin"]})) +// Excluded by Groups Guest +console.log("Feature flag with Targeting Filter is:", await featureManager.isEnabled("FeatureFlagWithTargetingFilter", {userId: "Guest1", groups: ["Guest"]})) + +// Targeted by default rollout percentage +console.log("Feature flag with Targeting Filter is:", await featureManager.isEnabled("FeatureFlagWithTargetingFilter", {userId: "Alicia"})) +console.log("Feature flag with Targeting Filter is:", await featureManager.isEnabled("FeatureFlagWithTargetingFilter", {userId: "Susan"})) +console.log("Feature flag with Targeting Filter is:", await featureManager.isEnabled("FeatureFlagWithTargetingFilter", {userId: "John"})) diff --git a/examples/console-app/featureFlagSampleWithAppConfig.mjs b/examples/console-app/featureFlagSampleWithAppConfig.mjs new file mode 100644 index 0000000..0255278 --- /dev/null +++ b/examples/console-app/featureFlagSampleWithAppConfig.mjs @@ -0,0 +1,23 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +import { load } from "@azure/app-configuration-provider"; +import { ConfigurationMapFeatureFlagProvider, FeatureManager } from "@microsoft/feature-management"; + +const connectionString = ""; +const appConfig = await load(connectionString, { + featureFlagOptions: { + enabled: true, + selectors: [{ + keyFilter: "*" + }], + refresh: { + enabled: true + } + } +}); + +const featureProvider = new ConfigurationMapFeatureFlagProvider(appConfig); +const featureManager = new FeatureManager(featureProvider); + +console.log("Feature Beta is:", await featureManager.isEnabled("Beta")); diff --git a/examples/console-app/package.json b/examples/console-app/package.json new file mode 100644 index 0000000..c43ceb2 --- /dev/null +++ b/examples/console-app/package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "@azure/app-configuration-provider": "latest", + "@microsoft/feature-management": "latest" + } +}