Skip to content

Commit 7679fa4

Browse files
authored
Merge pull request #299001 from zhiyuanliang-ms/zhiyuanliang/update-js-doc
Azure App Configuration - Update JS provider reference doc
2 parents a435eff + 3d1251e commit 7679fa4

File tree

5 files changed

+135
-68
lines changed

5 files changed

+135
-68
lines changed

articles/azure-app-configuration/configuration-provider-overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Entra ID Authentication | GA | GA | GA | GA | [GA](./reference-javascript-provid
5050
Dynamic Refresh (Poll Mode) | GA | GA | GA | GA | GA
5151
Dynamic Refresh (Push Mode) | GA | GA | N/A | N/A | N/A
5252
Dynamic Refresh (Collection Monitoring) | WIP | WIP | GA | WIP | [GA](./reference-javascript-provider.md#configuration-refresh)
53-
JSON Content Type Handling | GA | GA | GA | GA | GA
53+
JSON Content Type Handling | GA | GA | GA | GA | [GA](./reference-javascript-provider.md#json-content-type-handling)
5454
Configuration Setting Mapping | GA | N/A | N/A | N/A | N/A
5555
Key Vault References | GA | GA | GA | GA | [GA](./reference-javascript-provider.md#key-vault-reference)
5656
Key Vault Secret Refresh | GA | WIP | GA | WIP | WIP

articles/azure-app-configuration/howto-feature-filters-javascript.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,4 @@ To learn more about the built-in feature filters, continue to the following docu
8383
For the full feature rundown of the JavaScript feature management library, continue to the following document.
8484

8585
> [!div class="nextstepaction"]
86-
> [.NET Feature Management](./feature-management-javascript-reference.md)
86+
> [JavaScript Feature Management](./feature-management-javascript-reference.md)

articles/azure-app-configuration/howto-timewindow-filter-javascript.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,4 @@ To learn more about the feature filters, continue to the following documents.
7979
For the full feature rundown of the JavaScript feature management library, continue to the following document.
8080

8181
> [!div class="nextstepaction"]
82-
> [.NET Feature Management](./feature-management-javascript-reference.md)
82+
> [JavaScript Feature Management](./feature-management-javascript-reference.md)

articles/azure-app-configuration/quickstart-feature-flag-javascript.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ Add a feature flag called *Beta* to the App Configuration store and leave **Labe
198198

199199
## Next steps
200200

201-
For the full feature rundown of the JavaScript.NET feature management library, continue to the following document.
201+
For the full feature rundown of the JavaScript feature management library, continue to the following document.
202202

203203
> [!div class="nextstepaction"]
204204
> [JavaScript Feature Management](./feature-management-javascript-reference.md)

articles/azure-app-configuration/reference-javascript-provider.md

Lines changed: 131 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const credential = new DefaultAzureCredential(); // For more information, see ht
3535

3636
async function run() {
3737
// 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"));
4040
}
4141

4242
run();
@@ -50,8 +50,8 @@ const connectionString = process.env.AZURE_APPCONFIG_CONNECTION_STRING;
5050

5151
async function run() {
5252
// 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"));
5555
}
5656

5757
run();
@@ -85,8 +85,8 @@ The `AzureAppConfiguration` type extends the following interfaces:
8585
The `IGettable` interface provides `get` method to retrieve the value of a key-value from the Map-styled data structure.
8686

8787
```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
9090
```
9191

9292
- `ReadonlyMap`
@@ -112,20 +112,40 @@ The `AzureAppConfiguration` type extends the following interfaces:
112112
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.
113113

114114
```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
118118
const fontSize2 = settingsObj.app.font.size; // object-style configuration representation
119119
```
120120

121+
### JSON Content Type Handling
122+
123+
You can [create JSON key-values](./howto-leverage-json-content-type.md#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+
121141
### Load specific key-values using selectors
122142

123143
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.
124144

125145
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.
126146

127147
```typescript
128-
const settings = await load(endpoint, credential, {
148+
const appConfig = await load(endpoint, credential, {
129149
selectors: [
130150
{ // load the subset of keys starting with "app1." prefix and "test" label
131151
keyFilter: "app1.*",
@@ -146,62 +166,20 @@ const settings = await load(endpoint, credential, {
146166
You can trim the prefix off of keys by providing a list of trimmed key prefixes to the `AzureAppConfigurationOptions.trimKeyPrefixes` property.
147167

148168
```typescript
149-
const settings = await load(endpoint, credential, {
169+
const appConfig = await load(endpoint, credential, {
150170
selectors: [{
151171
keyFilter: "app.*"
152172
}],
153173
trimKeyPrefixes: ["app."]
154174
});
155175
```
156176

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 dont 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-
199177
## Configuration refresh
200178

201179
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.
202180

203181
```typescript
204-
const settings = await load(endpoint, credential, {
182+
const appConfig = await load(endpoint, credential, {
205183
refreshOptions: {
206184
enabled: true,
207185
refreshIntervalInMs: 15_000
@@ -213,7 +191,7 @@ Setting up `refreshOptions` alone won't automatically refresh the configuration.
213191

214192
```typescript
215193
// this call is not blocking, the configuration will be updated asynchronously
216-
settings.refresh();
194+
appConfig.refresh();
217195
```
218196

219197
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
222200
const server = express();
223201
// Use an express middleware to refresh configuration whenever a request comes in
224202
server.use((req, res, next) => {
225-
settings.refresh();
203+
appConfig.refresh();
226204
next();
227205
})
228206
```
@@ -234,16 +212,16 @@ Even if the refresh call fails for any reason, your application will continue to
234212
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
235213

236214
```typescript
237-
const settings = await load(endpoint, credential, {
215+
const appConfig = await load(endpoint, credential, {
238216
refreshOptions: {
239217
enabled: true
240218
}
241219
});
242-
const disposer = settings.onRefresh(() => {
220+
const disposer = appConfig.onRefresh(() => {
243221
console.log("Config refreshed.");
244222
});
245223
246-
settings.refresh();
224+
appConfig.refresh();
247225
// Once the refresh is successful, the callback function you registered will be executed.
248226
// In this example, the message "Config refreshed" will be printed.
249227
@@ -255,7 +233,7 @@ disposer.dispose();
255233
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.
256234

257235
```typescript
258-
const settings = await load(endpoint, credential, {
236+
const appConfig = await load(endpoint, credential, {
259237
refreshOptions: {
260238
enabled: true,
261239
watchedSettings: [
@@ -272,12 +250,12 @@ For more information about refresh configuration, go to [Use dynamic configurati
272250
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.
273251

274252
```typescript
275-
const settings = await load(endpoint, credential, {
253+
const appConfig = await load(endpoint, credential, {
276254
featureFlagOptions: {
277-
enabled: true,
255+
enabled: true, // enable loading feature flags
278256
selectors: [ { keyFilter: "*", labelFilter: "Prod" } ],
279257
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
281259
refreshIntervalInMs: 10_000
282260
}
283261
}
@@ -287,12 +265,96 @@ const settings = await load(endpoint, credential, {
287265
> [!NOTE]
288266
> 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.
289267

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+
290271
### Feature management
291272

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+
```
293313

294314
For more information about how to use the JavaScript feature management library, go to the [feature flag quickstart](./quickstart-feature-flag-javascript.md).
295315

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 dont 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+
296358
## Geo-replication
297359

298360
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
303365

304366
> [!div class="nextstepaction"]
305367
> [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

Comments
 (0)