From 7324919a557d1753b61c903ac92f8b94faf3aff0 Mon Sep 17 00:00:00 2001 From: Jorge Rangel Date: Tue, 22 Oct 2019 17:15:17 -0500 Subject: [PATCH] feat: adding configureService method for external config options (#66) --- lib/base-service.ts | 29 ++++++++++++++++++-- test/unit/base-service.test.js | 50 +++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/lib/base-service.ts b/lib/base-service.ts index cbb0f4c12..67873d95c 100644 --- a/lib/base-service.ts +++ b/lib/base-service.ts @@ -100,7 +100,6 @@ export class BaseService { this.baseOptions = extend( { qs: {}, serviceUrl: serviceClass.URL }, options, - this.readOptionsFromExternalConfig(), _options ); @@ -112,6 +111,9 @@ export class BaseService { } this.authenticator = options.authenticator; + + // temp: call the configureService method to ensure compatibility + this.configureService(this.name); } /** @@ -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. * @@ -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 diff --git a/test/unit/base-service.test.js b/test/unit/base-service.test.js index 1f776f945..f45a764c6 100644 --- a/test/unit/base-service.test.js +++ b/test/unit/base-service.test.js @@ -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); @@ -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) {