feat: implement getFieldsByAppId method

This commit is contained in:
StevanFreeborn
2023-02-03 11:45:15 -06:00
parent 0559f60726
commit 59a12cb8b6
6 changed files with 84 additions and 5 deletions
+31
View File
@@ -2,6 +2,7 @@ import { App } from './App';
import { CollectionResponse } from './CollectionResponse'; import { CollectionResponse } from './CollectionResponse';
import { Field } from './Field'; import { Field } from './Field';
import { GetPagedAppsResponse } from './GetPagedAppsResponse'; import { GetPagedAppsResponse } from './GetPagedAppsResponse';
import { GetPagedFieldsResponse } from './GetPagedFieldsResponse';
/** /**
* @class ApiResponse - A generic response object for API requests. * @class ApiResponse - A generic response object for API requests.
@@ -164,4 +165,34 @@ export class ApiResponse<T> {
collectionResponse collectionResponse
); );
} }
AsGetPagedFieldsResponseType(): ApiResponse<GetPagedFieldsResponse> {
const apiResponse = this as ApiResponse<any>;
const fields = apiResponse.data.items.map((item: any) => {
return new Field(
item.id,
item.appId,
item.name,
item.type,
item.status,
item.isRequired,
item.isUnique
);
});
const getPagedFieldsResponse = new GetPagedFieldsResponse(
fields,
apiResponse.data.pageNumber,
apiResponse.data.pageSize,
apiResponse.data.totalPages,
apiResponse.data.totalRecords
);
return new ApiResponse<GetPagedFieldsResponse>(
apiResponse.statusCode,
apiResponse.message,
getPagedFieldsResponse
);
}
} }
+6 -2
View File
@@ -58,10 +58,14 @@ export class EndpointFactory {
/** /**
* @method getFieldsByAppIdEndpoint - Gets the fields by app id endpoint. * @method getFieldsByAppIdEndpoint - Gets the fields by app id endpoint.
* @param {number} id - The id of the app. * @param {number} id - The id of the app.
* @param {PagingRequest} pagingRequest - Pagination information to use as query params in the endpoint string.
* @returns {string} - The fields by app id endpoint. * @returns {string} - The fields by app id endpoint.
*/ */
public static getFieldsByAppIdEndpoint(id: number): string { public static getFieldsByAppIdEndpoint(
return `/Fields/appId/${id}`; id: number,
pagingRequest: PagingRequest
): string {
return `/Fields/appId/${id}?pageSize=${pagingRequest.pageSize}&pageNumber=${pagingRequest.pageNumber}`;
} }
/** /**
+23
View File
@@ -0,0 +1,23 @@
import { type Field } from './Field';
import { PagedResponse } from './PagedResponse';
export class GetPagedFieldsResponse extends PagedResponse<Field> {
/**
* @constructor - Creates a new instance of the GetPagedAppsResponse class.
* @param {Field[]} items - The items in the collection.
* @param {number} pageNumber - The page number of the response.
* @param {number} pageSize - The page size of the response.
* @param {number} totalPages - The total number of pages in the response.
* @param {number} totalRecords - The total number of records in the response.
* @returns {GetPagedAppsResponse} - A new instance of the GetPagedAppsResponse class.
*/
constructor(
items: Field[],
pageNumber: number,
pageSize: number,
totalPages: number,
totalRecords: number
) {
super(items, pageNumber, pageSize, totalPages, totalRecords);
}
}
+18
View File
@@ -9,6 +9,7 @@ import { type GetPagedAppsResponse } from './GetPagedAppsResponse';
import { type App } from './App'; import { type App } from './App';
import { type CollectionResponse } from './CollectionResponse'; import { type CollectionResponse } from './CollectionResponse';
import { type Field } from './Field'; import { type Field } from './Field';
import { type GetPagedFieldsResponse } from './GetPagedFieldsResponse';
/** /**
* @class OnspringClient - A client that can communicate with the Onspring API. * @class OnspringClient - A client that can communicate with the Onspring API.
@@ -145,6 +146,23 @@ export class OnspringClient {
return apiResponse.AsFieldCollectionType(); return apiResponse.AsFieldCollectionType();
} }
public async getFieldsByAppId(
appId: number,
pagingRequest: PagingRequest = new PagingRequest(1, 50)
): Promise<ApiResponse<GetPagedFieldsResponse>> {
const endpoint = EndpointFactory.getFieldsByAppIdEndpoint(
appId,
pagingRequest
);
const apiResponse = await this.get<any>(endpoint);
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.AsGetPagedFieldsResponseType();
}
/** /**
* @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.
+5 -2
View File
@@ -49,8 +49,11 @@ describe('EndpointFactory', function () {
describe('getFieldsByAppIdEndpoint', function () { describe('getFieldsByAppIdEndpoint', function () {
it('should return the correct fields by app id endpoint', function () { it('should return the correct fields by app id endpoint', function () {
const result = EndpointFactory.getFieldsByAppIdEndpoint(1); const result = EndpointFactory.getFieldsByAppIdEndpoint(
expect(result).to.equal('/Fields/appId/1'); 1,
new PagingRequest(2, 1000)
);
expect(result).to.equal('/Fields/appId/1?pageSize=1000&pageNumber=2');
}); });
}); });
+1 -1
View File
@@ -1,4 +1,4 @@
{ {
"extends": "./tsconfig.json", "extends": "./tsconfig.json",
"include": ["tests/**/*.ts", "src/index.ts"] "include": ["src/**/*.ts", "tests/**/*.ts"]
} }