From d6c65791295d2d97c2eeaa7491e7b9dbf3a3a5b8 Mon Sep 17 00:00:00 2001 From: StevanFreeborn <=> Date: Mon, 13 Feb 2023 23:37:15 -0600 Subject: [PATCH] fix: adjust how passed in axios configs are combined with default axios configs in client constructor --- src/models/OnspringClient.ts | 16 ++++++++++------ tests/OnspringClient.spec.ts | 10 ++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/models/OnspringClient.ts b/src/models/OnspringClient.ts index 593851c..b10308b 100644 --- a/src/models/OnspringClient.ts +++ b/src/models/OnspringClient.ts @@ -63,16 +63,20 @@ export class OnspringClient { } const configDefaults: CreateAxiosDefaults = { - baseURL: baseUrl, headers: { 'x-apikey': apiKey, 'x-api-version': '2' }, validateStatus: null, }; - // allow user to specify their own config, but - // makes sure to override the baseURL, headers, - // and validateStatus properties with necessary - // default values - config = { ...config, ...configDefaults }; + // always set baseUrl to the value passed to the constructor + config.baseURL = baseUrl; + + // merge the config passed to the constructor with the default config + // to make sure the necessary api headers are set. + config.headers = { ...config.headers, ...configDefaults.headers } as any; + + // only set validateStatus if it is not already set + config.validateStatus = + config.validateStatus ?? configDefaults.validateStatus; this._client = axios.create(config); } diff --git a/tests/OnspringClient.spec.ts b/tests/OnspringClient.spec.ts index 07fcea4..0a70b31 100644 --- a/tests/OnspringClient.spec.ts +++ b/tests/OnspringClient.spec.ts @@ -116,6 +116,16 @@ describe('OnspringClient', function () { expect(new OnspringClient(baseUrl, apiKey)).to.have.property('_client'); }); + it('should create a new instance of an onspring client when passed an axios config object', function () { + const config = { + validateStatus: function (status) { + return status >= 200 && status < 300; + }, + }; + + expect(() => new OnspringClient(baseUrl, apiKey, config)).to.not.throw(); + }); + describe('canConnect', function () { it('should be defined', function () { expect(new OnspringClient(baseUrl, apiKey).canConnect).to.not.be