Skip to content

Commit 81d91c8

Browse files
add credential example
1 parent 825a516 commit 81d91c8

File tree

2 files changed

+172
-6
lines changed

2 files changed

+172
-6
lines changed

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

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,59 @@ Add a feature flag called *Beta* to the App Configuration store and leave **Labe
4141

4242
1. Open the file *app.js* and update it with the following code.
4343

44+
### [Use Azure Credential (Recommended)](#tab/azurecredential)
45+
46+
``` javascript
47+
const sleepInMs = require("util").promisify(setTimeout);
48+
const { load } = require("@azure/app-configuration-provider");
49+
const { getDefaultAzureCredential } = require("@azure/identity");
50+
const { FeatureManager, ConfigurationMapFeatureFlagProvider} = require("@microsoft/feature-management")
51+
const endpoint = process.env.AZURE_APPCONFIG_ENDPOINT;
52+
53+
async function run() {
54+
// Connect to Azure App Configuration using an endpoint with credential.
55+
// To learn more about Azure credential, please refer to
56+
// https://learn.microsoft.com/javascript/api/overview/azure/identity-readme#defaultazurecredential
57+
const settings = await load(endpoint, getDefaultAzureCredential(), {
58+
featureFlagOptions: {
59+
enabled: true,
60+
// Note: selectors must be explicitly provided for feature flags.
61+
selectors: [{
62+
keyFilter: "*"
63+
}],
64+
refresh: {
65+
enabled: true,
66+
refreshIntervalInMs: 10_000
67+
}
68+
}
69+
});
70+
71+
// Create a feature flag provider which uses a map as feature flag source
72+
const ffProvider = new ConfigurationMapFeatureFlagProvider(settings);
73+
// Create a feature manager which will evaluate the feature flag
74+
const fm = new FeatureManager(ffProvider);
75+
76+
while (true) {
77+
await settings.refresh(); // Refresh to get the latest feature flag settings
78+
const isEnabled = await fm.isEnabled("Beta"); // Evaluate the feature flag
79+
console.log(`Beta is enabled: ${isEnabled}`);
80+
await sleepInMs(5000);
81+
}
82+
}
83+
84+
run().catch(console.error);
85+
```
86+
87+
### [Use Connection String](#tab/connectionstring)
88+
4489
``` javascript
4590
const sleepInMs = require("util").promisify(setTimeout);
4691
const { load } = require("@azure/app-configuration-provider");
4792
const { FeatureManager, ConfigurationMapFeatureFlagProvider} = require("@microsoft/feature-management")
4893
const connectionString = process.env.AZURE_APPCONFIG_CONNECTION_STRING;
4994

5095
async function run() {
51-
// Connecting to Azure App Configuration using connection string
96+
// Connect to Azure App Configuration using connection string
5297
const settings = await load(connectionString, {
5398
featureFlagOptions: {
5499
enabled: true,
@@ -63,9 +108,9 @@ Add a feature flag called *Beta* to the App Configuration store and leave **Labe
63108
}
64109
});
65110

66-
// Creating a feature flag provider which uses a map as feature flag source
111+
// Create a feature flag provider which uses a map as feature flag source
67112
const ffProvider = new ConfigurationMapFeatureFlagProvider(settings);
68-
// Creating a feature manager which will evaluate the feature flag
113+
// Create a feature manager which will evaluate the feature flag
69114
const fm = new FeatureManager(ffProvider);
70115

71116
while (true) {
@@ -79,6 +124,8 @@ Add a feature flag called *Beta* to the App Configuration store and leave **Labe
79124
run().catch(console.error);
80125
```
81126

127+
---
128+
82129
## Run the application
83130

84131
1. Run your script:

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

Lines changed: 122 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,44 @@ Create a file named *app.js* in the *app-configuration-quickstart* directory and
7171
In this sample, you connect to Azure App Configuration using a connection string and load key-values without specifying advanced options.
7272
By default, it loads all key-values with no label.
7373

74-
```javascript
74+
### [Use Azure Credential (Recommended)](#tab/azurecredential)
75+
76+
``` javascript
77+
const { load } = require("@azure/app-configuration-provider");
78+
const { getDefaultAzureCredential } = require("@azure/identity");
79+
const endpoint = process.env.AZURE_APPCONFIG_ENDPOINT;
80+
81+
async function run() {
82+
console.log("Sample 1: Load key-values with default selector");
83+
84+
// Connect to Azure App Configuration using an endpoint with credential and load all key-values with null label.
85+
// To learn more about Azure credential, please refer to
86+
// https://learn.microsoft.com/javascript/api/overview/azure/identity-readme#defaultazurecredential
87+
const settings = await load(endpoint, getDefaultAzureCredential());
88+
89+
console.log("---Consume configuration as a Map---");
90+
// Find the key "message" and print its value.
91+
console.log('settings.get("message"):', settings.get("message")); // settings.get("message"): Message from Azure App Configuration
92+
// Find the key "app.greeting" and print its value.
93+
console.log('settings.get("app.greeting"):', settings.get("app.greeting")); // settings.get("app.greeting"): Hello World
94+
// Find the key "app.json" whose value is an object.
95+
console.log('settings.get("app.json"):', settings.get("app.json")); // settings.get("app.json"): { myKey: 'myValue' }
96+
97+
console.log("---Consume configuration as an object---");
98+
// Construct configuration object from loaded key-values, by default "." is used to separate hierarchical keys.
99+
const config = settings.constructConfigurationObject();
100+
// Use dot-notation to access configuration
101+
console.log("config.message:", config.message); // config.message: Message from Azure App Configuration
102+
console.log("config.app.greeting:", config.app.greeting); // config.app.greeting: Hello World
103+
console.log("config.app.json:", config.app.json); // config.app.json: { myKey: 'myValue' }
104+
}
105+
106+
run().catch(console.error);
107+
```
108+
109+
### [Use Connection String](#tab/connectionstring)
110+
111+
``` javascript
75112
const { load } = require("@azure/app-configuration-provider");
76113
const connectionString = process.env.AZURE_APPCONFIG_CONNECTION_STRING;
77114

@@ -101,14 +138,55 @@ async function run() {
101138
run().catch(console.error);
102139
```
103140

141+
---
142+
104143
### Sample 2: Load specific key-values using selectors
105144

106145
In this sample, you load a subset of key-values by specifying the `selectors` option.
107146
Only keys starting with "app." are loaded.
108147
Note that you can specify multiple selectors based on your needs, each with `keyFilter` and `labelFilter` properties.
109148

149+
### [Use Azure Credential (Recommended)](#tab/azurecredential)
150+
110151
```javascript
111152
const { load } = require("@azure/app-configuration-provider");
153+
const { getDefaultAzureCredential } = require("@azure/identity");
154+
const endpoint = process.env.AZURE_APPCONFIG_ENDPOINT;
155+
156+
async function run() {
157+
console.log("Sample 2: Load specific key-values using selectors");
158+
159+
// Load a subset of keys starting with "app." prefix.
160+
const settings = await load(endpoint, getDefaultAzureCredential(), {
161+
selectors: [{
162+
keyFilter: "app.*"
163+
}],
164+
});
165+
166+
console.log("---Consume configuration as a Map---");
167+
// The key "message" is not loaded as it does not start with "app."
168+
console.log('settings.has("message"):', settings.has("message")); // settings.has("message"): false
169+
// The key "app.greeting" is loaded
170+
console.log('settings.has("app.greeting"):', settings.has("app.greeting")); // settings.has("app.greeting"): true
171+
// The key "app.json" is loaded
172+
console.log('settings.has("app.json"):', settings.has("app.json")); // settings.has("app.json"): true
173+
174+
console.log("---Consume configuration as an object---");
175+
// Construct configuration object from loaded key-values
176+
const config = settings.constructConfigurationObject({ separator: "." });
177+
// Use dot-notation to access configuration
178+
console.log("config.message:", config.message); // config.message: undefined
179+
console.log("config.app.greeting:", config.app.greeting); // config.app.greeting: Hello World
180+
console.log("config.app.json:", config.app.json); // config.app.json: { myKey: 'myValue' }
181+
}
182+
183+
run().catch(console.error);
184+
```
185+
186+
### [Use Connection String](#tab/connectionstring)
187+
188+
``` javascript
189+
const { load } = require("@azure/app-configuration-provider");
112190
const connectionString = process.env.AZURE_APPCONFIG_CONNECTION_STRING;
113191

114192
async function run() {
@@ -141,13 +219,52 @@ async function run() {
141219
run().catch(console.error);
142220
```
143221

222+
---
223+
144224
### Sample 3: Load key-values and trim prefix from keys
145225

146226
In this sample, you load key-values with an option `trimKeyPrefixes`.
147227
After key-values are loaded, the prefix "app." is trimmed from all keys.
148228
This is useful when you want to load configurations that are specific to your application by filtering to a certain key prefix, but you don't want your code to carry the prefix every time it accesses the configuration.
149229

150-
```javascript
230+
### [Use Azure Credential (Recommended)](#tab/azurecredential)
231+
232+
``` javascript
233+
const { load } = require("@azure/app-configuration-provider");
234+
const { getDefaultAzureCredential } = require("@azure/identity");
235+
const endpoint = process.env.AZURE_APPCONFIG_ENDPOINT;
236+
237+
async function run() {
238+
console.log("Sample 3: Load key-values and trim prefix from keys");
239+
240+
// Load all key-values with no label, and trim "app." prefix from all keys.
241+
const settings = await load(endpoint, getDefaultAzureCredential(), {
242+
selectors: [{
243+
keyFilter: "app.*"
244+
}],
245+
trimKeyPrefixes: ["app."]
246+
});
247+
248+
console.log("---Consume configuration as a Map---");
249+
// The original key "app.greeting" is trimmed as "greeting".
250+
console.log('settings.get("greeting"):', settings.get("greeting")); // settings.get("greeting"): Hello World
251+
// The original key "app.json" is trimmed as "json".
252+
console.log('settings.get("json"):', settings.get("json")); // settings.get("json"): { myKey: 'myValue' }
253+
254+
console.log("---Consume configuration as an object---");
255+
// Construct configuration object from loaded key-values with trimmed keys.
256+
const config = settings.constructConfigurationObject();
257+
// Use dot-notation to access configuration
258+
console.log("config.greeting:", config.greeting); // config.greeting: Hello World
259+
console.log("config.json:", config.json); // config.json: { myKey: 'myValue' }
260+
}
261+
262+
run().catch(console.error);
263+
```
264+
265+
### [Use Connection String](#tab/connectionstring)
266+
267+
``` javascript
151268
const { load } = require("@azure/app-configuration-provider");
152269
const connectionString = process.env.AZURE_APPCONFIG_CONNECTION_STRING;
153270

@@ -179,9 +296,11 @@ async function run() {
179296
run().catch(console.error);
180297
```
181298

299+
---
300+
182301
## Run the application
183302

184-
1. Set an environment variable named **AZURE_APPCONFIG_CONNECTION_STRING**, and set it to the connection string of your App Configuration store. At the command line, run the following command:
303+
1. Set an environment variable named **AZURE_APPCONFIG_ENDPOINT** or **AZURE_APPCONFIG_CONNECTION_STRING**, depending on the method you used to connect to App Config. Set its value to the endpoint or connection string of your App Configuration store. The following example shows how to set an environment variable to store the connection string on different platforms.
185304

186305
### [Windows command prompt](#tab/windowscommandprompt)
187306

0 commit comments

Comments
 (0)