From 0b79b7e5052a30275db7822367d0747184284828 Mon Sep 17 00:00:00 2001 From: krutoo Date: Thu, 29 Feb 2024 15:25:43 +0500 Subject: [PATCH] #38 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - preset/isomorphic: классу HttpApiHostPool добавлен метод getAll (minor) --- .../isomorphic/utils/__test__/index.test.ts | 45 +++++++++++++++++++ src/preset/isomorphic/utils/index.ts | 9 ++++ 2 files changed, 54 insertions(+) diff --git a/src/preset/isomorphic/utils/__test__/index.test.ts b/src/preset/isomorphic/utils/__test__/index.test.ts index 7a55987..3b9da5a 100644 --- a/src/preset/isomorphic/utils/__test__/index.test.ts +++ b/src/preset/isomorphic/utils/__test__/index.test.ts @@ -131,6 +131,51 @@ describe('HttpApiHostPool', () => { expect(pool.get('foobar', { absolute: true })).toEqual('http://www.foobar.com'); }); + + it('getAll() should return all hosts', () => { + const source = new Env({ + API_HOST_FOO: 'http://www.foo.com', + API_HOST_BAR: 'http://www.bar.com', + API_HOST_BAZ: 'http://www.baz.com', + }); + + const pool = new HttpApiHostPool( + { + foo: 'API_HOST_FOO', + bar: 'API_HOST_BAR', + baz: 'API_HOST_BAZ', + }, + source, + ); + + expect(pool.getAll()).toEqual({ + foo: 'http://www.foo.com', + bar: 'http://www.bar.com', + baz: 'http://www.baz.com', + }); + }); + + it('getAll(keys) should return hosts for keys', () => { + const source = new Env({ + API_HOST_FOO: 'http://www.foo.com', + API_HOST_BAR: 'http://www.bar.com', + API_HOST_BAZ: 'http://www.baz.com', + }); + + const pool = new HttpApiHostPool( + { + foo: 'API_HOST_FOO', + bar: 'API_HOST_BAR', + baz: 'API_HOST_BAZ', + }, + source, + ); + + expect(pool.getAll(['foo', 'baz'])).toEqual({ + foo: 'http://www.foo.com', + baz: 'http://www.baz.com', + }); + }); }); describe('severityFromStatus', () => { diff --git a/src/preset/isomorphic/utils/index.ts b/src/preset/isomorphic/utils/index.ts index 01bbcca..4bfda9d 100644 --- a/src/preset/isomorphic/utils/index.ts +++ b/src/preset/isomorphic/utils/index.ts @@ -50,6 +50,15 @@ export class HttpApiHostPool implements StrictMap { return value; } + + /** + * Возвращает объект в котором ключи - переданные имена хостов а значения - хосты. + * @param keys Названия хостов. + * @return Объект. + */ + getAll(keys: Key[] = Object.keys(this.map) as Key[]): Record { + return Object.fromEntries(keys.map(key => [key, this.get(key)])) as Record; + } } /**