Skip to content
Snippets Groups Projects
Commit 52687368 authored by Rhys Arkins's avatar Rhys Arkins
Browse files

refactor: hostRules error

parent 61c648d2
No related branches found
No related tags found
No related merge requests found
...@@ -15,7 +15,7 @@ describe(getName(), () => { ...@@ -15,7 +15,7 @@ describe(getName(), () => {
domainName: 'github.com', domainName: 'github.com',
hostName: 'api.github.com', hostName: 'api.github.com',
}) })
).toThrow('hostRules cannot contain both a domainName and hostName'); ).toThrow();
}); });
it('throws if both domainName and baseUrl', () => { it('throws if both domainName and baseUrl', () => {
expect(() => expect(() =>
...@@ -24,7 +24,7 @@ describe(getName(), () => { ...@@ -24,7 +24,7 @@ describe(getName(), () => {
domainName: 'github.com', domainName: 'github.com',
baseUrl: 'https://api.github.com', baseUrl: 'https://api.github.com',
}) })
).toThrow('hostRules cannot contain both a domainName and baseUrl'); ).toThrow();
}); });
it('throws if both hostName and baseUrl', () => { it('throws if both hostName and baseUrl', () => {
expect(() => expect(() =>
...@@ -33,7 +33,7 @@ describe(getName(), () => { ...@@ -33,7 +33,7 @@ describe(getName(), () => {
hostName: 'api.github.com', hostName: 'api.github.com',
baseUrl: 'https://api.github.com', baseUrl: 'https://api.github.com',
}) })
).toThrow('hostRules cannot contain both a hostName and baseUrl'); ).toThrow();
}); });
it('supports baseUrl-only', () => { it('supports baseUrl-only', () => {
add({ add({
...@@ -45,6 +45,9 @@ describe(getName(), () => { ...@@ -45,6 +45,9 @@ describe(getName(), () => {
}); });
}); });
describe('find()', () => { describe('find()', () => {
beforeEach(() => {
clear();
});
it('warns and returns empty for bad search', () => { it('warns and returns empty for bad search', () => {
expect(find({ abc: 'def' } as any)).toEqual({}); expect(find({ abc: 'def' } as any)).toEqual({});
}); });
......
...@@ -7,15 +7,16 @@ import * as sanitize from './sanitize'; ...@@ -7,15 +7,16 @@ import * as sanitize from './sanitize';
let hostRules: HostRule[] = []; let hostRules: HostRule[] = [];
const matchFields = ['hostName', 'domainName', 'baseUrl'];
export function add(params: HostRule): void { export function add(params: HostRule): void {
if (params.domainName && params.hostName) { const matchedFields = matchFields.filter((field) => params[field]);
throw new Error('hostRules cannot contain both a domainName and hostName'); if (matchedFields.length > 1) {
} throw new Error(
if (params.domainName && params.baseUrl) { `hostRules cannot contain more than one host-matching field. Found: [${matchedFields.join(
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 confidentialFields = ['password', 'token']; const confidentialFields = ['password', 'token'];
let resolvedHost = params.baseUrl || params.hostName || params.domainName; let resolvedHost = params.baseUrl || params.hostName || params.domainName;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment