@@ -35,8 +35,8 @@ const credential = new DefaultAzureCredential(); // For more information, see ht
35
35
36
36
async function run () {
37
37
// Connect to Azure App Configuration using a token credential and load all key-values with no label.
38
- const settings = await load (endpoint, credential);
39
- console .log (' settings .get("message"):' , settings .get (" message" ));
38
+ const appConfig = await load (endpoint, credential);
39
+ console .log (' appConfig .get("message"):' , appConfig .get (" message" ));
40
40
}
41
41
42
42
run ();
@@ -50,8 +50,8 @@ const connectionString = process.env.AZURE_APPCONFIG_CONNECTION_STRING;
50
50
51
51
async function run () {
52
52
// Connect to Azure App Configuration using a connection string and load all key-values with no label.
53
- const settings = await load (connectionString);
54
- console .log (' settings .get("message"):' , settings .get (" message" ));
53
+ const appConfig = await load (connectionString);
54
+ console .log (' appConfig .get("message"):' , appConfig .get (" message" ));
55
55
}
56
56
57
57
run ();
@@ -85,8 +85,8 @@ The `AzureAppConfiguration` type extends the following interfaces:
85
85
The ` IGettable ` interface provides `get ` method to retrieve the value of a key -value from the Map -styled data structure .
86
86
87
87
```typescript
88
- const settings = await load (endpoint , credential );
89
- const fontSize = settings .get ("app :font :size "); // value of the key "app:font:size" from the App Configuration store
88
+ const appConfig = await load (endpoint , credential );
89
+ const fontSize = appConfig .get ("app :font :size "); // value of the key "app:font:size" from the App Configuration store
90
90
```
91
91
92
92
- `ReadonlyMap `
@@ -112,20 +112,40 @@ The `AzureAppConfiguration` type extends the following interfaces:
112
112
In JavaScript , objects or maps are commonly used as the primary data structures to represent configurations . The JavaScript configuration provider library supports both of the configuration approaches , providing developers with the flexibility to choose the option that best fits their needs .
113
113
114
114
` ` ` typescript
115
- const settings = await load(endpoint, credential);
116
- const settingsObj = settings .constructConfigurationObject({separator: ":"});
117
- const fontSize1 = settings .get("app:font:size"); // map-style configuration representation
115
+ const appConfig = await load(endpoint, credential);
116
+ const settingsObj = appConfig .constructConfigurationObject({separator: ":"});
117
+ const fontSize1 = appConfig .get("app:font:size"); // map-style configuration representation
118
118
const fontSize2 = settingsObj.app.font.size; // object-style configuration representation
119
119
` ` `
120
120
121
+ ### JSON Content Type Handling
122
+
123
+ You can [create JSON key - values ](./ howto - leverage - json - content - type #create - json - key - values - in - app - configuration ) in App Configuration . When loading key - values from Azure App Configuration , the configuration provider will automatically convert the key - values of valid JSON content type (e .g . application / json ) into object .
124
+
125
+ ` ` ` json
126
+ {
127
+ "key": "font",
128
+ "label": null,
129
+ "value": "{\r\n\t\" size\" : 12,\r\n\t\" color\" : \" red\"\r\n }",
130
+ "content_type": "application/json"
131
+ }
132
+ ` ` `
133
+
134
+ The above key - value will be loaded as ` { size: 12, color: "red" } ` .
135
+
136
+ ` ` ` typescript
137
+ const appConfig = await load(endpoint, credential);
138
+ const { size, color } = appConfig.get("font");
139
+ ` ` `
140
+
121
141
### Load specific key - values using selectors
122
142
123
143
By default , the ` load ` method will load all configurations with no label from the configuration store . You can configure the behavior of the ` load ` method through the optional parameter of [` AzureAppConfigurationOptions ` ](https :// github.com/Azure/AppConfiguration-JavaScriptProvider/blob/main/src/AzureAppConfigurationOptions.ts) type.
124
144
125
145
To refine or expand the configurations loaded from the App Configuration store , you can specify the key or label selectors under the ` AzureAppConfigurationOptions.selectors ` property .
126
146
127
147
` ` ` typescript
128
- const settings = await load(endpoint, credential, {
148
+ const appConfig = await load(endpoint, credential, {
129
149
selectors: [
130
150
{ // load the subset of keys starting with "app1." prefix and "test" label
131
151
keyFilter: "app1.*",
@@ -146,62 +166,20 @@ const settings = await load(endpoint, credential, {
146
166
You can trim the prefix off of keys by providing a list of trimmed key prefixes to the ` AzureAppConfigurationOptions.trimKeyPrefixes ` property .
147
167
148
168
` ` ` typescript
149
- const settings = await load(endpoint, credential, {
169
+ const appConfig = await load(endpoint, credential, {
150
170
selectors: [{
151
171
keyFilter: "app.*"
152
172
}],
153
173
trimKeyPrefixes: ["app."]
154
174
});
155
175
` ` `
156
176
157
- ## Key Vault reference
158
-
159
- Azure App Configuration supports referencing secrets stored in Azure Key Vault . In App Configuration , you can create keys that map to secrets stored in Key Vault . The secrets are securely stored in Key Vault , but can be accessed like any other configuration once loaded .
160
-
161
- The configuration provider library retrieves Key Vault references , just as it does for any other keys stored in App Configuration . Because the client recognizes the keys as Key Vault references , they have a unique content - type , and the client will connect to Key Vault to retrieve their values for your application . You need to configure ` AzureAppConfigurationOptions.KeyVaultOptions ` property with the proper credential to allow the configuration provider to connect to Azure Key Vault .
162
-
163
- ` ` ` typescript
164
- const credential = new DefaultAzureCredential();
165
- const settings = await load(endpoint, credential, {
166
- keyVaultOptions: {
167
- credential: credential
168
- }
169
- });
170
- ` ` `
171
-
172
- You can also provide ` SecretClient ` instance directly to ` KeyVaultOptions ` . In this way , you can customize the options while creating ` SecretClient ` .
173
-
174
- ` ` ` typescript
175
- const { SecretClient } = require("@azure/keyvault-secrets");
176
-
177
- const credential = new DefaultAzureCredential();
178
- const secretClient = new SecretClient(keyVaultUrl, credential, {
179
- serviceVersion: "7.0",
180
- });
181
- const settings = await load(endpoint, credential, {
182
- keyVaultOptions: {
183
- secretClients: [ secretClient ]
184
- }
185
- });
186
- ` ` `
187
-
188
- You can also set ` secretResolver ` property to locally resolve secrets that don ’t have a Key Vault associated with them .
189
-
190
- ` ` ` typescript
191
- const resolveSecret = (url) => "From Secret Resolver";
192
- const settings = await load(endpoint, credential, {
193
- keyVaultOptions: {
194
- secretResolver: resolveSecret
195
- }
196
- });
197
- ` ` `
198
-
199
177
## Configuration refresh
200
178
201
179
Dynamic refresh for the configurations lets you pull their latest values from the App Configuration store without having to restart the application . You can set ` AzureAppConfigurationOptions.refreshOptions ` to enable the refresh and configure refresh options . The loaded configuration will be updated when any change of selected key - values is detected on the server . By default , a refresh interval of 30 seconds is used , but you can override it with the ` refreshIntervalInMs ` property .
202
180
203
181
` ` ` typescript
204
- const settings = await load(endpoint, credential, {
182
+ const appConfig = await load(endpoint, credential, {
205
183
refreshOptions: {
206
184
enabled: true,
207
185
refreshIntervalInMs: 15_000
@@ -213,7 +191,7 @@ Setting up `refreshOptions` alone won't automatically refresh the configuration.
213
191
214
192
` ` ` typescript
215
193
// this call is not blocking, the configuration will be updated asynchronously
216
- settings .refresh();
194
+ appConfig .refresh();
217
195
` ` `
218
196
219
197
This design prevents unnecessary requests to App Configuration when your application is idle . You should include the ` refresh ` call where your application activity occurs . This is known as **activity - driven configuration refresh ** . For example , you can call ` refresh ` when processing an incoming request or inside an iteration where you perform a complex task .
@@ -222,7 +200,7 @@ This design prevents unnecessary requests to App Configuration when your applica
222
200
const server = express();
223
201
// Use an express middleware to refresh configuration whenever a request comes in
224
202
server.use((req, res, next) => {
225
- settings .refresh();
203
+ appConfig .refresh();
226
204
next();
227
205
})
228
206
` ` `
@@ -234,16 +212,16 @@ Even if the refresh call fails for any reason, your application will continue to
234
212
The ` onRefresh ` method lets you custom callback functions that will be invoked each time the local configuration is successfully updated with changes from the Azure App Configuration store . It returns a Disposable object , which you can use to remove the registered callback
235
213
236
214
` ` ` typescript
237
- const settings = await load(endpoint, credential, {
215
+ const appConfig = await load(endpoint, credential, {
238
216
refreshOptions: {
239
217
enabled: true
240
218
}
241
219
});
242
- const disposer = settings .onRefresh(() => {
220
+ const disposer = appConfig .onRefresh(() => {
243
221
console.log("Config refreshed.");
244
222
});
245
223
246
- settings .refresh();
224
+ appConfig .refresh();
247
225
// Once the refresh is successful, the callback function you registered will be executed.
248
226
// In this example, the message "Config refreshed" will be printed.
249
227
@@ -255,7 +233,7 @@ disposer.dispose();
255
233
A sentinel key is a key that you update after you complete the change of all other keys . The configuration provider will monitor the sentinel key instead of all selected key - values . When a change is detected , your app refreshes all configuration values .
256
234
257
235
` ` ` typescript
258
- const settings = await load(endpoint, credential, {
236
+ const appConfig = await load(endpoint, credential, {
259
237
refreshOptions: {
260
238
enabled: true,
261
239
watchedSettings: [
@@ -272,12 +250,12 @@ For more information about refresh configuration, go to [Use dynamic configurati
272
250
You can [create feature flags ](./ manage - feature - flags .md #create - a - feature - flag ) in Azure App Configuration . By default , the feature flags will not be loaded by configuration provider . You can enable loading and refreshing feature flags through ` AzureAppConfigurationOptions.featureFlagOptions ` property when calling the ` load ` method .
273
251
274
252
` ` ` typescript
275
- const settings = await load(endpoint, credential, {
253
+ const appConfig = await load(endpoint, credential, {
276
254
featureFlagOptions: {
277
- enabled: true,
255
+ enabled: true, // enable loading feature flags
278
256
selectors: [ { keyFilter: "*", labelFilter: "Prod" } ],
279
257
refresh: {
280
- enabled: true, // the refresh for feature flags need to be enbaled in addition to key-values
258
+ enabled: true, // enable refreshing feature flags
281
259
refreshIntervalInMs: 10_000
282
260
}
283
261
}
@@ -287,12 +265,96 @@ const settings = await load(endpoint, credential, {
287
265
> [! NOTE ]
288
266
> If ` featureFlagOptions ` is enabled and no selector is specified , the configuration provider will load all feature flags with no label from the App Configuration store .
289
267
268
+ > [! IMPORTANT ]
269
+ > To effectively consume and manage feature flags loaded from Azure App Configuration , install and use the [` @microsoft/feature-management ` ](https :// www.npmjs.com/package/@microsoft/feature-management) package. This library provides a structured way to control feature behavior in your application.
270
+
290
271
### Feature management
291
272
292
- Feature management library provides a way to develop and expose application functionality based on feature flags . The feature management library is designed to work in conjunction with the configuration provider library . The configuration provider will load all selected feature flags into the configuration under the ` feature_flags ` list of the ` feature_management ` section . The feature management library will consume and manage the loaded feature flags for your application .
273
+ Feature management library provides a way to develop and expose application functionality based on feature flags . The feature management library is designed to work in conjunction with the configuration provider library . The configuration provider will load all selected feature flags into the configuration under the ` feature_flags ` list of the ` feature_management ` section . The feature management library will consume and manage the loaded feature flags for your application .
274
+
275
+ The following example demonstrates how to integrate the ` @microsoft/feature-management ` library with the configuration provider to dynamically control API accessibility in an Express application based on the status of the ` Beta ` feature flag .
276
+
277
+ ` ` ` typescript
278
+ // Load feature flags from Azure App Configuration
279
+ import { load } from "@azure/app-configuration-provider";
280
+ const appConfig = await load(endpoint, credential, {
281
+ featureFlagOptions: {
282
+ enabled: true, // enable loading feature flags
283
+ refresh: {
284
+ enabled: true // enable refreshing feature flags
285
+ }
286
+ }
287
+ });
288
+
289
+ import { ConfigurationMapFeatureFlagProvider, FeatureManager } from "@microsoft/feature-management";
290
+ // Create a feature flag provider which uses the configuration provider as feature flag source
291
+ const ffProvider = new ConfigurationMapFeatureFlagProvider(appConfig);
292
+ // Create a feature manager which will evaluate the feature flag
293
+ const fm = new FeatureManager(ffProvider);
294
+
295
+ import express from "express";
296
+ const server = express();
297
+
298
+ // Use a middleware to achieve request-driven configuration refresh
299
+ server.use((req, res, next) => {
300
+ // this call is not blocking, the configuration will be updated asynchronously
301
+ appConfig.refresh();
302
+ next();
303
+ });
304
+
305
+ server.get("/Beta", async (req, res) => {
306
+ if (await featureManager.isEnabled("Beta")) {
307
+ res.send("Welcome to the Beta page!");
308
+ } else {
309
+ res.status(404).send("Page not found");
310
+ }
311
+ });
312
+ ` ` `
293
313
294
314
For more information about how to use the JavaScript feature management library , go to the [feature flag quickstart ](./ quickstart - feature - flag - javascript .md ).
295
315
316
+ ## Key Vault reference
317
+
318
+ Azure App Configuration supports referencing secrets stored in Azure Key Vault . In App Configuration , you can create keys that map to secrets stored in Key Vault . The secrets are securely stored in Key Vault , but can be accessed like any other configuration once loaded .
319
+
320
+ The configuration provider library retrieves Key Vault references , just as it does for any other keys stored in App Configuration . Because the client recognizes the keys as Key Vault references , they have a unique content - type , and the client will connect to Key Vault to retrieve their values for your application . You need to configure ` AzureAppConfigurationOptions.KeyVaultOptions ` property with the proper credential to allow the configuration provider to connect to Azure Key Vault .
321
+
322
+ ` ` ` typescript
323
+ const credential = new DefaultAzureCredential();
324
+ const appConfig = await load(endpoint, credential, {
325
+ keyVaultOptions: {
326
+ credential: credential
327
+ }
328
+ });
329
+ ` ` `
330
+
331
+ You can also provide ` SecretClient ` instance directly to ` KeyVaultOptions ` . In this way , you can customize the options while creating ` SecretClient ` .
332
+
333
+ ` ` ` typescript
334
+ import { SecretClient } from "@azure/keyvault-secrets";
335
+
336
+ const credential = new DefaultAzureCredential();
337
+ const secretClient = new SecretClient(keyVaultUrl, credential, {
338
+ serviceVersion: "7.0",
339
+ });
340
+ const appConfig = await load(endpoint, credential, {
341
+ keyVaultOptions: {
342
+ secretClients: [ secretClient ]
343
+ }
344
+ });
345
+ ` ` `
346
+
347
+ You can also set ` secretResolver ` property to locally resolve secrets that don ’t have a Key Vault associated with them .
348
+
349
+ ` ` ` typescript
350
+ const resolveSecret = (url) => "From Secret Resolver";
351
+ const appConfig = await load(endpoint, credential, {
352
+ keyVaultOptions: {
353
+ secretResolver: resolveSecret
354
+ }
355
+ });
356
+ ` ` `
357
+
296
358
## Geo - replication
297
359
298
360
For information about using geo - replication , go to [Enable geo - replication ](./ howto - geo - replication .md ).
@@ -303,3 +365,8 @@ To learn how to use the JavaScript configuration provider, continue to the follo
303
365
304
366
> [! div class = " nextstepaction" ]
305
367
> [Use dynamic configuration in JavaScript ](./ enable - dynamic - configuration - javascript .md )
368
+
369
+ To learn how to use the JavaScript feature management library , continue to the following documentation .
370
+
371
+ > [! div class = " nextstepaction" ]
372
+ > [JavaScript Feature Management ](./ feature - management - javascript - reference .md )
0 commit comments