From 52687368cb6fa163f171f633e7ed5080e3162633 Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Sat, 1 May 2021 23:18:14 +0200 Subject: [PATCH] refactor: hostRules error --- lib/util/host-rules.spec.ts | 9 ++++++--- lib/util/host-rules.ts | 17 +++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/util/host-rules.spec.ts b/lib/util/host-rules.spec.ts index 92285980617040..fbd70ce22c6283 100644 --- a/lib/util/host-rules.spec.ts +++ b/lib/util/host-rules.spec.ts @@ -15,7 +15,7 @@ describe(getName(), () => { domainName: 'github.com', hostName: 'api.github.com', }) - ).toThrow('hostRules cannot contain both a domainName and hostName'); + ).toThrow(); }); it('throws if both domainName and baseUrl', () => { expect(() => @@ -24,7 +24,7 @@ describe(getName(), () => { domainName: 'github.com', baseUrl: 'https://api.github.com', }) - ).toThrow('hostRules cannot contain both a domainName and baseUrl'); + ).toThrow(); }); it('throws if both hostName and baseUrl', () => { expect(() => @@ -33,7 +33,7 @@ describe(getName(), () => { hostName: 'api.github.com', baseUrl: 'https://api.github.com', }) - ).toThrow('hostRules cannot contain both a hostName and baseUrl'); + ).toThrow(); }); it('supports baseUrl-only', () => { add({ @@ -45,6 +45,9 @@ describe(getName(), () => { }); }); describe('find()', () => { + beforeEach(() => { + clear(); + }); it('warns and returns empty for bad search', () => { expect(find({ abc: 'def' } as any)).toEqual({}); }); diff --git a/lib/util/host-rules.ts b/lib/util/host-rules.ts index d5bbfdf265229f..20e6e1a934b28e 100644 --- a/lib/util/host-rules.ts +++ b/lib/util/host-rules.ts @@ -7,15 +7,16 @@ import * as sanitize from './sanitize'; let hostRules: HostRule[] = []; +const matchFields = ['hostName', 'domainName', 'baseUrl']; + export function add(params: HostRule): void { - if (params.domainName && params.hostName) { - throw new Error('hostRules cannot contain both a domainName and hostName'); - } - if (params.domainName && params.baseUrl) { - throw new Error('hostRules cannot contain both a domainName and baseUrl'); - } - if (params.hostName && params.baseUrl) { - throw new Error('hostRules cannot contain both a hostName and baseUrl'); + const matchedFields = matchFields.filter((field) => params[field]); + if (matchedFields.length > 1) { + throw new Error( + `hostRules cannot contain more than one host-matching field. Found: [${matchedFields.join( + ', ' + )}]` + ); } const confidentialFields = ['password', 'token']; let resolvedHost = params.baseUrl || params.hostName || params.domainName;