From a915422473fc565a05fb7b2d80e454f23724610e Mon Sep 17 00:00:00 2001 From: Andrew Hynes Date: Fri, 4 Jun 2021 14:08:34 -0400 Subject: [PATCH 1/2] fix: change constructServiceURL from method to function Now it can be used as a utility function instead of an inherited method. An SDK class can now include a `constructServiceURL` method with a different signature than this function. --- lib/base-service.ts | 44 ----------------------------- lib/helper.ts | 44 +++++++++++++++++++++++++++++ test/unit/parameterized-url.test.js | 18 ++++++------ 3 files changed, 53 insertions(+), 53 deletions(-) diff --git a/lib/base-service.ts b/lib/base-service.ts index c2d2090c4..c23afb549 100644 --- a/lib/base-service.ts +++ b/lib/base-service.ts @@ -61,50 +61,6 @@ export class BaseService { static DEFAULT_SERVICE_NAME: string; - /** - * Constructs a service URL by formatting a parameterized URL. - * - * @param {string} parameterizedUrl URL that contains variable placeholders, e.g. '{scheme}://ibm.com'. - * @param {Map} defaultUrlVariables Map from variable names to default values. - * Each variable in the parameterized URL must have a default value specified in this map. - * @param {Map} providedUrlVariables Map from variable names to desired values. - * If a variable is not provided in this map, - * the default variable value will be used instead. - * @returns {string} The formatted URL with all variable placeholders replaced by values. - */ - static constructServiceURL( - parameterizedUrl: string, - defaultUrlVariables: Map, - providedUrlVariables: Map | null - ): string { - // If null was passed, we set the variables to an empty map. - // This results in all default variable values being used. - if (providedUrlVariables === null) { - providedUrlVariables = new Map(); - } - - // Verify the provided variable names. - providedUrlVariables.forEach((_, name) => { - if (!defaultUrlVariables.has(name)) { - throw new Error(`'${name}' is an invalid variable name. - Valid variable names: [${Array.from(defaultUrlVariables.keys()).sort()}].`); - } - }); - - // Format the URL with provided or default variable values. - let formattedUrl = parameterizedUrl; - - defaultUrlVariables.forEach((defaultValue, name) => { - // Use the default variable value if none was provided. - const providedValue = providedUrlVariables.get(name); - const formatValue = providedValue !== undefined ? providedValue : defaultValue; - - formattedUrl = formattedUrl.replace(`{${name}}`, formatValue); - }); - - return formattedUrl; - } - protected baseOptions: BaseServiceOptions; private authenticator: AuthenticatorInterface; diff --git a/lib/helper.ts b/lib/helper.ts index 480e4e963..4d7ed74fc 100644 --- a/lib/helper.ts +++ b/lib/helper.ts @@ -241,3 +241,47 @@ export function toLowerKeys(obj: Object): Object { } return lowerCaseObj; } + +/** + * Constructs a service URL by formatting a parameterized URL. + * + * @param {string} parameterizedUrl URL that contains variable placeholders, e.g. '{scheme}://ibm.com'. + * @param {Map} defaultUrlVariables Map from variable names to default values. + * Each variable in the parameterized URL must have a default value specified in this map. + * @param {Map} providedUrlVariables Map from variable names to desired values. + * If a variable is not provided in this map, + * the default variable value will be used instead. + * @returns {string} The formatted URL with all variable placeholders replaced by values. + */ +export function constructServiceURL( + parameterizedUrl: string, + defaultUrlVariables: Map, + providedUrlVariables: Map | null +): string { + // If null was passed, we set the variables to an empty map. + // This results in all default variable values being used. + if (providedUrlVariables === null) { + providedUrlVariables = new Map(); + } + + // Verify the provided variable names. + providedUrlVariables.forEach((_, name) => { + if (!defaultUrlVariables.has(name)) { + throw new Error(`'${name}' is an invalid variable name. + Valid variable names: [${Array.from(defaultUrlVariables.keys()).sort()}].`); + } + }); + + // Format the URL with provided or default variable values. + let formattedUrl = parameterizedUrl; + + defaultUrlVariables.forEach((defaultValue, name) => { + // Use the default variable value if none was provided. + const providedValue = providedUrlVariables.get(name); + const formatValue = providedValue !== undefined ? providedValue : defaultValue; + + formattedUrl = formattedUrl.replace(`{${name}}`, formatValue); + }); + + return formattedUrl; +} diff --git a/test/unit/parameterized-url.test.js b/test/unit/parameterized-url.test.js index 3eb6015ff..f96bd10b5 100644 --- a/test/unit/parameterized-url.test.js +++ b/test/unit/parameterized-url.test.js @@ -1,4 +1,4 @@ -const { BaseService } = require('../../dist/lib/base-service'); +const { constructServiceURL } = require('../../dist/lib/helper'); const parameterizedUrl = '{scheme}://{domain}:{port}'; const defaultUrlVariables = new Map([ @@ -9,7 +9,7 @@ const defaultUrlVariables = new Map([ describe('constructServiceURL', () => { it('should use default variable values when null is passed', () => { - expect(BaseService.constructServiceURL(parameterizedUrl, defaultUrlVariables, null)).toBe( + expect(constructServiceURL(parameterizedUrl, defaultUrlVariables, null)).toBe( 'http://ibm.com:9300' ); }); @@ -20,9 +20,9 @@ describe('constructServiceURL', () => { ['port', '22'], ]); - expect( - BaseService.constructServiceURL(parameterizedUrl, defaultUrlVariables, providedUrlVariables) - ).toBe('https://ibm.com:22'); + expect(constructServiceURL(parameterizedUrl, defaultUrlVariables, providedUrlVariables)).toBe( + 'https://ibm.com:22' + ); }); it('should use all provided values', () => { @@ -32,16 +32,16 @@ describe('constructServiceURL', () => { ['port', '22'], ]); - expect( - BaseService.constructServiceURL(parameterizedUrl, defaultUrlVariables, providedUrlVariables) - ).toBe('https://google.com:22'); + expect(constructServiceURL(parameterizedUrl, defaultUrlVariables, providedUrlVariables)).toBe( + 'https://google.com:22' + ); }); it('should throw an error if a provided variable name is wrong', () => { const providedUrlVariables = new Map([['server', 'value']]); expect(() => - BaseService.constructServiceURL(parameterizedUrl, defaultUrlVariables, providedUrlVariables) + constructServiceURL(parameterizedUrl, defaultUrlVariables, providedUrlVariables) ).toThrow( /'server' is an invalid variable name\.\n\s*Valid variable names: \[domain,port,scheme\]\./ ); From 6fc7c14e7f1694aeeb0f688048fbf2a158149634 Mon Sep 17 00:00:00 2001 From: Andrew Hynes Date: Mon, 7 Jun 2021 12:12:14 -0400 Subject: [PATCH 2/2] fix: Change `constructServiceURL` to `constructServiceUrl` This is consistent with the existing `setServiceUrl` method. --- lib/helper.ts | 2 +- test/unit/parameterized-url.test.js | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/helper.ts b/lib/helper.ts index 4d7ed74fc..4520fcd46 100644 --- a/lib/helper.ts +++ b/lib/helper.ts @@ -253,7 +253,7 @@ export function toLowerKeys(obj: Object): Object { * the default variable value will be used instead. * @returns {string} The formatted URL with all variable placeholders replaced by values. */ -export function constructServiceURL( +export function constructServiceUrl( parameterizedUrl: string, defaultUrlVariables: Map, providedUrlVariables: Map | null diff --git a/test/unit/parameterized-url.test.js b/test/unit/parameterized-url.test.js index f96bd10b5..9d34f7830 100644 --- a/test/unit/parameterized-url.test.js +++ b/test/unit/parameterized-url.test.js @@ -1,4 +1,4 @@ -const { constructServiceURL } = require('../../dist/lib/helper'); +const { constructServiceUrl } = require('../../dist/lib/helper'); const parameterizedUrl = '{scheme}://{domain}:{port}'; const defaultUrlVariables = new Map([ @@ -7,9 +7,9 @@ const defaultUrlVariables = new Map([ ['port', '9300'], ]); -describe('constructServiceURL', () => { +describe('constructServiceUrl', () => { it('should use default variable values when null is passed', () => { - expect(constructServiceURL(parameterizedUrl, defaultUrlVariables, null)).toBe( + expect(constructServiceUrl(parameterizedUrl, defaultUrlVariables, null)).toBe( 'http://ibm.com:9300' ); }); @@ -20,7 +20,7 @@ describe('constructServiceURL', () => { ['port', '22'], ]); - expect(constructServiceURL(parameterizedUrl, defaultUrlVariables, providedUrlVariables)).toBe( + expect(constructServiceUrl(parameterizedUrl, defaultUrlVariables, providedUrlVariables)).toBe( 'https://ibm.com:22' ); }); @@ -32,7 +32,7 @@ describe('constructServiceURL', () => { ['port', '22'], ]); - expect(constructServiceURL(parameterizedUrl, defaultUrlVariables, providedUrlVariables)).toBe( + expect(constructServiceUrl(parameterizedUrl, defaultUrlVariables, providedUrlVariables)).toBe( 'https://google.com:22' ); }); @@ -41,7 +41,7 @@ describe('constructServiceURL', () => { const providedUrlVariables = new Map([['server', 'value']]); expect(() => - constructServiceURL(parameterizedUrl, defaultUrlVariables, providedUrlVariables) + constructServiceUrl(parameterizedUrl, defaultUrlVariables, providedUrlVariables) ).toThrow( /'server' is an invalid variable name\.\n\s*Valid variable names: \[domain,port,scheme\]\./ );