Skip to content

Commit f86cad2

Browse files
authored
Merge pull request #50 from Azure/main
Merge main to release/preview/v1
2 parents 36a89ad + cd5552a commit f86cad2

28 files changed

+1394
-205
lines changed

examples/README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,47 @@ These examples show how to use the JavaScript Provider for Azure App Configurati
44

55
## Prerequisites
66

7-
The sample programs are compatible with [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule).
7+
The examples are compatible with [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule).
88

9-
You need [an Azure subscription](https://azure.microsoft.com/free/) and the following Azure resources to run these sample programs:
9+
You need [an Azure subscription](https://azure.microsoft.com/free/) and the following Azure resources to run the examples:
1010

1111
- [Azure App Configuration store](https://learn.microsoft.com/en-us/azure/azure-app-configuration/quickstart-azure-app-configuration-create?tabs=azure-portal)
1212

13-
Samples retrieve credentials to access your App Configuration store from environment variables.
13+
The examples retrieve credentials to access your App Configuration store from environment variables.
1414
Alternatively, edit the source code to include the appropriate credentials.
15-
See each individual sample for details on which environment variables/credentials it requires to function.
15+
See each individual example for details on which environment variables/credentials it requires to function.
1616

17-
## Setup
17+
## Add a key-value
18+
Add the following key-value to the App Configuration store and leave **Label** and **Content Type** with their default values. For more information about how to add key-values to a store using the Azure portal or the CLI, go to [Create a key-value](./quickstart-azure-app-configuration-create.md#create-a-key-value).
1819

19-
To run the samples using the published version of the package:
20+
| Key | Value |
21+
|------------------------|----------------|
22+
| *app.settings.message* | *Hello World!* |
23+
24+
## Setup & Run
25+
26+
To run the examples using the published version of the package:
2027

2128
1. Install the dependencies using `npm`:
2229

2330
```bash
2431
npm install
2532
```
2633

27-
2. There are two ways to run the samples using correct credentials:
34+
2. There are two ways to run the examples using correct credentials:
35+
36+
- Edit the file `.env.template`, adding the access keys to your App Configuration store. and rename the file from `.env.template` to just `.env`. The examples will read this file automatically.
2837

29-
- Edit the file `.env.template`, adding the correct credentials to access your Azure App Configuration store and rename the file from `.env.template` to just `.env`.
30-
Then run the samples, it will read this file automatically.
38+
- Alternatively, you can set the environment variables to the access keys to your App Configuration store. In this case, setting up the `.env` file is not required.
39+
```bash
40+
npx cross-env APPCONFIG_CONNECTION_STRING="<appconfig connection string>"
41+
```
42+
43+
3. Run the examples:
3144
```bash
3245
node helloworld.mjs
3346
```
34-
35-
- Alternatively, run a single sample with the correct environment variables set (setting up the `.env` file is not required if you do this), for example (cross-platform):
36-
```bash
37-
npx cross-env APPCONFIG_CONNECTION_STRING="<appconfig connection string>" node helloworld.mjs
47+
You should see the following output:
48+
```Output
49+
Message from Azure App Configuration: Hello World!
3850
```

examples/configObject.mjs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import * as dotenv from "dotenv";
5+
dotenv.config()
6+
7+
/**
8+
* This example demonstrates how to construct a configuration object from settings loaded from Azure App Configuration.
9+
* If you are using configuration object instead of Map-styled settings, it would minimize the code changes required to use Azure App Configuration in your application.
10+
*
11+
* When you import configuration into Azure App Configuration from a local .json file, the keys are automatically flattened with a separator if specified.
12+
* E.g. if you import the following .json file, specifying the separator as ".":
13+
* {
14+
* "app": {
15+
* "settings": {
16+
* "message": "Hello, Azure!"
17+
* }
18+
* }
19+
* }
20+
*
21+
* In the configuration explorer, the key-values will be:
22+
* - Key: "app.settings.message", Value: "Hello, Azure!"
23+
*
24+
* With the API `constructConfigurationObject`, you can construct a configuration object with the same shape as the original .json file.
25+
* The separator is used to split the keys and construct the object.
26+
* The constructed object will be: { app: { settings: { message: "Hello, Azure!" } } }
27+
*
28+
* Below environment variables are required for this example:
29+
* - APPCONFIG_CONNECTION_STRING
30+
*/
31+
32+
import { load } from "@azure/app-configuration-provider";
33+
const connectionString = process.env.APPCONFIG_CONNECTION_STRING;
34+
const settings = await load(connectionString, {
35+
selectors: [{
36+
keyFilter: "app.settings.*"
37+
}]
38+
});
39+
40+
const config = settings.constructConfigurationObject({
41+
separator: "."
42+
});
43+
44+
console.log("Constructed object 'config': ", config);
45+
console.log(`Message from Azure App Configuration: ${config.app.settings.message}`);

examples/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"dependencies": {
33
"@azure/app-configuration-provider": "latest",
4-
"@azure/identity": "^3.3.0",
4+
"@azure/identity": "^4.0.0",
55
"dotenv": "^16.3.1"
66
}
77
}

examples/refresh.mjs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import * as dotenv from "dotenv";
5+
import { promisify } from "util";
6+
dotenv.config();
7+
const sleepInMs = promisify(setTimeout);
8+
9+
/**
10+
* This example retrives all settings with key following pattern "app.settings.*", i.e. starting with "app.settings.".
11+
* With the option `trimKeyPrefixes`, it trims the prefix "app.settings." from keys for simplicity.
12+
* Value of config "app.settings.message" will be printed.
13+
* It also watches for changes to the key "app.settings.sentinel" and refreshes the configuration when it changes.
14+
*
15+
* Below environment variables are required for this example:
16+
* - APPCONFIG_CONNECTION_STRING
17+
*/
18+
19+
import { load } from "@azure/app-configuration-provider";
20+
const connectionString = process.env.APPCONFIG_CONNECTION_STRING;
21+
const settings = await load(connectionString, {
22+
selectors: [{
23+
keyFilter: "app.settings.*"
24+
}],
25+
trimKeyPrefixes: ["app.settings."],
26+
refreshOptions: {
27+
watchedSettings: [{ key: "app.settings.sentinel" }],
28+
refreshIntervalInMs: 10 * 1000 // Default value is 30 seconds, shorted for this sample
29+
}
30+
});
31+
32+
console.log("Using Azure portal or CLI, update the `app.settings.message` value, and then update the `app.settings.sentinel` value in your App Configuration store.")
33+
34+
// eslint-disable-next-line no-constant-condition
35+
while (true) {
36+
// Refreshing the configuration setting
37+
await settings.refresh();
38+
39+
// Current value of message
40+
console.log(settings.get("message"));
41+
42+
// Waiting before the next refresh
43+
await sleepInMs(5000);
44+
}

package-lock.json

Lines changed: 37 additions & 56 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"dev": "rollup --config --watch",
2626
"lint": "eslint src/ test/",
2727
"fix-lint": "eslint src/ test/ --fix",
28-
"test": "mocha out/test/*.test.{js,cjs,mjs} --timeout 10000"
28+
"test": "mocha out/test/*.test.{js,cjs,mjs} --parallel"
2929
},
3030
"repository": {
3131
"type": "git",
@@ -56,7 +56,7 @@
5656
},
5757
"dependencies": {
5858
"@azure/app-configuration": "^1.5.0",
59-
"@azure/identity": "^3.3.2",
59+
"@azure/identity": "^4.0.0",
6060
"@azure/keyvault-secrets": "^4.7.0"
6161
}
6262
}

scripts/build-and-pack.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
# Stop on error.
4+
set -e
5+
6+
# Get the directory of the script.
7+
SCRIPT_DIR=$(dirname $(readlink -f $0))
8+
9+
# Get the directory of the project.
10+
PROJECT_BASE_DIR=$(dirname $SCRIPT_DIR)
11+
12+
# Change to the project directory.
13+
cd $PROJECT_BASE_DIR
14+
15+
# Install dependencies, build, and test.
16+
echo "npm clean install"
17+
npm ci
18+
19+
echo "npm run build"
20+
npm run build
21+
22+
echo "npm run test"
23+
npm run test
24+
25+
# Create a tarball.
26+
echo "npm pack"
27+
npm pack

0 commit comments

Comments
 (0)