config-loader: rename readEnv to readEnvConfig and export from package

This commit is contained in:
Patrik Oldsberg
2020-08-16 09:10:09 +02:00
parent 9ae483312e
commit c13de0b115
5 changed files with 26 additions and 25 deletions
+1
View File
@@ -14,5 +14,6 @@
* limitations under the License.
*/
export { readEnvConfig } from './lib';
export { loadConfig } from './loader';
export type { LoadConfigOptions } from './loader';
+21 -21
View File
@@ -14,16 +14,16 @@
* limitations under the License.
*/
import { readEnv } from './env';
import { readEnvConfig } from './env';
describe('readEnv', () => {
describe('readEnvConfig', () => {
it('should return empty config for empty env', () => {
expect(readEnv({})).toEqual([]);
expect(readEnvConfig({})).toEqual([]);
});
it('should return empty config for no matching keys', () => {
expect(
readEnv({
readEnvConfig({
NODE_ENV: 'production',
NOPE_ENV: 'development',
APP_CONFIG: 'foo',
@@ -34,7 +34,7 @@ describe('readEnv', () => {
it('should create config from env', () => {
expect(
readEnv({
readEnvConfig({
NODE_ENV: 'production',
APP_CONFIG_foo: '"bar"',
APP_CONFIG_numbers_a: '1',
@@ -57,22 +57,22 @@ describe('readEnv', () => {
});
it('should accept string values', () => {
expect(readEnv({ APP_CONFIG_foo: '"abc"', APP_CONFIG_bar: 'xyz' })).toEqual(
[
{
data: {
foo: 'abc',
bar: 'xyz',
},
context: 'env',
expect(
readEnvConfig({ APP_CONFIG_foo: '"abc"', APP_CONFIG_bar: 'xyz' }),
).toEqual([
{
data: {
foo: 'abc',
bar: 'xyz',
},
],
);
context: 'env',
},
]);
});
it('should accept complex objects', () => {
expect(
readEnv({
readEnvConfig({
APP_CONFIG_foo: '{ "a": 123, "b": "123", "c": [] }',
APP_CONFIG_bar: '[123, "abc", {}]',
}),
@@ -95,7 +95,7 @@ describe('readEnv', () => {
['APP_CONFIG_fo o'],
['APP_CONFIG_foo_(foo)_foo'],
])('should reject invalid key %p', key => {
expect(() => readEnv({ [key]: '0' })).toThrow(
expect(() => readEnvConfig({ [key]: '0' })).toThrow(
`Invalid env config key '${key.replace('APP_CONFIG_', '')}'`,
);
});
@@ -103,7 +103,7 @@ describe('readEnv', () => {
it.each([['hello'], ['"hello'], ['{'], ['}']])(
'should fallback to string when invalid json value %p',
value => {
expect(readEnv({ APP_CONFIG_foo: value })).toEqual([
expect(readEnvConfig({ APP_CONFIG_foo: value })).toEqual([
{
data: {
foo: value,
@@ -116,7 +116,7 @@ describe('readEnv', () => {
it('should not allow null as a value', () => {
expect(() =>
readEnv({
readEnvConfig({
APP_CONFIG_foo: 'null',
}),
).toThrow(
@@ -126,7 +126,7 @@ describe('readEnv', () => {
it('should not allow duplicate values', () => {
expect(() =>
readEnv({
readEnvConfig({
APP_CONFIG_foo_bar: '1',
APP_CONFIG_foo_bar_baz: '2',
}),
@@ -137,7 +137,7 @@ describe('readEnv', () => {
it('should not allow mixing of objects and other values', () => {
expect(() =>
readEnv({
readEnvConfig({
APP_CONFIG_nested_foo: '1',
APP_CONFIG_nested: '2',
}),
+1 -1
View File
@@ -39,7 +39,7 @@ const CONFIG_KEY_PART_PATTERN = /^[a-z][a-z0-9]*(?:[-_][a-z][a-z0-9]*)*$/i;
*
* APP_CONFIG_app_title='"My Title"'
*/
export function readEnv(env: {
export function readEnvConfig(env: {
[name: string]: string | undefined;
}): AppConfig[] {
let data: JsonObject | undefined = undefined;
+1 -1
View File
@@ -16,5 +16,5 @@
export { resolveStaticConfig } from './resolver';
export { readConfigFile } from './reader';
export { readEnv } from './env';
export { readEnvConfig } from './env';
export { readSecret } from './secrets';
+2 -2
View File
@@ -20,7 +20,7 @@ import { AppConfig, JsonObject } from '@backstage/config';
import {
resolveStaticConfig,
readConfigFile,
readEnv,
readEnvConfig,
readSecret,
} from './lib';
@@ -102,7 +102,7 @@ export async function loadConfig(
);
}
configs.push(...readEnv(process.env));
configs.push(...readEnvConfig(process.env));
return configs;
}