Skip to content

Commit 01a854f

Browse files
update
1 parent 3069058 commit 01a854f

File tree

1 file changed

+24
-33
lines changed

1 file changed

+24
-33
lines changed

articles/azure-app-configuration/feature-management-javascript-reference.md

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ms.service: azure-app-configuration
99
ms.devlang: javascript
1010
ms.custom: devx-track-javascript
1111
ms.topic: tutorial
12-
ms.date: 10/15/2024
12+
ms.date: 01/20/2025
1313
zone_pivot_groups: feature-management
1414
#Customer intent: I want to control feature availability in my app by using the Feature Management library.
1515
---
@@ -55,7 +55,7 @@ In JavaScript, developers commonly use objects or maps as the primary data struc
5555

5656
### [Use map configuration](#tab/map-configuration)
5757

58-
``` javascript
58+
```typescript
5959
const config = new Map([
6060
["feature_management", {
6161
"feature_flags": [
@@ -79,7 +79,7 @@ const featureManager = new FeatureManager(featureProvider);
7979

8080
### [Use object configuration](#tab/object-configuration)
8181

82-
``` javascript
82+
```typescript
8383
const config = {
8484
"feature_management": {
8585
"feature_flags": [
@@ -103,7 +103,7 @@ const featureManager = new FeatureManager(featureProvider);
103103

104104
The object can also be parsed from a JSON file:
105105

106-
``` javascript
106+
```typescript
107107
const config = JSON.parse(await fs.readFile("path/to/config.json"));
108108
const featureProvider = new ConfigurationObjectFeatureFlagProvider(config);
109109
const featureManager = new FeatureManager(featureProvider);
@@ -119,7 +119,7 @@ The Azure App Configuration service also delivers the feature flags to your appl
119119

120120
The App Configuration JavaScript provider provides feature flags in a `Map` object. The built-in `ConfigurationMapFeatureFlagProvider` helps to load feature flags in this case.
121121

122-
``` javascript
122+
```typescript
123123
import { DefaultAzureCredential } from "@azure/identity";
124124
import { load } from "@azure/app-configuration-provider";
125125
import { ConfigurationMapFeatureFlagProvider, FeatureManager } from "@microsoft/feature-management";
@@ -137,7 +137,7 @@ const featureManager = new FeatureManager(featureProvider);
137137

138138
The following example shows the format used to set up feature flags in a JSON file.
139139

140-
``` json
140+
```json
141141
{
142142
"feature_management": {
143143
"feature_flags": [
@@ -184,7 +184,7 @@ The `requirement_type` property of a feature flag is used to determine if the fi
184184

185185
A `requirement_type` of `All` changes the traversal. First, if there are no filters, the feature is disabled. Then, the feature filters are traversed until one of the filters decides that the feature should be disabled. If no filter indicates that the feature should be disabled, it's considered enabled.
186186

187-
``` json
187+
```json
188188
{
189189
"feature_management": {
190190
"feature_flags": [
@@ -223,7 +223,7 @@ The basic form of feature management is checking if a feature flag is enabled an
223223

224224
### [Use map configuration](#tab/map-configuration)
225225

226-
``` javascript
226+
```typescript
227227
import { ConfigurationMapFeatureFlagProvider, FeatureManager } from "@microsoft/feature-management";
228228
const featureProvider = new ConfigurationMapFeatureFlagProvider(config);
229229
const featureManager = new FeatureManager(featureProvider);
@@ -236,7 +236,7 @@ if (isBetaEnabled) {
236236

237237
### [Use object configuration](#tab/object-configuration)
238238

239-
``` javascript
239+
```typescript
240240
import { ConfigurationObjectFeatureFlagProvider, FeatureManager } from "@microsoft/feature-management";
241241
const featureProvider = new ConfigurationObjectFeatureFlagProvider(config);
242242
const featureManager = new FeatureManager(featureProvider);
@@ -253,7 +253,7 @@ if (isBetaEnabled) {
253253

254254
Creating a feature filter provides a way to enable features based on criteria that you define. To implement a feature filter, the `IFeatureFilter` interface must be implemented. `IFeatureFilter` has a `name` property and a method named `evaluate`. The `name` should be used in configuration to reference the feature filter within a feature flag. When a feature specifies that it can be enabled for a feature filter, the `evaluate` method is called. If `evaluate` returns `true`, it means the feature should be enabled.
255255

256-
``` typescript
256+
```typescript
257257
interface IFeatureFilter {
258258
name: string;
259259
evaluate(context: IFeatureFilterEvaluationContext, appContext?: unknown): boolean | Promise<boolean>;
@@ -262,7 +262,7 @@ interface IFeatureFilter {
262262

263263
The following snippet demonstrates how to implement a customized feature filter with name `MyCriteria`.
264264

265-
``` javascript
265+
```typescript
266266
class MyCriteriaFilter {
267267
name = "MyCriteria";
268268
evaluate(context, appContext) {
@@ -278,15 +278,15 @@ The following snippet demonstrates how to implement a customized feature filter
278278

279279
You need to register the custom filter when creating the `FeatureManager`.
280280

281-
``` javascript
281+
```typescript
282282
const featureManager = new FeatureManager(ffProvider, {customFilters: [new MyCriteriaFilter()]});
283283
```
284284

285285
### Parameterized feature filters
286286

287287
Some feature filters require parameters to decide whether a feature should be turned on or not. For example, a browser feature filter may turn on a feature for a certain set of browsers. It may be desired that Edge and Chrome browsers enable a feature, while Firefox does not. To do this, a feature filter can be designed to expect parameters. These parameters would be specified in the feature configuration, and in code would be accessible via the `IFeatureFilterEvaluationContext` parameter of `IFeatureFilter.Evaluate`.
288288

289-
``` typescript
289+
```typescript
290290
interface IFeatureFilterEvaluationContext {
291291
featureName: string;
292292
parameters?: unknown;
@@ -299,7 +299,7 @@ interface IFeatureFilterEvaluationContext {
299299

300300
A feature filter may need runtime application context to evaluate a feature flag. You can pass in the context as a parameter when calling `isEnabled`.
301301

302-
``` javascript
302+
```typescript
303303
featureManager.isEnabled("Beta", { userId : "Sam" })
304304
```
305305

@@ -331,7 +331,7 @@ This filter provides the capability to enable a feature based on a time window.
331331

332332
This filter provides the capability to enable a feature for a target audience. An in-depth explanation of targeting is explained in the [targeting](#targeting) section below. The filter parameters include an `Audience` object that describes users, groups, excluded users/groups, and a default percentage of the user base that should have access to the feature. Each group object that is listed in the `Groups` section must also specify what percentage of the group's members should have access. If a user is specified in the `Exclusion` section, either directly or if the user is in an excluded group, the feature is disabled. Otherwise, if a user is specified in the `Users` section directly, or if the user is in the included percentage of any of the group rollouts, or if the user falls into the default rollout percentage then that user will have the feature enabled.
333333

334-
``` JavaScript
334+
```typescript
335335
"client_filters": [
336336
{
337337
"name": "Microsoft.Targeting",
@@ -385,15 +385,15 @@ This strategy for rolling out a feature is built into the library through the in
385385

386386
The targeting filter relies on a targeting context to evaluate whether a feature should be turned on. This targeting context contains information such as what user is currently being evaluated, and what groups the user is in. The targeting context must be passed directly when `isEnabled` is called.
387387

388-
``` javascript
388+
```typescript
389389
featureManager.isEnabled("Beta", { userId: "Aiden", groups: ["Ring1"] })
390390
```
391391

392392
### Targeting exclusion
393393

394394
When defining an audience, users and groups can be excluded from the audience. Exclusions are useful for when a feature is being rolled out to a group of users, but a few users or groups need to be excluded from the rollout. Exclusion is defined by adding a list of users and groups to the `Exclusion` property of the audience.
395395

396-
``` json
396+
```json
397397
"Audience": {
398398
"Users": [
399399
"Jeff",
@@ -416,11 +416,6 @@ When defining an audience, users and groups can be excluded from the audience. E
416416

417417
In the above example, the feature is enabled for users named `Jeff` and `Alicia`. It's also enabled for users in the group named `Ring0`. However, if the user is named `Mark`, the feature is disabled, regardless of if they are in the group `Ring0` or not. Exclusions take priority over the rest of the targeting filter.
418418

419-
:::zone target="docs" pivot="stable-version"
420-
:::zone-end
421-
422-
:::zone target="docs" pivot="preview-version"
423-
424419
## Variants
425420

426421
When new features are added to an application, there may come a time when a feature has multiple different proposed design options. A common solution for deciding on a design is some form of A/B testing, which involves providing a different version of the feature to different segments of the user base and choosing a version based on user interaction. In this library, this functionality is enabled by representing different configurations of a feature with variants.
@@ -431,7 +426,7 @@ Variants enable a feature flag to become more than a simple on/off flag. A varia
431426

432427
For each feature, a variant can be retrieved using the `FeatureManager`'s `getVariant` method. The variant assignment is dependent on the user currently being evaluated, and that information is obtained from the targeting context you passed in.
433428

434-
``` javascript
429+
```typescript
435430
const variant = await featureManager.getVariant("MyVariantFeatureFlag", { userId: "Sam" });
436431

437432
const variantName = variant.name;
@@ -440,12 +435,11 @@ const variantConfiguration = variant.configuration;
440435
// Do something with the resulting variant and its configuration
441436
```
442437

443-
444438
### Variant feature flag declaration
445439

446440
Compared to normal feature flags, variant feature flags have two more properties: `variants` and `allocation`. The `variants` property is an array that contains the variants defined for this feature. The `allocation` property defines how these variants should be allocated for the feature. Just like declaring normal feature flags, you can set up variant feature flags in a JSON file. Here's an example of a variant feature flag.
447441

448-
``` json
442+
```json
449443
{
450444
"feature_management": {
451445
"feature_flags": [
@@ -483,7 +477,7 @@ Each variant has two properties: a name and a configuration. The name is used to
483477

484478
A list of all possible variants is defined for each feature under the `variants` property.
485479

486-
``` json
480+
```json
487481
{
488482
"feature_management": {
489483
"feature_flags": [
@@ -513,7 +507,7 @@ A list of all possible variants is defined for each feature under the `variants`
513507

514508
The process of allocating a feature's variants is determined by the `allocation` property of the feature.
515509

516-
``` json
510+
```json
517511
"allocation": {
518512
"default_when_enabled": "Small",
519513
"default_when_disabled": "Small",
@@ -571,7 +565,6 @@ If the feature is enabled, the feature manager checks the `user`, `group`, and `
571565

572566
Allocation logic is similar to the [Microsoft.Targeting](#microsofttargeting) feature filter, but there are some parameters that are present in targeting that aren't in allocation, and vice versa. The outcomes of targeting and allocation aren't related.
573567

574-
575568
### Overriding enabled state with a variant
576569

577570
You can use variants to override the enabled state of a feature flag. Overriding gives variants an opportunity to extend the evaluation of a feature flag. When calling `is_enabled` on a flag with variants, the feature manager will check if the variant assigned to the current user is configured to override the result. Overriding is done using the optional variant property `status_override`. By default, this property is set to `None`, which means the variant doesn't affect whether the flag is considered enabled or disabled. Setting `status_override` to `Enabled` allows the variant, when chosen, to override a flag to be enabled. Setting `status_override` to `Disabled` provides the opposite functionality, therefore disabling the flag when the variant is chosen. A feature with an `enabled` state of `false` can't be overridden.
@@ -655,7 +648,7 @@ You can register an `onFeatureEvaluated` callback function when creating `Featur
655648

656649
The following example shows how to implement a custom callback function to send telemetry with the information extracted from the feature evaluation result and register it to the feature manager.
657650

658-
``` javascript
651+
```typescript
659652
const sendTelemetry = (evaluationResult) => {
660653
const featureId = evaluationResult.feature.id;
661654
const featureEnabled = evaluationResult.enabled;
@@ -678,7 +671,7 @@ The Application Insights offers different sdks for [web](https://www.npmjs.com/p
678671

679672
If your application runs in the browser, install the [`"@microsoft/feature-management-applicationinsights-browser"`](https://www.npmjs.com/package/@microsoft/feature-management-applicationinsights-browser) package. The following example shows how you can create a built-in Application Insights telemetry publisher and register it to the feature manager.
680673

681-
``` javascript
674+
```typescript
682675
import { ApplicationInsights } from "@microsoft/applicationinsights-web"
683676
import { createTelemetryPublisher, trackEvent } from "@microsoft/feature-management-applicationinsights-browser"
684677

@@ -700,7 +693,7 @@ trackEvent(appInsights, TARGETING_ID, {name: "TestEvent"}, {"Tag": "Some Value"}
700693

701694
If your application runs in the Node.js, install the [`"@microsoft/feature-management-applicationinsights-node"`](https://www.npmjs.com/package/@microsoft/feature-management-applicationinsights-node) package. The following example shows how you can create a built-in Application Insights telemetry publisher and register it to the feature manager.
702695

703-
``` javascript
696+
```typescript
704697
import appInsights from "applicationinsights"
705698
import { createTelemetryPublisher, trackEvent } from "@microsoft/feature-management-applicationinsights-node"
706699

@@ -719,8 +712,6 @@ trackEvent(appInsights.defaultClient, TARGETING_ID, {name: "TestEvent"});
719712

720713
The telemetry publisher sends `FeatureEvaluation` custom events to the Application Insights when a feature flag enabled with telemetry is evaluated. The custom event follows the [FeatureEvaluationEvent](https://github.com/microsoft/FeatureManagement/tree/main/Schema/FeatureEvaluationEvent) schema.
721714

722-
:::zone-end
723-
724715
## Next steps
725716

726717
To learn how to use feature flags in your applications, continue to the following quickstarts.

0 commit comments

Comments
 (0)