From f8780ff32509d0326bc513791ea60846d7614b34 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sat, 8 Aug 2020 12:05:13 +0200 Subject: [PATCH] config: allow config.get to be called without args --- packages/config/src/reader.test.ts | 34 ++++++++++++++++++++++++++++++ packages/config/src/reader.ts | 10 ++++----- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/packages/config/src/reader.test.ts b/packages/config/src/reader.test.ts index d93dce65d7..41cdc92938 100644 --- a/packages/config/src/reader.test.ts +++ b/packages/config/src/reader.test.ts @@ -137,6 +137,8 @@ describe('ConfigReader', () => { it('should read empty config with valid keys', () => { const config = new ConfigReader({}, CTX); expect(config.keys()).toEqual([]); + expect(config.getOptional()).toEqual({}); + expect(config.getOptional('x')).toBeUndefined(); expect(config.getOptionalString('x')).toBeUndefined(); expect(config.getOptionalString('x_x')).toBeUndefined(); expect(config.getOptionalString('x-X')).toBeUndefined(); @@ -442,6 +444,21 @@ describe('ConfigReader.get()', () => { }); it('should merge in fallback configs', () => { + expect( + ConfigReader.fromConfigs([configs[0], configs[1], configs[2]]).get(), + ).toEqual({ + a: { + x: 'x1', + y: ['y11', 'y12', 'y13'], + z: false, + }, + b: { + x: 'x2', + y: ['y21', 'y22'], + z: 'z2', + }, + c: { c1: 'c1' }, + }); expect(ConfigReader.fromConfigs([configs[1], configs[0]]).get('a')).toEqual( { x: 'x1', @@ -545,5 +562,22 @@ describe('ConfigReader.get()', () => { expect(config.get('f')).toEqual('foo'); expect(config.get('g')).toEqual({ z: 'z' }); expect(config.get('h')).toEqual({ a: 'a1', b: 'b2', c: 'c1' }); + expect(config.getConfig('h').get()).toEqual({ a: 'a1', b: 'b2', c: 'c1' }); + expect(config.getOptional()).toEqual({ + a: ['1', '2'], + b: ['1'], + c: [], + d: { + x: 'x', + }, + e: ['3'], + f: 'foo', + g: { z: 'z' }, + h: { + a: 'a1', + b: 'b2', + c: 'c1', + }, + }); }); }); diff --git a/packages/config/src/reader.ts b/packages/config/src/reader.ts index a0ecfd5ef0..65143bbd95 100644 --- a/packages/config/src/reader.ts +++ b/packages/config/src/reader.ts @@ -85,15 +85,15 @@ export class ConfigReader implements Config { return [...new Set([...localKeys, ...fallbackKeys])]; } - get(key: string): JsonValue { + get(key?: string): JsonValue { const value = this.getOptional(key); if (value === undefined) { - throw new Error(errors.missing(this.fullKey(key))); + throw new Error(errors.missing(this.fullKey(key ?? ''))); } return value; } - getOptional(key: string): JsonValue | undefined { + getOptional(key?: string): JsonValue | undefined { const value = this.readValue(key); const fallbackValue = this.fallback?.getOptional(key); @@ -279,8 +279,8 @@ export class ConfigReader implements Config { return value as T; } - private readValue(key: string): JsonValue | undefined { - const parts = key.split('.'); + private readValue(key?: string): JsonValue | undefined { + const parts = key ? key.split('.') : []; for (const part of parts) { if (!CONFIG_KEY_PART_PATTERN.test(part)) { throw new TypeError(`Invalid config key '${key}'`);