Skip to content

Commit

Permalink
feat: adding configureService method for external config options (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorge-ibm committed Oct 22, 2019
1 parent 5f963eb commit 7324919
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
29 changes: 26 additions & 3 deletions lib/base-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ export class BaseService {
this.baseOptions = extend(
{ qs: {}, serviceUrl: serviceClass.URL },
options,
this.readOptionsFromExternalConfig(),
_options
);

Expand All @@ -112,6 +111,9 @@ export class BaseService {
}

this.authenticator = options.authenticator;

// temp: call the configureService method to ensure compatibility
this.configureService(this.name);
}

/**
Expand All @@ -132,6 +134,27 @@ export class BaseService {
this.baseOptions.serviceUrl = url;
}

/**
* Configure the service using external configuration
*
* @param {string} the name of the service. Will be used to read from external
* configuration
*/
protected configureService(serviceName: string): void {
if (!serviceName) {
const err = 'Error configuring service. Service name is required.';
logger.error(err);
throw new Error(err);
}

extend(
this.baseOptions,
this.readOptionsFromExternalConfig(serviceName)
);
// overwrite the requestWrapperInstance with the new base options if applicable
this.requestWrapperInstance = new RequestWrapper(this.baseOptions);
}

/**
* Wrapper around `sendRequest` that enforces the request will be authenticated.
*
Expand Down Expand Up @@ -166,9 +189,9 @@ export class BaseService {
});
}

private readOptionsFromExternalConfig() {
private readOptionsFromExternalConfig(serviceName: string) {
const results = {} as any;
const properties = readExternalSources(this.name);
const properties = readExternalSources(serviceName);

if (properties !== null) {
// the user can define two client-level variables in the credentials file: url and disableSsl
Expand Down
50 changes: 49 additions & 1 deletion test/unit/base-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ describe('Base Service', () => {
authenticator: AUTHENTICATOR,
});

const fromCredsFile = testService.readOptionsFromExternalConfig();
const fromCredsFile = testService.readOptionsFromExternalConfig(DEFAULT_NAME);

expect(fromCredsFile.serviceUrl).toBe(serviceUrl);
expect(fromCredsFile.disableSslVerification).toBe(disableSsl);
Expand Down Expand Up @@ -338,6 +338,54 @@ describe('Base Service', () => {
});
}).toThrow(/Revise these credentials/);
});

it('should have the default baseOptions values when instantiating', () => {
const testService = new TestService({
authenticator: AUTHENTICATOR,
});
expect(testService.baseOptions.serviceUrl).toEqual(DEFAULT_URL);
expect(testService.baseOptions.disableSslVerification).toEqual(false);
expect(testService.baseOptions.qs).toBeDefined();
expect(testService.baseOptions.qs).toEqual(EMPTY_OBJECT);
});

it('should configure service by calling configureService method after instantiating', () => {
const testService = new TestService({
authenticator: AUTHENTICATOR,
});

expect(testService.baseOptions.serviceUrl).toEqual(
'https://gateway.watsonplatform.net/test/api'
);
expect(testService.baseOptions.disableSslVerification).toEqual(false);

readExternalSourcesMock.mockImplementation(() => ({
url: 'abc123.com',
disableSsl: true,
}));

testService.configureService(DEFAULT_NAME);

expect(readExternalSourcesMock).toHaveBeenCalled();
expect(testService.baseOptions.serviceUrl).toEqual('abc123.com');
expect(testService.baseOptions.disableSslVerification).toEqual(true);
});

it('configureService method should throw error if service name is not provided', () => {
const testService = new TestService({
authenticator: AUTHENTICATOR,
});
const fakeError = new Error('Error configuring service. Service name is required.');
let err;

try {
testService.configureService();
} catch (e) {
err = e;
}

expect(err).toStrictEqual(fakeError);
});
});

function TestService(options) {
Expand Down

0 comments on commit 7324919

Please sign in to comment.