Skip to content

Commit

Permalink
fix: change constructServiceURL from method to function (#141)
Browse files Browse the repository at this point in the history
* 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.

* fix: Change `constructServiceURL` to `constructServiceUrl`

This is consistent with the existing `setServiceUrl` method.
  • Loading branch information
ajhynes7 committed Jun 7, 2021
1 parent 5b6f7cd commit cd2d28c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 54 deletions.
44 changes: 0 additions & 44 deletions lib/base-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>} 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<string, string>} 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<string, string>,
providedUrlVariables: Map<string, string> | 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<string, string>();
}

// 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;
Expand Down
44 changes: 44 additions & 0 deletions lib/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>} 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<string, string>} 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<string, string>,
providedUrlVariables: Map<string, string> | 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<string, string>();
}

// 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;
}
20 changes: 10 additions & 10 deletions test/unit/parameterized-url.test.js
Original file line number Diff line number Diff line change
@@ -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([
Expand All @@ -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(BaseService.constructServiceURL(parameterizedUrl, defaultUrlVariables, null)).toBe(
expect(constructServiceUrl(parameterizedUrl, defaultUrlVariables, null)).toBe(
'http://ibm.com:9300'
);
});
Expand All @@ -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', () => {
Expand All @@ -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\]\./
);
Expand Down

0 comments on commit cd2d28c

Please sign in to comment.