Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change constructServiceURL from method to function #141

Merged
merged 2 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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