fix: set validateStatus property of axios instance to null so that the package can handle all the api responses internally. feat: added ability to provide axios instance configuration to the OnspringClient constructor.

This commit is contained in:
StevanFreeborn
2023-02-13 23:24:47 -06:00
parent 6f0d85b414
commit 64dfa6f1e1
2 changed files with 39 additions and 5 deletions
+21
View File
@@ -6,6 +6,7 @@ const envPath = path.resolve(__dirname, '.env');
dotenv.config({ path: envPath }); dotenv.config({ path: envPath });
describe('getApps', function () { describe('getApps', function () {
this.timeout('5s');
let baseURL; let baseURL;
let apiKey; let apiKey;
@@ -67,4 +68,24 @@ describe('getApps', function () {
} }
} }
}); });
it('should return a 400 response when an invalid page size is used', async function () {
const client = new OnspringClient(baseURL, apiKey);
const response = await client.getApps({ pageNumber: 1, pageSize: 1001 });
expect(response.statusCode).to.equal(400);
expect(response.isSuccessful).to.be.false;
expect(response.message).to.not.be.null;
expect(response.data).to.be.null;
});
it('should return a 401 response when an invalid api key is used', async function () {
const client = new OnspringClient(baseURL, 'invalid');
const response = await client.getApps();
expect(response.statusCode).to.equal(401);
expect(response.isSuccessful).to.be.false;
expect(response.message).to.be.undefined;
expect(response.data).to.be.null;
});
}); });
+18 -5
View File
@@ -1,4 +1,8 @@
import axios from 'axios'; import axios, {
type CreateAxiosDefaults,
type AxiosInstance,
type AxiosRequestConfig,
} from 'axios';
import { PagingRequest } from './PagingRequest'; import { PagingRequest } from './PagingRequest';
import { ArgumentValidator } from './ArgumentValidator'; import { ArgumentValidator } from './ArgumentValidator';
import { EndpointFactory } from './EndpointFactory'; import { EndpointFactory } from './EndpointFactory';
@@ -6,7 +10,6 @@ import { ApiResponseFactory } from './ApiResponseFactory';
import { DataFormat } from '../enums/DataFormat'; import { DataFormat } from '../enums/DataFormat';
import { ReportDataType } from '../enums/ReportDataType'; import { ReportDataType } from '../enums/ReportDataType';
import { Record } from './Record'; import { Record } from './Record';
import { type AxiosInstance, type AxiosRequestConfig } from 'axios';
import { type ApiResponse } from './ApiResponse'; import { type ApiResponse } from './ApiResponse';
import { type GetPagedAppsResponse } from './GetPagedAppsResponse'; import { type GetPagedAppsResponse } from './GetPagedAppsResponse';
import { type App } from './App'; import { type App } from './App';
@@ -48,7 +51,8 @@ export class OnspringClient {
*/ */
constructor( constructor(
baseUrl: string | undefined | null, baseUrl: string | undefined | null,
apiKey: string | undefined | null apiKey: string | undefined | null,
config: CreateAxiosDefaults = {}
) { ) {
if (ArgumentValidator.isValidUrl(baseUrl) === false || baseUrl === null) { if (ArgumentValidator.isValidUrl(baseUrl) === false || baseUrl === null) {
throw new Error('baseUrl must be an absolute and well-formed URI.'); throw new Error('baseUrl must be an absolute and well-formed URI.');
@@ -58,10 +62,19 @@ export class OnspringClient {
throw new Error('apiKey cannot be null/empty/whitespace.'); throw new Error('apiKey cannot be null/empty/whitespace.');
} }
this._client = axios.create({ const configDefaults: CreateAxiosDefaults = {
baseURL: baseUrl, baseURL: baseUrl,
headers: { 'x-apikey': apiKey, 'x-api-version': '2' }, 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 };
this._client = axios.create(config);
} }
/** /**