Skip to content

Commit

Permalink
Fix the (baseTemplate, params) overload
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsbotond committed Sep 24, 2020
1 parent 47d0af8 commit 310fad8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,19 @@ export default function urlcat(baseTemplate: string, params: ParamMap): string;
export default function urlcat(baseUrl: string, pathTemplate: string): string;
export default function urlcat(baseUrl: string, pathTemplate: string, params: ParamMap): string;
export default function urlcat(baseUrlOrTemplate: string, pathTemplateOrParams: string | ParamMap, maybeParams: ParamMap = {}): string {
let baseUrl: string;
let pathTemplate: string;
let params: ParamMap;

if (typeof pathTemplateOrParams === 'string') {
baseUrl = baseUrlOrTemplate;
pathTemplate = pathTemplateOrParams;
params = maybeParams;
const baseUrl = baseUrlOrTemplate;
const pathTemplate = pathTemplateOrParams;
const params = maybeParams;
return urlcatImpl(pathTemplate, params, baseUrl);
} else {
baseUrl = '';
pathTemplate = baseUrlOrTemplate;
params = pathTemplateOrParams;
const baseTemplate = baseUrlOrTemplate;
const params = pathTemplateOrParams;
return urlcatImpl(baseTemplate, params);
}

return urlcatImpl(baseUrl, pathTemplate, params);
}

function urlcatImpl(baseUrl: string, pathTemplate: string, params: ParamMap) {
function urlcatImpl(pathTemplate: string, params: ParamMap, baseUrl?: string) {
const cleanParams = removeNullOrUndef(params);
const { renderedPath, remainingParams } = path(pathTemplate, cleanParams);
const url = new URL(renderedPath, baseUrl);
Expand Down
12 changes: 12 additions & 0 deletions test/urlcat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,16 @@ describe('urlcat', () => {
expect(actual).to.equal(expected);
});

it ('Provides an overload (baseUrl, pathTemplate) that works correctly', () => {
const expected = 'http://example.com/path';
const actual = urlcat('http://example.com/', '/path');
expect(actual).to.equal(expected);
});

it ('Provides an overload (baseTemplate, params) that works correctly', () => {
const expected = 'http://example.com/path/a%20b?q=b+c';
const actual = urlcat('http://example.com/path/:p', { p: 'a b', q: 'b c' });
expect(actual).to.equal(expected);
});

});

0 comments on commit 310fad8

Please sign in to comment.