From 49198863bcd92191be7ba0efe52ff7bf5797d622 Mon Sep 17 00:00:00 2001 From: Yan Zhang Date: Wed, 20 Mar 2024 15:23:53 +0800 Subject: [PATCH 1/2] Add example for configuration object usage --- examples/configObject.mjs | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 examples/configObject.mjs diff --git a/examples/configObject.mjs b/examples/configObject.mjs new file mode 100644 index 00000000..d8dad437 --- /dev/null +++ b/examples/configObject.mjs @@ -0,0 +1,45 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +import * as dotenv from "dotenv"; +dotenv.config() + +/** + * This example demostrates 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 specified separator (default is "."). + * E.g. if you import the following .json file: + * { + * "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}`); From dc4578dba9102a03925cbafca710fbaaa3d008d8 Mon Sep 17 00:00:00 2001 From: Yan Zhang Date: Thu, 21 Mar 2024 09:51:17 +0800 Subject: [PATCH 2/2] fix description about separator for import --- examples/configObject.mjs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/configObject.mjs b/examples/configObject.mjs index d8dad437..93de722a 100644 --- a/examples/configObject.mjs +++ b/examples/configObject.mjs @@ -5,11 +5,11 @@ import * as dotenv from "dotenv"; dotenv.config() /** - * This example demostrates how to construct a configuration object from settings loaded from Azure App Configuration. + * 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 specified separator (default is "."). - * E.g. if you import the following .json file: + * 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": {