From 0ca5e6c16ab91ee008ca8497e4df137276645887 Mon Sep 17 00:00:00 2001 From: Kyle Hayes Date: Wed, 4 Nov 2020 07:18:57 -0800 Subject: [PATCH] feat: support for labels - Adding support for labels - Mixing in process.env for Azure loadFromAzure --- package-lock.json | 2 +- package.json | 2 +- src/dotenv-azure.ts | 9 +++++---- test/azure.mock.ts | 1 + test/dotenv-azure.test.ts | 7 +++++++ 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3f0fb96f..e1ed44cb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "dotenv-azure", - "version": "2.0.5", + "version": "2.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index bf97fcaf..394ad0c8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dotenv-azure", - "version": "2.0.5", + "version": "2.0.0", "description": "Load environment variables from Azure's services App Configuration, Key Vault or a .env file", "keywords": [ "azure", diff --git a/src/dotenv-azure.ts b/src/dotenv-azure.ts index 7e5aae66..4562ae22 100644 --- a/src/dotenv-azure.ts +++ b/src/dotenv-azure.ts @@ -60,7 +60,10 @@ export default class DotenvAzure { async config(options: DotenvAzureConfigOptions = {}): Promise { const { safe = false } = options const dotenvResult = dotenv.config(options) - const vars:Record = {...(dotenvResult.parsed || {}), ...process.env} + const vars: Record = { + ...(dotenvResult.parsed || {}), + ...process.env, + } const azureVars = await this.loadFromAzure(vars) const joinedVars = { ...azureVars, ...dotenvResult.parsed } @@ -97,7 +100,6 @@ export default class DotenvAzure { * @returns an object with keys and values */ async loadFromAzure(dotenvVars?: Record): Promise { - // const vars = {...dotenvVars, ...process.env} const credentials = this.getAzureCredentials(dotenvVars) const appConfigClient = new AppConfigurationClient(credentials.connectionString) const labels = dotenvVars?.AZURE_APP_CONFIG_LABELS || '' @@ -202,8 +204,7 @@ export default class DotenvAzure { ) } - private getAzureCredentials(dotenvVars: Record = {}): AzureCredentials { - const vars = { ...dotenvVars, ...process.env } + private getAzureCredentials(vars: Record = {}): AzureCredentials { const connectionString = this.connectionString || vars.AZURE_APP_CONFIG_CONNECTION_STRING if (!connectionString) { diff --git a/test/azure.mock.ts b/test/azure.mock.ts index eade5d46..16a8ed6f 100644 --- a/test/azure.mock.ts +++ b/test/azure.mock.ts @@ -18,6 +18,7 @@ export const appConfigListMock = jest.fn(() => isReadOnly: false, key: 'APP_CONFIG_VAR', value: 'ok', + label: 'the-label', }, { isReadOnly: true, diff --git a/test/dotenv-azure.test.ts b/test/dotenv-azure.test.ts index d20ad473..25c61001 100644 --- a/test/dotenv-azure.test.ts +++ b/test/dotenv-azure.test.ts @@ -19,6 +19,7 @@ const mockReadFileSync = readFileSync as jest.Mock describe('DotenvAzure', () => { const OLD_ENV = process.env const AZURE_APP_CONFIG_CONNECTION_STRING = 'app-config-conneciton-string' + const AZURE_APP_CONFIG_LABELS = 'app-config-labels' const AZURE_TENANT_ID = 'tenant-id' const AZURE_CLIENT_ID = 'client-id' const AZURE_CLIENT_SECRET = 'client-secret' @@ -44,6 +45,12 @@ describe('DotenvAzure', () => { expect(await dotenvAzure.config()).toBeDefined() }) + it('does not throw when AZURE_APP_CONFIG_LABELS is defined', async () => { + process.env = { ...OLD_ENV, AZURE_APP_CONFIG_CONNECTION_STRING, AZURE_APP_CONFIG_LABELS } + const dotenvAzure = new DotenvAzure() + expect(await dotenvAzure.config()).toBeDefined() + }) + it('throws when AZURE_APP_CONFIG_URL and AZURE_APP_CONFIG_CONNECTION_STRING are not defined', () => { const dotenvAzure = new DotenvAzure() expect(dotenvAzure.config()).rejects.toThrowError(MissingAppConfigCredentialsError)