Skip to content

Commit

Permalink
feat: support for labels
Browse files Browse the repository at this point in the history
- Adding support for labels
- Mixing in process.env for Azure loadFromAzure
  • Loading branch information
Kyle Hayes committed Nov 24, 2020
1 parent b47d650 commit 0ca5e6c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
9 changes: 5 additions & 4 deletions src/dotenv-azure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ export default class DotenvAzure {
async config(options: DotenvAzureConfigOptions = {}): Promise<DotenvAzureConfigOutput> {
const { safe = false } = options
const dotenvResult = dotenv.config(options)
const vars:Record<string, string | undefined> = {...(dotenvResult.parsed || {}), ...process.env}
const vars: Record<string, string | undefined> = {
...(dotenvResult.parsed || {}),
...process.env,
}
const azureVars = await this.loadFromAzure(vars)
const joinedVars = { ...azureVars, ...dotenvResult.parsed }

Expand Down Expand Up @@ -97,7 +100,6 @@ export default class DotenvAzure {
* @returns an object with keys and values
*/
async loadFromAzure(dotenvVars?: Record<string, string | undefined>): Promise<VariablesObject> {
// const vars = {...dotenvVars, ...process.env}
const credentials = this.getAzureCredentials(dotenvVars)
const appConfigClient = new AppConfigurationClient(credentials.connectionString)
const labels = dotenvVars?.AZURE_APP_CONFIG_LABELS || ''
Expand Down Expand Up @@ -202,8 +204,7 @@ export default class DotenvAzure {
)
}

private getAzureCredentials(dotenvVars: Record<string, string | undefined> = {}): AzureCredentials {
const vars = { ...dotenvVars, ...process.env }
private getAzureCredentials(vars: Record<string, string | undefined> = {}): AzureCredentials {
const connectionString = this.connectionString || vars.AZURE_APP_CONFIG_CONNECTION_STRING

if (!connectionString) {
Expand Down
1 change: 1 addition & 0 deletions test/azure.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const appConfigListMock = jest.fn(() =>
isReadOnly: false,
key: 'APP_CONFIG_VAR',
value: 'ok',
label: 'the-label',
},
{
isReadOnly: true,
Expand Down
7 changes: 7 additions & 0 deletions test/dotenv-azure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
Expand Down

0 comments on commit 0ca5e6c

Please sign in to comment.