|
| 1 | +# Microsoft Feature Management for JavaScript |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/@microsoft/feature-management) |
| 4 | + |
| 5 | +Feature Management is a library for enabling/disabling features at runtime. |
| 6 | +Developers can use feature flags in simple use cases like conditional statement to more advanced scenarios like conditionally adding routes. |
| 7 | + |
| 8 | +## Getting Started |
| 9 | + |
| 10 | +### Prerequisites |
| 11 | + |
| 12 | +- Node.js LTS version |
| 13 | + |
| 14 | +### Usage |
| 15 | + |
| 16 | +You can use feature flags from the Azure App Configuration service, local files or any other sources. |
| 17 | + |
| 18 | +#### Use feature flags from Azure App Configuration |
| 19 | + |
| 20 | +The App Configuration JavaScript provider provides feature flags in as a `Map` object. |
| 21 | +A builtin `ConfigurationMapFeatureFlagProvider` helps to load feature flags in this case. |
| 22 | + |
| 23 | +```js |
| 24 | +const appConfig = load(connectionString, {featureFlagOptions}); // load feature flags from Azure App Configuration service |
| 25 | +const featureProvider = new ConfigurationMapFeatureFlagProvider(appConfig); |
| 26 | +const featureManager = new FeatureManager(featureProvider); |
| 27 | +const isAlphaEnabled = await featureManager.isEnabled("Alpha"); |
| 28 | +console.log("Feature Alpha is:", isAlphaEnabled); |
| 29 | +``` |
| 30 | + |
| 31 | +#### Use feature flags from a json file |
| 32 | + |
| 33 | +A sample JSON file with the following format can be used to load feature flags. |
| 34 | +The JSON file can be read and parsed as an object as a whole. |
| 35 | +A builtin `ConfigurationObjectFeatureFlagProvider` helps to load feature flags in this case. |
| 36 | + |
| 37 | +Content of `sample.json`: |
| 38 | +```json |
| 39 | +{ |
| 40 | + "feature_management": { |
| 41 | + "feature_flags": [ |
| 42 | + { |
| 43 | + "id": "Alpha", |
| 44 | + "description": "", |
| 45 | + "enabled": "true", |
| 46 | + "conditions": { |
| 47 | + "client_filters": [] |
| 48 | + } |
| 49 | + } |
| 50 | + ] |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +Load feature flags from `sample.json` file. |
| 56 | +```js |
| 57 | +const config = JSON.parse(await fs.readFile("path/to/sample.json")); |
| 58 | +const featureProvider = new ConfigurationObjectFeatureFlagProvider(config); |
| 59 | +const featureManager = new FeatureManager(featureProvider); |
| 60 | +const isAlphaEnabled = await featureManager.isEnabled("Alpha"); |
| 61 | +console.log("Feature Alpha is:", isAlphaEnabled); |
| 62 | +``` |
| 63 | + |
| 64 | +## Contributing |
| 65 | + |
| 66 | +This project welcomes contributions and suggestions. Most contributions require you to agree to a |
| 67 | +Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us |
| 68 | +the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com. |
| 69 | + |
| 70 | +When you submit a pull request, a CLA bot will automatically determine whether you need to provide |
| 71 | +a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions |
| 72 | +provided by the bot. You will only need to do this once across all repos using our CLA. |
| 73 | + |
| 74 | +This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). |
| 75 | +For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or |
| 76 | +contact [[email protected]](mailto:[email protected]) with any additional questions or comments. |
| 77 | + |
| 78 | +## Trademarks |
| 79 | + |
| 80 | +This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft |
| 81 | +trademarks or logos is subject to and must follow |
| 82 | +[Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general). |
| 83 | +Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. |
| 84 | +Any use of third-party trademarks or logos are subject to those third-party's policies. |
0 commit comments