@@ -71,7 +71,44 @@ Create a file named *app.js* in the *app-configuration-quickstart* directory and
71
71
In this sample, you connect to Azure App Configuration using a connection string and load key-values without specifying advanced options.
72
72
By default, it loads all key-values with no label.
73
73
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
75
112
const { load } = require (" @azure/app-configuration-provider" );
76
113
const connectionString = process .env .AZURE_APPCONFIG_CONNECTION_STRING ;
77
114
@@ -101,14 +138,55 @@ async function run() {
101
138
run ().catch (console .error );
102
139
```
103
140
141
+ ---
142
+
104
143
### Sample 2: Load specific key-values using selectors
105
144
106
145
In this sample, you load a subset of key-values by specifying the ` selectors ` option.
107
146
Only keys starting with "app." are loaded.
108
147
Note that you can specify multiple selectors based on your needs, each with ` keyFilter ` and ` labelFilter ` properties.
109
148
149
+ ### [ Use Azure Credential (Recommended)] ( #tab/azurecredential )
150
+
110
151
``` javascript
111
152
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" );
112
190
const connectionString = process .env .AZURE_APPCONFIG_CONNECTION_STRING ;
113
191
114
192
async function run () {
@@ -141,13 +219,52 @@ async function run() {
141
219
run ().catch (console .error );
142
220
```
143
221
222
+ ---
223
+
144
224
### Sample 3: Load key-values and trim prefix from keys
145
225
146
226
In this sample, you load key-values with an option ` trimKeyPrefixes ` .
147
227
After key-values are loaded, the prefix "app." is trimmed from all keys.
148
228
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.
149
229
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
151
268
const { load } = require (" @azure/app-configuration-provider" );
152
269
const connectionString = process .env .AZURE_APPCONFIG_CONNECTION_STRING ;
153
270
@@ -179,9 +296,11 @@ async function run() {
179
296
run ().catch (console .error );
180
297
```
181
298
299
+ ---
300
+
182
301
## Run the application
183
302
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.
185
304
186
305
### [ Windows command prompt] ( #tab/windowscommandprompt )
187
306
0 commit comments