feat: implement getReportById method

This commit is contained in:
StevanFreeborn
2023-02-09 18:23:49 -06:00
parent 61cb4d2929
commit 3b3e210920
10 changed files with 393 additions and 4 deletions
+18
View File
@@ -15,6 +15,8 @@ import { ListItemResponse } from './ListItemResponse';
import { ListValue } from './ListValue'; import { ListValue } from './ListValue';
import { ReferenceField } from './ReferenceField'; import { ReferenceField } from './ReferenceField';
import { Report } from './Report'; import { Report } from './Report';
import { ReportData } from './ReportData';
import { Row } from './Row';
/** /**
* @class ApiResponse - A generic response object for API requests. * @class ApiResponse - A generic response object for API requests.
@@ -303,6 +305,22 @@ export class ApiResponse<T> {
); );
} }
asReportDataType(): ApiResponse<ReportData> {
const apiResponse = this as ApiResponse<any>;
const rows = apiResponse.data.rows.map((row) => {
return new Row(row.recordId, row.cells);
});
const reportData = new ReportData(apiResponse.data.columns, rows);
return new ApiResponse<ReportData>(
apiResponse.statusCode,
apiResponse.message,
reportData
);
}
/** /**
* @method asFileCollectionType - Converts the field item to the appropriate field object based upon the field item's type. * @method asFileCollectionType - Converts the field item to the appropriate field object based upon the field item's type.
* @param {any} fieldItem - The field item to convert. * @param {any} fieldItem - The field item to convert.
+10 -2
View File
@@ -1,3 +1,5 @@
import { type DataFormat } from '../enums/DataFormat';
import { type ReportDataType } from '../enums/ReportDataType';
import { type PagingRequest } from './PagingRequest'; import { type PagingRequest } from './PagingRequest';
/** /**
@@ -210,10 +212,16 @@ export class EndpointFactory {
/** /**
* @method getReportByIdEndpoint - Gets the get report by id endpoint. * @method getReportByIdEndpoint - Gets the get report by id endpoint.
* @param {number} reportId - The id of the report. * @param {number} reportId - The id of the report.
* @param {DataFormat} apiDataFormat - The data format that will be used to make the request.
* @param {ReportDataType} reportDataType - The report data type that will be used to make the request.
* @returns {string} - The get report by id endpoint. * @returns {string} - The get report by id endpoint.
*/ */
public static getReportByIdEndpoint(reportId: number): string { public static getReportByIdEndpoint(
return `/Reports/id/${reportId}`; reportId: number,
apiDataFormat: DataFormat,
reportDataType: ReportDataType
): string {
return `/Reports/id/${reportId}?apiDataFormat=${apiDataFormat}&dataType=${reportDataType}`;
} }
/** /**
+23
View File
@@ -3,6 +3,8 @@ import { PagingRequest } from './PagingRequest';
import { ArgumentValidator } from './ArgumentValidator'; import { ArgumentValidator } from './ArgumentValidator';
import { EndpointFactory } from './EndpointFactory'; import { EndpointFactory } from './EndpointFactory';
import { ApiResponseFactory } from './ApiResponseFactory'; import { ApiResponseFactory } from './ApiResponseFactory';
import { DataFormat } from '../enums/DataFormat';
import { ReportDataType } from '../enums/ReportDataType';
import { type AxiosInstance, type AxiosRequestConfig } from 'axios'; 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';
@@ -17,6 +19,7 @@ import { type File } from './File';
import { type ListItemResponse } from './ListItemResponse'; import { type ListItemResponse } from './ListItemResponse';
import { type ListItemRequest } from './ListItemRequest'; import { type ListItemRequest } from './ListItemRequest';
import { type GetPagedReportsResponse } from './GetPagedReportsResponse'; import { type GetPagedReportsResponse } from './GetPagedReportsResponse';
import { type ReportData } from './ReportData';
/** /**
* @class OnspringClient - A client that can communicate with the Onspring API. * @class OnspringClient - A client that can communicate with the Onspring API.
@@ -331,6 +334,26 @@ export class OnspringClient {
return apiResponse.asGetPagedReportsResponseType(); return apiResponse.asGetPagedReportsResponseType();
} }
public async getReportById(
reportId: number,
apiDataFormat: DataFormat = DataFormat.Raw,
reportDataType: ReportDataType = ReportDataType.ReportData
): Promise<ApiResponse<ReportData>> {
const endpoint = EndpointFactory.getReportByIdEndpoint(
reportId,
apiDataFormat,
reportDataType
);
const apiResponse = await this.get<any>(endpoint);
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.asReportDataType();
}
/** /**
* @method get - Makes a GET request to the specified endpoint. * @method get - Makes a GET request to the specified endpoint.
* @param {string} endpoint - The endpoint that will be used to make the request. * @param {string} endpoint - The endpoint that will be used to make the request.
+11
View File
@@ -0,0 +1,11 @@
import { type Row } from './Row';
export class ReportData {
public columns: string[];
public rows: Row[];
constructor(columns: string[], rows: Row[]) {
this.columns = columns;
this.rows = rows;
}
}
+9
View File
@@ -0,0 +1,9 @@
export class Row {
public recordId: number;
public cells: object[];
constructor(recordId: number, cells: object[]) {
this.recordId = recordId;
this.cells = cells;
}
}
+52
View File
@@ -21,6 +21,8 @@ import path from 'path';
import { ListItemResponse } from '../src/models/ListItemResponse'; import { ListItemResponse } from '../src/models/ListItemResponse';
import { GetPagedReportsResponse } from '../src/models/GetPagedReportsResponse'; import { GetPagedReportsResponse } from '../src/models/GetPagedReportsResponse';
import { Report } from '../src/models/Report'; import { Report } from '../src/models/Report';
import { ReportData } from '../src/models/ReportData';
import { Row } from '../src/models/Row';
describe('ApiResponse', function () { describe('ApiResponse', function () {
it('should be defined', function () { it('should be defined', function () {
@@ -1014,4 +1016,54 @@ describe('ApiResponse', function () {
} }
}); });
}); });
describe('asReportDataType', function () {
it('should be defined', function () {
expect(ApiResponse.prototype.asReportDataType).to.not.be.undefined;
});
it('should be a function', function () {
expect(ApiResponse.prototype.asReportDataType).to.be.a('function');
});
it('should have no parameters', function () {
expect(ApiResponse.prototype.asReportDataType).to.have.lengthOf(0);
});
it('should return an ApiResponse<ReportData>', function () {
const mockResponseData = {
columns: ['field 1', 'field 2'],
rows: [
{
recordId: 1,
cells: ['field value 1', 'field value 2'],
},
{
recordId: 2,
cells: ['field value 1', 'field value 2'],
},
],
};
const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const reportData = apiResponse.asReportDataType();
expect(reportData).to.be.an.instanceof(ApiResponse<ReportData>);
expect(reportData.data).to.be.an.instanceof(ReportData);
expect(reportData.data).to.have.property('columns').that.is.an('array');
expect(reportData.data).to.have.property('rows').that.is.an('array');
expect(reportData.data).to.not.be.null;
if (reportData.data != null) {
expect(reportData.data.columns).to.have.lengthOf(2);
reportData.data.columns.forEach((column) => {
expect(column).to.be.a('string');
});
expect(reportData.data.rows).to.have.lengthOf(2);
reportData.data.rows.forEach((row) => {
expect(row).to.be.an.instanceOf(Row);
});
}
});
});
}); });
+10 -2
View File
@@ -1,6 +1,8 @@
import { EndpointFactory } from '../src/models/EndpointFactory'; import { EndpointFactory } from '../src/models/EndpointFactory';
import { expect } from 'chai'; import { expect } from 'chai';
import { PagingRequest } from '../src/models/PagingRequest'; import { PagingRequest } from '../src/models/PagingRequest';
import { DataFormat } from '../src/enums/DataFormat';
import { ReportDataType } from '../src/enums/ReportDataType';
describe('EndpointFactory', function () { describe('EndpointFactory', function () {
describe('getPingEndpoint', function () { describe('getPingEndpoint', function () {
@@ -155,8 +157,14 @@ describe('EndpointFactory', function () {
describe('getReportByIdEndpoint', function () { describe('getReportByIdEndpoint', function () {
it('should return the correct report by id endpoint', function () { it('should return the correct report by id endpoint', function () {
const result = EndpointFactory.getReportByIdEndpoint(1); const result = EndpointFactory.getReportByIdEndpoint(
expect(result).to.equal('/Reports/id/1'); 1,
DataFormat.Raw,
ReportDataType.ReportData
);
expect(result).to.equal(
'/Reports/id/1?apiDataFormat=Raw&dataType=ReportData'
);
}); });
}); });
+191
View File
@@ -24,6 +24,7 @@ import { ListItemRequest } from '../src/models/ListItemRequest';
import { ListItemResponse } from '../src/models/ListItemResponse'; import { ListItemResponse } from '../src/models/ListItemResponse';
import { Report } from '../src/models/Report'; import { Report } from '../src/models/Report';
import { GetPagedReportsResponse } from '../src/models/GetPagedReportsResponse'; import { GetPagedReportsResponse } from '../src/models/GetPagedReportsResponse';
import { ReportData } from '../src/models/ReportData';
describe('OnspringClient', function () { describe('OnspringClient', function () {
const baseUrl = 'https://api.onspring.dev'; const baseUrl = 'https://api.onspring.dev';
@@ -2631,4 +2632,194 @@ describe('OnspringClient', function () {
expect(result).to.have.property('data', null); expect(result).to.have.property('data', null);
}); });
}); });
describe('getReportById', function () {
it('should be defined', function () {
expect(OnspringClient.prototype.getReportById).to.not.be.undefined;
});
it('should be a function', function () {
expect(OnspringClient.prototype.getReportById).to.be.a('function');
});
it('should return a promise', function () {
expect(
new OnspringClient(baseUrl, apiKey).getReportById(1)
).to.be.instanceOf(Promise);
});
it('should return a promise that resolves to an api response 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: {
columns: ['field 1', 'field 2'],
rows: [
{
recordId: 1,
cells: ['field value 1', 'field value 2'],
},
{
recordId: 2,
cells: ['field value 1', 'field value 2'],
},
],
},
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.getReportById(1);
expect(result).to.be.instanceOf(ApiResponse<ReportData>);
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(ReportData);
expect(result.data).to.have.property('columns').that.is.an('array');
expect(result.data).to.have.property('rows').that.is.an('array');
});
it('should return a promise that resolves to an api response when request receives a 400 response', 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: 400,
statusText: 'Bad Request',
data: { message: 'Bad Request' },
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.getReportById(1);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 400);
expect(result).to.have.property('isSuccessful', false);
expect(result).to.have.property('message', '{"message":"Bad Request"}');
expect(result).to.have.property('data', null);
});
it('should return a promise that resolves to an api response when request receives a 401 response', 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: 401,
statusText: 'Unauthorized',
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.getReportById(1);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 401);
expect(result).to.have.property('isSuccessful', false);
expect(result).to.have.property('message', undefined);
expect(result).to.have.property('data', null);
});
it('should return a promise that resolves to an api response when request receives a 403 response', 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: 403,
statusText: 'Forbidden',
data: { message: 'Forbidden' },
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.getReportById(1);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 403);
expect(result).to.have.property('isSuccessful', false);
expect(result).to.have.property('message', 'Forbidden');
expect(result).to.have.property('data', null);
});
it('should return a promise that resolves to an api response when request receives a 404 response', 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: 404,
statusText: 'Not Found',
data: { message: 'Not Found' },
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.getReportById(1);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 404);
expect(result).to.have.property('isSuccessful', false);
expect(result).to.have.property('message', 'Not Found');
expect(result).to.have.property('data', null);
});
});
}); });
+50
View File
@@ -0,0 +1,50 @@
import { ReportData } from '../src/models/ReportData';
import { expect } from 'chai';
describe('ReportData', function () {
it('should be defined', function () {
expect(ReportData).to.not.be.undefined;
});
it('should have a constructor with 2 parameters', function () {
expect(ReportData).to.have.property('constructor');
expect(ReportData).to.have.lengthOf(2);
});
it('should have a construct that sets its properties correctly', function () {
const reportData = new ReportData(
['a', 'b'],
[
{
recordId: 1,
cells: [{}, {}],
},
{
recordId: 2,
cells: [{}, {}],
},
]
);
expect(reportData)
.to.have.property('columns')
.that.is.an('array')
.with.lengthOf(2);
expect(reportData)
.to.have.property('rows')
.that.is.an('array')
.with.lengthOf(2);
reportData.columns.forEach((column) => {
expect(column).to.be.a('string');
});
reportData.rows.forEach((row) => {
expect(row).to.have.property('recordId').that.is.a('number');
expect(row)
.to.have.property('cells')
.that.is.an('array')
.with.lengthOf(2);
});
});
});
+19
View File
@@ -0,0 +1,19 @@
import { Row } from '../src/models/Row';
import { expect } from 'chai';
describe('Row', function () {
it('should be defined', function () {
expect(Row).to.not.be.undefined;
});
it('should have a constructor with 2 parameters', function () {
expect(Row).to.have.property('constructor');
expect(Row).to.have.lengthOf(2);
});
it('should have a construct that sets its properties correctly', function () {
const row = new Row(1, [{}, {}]);
expect(row).to.have.property('recordId', 1);
expect(row).to.have.property('cells').that.is.an('array').with.lengthOf(2);
});
});