Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
refactor: hostRules error
  • Loading branch information
rarkins committed May 1, 2021
1 parent 61c648d commit 5268736
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
9 changes: 6 additions & 3 deletions lib/util/host-rules.spec.ts
Expand Up @@ -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(() =>
Expand All @@ -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(() =>
Expand All @@ -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({
Expand All @@ -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({});
});
Expand Down
17 changes: 9 additions & 8 deletions lib/util/host-rules.ts
Expand Up @@ -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;
Expand Down

0 comments on commit 5268736

Please sign in to comment.