Commit 68f3ef05 authored by Robert Knight's avatar Robert Knight

Rename property containing unused params in `replaceURLParams` return value

Rename property from `params` to `unusedParams` for clarity.
parent 6b0c776f
......@@ -122,7 +122,7 @@ function createAPICall(
headers['X-Client-Id'] = clientId;
}
const { url, params: queryParams } = replaceURLParams(
const { url, unusedParams: queryParams } = replaceURLParams(
descriptor.url,
params
);
......
......@@ -34,7 +34,7 @@ function updateLinks(newLinks) {
* Returns an empty string if links have not been fetched yet.
*
* @param {string} linkName
* @param {Record<string,string>} params
* @param {Record<string, string>} [params]
* @return {string}
*/
function getLink(state, linkName, params = {}) {
......@@ -45,11 +45,10 @@ function getLink(state, linkName, params = {}) {
if (!template) {
throw new Error(`Unknown link "${linkName}"`);
}
const { url, params: unusedParams } = replaceURLParams(template, params);
if (Object.keys(unusedParams).length > 0) {
throw new Error(
`Unused parameters: ${Object.keys(unusedParams).join(', ')}`
);
const { url, unusedParams } = replaceURLParams(template, params);
const unusedKeys = Object.keys(unusedParams);
if (unusedKeys.length > 0) {
throw new Error(`Unused parameters: ${unusedKeys.join(', ')}`);
}
return url;
}
......
import * as urlUtil from '../url';
import { replaceURLParams } from '../url';
describe('sidebar/util/url', function () {
describe('replaceURLParams()', function () {
it('should replace params in URLs', function () {
const replaced = urlUtil.replaceURLParams('http://foo.com/things/:id', {
const replaced = replaceURLParams('http://foo.com/things/:id', {
id: 'test',
});
assert.equal(replaced.url, 'http://foo.com/things/test');
});
it('should URL encode params in URLs', function () {
const replaced = urlUtil.replaceURLParams('http://foo.com/things/:id', {
const replaced = replaceURLParams('http://foo.com/things/:id', {
id: 'foo=bar',
});
assert.equal(replaced.url, 'http://foo.com/things/foo%3Dbar');
});
it('should return unused params', function () {
const replaced = urlUtil.replaceURLParams('http://foo.com/:id', {
const replaced = replaceURLParams('http://foo.com/:id', {
id: 'test',
q: 'unused',
});
assert.deepEqual(replaced.params, { q: 'unused' });
assert.deepEqual(replaced.unusedParams, { q: 'unused' });
});
});
});
......@@ -5,11 +5,11 @@
* parameters.
*
* replaceURLParams('/things/:id', {id: 'foo', q: 'bar'}) =>
* {url: '/things/foo', params: {q: 'bar'}}
* {url: '/things/foo', unusedParams: {q: 'bar'}}
*
* @param {string} url
* @param {Record<string, string>} params
* @return {{ url: string, params: Record<string, string>}}
* @return {{ url: string, unusedParams: Record<string, string>}}
*/
export function replaceURLParams(url, params) {
/** @type {Record<string, string>} */
......@@ -25,7 +25,7 @@ export function replaceURLParams(url, params) {
}
}
}
return { url: url, params: unusedParams };
return { url, unusedParams };
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment