Skip to content

Merge main to release/v2 #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
100 changes: 50 additions & 50 deletions examples/README.md → examples/console-app/README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
# Examples for Azure App Configuration JavaScript Provider
These examples show how to use the JavaScript Provider for Azure App Configuration in some common scenarios.
## Prerequisites
The examples are compatible with [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule).
You need [an Azure subscription](https://azure.microsoft.com/free/) and the following Azure resources to run the examples:
- [Azure App Configuration store](https://learn.microsoft.com/en-us/azure/azure-app-configuration/quickstart-azure-app-configuration-create?tabs=azure-portal)
The examples retrieve credentials to access your App Configuration store from environment variables.
Alternatively, edit the source code to include the appropriate credentials.
See each individual example for details on which environment variables/credentials it requires to function.
## Add a key-value
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).
| Key | Value |
|------------------------|----------------|
| *app.settings.message* | *Hello World!* |
## Setup & Run
To run the examples using the published version of the package:
1. Install the dependencies using `npm`:
```bash
npm install
```
2. There are two ways to run the examples using correct credentials:
- 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.
- 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.
```bash
npx cross-env APPCONFIG_CONNECTION_STRING="<appconfig connection string>"
```
3. Run the examples:
```bash
node helloworld.mjs
```
You should see the following output:
```Output
Message from Azure App Configuration: Hello World!
```
# Examples for Azure App Configuration JavaScript Provider

These examples show how to use the JavaScript Provider for Azure App Configuration in some common scenarios.

## Prerequisites

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

You need [an Azure subscription](https://azure.microsoft.com/free/) and the following Azure resources to run the examples:

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

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

## Add a key-value
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).

| Key | Value |
|------------------------|----------------|
| *app.settings.message* | *Hello World!* |

## Setup & Run

To run the examples using the published version of the package:

1. Install the dependencies using `npm`:

```bash
npm install
```

2. There are two ways to run the examples using correct credentials:

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

- 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.
```bash
npx cross-env APPCONFIG_CONNECTION_STRING="<appconfig connection string>"
```

3. Run the examples:
```bash
node helloworld.mjs
```
You should see the following output:
```Output
Message from Azure App Configuration: Hello World!
```
92 changes: 47 additions & 45 deletions examples/configObject.mjs → examples/console-app/configObject.mjs
Original file line number Diff line number Diff line change
@@ -1,45 +1,47 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import * as dotenv from "dotenv";
dotenv.config()

/**
* This example demonstrates how to construct a configuration object from settings loaded from Azure App Configuration.
* 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.
*
* When you import configuration into Azure App Configuration from a local .json file, the keys are automatically flattened with a separator if specified.
* E.g. if you import the following .json file, specifying the separator as ".":
* {
* "app": {
* "settings": {
* "message": "Hello, Azure!"
* }
* }
* }
*
* In the configuration explorer, the key-values will be:
* - Key: "app.settings.message", Value: "Hello, Azure!"
*
* With the API `constructConfigurationObject`, you can construct a configuration object with the same shape as the original .json file.
* The separator is used to split the keys and construct the object.
* The constructed object will be: { app: { settings: { message: "Hello, Azure!" } } }
*
* Below environment variables are required for this example:
* - APPCONFIG_CONNECTION_STRING
*/

import { load } from "@azure/app-configuration-provider";
const connectionString = process.env.APPCONFIG_CONNECTION_STRING;
const settings = await load(connectionString, {
selectors: [{
keyFilter: "app.settings.*"
}]
});

const config = settings.constructConfigurationObject({
separator: "."
});

console.log("Constructed object 'config': ", config);
console.log(`Message from Azure App Configuration: ${config.app.settings.message}`);
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import * as dotenv from "dotenv";
dotenv.config()

/**
* This example demonstrates how to construct a configuration object from settings loaded from Azure App Configuration.
* 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.
*
* When you import configuration into Azure App Configuration from a local .json file, the keys are automatically flattened with a separator if specified.
* E.g. if you import the following .json file, specifying the separator as ".":
* {
* "app": {
* "settings": {
* "message": "Hello, Azure!"
* }
* }
* }
*
* In the configuration explorer, the key-values will be:
* - Key: "app.settings.message", Value: "Hello, Azure!"
*
* With the API `constructConfigurationObject`, you can construct a configuration object with the same shape as the original .json file.
* The separator is used to split the keys and construct the object.
* The constructed object will be: { app: { settings: { message: "Hello, Azure!" } } }
*
* Below environment variables are required for this example:
* - APPCONFIG_CONNECTION_STRING
*/

import { load } from "@azure/app-configuration-provider";
const connectionString = process.env.APPCONFIG_CONNECTION_STRING;
const settings = await load(connectionString, {
selectors: [{
keyFilter: "app.settings.*"
}]
});

/**
* Construct configuration object based on Map-styled data structure and hierarchical keys.
* The default separator is ".", you can specify a custom separator by constructConfigurationObject({separator: "<custom_separator>"}).
*/
const config = settings.constructConfigurationObject();

console.log("Constructed object 'config': ", config);
console.log(`Message from Azure App Configuration: ${config.app.settings.message}`);
50 changes: 25 additions & 25 deletions examples/helloworld.mjs → examples/console-app/helloworld.mjs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import * as dotenv from "dotenv";
dotenv.config()
/**
* This example retrives all settings with key following pattern "app.settings.*", i.e. starting with "app.settings.".
* With the option `trimKeyPrefixes`, it trims the prefix "app.settings." from keys for simplicity.
* Value of config "app.settings.message" will be printed.
*
* Below environment variables are required for this example:
* - APPCONFIG_CONNECTION_STRING
*/
import { load } from "@azure/app-configuration-provider";
const connectionString = process.env.APPCONFIG_CONNECTION_STRING;
const settings = await load(connectionString, {
selectors: [{
keyFilter: "app.settings.*"
}],
trimKeyPrefixes: ["app.settings."]
});
const message = settings.get("message");
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import * as dotenv from "dotenv";
dotenv.config()

/**
* This example retrives all settings with key following pattern "app.settings.*", i.e. starting with "app.settings.".
* With the option `trimKeyPrefixes`, it trims the prefix "app.settings." from keys for simplicity.
* Value of config "app.settings.message" will be printed.
*
* Below environment variables are required for this example:
* - APPCONFIG_CONNECTION_STRING
*/

import { load } from "@azure/app-configuration-provider";
const connectionString = process.env.APPCONFIG_CONNECTION_STRING;
const settings = await load(connectionString, {
selectors: [{
keyFilter: "app.settings.*"
}],
trimKeyPrefixes: ["app.settings."]
});
const message = settings.get("message");

console.log(`Message from Azure App Configuration: ${message}`);
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import * as dotenv from "dotenv";
dotenv.config()
/**
* This example retrives all settings with key following pattern "app.settings.*", i.e. starting with "app.settings.".
* With the option `trimKeyPrefixes`, it trims the prefix "app.settings." from keys for simplicity.
* Value of config "app.settings.message" will be printed.
*
* Below environment variables are required for this example:
* - APPCONFIG_ENDPOINT
* - AZURE_TENANT_ID
* - AZURE_CLIENT_ID
* - AZURE_CLIENT_SECRET
*/
import { load } from "@azure/app-configuration-provider";
import { getDefaultAzureCredential } from "@azure/identity";
const endpoint = process.env.APPCONFIG_ENDPOINT;
const credential = getDefaultAzureCredential();
const settings = await load(endpoint, credential, {
selectors: [{
keyFilter: "app.settings.*"
}],
trimKeyPrefixes: ["app.settings."]
});
const message = settings.get("message");
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import * as dotenv from "dotenv";
dotenv.config()

/**
* This example retrives all settings with key following pattern "app.settings.*", i.e. starting with "app.settings.".
* With the option `trimKeyPrefixes`, it trims the prefix "app.settings." from keys for simplicity.
* Value of config "app.settings.message" will be printed.
*
* Below environment variables are required for this example:
* - APPCONFIG_ENDPOINT
* - AZURE_TENANT_ID
* - AZURE_CLIENT_ID
* - AZURE_CLIENT_SECRET
*/

import { load } from "@azure/app-configuration-provider";
import { getDefaultAzureCredential } from "@azure/identity";
const endpoint = process.env.APPCONFIG_ENDPOINT;
const credential = getDefaultAzureCredential();
const settings = await load(endpoint, credential, {
selectors: [{
keyFilter: "app.settings.*"
}],
trimKeyPrefixes: ["app.settings."]
});
const message = settings.get("message");

console.log(`Message from Azure App Configuration: ${message}`);
Loading
Loading