diff --git a/integrationTests/getApps.spec.ts b/integrationTests/getApps.spec.ts index 92ed540..8e535b3 100644 --- a/integrationTests/getApps.spec.ts +++ b/integrationTests/getApps.spec.ts @@ -6,6 +6,7 @@ const envPath = path.resolve(__dirname, '.env'); dotenv.config({ path: envPath }); describe('getApps', function () { + this.timeout('5s'); let baseURL; 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; + }); }); diff --git a/src/models/OnspringClient.ts b/src/models/OnspringClient.ts index fbab5e2..593851c 100644 --- a/src/models/OnspringClient.ts +++ b/src/models/OnspringClient.ts @@ -1,4 +1,8 @@ -import axios from 'axios'; +import axios, { + type CreateAxiosDefaults, + type AxiosInstance, + type AxiosRequestConfig, +} from 'axios'; import { PagingRequest } from './PagingRequest'; import { ArgumentValidator } from './ArgumentValidator'; import { EndpointFactory } from './EndpointFactory'; @@ -6,7 +10,6 @@ import { ApiResponseFactory } from './ApiResponseFactory'; import { DataFormat } from '../enums/DataFormat'; import { ReportDataType } from '../enums/ReportDataType'; import { Record } from './Record'; -import { type AxiosInstance, type AxiosRequestConfig } from 'axios'; import { type ApiResponse } from './ApiResponse'; import { type GetPagedAppsResponse } from './GetPagedAppsResponse'; import { type App } from './App'; @@ -48,7 +51,8 @@ export class OnspringClient { */ constructor( baseUrl: string | undefined | null, - apiKey: string | undefined | null + apiKey: string | undefined | null, + config: CreateAxiosDefaults = {} ) { if (ArgumentValidator.isValidUrl(baseUrl) === false || baseUrl === null) { 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.'); } - this._client = axios.create({ + 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 }; + + this._client = axios.create(config); } /**