Skip to content

Commit 7324919

Browse files
authored
feat: adding configureService method for external config options (#66)
1 parent 5f963eb commit 7324919

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

lib/base-service.ts

+26-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ export class BaseService {
100100
this.baseOptions = extend(
101101
{ qs: {}, serviceUrl: serviceClass.URL },
102102
options,
103-
this.readOptionsFromExternalConfig(),
104103
_options
105104
);
106105

@@ -112,6 +111,9 @@ export class BaseService {
112111
}
113112

114113
this.authenticator = options.authenticator;
114+
115+
// temp: call the configureService method to ensure compatibility
116+
this.configureService(this.name);
115117
}
116118

117119
/**
@@ -132,6 +134,27 @@ export class BaseService {
132134
this.baseOptions.serviceUrl = url;
133135
}
134136

137+
/**
138+
* Configure the service using external configuration
139+
*
140+
* @param {string} the name of the service. Will be used to read from external
141+
* configuration
142+
*/
143+
protected configureService(serviceName: string): void {
144+
if (!serviceName) {
145+
const err = 'Error configuring service. Service name is required.';
146+
logger.error(err);
147+
throw new Error(err);
148+
}
149+
150+
extend(
151+
this.baseOptions,
152+
this.readOptionsFromExternalConfig(serviceName)
153+
);
154+
// overwrite the requestWrapperInstance with the new base options if applicable
155+
this.requestWrapperInstance = new RequestWrapper(this.baseOptions);
156+
}
157+
135158
/**
136159
* Wrapper around `sendRequest` that enforces the request will be authenticated.
137160
*
@@ -166,9 +189,9 @@ export class BaseService {
166189
});
167190
}
168191

169-
private readOptionsFromExternalConfig() {
192+
private readOptionsFromExternalConfig(serviceName: string) {
170193
const results = {} as any;
171-
const properties = readExternalSources(this.name);
194+
const properties = readExternalSources(serviceName);
172195

173196
if (properties !== null) {
174197
// the user can define two client-level variables in the credentials file: url and disableSsl

test/unit/base-service.test.js

+49-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ describe('Base Service', () => {
174174
authenticator: AUTHENTICATOR,
175175
});
176176

177-
const fromCredsFile = testService.readOptionsFromExternalConfig();
177+
const fromCredsFile = testService.readOptionsFromExternalConfig(DEFAULT_NAME);
178178

179179
expect(fromCredsFile.serviceUrl).toBe(serviceUrl);
180180
expect(fromCredsFile.disableSslVerification).toBe(disableSsl);
@@ -338,6 +338,54 @@ describe('Base Service', () => {
338338
});
339339
}).toThrow(/Revise these credentials/);
340340
});
341+
342+
it('should have the default baseOptions values when instantiating', () => {
343+
const testService = new TestService({
344+
authenticator: AUTHENTICATOR,
345+
});
346+
expect(testService.baseOptions.serviceUrl).toEqual(DEFAULT_URL);
347+
expect(testService.baseOptions.disableSslVerification).toEqual(false);
348+
expect(testService.baseOptions.qs).toBeDefined();
349+
expect(testService.baseOptions.qs).toEqual(EMPTY_OBJECT);
350+
});
351+
352+
it('should configure service by calling configureService method after instantiating', () => {
353+
const testService = new TestService({
354+
authenticator: AUTHENTICATOR,
355+
});
356+
357+
expect(testService.baseOptions.serviceUrl).toEqual(
358+
'https://gateway.watsonplatform.net/test/api'
359+
);
360+
expect(testService.baseOptions.disableSslVerification).toEqual(false);
361+
362+
readExternalSourcesMock.mockImplementation(() => ({
363+
url: 'abc123.com',
364+
disableSsl: true,
365+
}));
366+
367+
testService.configureService(DEFAULT_NAME);
368+
369+
expect(readExternalSourcesMock).toHaveBeenCalled();
370+
expect(testService.baseOptions.serviceUrl).toEqual('abc123.com');
371+
expect(testService.baseOptions.disableSslVerification).toEqual(true);
372+
});
373+
374+
it('configureService method should throw error if service name is not provided', () => {
375+
const testService = new TestService({
376+
authenticator: AUTHENTICATOR,
377+
});
378+
const fakeError = new Error('Error configuring service. Service name is required.');
379+
let err;
380+
381+
try {
382+
testService.configureService();
383+
} catch (e) {
384+
err = e;
385+
}
386+
387+
expect(err).toStrictEqual(fakeError);
388+
});
341389
});
342390

343391
function TestService(options) {

0 commit comments

Comments
 (0)