feat: implement addOrUpdateListItem method

This commit is contained in:
Stevan Freeborn
2023-02-07 11:52:46 -06:00
parent a68ea4f63e
commit 190c829dc3
8 changed files with 391 additions and 0 deletions
+13
View File
@@ -10,6 +10,7 @@ import { FormulaField } from './FormulaField';
import { GetPagedAppsResponse } from './GetPagedAppsResponse';
import { GetPagedFieldsResponse } from './GetPagedFieldsResponse';
import { ListField } from './ListField';
import { ListItemResponse } from './ListItemResponse';
import { ListValue } from './ListValue';
import { ReferenceField } from './ReferenceField';
@@ -258,6 +259,18 @@ export class ApiResponse<T> {
);
}
public asListItemResponseType(): ApiResponse<ListItemResponse> {
const apiResponse = this as ApiResponse<any>;
const listItemResponse = new ListItemResponse(apiResponse.data.id);
return new ApiResponse<ListItemResponse>(
apiResponse.statusCode,
apiResponse.message,
listItemResponse
);
}
/**
* @method asFileCollectionType - Converts the field item to the appropriate field object based upon the field item's type.
* @param fieldItem - The field item to convert.
+52
View File
@@ -0,0 +1,52 @@
/**
* @class ListItemRequest - Represents a list item request
*/
export class ListItemRequest {
/**
* @property {number} listId - The id for the list that the item belongs to.
*/
public listId: number;
/**
* @property {string} id - The id for the list item.
*/
public id: string;
/**
* @property {string} name - The name of the list item.
*/
public name: string;
/**
* @property {number} numericValue - The numeric value of the list item.
*/
public numericValue: number;
/**
* @property {string} color - The color of the list item.
*/
public color: string;
/**
* @constructor - Creates a new instance of the ListItemRequest class.
* @param {number} listId - The id for the list that the item belongs to.
* @param {string} id - The id for the list item.
* @param {string} name - The name of the list item.
* @param {number} numericValue - The numeric value of the list item.
* @param {string} color - The color of the list item.
* @returns {ListItemRequest} - A new instance of the ListItemRequest class.
*/
constructor(
listId: number,
id: string,
name: string,
numericValue: number,
color: string
) {
this.listId = listId;
this.id = id;
this.name = name;
this.numericValue = numericValue;
this.color = color;
}
}
+10
View File
@@ -0,0 +1,10 @@
import { CreatedWithIdResponse } from './CreatedWithIdResponse';
/**
* @class ListItemResponse - Represents a respons when a list item is created or updated.
*/
export class ListItemResponse extends CreatedWithIdResponse {
constructor(id: number) {
super(id);
}
}
+41
View File
@@ -14,6 +14,8 @@ import { type SaveFileRequest } from './SaveFileRequest';
import { type CreatedWithIdResponse } from './CreatedWithIdResponse';
import { type FileInfo } from './FileInfo';
import { type File } from './File';
import { type ListItemResponse } from './ListItemResponse';
import { type ListItemRequest } from './ListItemRequest';
/**
* @class OnspringClient - A client that can communicate with the Onspring API.
@@ -269,6 +271,22 @@ export class OnspringClient {
return apiResponse;
}
public async addOrUpdateListItem(
listItemRequest: ListItemRequest
): Promise<ApiResponse<ListItemResponse>> {
const { listId, ...data } = listItemRequest;
const endpoint = EndpointFactory.getAddOrUpdateListItemEndpoint(listId);
const apiResponse = await this.put<any>(endpoint, data);
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.asListItemResponseType();
}
/**
* @method get - Makes a GET request to the specified endpoint.
* @param {string} endpoint - The endpoint that will be used to make the request.
@@ -301,6 +319,29 @@ export class OnspringClient {
return apiResponse;
}
/**
* @method put - Makes a PUT request to the specified endpoint.
* @param {string} endpoint - The endpoint that will be used to make the request.
* @param {any} data - The data that will be sent with the request.
* @param {AxiosRequestConfig} config - The configuration that will be used to make the request.
* @returns {Promise<ApiResponse<T>>} - A promise that resolves to an ApiResponse of type T.
*/
private async put<T>(
endpoint: string,
data: any,
config: AxiosRequestConfig = {}
): Promise<ApiResponse<T>> {
const response = await this._client.put(endpoint, data, config);
const apiResponse = ApiResponseFactory.getApiResponse<T>(response);
return apiResponse;
}
/**
* @method delete - Makes a DELETE request to the specified endpoint.
* @param {string} endpoint - The endpoint that will be used to make the request.
* @param {AxiosRequestConfig} config - The configuration that will be used to make the request.
* @returns {Promise<ApiResponse<T>>} - A promise that resolves to an ApiResponse of type T.
*/
private async delete<T>(
endpoint: string,
config: AxiosRequestConfig = {}