fix: adjust how passed in axios configs are combined with default axios configs in client constructor
This commit is contained in:
@@ -63,16 +63,20 @@ export class OnspringClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const configDefaults: CreateAxiosDefaults = {
|
const configDefaults: CreateAxiosDefaults = {
|
||||||
baseURL: baseUrl,
|
|
||||||
headers: { 'x-apikey': apiKey, 'x-api-version': '2' },
|
headers: { 'x-apikey': apiKey, 'x-api-version': '2' },
|
||||||
validateStatus: null,
|
validateStatus: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
// allow user to specify their own config, but
|
// always set baseUrl to the value passed to the constructor
|
||||||
// makes sure to override the baseURL, headers,
|
config.baseURL = baseUrl;
|
||||||
// and validateStatus properties with necessary
|
|
||||||
// default values
|
// merge the config passed to the constructor with the default config
|
||||||
config = { ...config, ...configDefaults };
|
// 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);
|
this._client = axios.create(config);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -116,6 +116,16 @@ describe('OnspringClient', function () {
|
|||||||
expect(new OnspringClient(baseUrl, apiKey)).to.have.property('_client');
|
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 () {
|
describe('canConnect', function () {
|
||||||
it('should be defined', function () {
|
it('should be defined', function () {
|
||||||
expect(new OnspringClient(baseUrl, apiKey).canConnect).to.not.be
|
expect(new OnspringClient(baseUrl, apiKey).canConnect).to.not.be
|
||||||
|
|||||||
Reference in New Issue
Block a user