Skip to content

Commit de56758

Browse files
authored
Add examples and getting started instructions (#12)
1 parent ad0ee3f commit de56758

File tree

9 files changed

+182
-16
lines changed

9 files changed

+182
-16
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,4 +406,7 @@ types/
406406
.env
407407

408408
# npm pack
409-
*.tgz
409+
*.tgz
410+
411+
# examples
412+
examples/package-lock.json

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,36 @@
22

33
The [Azure App Configuration](https://docs.microsoft.com/en-us/azure/azure-app-configuration/overview) provider for JavaScript enables developers to configure their applications using centralized configuration located in Azure App Configuration.
44

5+
## Getting started
6+
7+
### Prerequisites
8+
9+
- An [Azure Subscription](https://azure.microsoft.com)
10+
- An [App Configuration](https://learn.microsoft.com/en-us/azure/azure-app-configuration/quickstart-azure-app-configuration-create?tabs=azure-portal) resource
11+
12+
### Install the package
13+
14+
```bash
15+
npm install @azure/app-configuration-provider
16+
```
17+
18+
### Use the API
19+
20+
```js
21+
import { load } from "@azure/app-configuration-provider";
22+
23+
// Load settings from App Configuration as a readonly Map.
24+
const settings = await load("<app-configuration-connection-string>");
25+
26+
// Consume the settings by calling `get(key)`, e.g.
27+
const value = settings.get("<key-of-a-config>");
28+
```
29+
30+
31+
## Examples
32+
33+
See code snippets under [examples/](./examples/) folder.
34+
535
## Contributing
636

737
This project welcomes contributions and suggestions. Most contributions require you to agree to a
@@ -24,6 +54,6 @@ trademarks or logos is subject to and must follow
2454
Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.
2555
Any use of third-party trademarks or logos are subject to those third-party's policies.
2656

27-
# Data Collection
57+
## Data Collection
2858

2959
The software may collect information about you and your use of the software and send it to Microsoft. Microsoft may use this information to provide services and improve our products and services. You may turn off the telemetry by setting the environment variable `AZURE_APP_CONFIGURATION_TRACING_DISABLED` to `TRUE`. There are also some features in the software that may enable you and Microsoft to collect data from users of your applications. If you use these features, you must comply with applicable law, including providing appropriate notices to users of your applications together with a copy of Microsoft’s privacy statement. Our privacy statement is located at https://go.microsoft.com/fwlink/?LinkID=824704. You can learn more about data collection and use in the help documentation and our privacy statement. Your use of the software operates as your consent to these practices.

SUPPORT.md

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
# TODO: The maintainer of this repo has not yet edited this file
2-
3-
**REPO OWNER**: Do you want Customer Service & Support (CSS) support for this product/project?
4-
5-
- **No CSS support:** Fill out this template with information about how to file issues and get help.
6-
- **Yes CSS support:** Fill out an intake form at [aka.ms/onboardsupport](https://aka.ms/onboardsupport). CSS will work with/help you to determine next steps.
7-
- **Not sure?** Fill out an intake as though the answer were "Yes". CSS will help you decide.
8-
9-
*Then remove this first heading from this SUPPORT.MD file before publishing your repo.*
10-
111
# Support
122

133
## How to file issues and get help
@@ -16,10 +6,8 @@ This project uses GitHub Issues to track bugs and feature requests. Please searc
166
issues before filing new issues to avoid duplicates. For new issues, file your bug or
177
feature request as a new Issue.
188

19-
For help and questions about using this project, please **REPO MAINTAINER: INSERT INSTRUCTIONS HERE
20-
FOR HOW TO ENGAGE REPO OWNERS OR COMMUNITY FOR HELP. COULD BE A STACK OVERFLOW TAG OR OTHER
21-
CHANNEL. WHERE WILL YOU HELP PEOPLE?**.
9+
For help and questions about using this project, please ask a question in Stack Overflow with [azure-app-configuration](https://stackoverflow.com/questions/tagged/azure-app-configuration) tag.
2210

2311
## Microsoft Support Policy
2412

25-
Support for this **PROJECT or PRODUCT** is limited to the resources listed above.
13+
Support for this project is limited to the resources listed above.

examples/.env.template

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# You can define environment variables in .env file and load them with 'dotenv' package.
2+
# This is a template of related environment variables in examples.
3+
# To use this file directly, please rename it to .env
4+
APPCONFIG_CONNECTION_STRING=<app-configuration-connection-string>
5+
6+
APPCONFIG_ENDPOINT=<app-configuration-endpoint>
7+
8+
# Credential used to authenticate using Microsoft Entra ID (Azure AD) role-based authentication.
9+
# https://docs.microsoft.com/javascript/api/@azure/identity/environmentcredential
10+
AZURE_TENANT_ID=<AD tenant id or name>
11+
AZURE_CLIENT_ID=<ID of the user/service principal to authenticate as>
12+
AZURE_CLIENT_SECRET=<client secret used to authenticate to Azure AD>

examples/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Examples for Azure App Configuration JavaScript Provider
2+
3+
These examples show how to use the JavaScript Provider for Azure App Configuration in some common scenarios.
4+
5+
## Prerequisites
6+
7+
The sample programs are compatible with [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule).
8+
9+
You need [an Azure subscription](https://azure.microsoft.com/free/) and the following Azure resources to run these sample programs:
10+
11+
- [Azure App Configuration store](https://learn.microsoft.com/en-us/azure/azure-app-configuration/quickstart-azure-app-configuration-create?tabs=azure-portal)
12+
13+
Samples retrieve credentials to access your App Configuration store from environment variables.
14+
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.
16+
17+
## Setup
18+
19+
To run the samples using the published version of the package:
20+
21+
1. Install the dependencies using `npm`:
22+
23+
```bash
24+
npm install
25+
```
26+
27+
2. There are two ways to run the samples using correct credentials:
28+
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.
31+
```bash
32+
node helloworld.mjs
33+
```
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
38+
```

examples/helloworld.mjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 retrives all settings with key following pattern "app.settings.*", i.e. starting with "app.settings.".
9+
* With the option `trimKeyPrefixes`, it trims the prefix "app.settings." from keys for simplicity.
10+
* Value of config "app.settings.message" will be printed.
11+
*
12+
* Below environment variables are required for this example:
13+
* - APPCONFIG_CONNECTION_STRING
14+
*/
15+
16+
import { load } from "@azure/app-configuration-provider";
17+
const connectionString = process.env.APPCONFIG_CONNECTION_STRING;
18+
const settings = await load(connectionString, {
19+
selectors: [{
20+
keyFilter: "app.settings.*"
21+
}],
22+
trimKeyPrefixes: ["app.settings."]
23+
});
24+
const message = settings.get("message");
25+
26+
console.log(`Message from Azure App Configuration: ${message}`);

examples/helloworld_aad.mjs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 retrives all settings with key following pattern "app.settings.*", i.e. starting with "app.settings.".
9+
* With the option `trimKeyPrefixes`, it trims the prefix "app.settings." from keys for simplicity.
10+
* Value of config "app.settings.message" will be printed.
11+
*
12+
* Below environment variables are required for this example:
13+
* - APPCONFIG_ENDPOINT
14+
* - AZURE_TENANT_ID
15+
* - AZURE_CLIENT_ID
16+
* - AZURE_CLIENT_SECRET
17+
*/
18+
19+
import { load } from "@azure/app-configuration-provider";
20+
import { getDefaultAzureCredential } from "@azure/identity";
21+
const endpoint = process.env.APPCONFIG_ENDPOINT;
22+
const credential = getDefaultAzureCredential();
23+
const settings = await load(endpoint, credential, {
24+
selectors: [{
25+
keyFilter: "app.settings.*"
26+
}],
27+
trimKeyPrefixes: ["app.settings."]
28+
});
29+
const message = settings.get("message");
30+
31+
console.log(`Message from Azure App Configuration: ${message}`);

examples/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dependencies": {
3+
"@azure/app-configuration-provider": "latest",
4+
"@azure/identity": "^3.3.0",
5+
"dotenv": "^16.3.1"
6+
}
7+
}

examples/secretReference.mjs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import * as dotenv from "dotenv";
5+
dotenv.config()
6+
7+
/**
8+
* Before you run it, please add a Key Vault reference with key "app.secret" in your App Configuration store.
9+
* This example uses the same identity to authenticate with both App Configuration and Key Vault. Make sure that this identity has access to read secrets from Key Vault.
10+
* Value of secret "app.secret" will be printed.
11+
*
12+
* Below environment variables are required for this example:
13+
* - APPCONFIG_ENDPOINT
14+
* - AZURE_TENANT_ID
15+
* - AZURE_CLIENT_ID
16+
* - AZURE_CLIENT_SECRET
17+
*/
18+
19+
import { load } from "@azure/app-configuration-provider";
20+
import { getDefaultAzureCredential } from "@azure/identity";
21+
const endpoint = process.env.APPCONFIG_ENDPOINT;
22+
const credential = getDefaultAzureCredential();
23+
const settings = await load(endpoint, credential, {
24+
keyVaultOptions: {
25+
credential: credential
26+
}
27+
});
28+
const secretKey = "app.secret";
29+
const value = settings.get(secretKey);
30+
31+
console.log(`Get the secret from keyvault key: ${secretKey}, value: ${value}`);

0 commit comments

Comments
 (0)