fix: work through how i want to return the api response with the data property being of the right type.
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
import { AxiosInstance } from 'axios';
|
import { AxiosInstance, AxiosResponse } from 'axios';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { ArgumentValidator } from './models/ArgumentValidator';
|
import { ArgumentValidator } from './models/ArgumentValidator';
|
||||||
import { EndpointFactory } from './models/EndpointFactory';
|
import { EndpointFactory } from './models/EndpointFactory';
|
||||||
import { ApiResponseFactory } from './models/ApiResponseFactory';
|
import { ApiResponseFactory } from './models/ApiResponseFactory';
|
||||||
import { ApiResponse } from './models/ApiResponse';
|
import { ApiResponse } from './models/ApiResponse';
|
||||||
import { App } from './models/App';
|
|
||||||
import { PagingRequest } from './models/PagingRequest';
|
import { PagingRequest } from './models/PagingRequest';
|
||||||
import { GetPagedAppsResponse } from './models/GetPagedAppsResponse';
|
import { GetPagedAppsResponse } from './models/GetPagedAppsResponse';
|
||||||
|
import { App } from './models/App';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class OnspringClient - A client that can communicate with the Onspring API.
|
* @class OnspringClient - A client that can communicate with the Onspring API.
|
||||||
@@ -55,14 +55,14 @@ export class OnspringClient {
|
|||||||
this._client.defaults.baseURL
|
this._client.defaults.baseURL
|
||||||
);
|
);
|
||||||
|
|
||||||
const response = await this.get<ApiResponse<boolean>>(endpoint);
|
const response = await this.get<any>(endpoint);
|
||||||
return response.isSuccessful;
|
return response.isSuccessful;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param pagingRequest - The paging request that will be used to get the apps.
|
* @param pagingRequest - The paging request that will be used to get the apps.
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
public async getApps(
|
public async getApps(
|
||||||
pagingRequest: PagingRequest = new PagingRequest(1, 50)
|
pagingRequest: PagingRequest = new PagingRequest(1, 50)
|
||||||
@@ -72,7 +72,8 @@ export class OnspringClient {
|
|||||||
pagingRequest
|
pagingRequest
|
||||||
);
|
);
|
||||||
|
|
||||||
return await this.get<GetPagedAppsResponse>(endpoint);
|
var apiResponse = await this.get<any>(endpoint);
|
||||||
|
return apiResponse.AsGetPagedAppsResponseType();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
import { App } from "./App";
|
||||||
|
import { GetPagedAppsResponse } from "./GetPagedAppsResponse";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class ApiResponse - A generic response object for API requests.
|
* @class ApiResponse - A generic response object for API requests.
|
||||||
*/
|
*/
|
||||||
@@ -5,22 +8,22 @@ export class ApiResponse<T> {
|
|||||||
/**
|
/**
|
||||||
* @property {number} statusCode - The status code of the response.
|
* @property {number} statusCode - The status code of the response.
|
||||||
*/
|
*/
|
||||||
readonly statusCode: number;
|
public statusCode: number;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @property {boolean} isSuccessful - True if the status code is less than 400; otherwise, false.
|
* @property {boolean} isSuccessful - True if the status code is less than 400; otherwise, false.
|
||||||
*/
|
*/
|
||||||
readonly isSuccessful: boolean;
|
public isSuccessful: boolean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @property {string} message - The message of the response.
|
* @property {string} message - The message of the response.
|
||||||
*/
|
*/
|
||||||
readonly message: string;
|
public message: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @property {T} data - The data of the response.
|
* @property {T} data - The data of the response.
|
||||||
*/
|
*/
|
||||||
readonly data: T;
|
public data: T;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @constructor - Creates a new instance of the ApiResponse class.
|
* @constructor - Creates a new instance of the ApiResponse class.
|
||||||
@@ -35,4 +38,25 @@ export class ApiResponse<T> {
|
|||||||
this.message = message;
|
this.message = message;
|
||||||
this.data = data;
|
this.data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AsGetPagedAppsResponseType(): ApiResponse<GetPagedAppsResponse> {
|
||||||
|
var apiResponse = this as ApiResponse<any>;
|
||||||
|
var apps = apiResponse.data.items.map((item: any) => {
|
||||||
|
return new App(item.href, item.id, item.name);
|
||||||
|
});
|
||||||
|
|
||||||
|
var getAppsPagedResponse = new GetPagedAppsResponse(
|
||||||
|
apps,
|
||||||
|
apiResponse.data.pageNumber,
|
||||||
|
apiResponse.data.pageSize,
|
||||||
|
apiResponse.data.totalPages,
|
||||||
|
apiResponse.data.totalRecords
|
||||||
|
);
|
||||||
|
|
||||||
|
return new ApiResponse<GetPagedAppsResponse>(
|
||||||
|
apiResponse.statusCode,
|
||||||
|
apiResponse.message,
|
||||||
|
getAppsPagedResponse
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,8 +15,7 @@ export class ApiResponseFactory {
|
|||||||
const message = this.TryToGetMessage(response);
|
const message = this.TryToGetMessage(response);
|
||||||
|
|
||||||
if (this.isSuccessStatusCode(response.status) === true) {
|
if (this.isSuccessStatusCode(response.status) === true) {
|
||||||
const data = response.data as T;
|
return new ApiResponse<T>(response.status, message, response.data);
|
||||||
return new ApiResponse<T>(response.status, message, data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ApiResponse<T>(response.status, message, null);
|
return new ApiResponse<T>(response.status, message, null);
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { PagedResponse } from "./PagedResponse";
|
|||||||
import { App } from "./App";
|
import { App } from "./App";
|
||||||
|
|
||||||
export class GetPagedAppsResponse extends PagedResponse<App> {
|
export class GetPagedAppsResponse extends PagedResponse<App> {
|
||||||
constructor(items: App[], pageNumber: number, pageSize: number, totalCount: number) {
|
constructor(items: App[], pageNumber: number, pageSize: number, totalPages: number, totalRecords: number) {
|
||||||
super(items, pageNumber, pageSize, totalCount);
|
super(items, pageNumber, pageSize, totalPages, totalRecords);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,12 +2,14 @@ export class PagedResponse<T> {
|
|||||||
public items: T[];
|
public items: T[];
|
||||||
public pageNumber: number;
|
public pageNumber: number;
|
||||||
public pageSize: number;
|
public pageSize: number;
|
||||||
public totalCount: number;
|
public totalPages: number;
|
||||||
|
public totalRecords: number;
|
||||||
|
|
||||||
constructor(items: T[], pageNumber: number, pageSize: number, totalCount: number) {
|
constructor(items: T[], pageNumber: number, pageSize: number, totalPages: number, totalRecords: number) {
|
||||||
this.items = items;
|
this.items = items;
|
||||||
this.pageNumber = pageNumber;
|
this.pageNumber = pageNumber;
|
||||||
this.pageSize = pageSize;
|
this.pageSize = pageSize;
|
||||||
this.totalCount = totalCount;
|
this.totalPages = totalPages;
|
||||||
|
this.totalRecords = totalRecords;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -15,7 +15,7 @@ describe('EndpointFactory', function () {
|
|||||||
describe('getAppsEndpoint', function () {
|
describe('getAppsEndpoint', function () {
|
||||||
it('should return the correct apps endpoint with paging params based on paging request parameter passed', function () {
|
it('should return the correct apps endpoint with paging params based on paging request parameter passed', function () {
|
||||||
const result = EndpointFactory.getAppsEndpoint(baseUrl, new PagingRequest(2, 1000));
|
const result = EndpointFactory.getAppsEndpoint(baseUrl, new PagingRequest(2, 1000));
|
||||||
expect(result).to.equal(`${baseUrl}/Apps?page=2&pageSize=1000`);
|
expect(result).to.equal(`${baseUrl}/Apps?pageSize=1000&pageNumber=2`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -12,17 +12,18 @@ describe('GetPagedAppsResponse', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should have 4 parameters', function () {
|
it('should have 4 parameters', function () {
|
||||||
expect(GetPagedAppsResponse).to.have.lengthOf(4);
|
expect(GetPagedAppsResponse).to.have.lengthOf(5);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should construct a new instance of GetPagedAppsResponse', function () {
|
it('should construct a new instance of GetPagedAppsResponse', function () {
|
||||||
const getPagedAppsResponse = new GetPagedAppsResponse([new App('test', 1, 'test'), new App('test', 1, 'test')], 1, 10, 100);
|
const getPagedAppsResponse = new GetPagedAppsResponse([new App('test', 1, 'test'), new App('test', 1, 'test')], 1, 10, 100, 100);
|
||||||
|
|
||||||
expect(getPagedAppsResponse).to.not.be.undefined;
|
expect(getPagedAppsResponse).to.not.be.undefined;
|
||||||
expect(getPagedAppsResponse).to.be.instanceOf(GetPagedAppsResponse);
|
expect(getPagedAppsResponse).to.be.instanceOf(GetPagedAppsResponse);
|
||||||
expect(getPagedAppsResponse).to.have.property('items').to.be.an('array').to.have.lengthOf(2);
|
expect(getPagedAppsResponse).to.have.property('items').to.be.an('array').to.have.lengthOf(2);
|
||||||
expect(getPagedAppsResponse).to.have.property('pageNumber').to.be.a('number').to.equal(1);
|
expect(getPagedAppsResponse).to.have.property('pageNumber').to.be.a('number').to.equal(1);
|
||||||
expect(getPagedAppsResponse).to.have.property('pageSize').to.be.a('number').to.equal(10);
|
expect(getPagedAppsResponse).to.have.property('pageSize').to.be.a('number').to.equal(10);
|
||||||
expect(getPagedAppsResponse).to.have.property('totalCount').to.be.a('number').to.equal(100);
|
expect(getPagedAppsResponse).to.have.property('totalPages').to.be.a('number').to.equal(100);
|
||||||
|
expect(getPagedAppsResponse).to.have.property('totalRecords').to.be.a('number').to.equal(100);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
+112
-15
@@ -3,6 +3,8 @@ import { ApiResponse } from '../src/models/ApiResponse';
|
|||||||
import axios, { AxiosResponse, InternalAxiosRequestConfig } from 'axios';
|
import axios, { AxiosResponse, InternalAxiosRequestConfig } from 'axios';
|
||||||
import { expect } from 'chai';
|
import { expect } from 'chai';
|
||||||
import * as sinon from 'sinon';
|
import * as sinon from 'sinon';
|
||||||
|
import { GetPagedAppsResponse } from '../src/models/GetPagedAppsResponse';
|
||||||
|
import { App } from '../src/models/App';
|
||||||
|
|
||||||
describe('OnspringClient', function () {
|
describe('OnspringClient', function () {
|
||||||
const baseUrl = 'https://api.onspring.dev';
|
const baseUrl = 'https://api.onspring.dev';
|
||||||
@@ -109,22 +111,16 @@ describe('OnspringClient', function () {
|
|||||||
|
|
||||||
it('should return a promise that resolves to a boolean', async function () {
|
it('should return a promise that resolves to a boolean', async function () {
|
||||||
const client = new OnspringClient(baseUrl, apiKey);
|
const client = new OnspringClient(baseUrl, apiKey);
|
||||||
const result = await client.canConnect();
|
|
||||||
expect(result).to.be.a('boolean');
|
const mockAxiosClient = axios.create({
|
||||||
});
|
|
||||||
|
|
||||||
it('should return a promise that resolves to true when able to connect to the Onspring API', async function () {
|
|
||||||
const client = new OnspringClient(baseUrl, apiKey);
|
|
||||||
|
|
||||||
const mockClient = axios.create({
|
|
||||||
baseURL: baseUrl,
|
baseURL: baseUrl,
|
||||||
headers: {
|
headers: {
|
||||||
'x-apikey': apiKey,
|
'x-apikey': apiKey,
|
||||||
'x-api-version': '2',
|
'x-api-version': '2',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
sinon.stub(mockClient, 'get').returns(Promise.resolve({
|
sinon.stub(mockAxiosClient, 'get').returns(Promise.resolve({
|
||||||
status: 200,
|
status: 200,
|
||||||
statusText: 'OK',
|
statusText: 'OK',
|
||||||
data: null,
|
data: null,
|
||||||
@@ -132,7 +128,32 @@ describe('OnspringClient', function () {
|
|||||||
config: {} as InternalAxiosRequestConfig,
|
config: {} as InternalAxiosRequestConfig,
|
||||||
} as AxiosResponse));
|
} as AxiosResponse));
|
||||||
|
|
||||||
sinon.stub(client, '_client' as any).value(mockClient);
|
sinon.stub(client, '_client' as any).value(mockAxiosClient);
|
||||||
|
|
||||||
|
const result = await client.canConnect();
|
||||||
|
expect(result).to.be.a('boolean');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return a promise that resolves to true when able to connect to the Onspring API', async function () {
|
||||||
|
const client = new OnspringClient(baseUrl, apiKey);
|
||||||
|
|
||||||
|
const mockAxiosClient = axios.create({
|
||||||
|
baseURL: baseUrl,
|
||||||
|
headers: {
|
||||||
|
'x-apikey': apiKey,
|
||||||
|
'x-api-version': '2',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
sinon.stub(mockAxiosClient, 'get').returns(Promise.resolve({
|
||||||
|
status: 200,
|
||||||
|
statusText: 'OK',
|
||||||
|
data: null,
|
||||||
|
headers: {},
|
||||||
|
config: {} as InternalAxiosRequestConfig,
|
||||||
|
} as AxiosResponse));
|
||||||
|
|
||||||
|
sinon.stub(client, '_client' as any).value(mockAxiosClient);
|
||||||
|
|
||||||
const result = await client.canConnect();
|
const result = await client.canConnect();
|
||||||
expect(result).to.be.true;
|
expect(result).to.be.true;
|
||||||
@@ -140,8 +161,8 @@ describe('OnspringClient', function () {
|
|||||||
|
|
||||||
it('should return a promise that resolves to false when unable to connect to the Onspring API', async function () {
|
it('should return a promise that resolves to false when unable to connect to the Onspring API', async function () {
|
||||||
const client = new OnspringClient(baseUrl, apiKey);
|
const client = new OnspringClient(baseUrl, apiKey);
|
||||||
// sinon.stub(client, 'get' as any).returns(Promise.resolve(new ApiResponse(500, 'Internal Server Error', null)));
|
|
||||||
const mockClient = axios.create({
|
const mockAxiosClient = axios.create({
|
||||||
baseURL: baseUrl,
|
baseURL: baseUrl,
|
||||||
headers: {
|
headers: {
|
||||||
'x-apikey': apiKey,
|
'x-apikey': apiKey,
|
||||||
@@ -149,7 +170,7 @@ describe('OnspringClient', function () {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
sinon.stub(mockClient, 'get').returns(Promise.reject({
|
sinon.stub(mockAxiosClient, 'get').returns(Promise.resolve({
|
||||||
status: 500,
|
status: 500,
|
||||||
statusText: 'Internal Server Error',
|
statusText: 'Internal Server Error',
|
||||||
data: null,
|
data: null,
|
||||||
@@ -157,10 +178,86 @@ describe('OnspringClient', function () {
|
|||||||
config: {} as InternalAxiosRequestConfig,
|
config: {} as InternalAxiosRequestConfig,
|
||||||
} as AxiosResponse));
|
} as AxiosResponse));
|
||||||
|
|
||||||
sinon.stub(client, '_client' as any).value(mockClient);
|
sinon.stub(client, '_client' as any).value(mockAxiosClient);
|
||||||
|
|
||||||
const result = await client.canConnect();
|
const result = await client.canConnect();
|
||||||
expect(result).to.be.false;
|
expect(result).to.be.false;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('getApps', function () {
|
||||||
|
it('should be defined', function () {
|
||||||
|
expect(new OnspringClient(baseUrl, apiKey).getApps).to.not.be.undefined;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be a function', function () {
|
||||||
|
expect(new OnspringClient(baseUrl, apiKey).getApps).to.be.a('function');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should have 0 parameters', function () {
|
||||||
|
expect(new OnspringClient(baseUrl, apiKey).getApps).to.have.lengthOf(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return a promise', function () {
|
||||||
|
expect(new OnspringClient(baseUrl, apiKey).getApps()).to.be.a('promise');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return a promise that resolves to a paged response of apps when request is successful', async function () {
|
||||||
|
const client = new OnspringClient(baseUrl, apiKey);
|
||||||
|
|
||||||
|
const mockAxiosClient = axios.create({
|
||||||
|
baseURL: baseUrl,
|
||||||
|
headers: {
|
||||||
|
'x-apikey': apiKey,
|
||||||
|
'x-api-version': '2',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
sinon.stub(mockAxiosClient, 'get').returns(
|
||||||
|
Promise.resolve({
|
||||||
|
status: 200,
|
||||||
|
statusText: 'OK',
|
||||||
|
data: {
|
||||||
|
pageNumber: 1,
|
||||||
|
pageSize: 2,
|
||||||
|
totalPages: 1,
|
||||||
|
totalRecords: 2,
|
||||||
|
items: [
|
||||||
|
{
|
||||||
|
href: 'https://api.onspring.dev/Apps/id/1',
|
||||||
|
id: '1',
|
||||||
|
name: 'Test App 1',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
href: 'https://api.onspring.dev/Apps/id/2',
|
||||||
|
id: '2',
|
||||||
|
name: 'Test App 2',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
headers: {},
|
||||||
|
config: {} as InternalAxiosRequestConfig,
|
||||||
|
} as AxiosResponse)
|
||||||
|
);
|
||||||
|
|
||||||
|
sinon.stub(client, '_client' as any).value(mockAxiosClient);
|
||||||
|
|
||||||
|
const result = await client.getApps();
|
||||||
|
expect(result).to.be.instanceOf(ApiResponse<GetPagedAppsResponse>);
|
||||||
|
expect(result).to.have.property('statusCode', 200);
|
||||||
|
expect(result).to.have.property('isSuccessful', true);
|
||||||
|
expect(result).to.have.property('message', '');
|
||||||
|
expect(result).to.have.property('data');
|
||||||
|
expect(result.data).to.be.instanceOf(GetPagedAppsResponse);
|
||||||
|
expect(result.data).to.have.property('pageNumber', 1);
|
||||||
|
expect(result.data).to.have.property('pageSize', 2);
|
||||||
|
expect(result.data).to.have.property('totalPages', 1);
|
||||||
|
expect(result.data).to.have.property('totalRecords', 2);
|
||||||
|
expect(result.data).to.have.property('items');
|
||||||
|
expect(result.data.items).to.be.instanceOf(Array);
|
||||||
|
expect(result.data.items).to.have.lengthOf(2);
|
||||||
|
expect(result.data.items[0]).to.be.instanceOf(App);
|
||||||
|
expect(result.data.items[1]).to.be.instanceOf(App);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -11,17 +11,18 @@ describe('PagedResponse', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should have 4 parameters', function () {
|
it('should have 4 parameters', function () {
|
||||||
expect(PagedResponse).to.have.lengthOf(4);
|
expect(PagedResponse).to.have.lengthOf(5);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should construct a new instance of PagedResponse', function () {
|
it('should construct a new instance of PagedResponse', function () {
|
||||||
const pagedResponse = new PagedResponse<number>([1, 2, 3, 4], 1, 10, 100);
|
const pagedResponse = new PagedResponse<number>([1, 2, 3, 4], 1, 10, 100, 100);
|
||||||
|
|
||||||
expect(pagedResponse).to.not.be.undefined;
|
expect(pagedResponse).to.not.be.undefined;
|
||||||
expect(pagedResponse).to.be.instanceOf(PagedResponse);
|
expect(pagedResponse).to.be.instanceOf(PagedResponse);
|
||||||
expect(pagedResponse).to.have.property('items').to.be.an('array').to.have.lengthOf(4);
|
expect(pagedResponse).to.have.property('items').to.be.an('array').to.have.lengthOf(4);
|
||||||
expect(pagedResponse).to.have.property('pageNumber').to.be.a('number').to.equal(1);
|
expect(pagedResponse).to.have.property('pageNumber').to.be.a('number').to.equal(1);
|
||||||
expect(pagedResponse).to.have.property('pageSize').to.be.a('number').to.equal(10);
|
expect(pagedResponse).to.have.property('pageSize').to.be.a('number').to.equal(10);
|
||||||
expect(pagedResponse).to.have.property('totalCount').to.be.a('number').to.equal(100);
|
expect(pagedResponse).to.have.property('totalPages').to.be.a('number').to.equal(100);
|
||||||
|
expect(pagedResponse).to.have.property('totalRecords').to.be.a('number').to.equal(100);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user