Files
onspring-api-sdk-javascript/src/models/OnspringClient.ts
T

507 lines
19 KiB
TypeScript
Raw Normal View History

import axios from 'axios';
2023-02-06 21:28:11 -06:00
import { PagingRequest } from './PagingRequest';
import { ArgumentValidator } from './ArgumentValidator';
import { EndpointFactory } from './EndpointFactory';
import { ApiResponseFactory } from './ApiResponseFactory';
2023-02-09 18:23:49 -06:00
import { DataFormat } from '../enums/DataFormat';
import { ReportDataType } from '../enums/ReportDataType';
2023-02-06 21:28:11 -06:00
import { type AxiosInstance, type AxiosRequestConfig } from 'axios';
2023-02-03 00:16:38 -06:00
import { type ApiResponse } from './ApiResponse';
import { type GetPagedAppsResponse } from './GetPagedAppsResponse';
import { type App } from './App';
import { type CollectionResponse } from './CollectionResponse';
import { type Field } from './Field';
2023-02-03 11:45:15 -06:00
import { type GetPagedFieldsResponse } from './GetPagedFieldsResponse';
2023-02-03 16:01:20 -06:00
import { type SaveFileRequest } from './SaveFileRequest';
import { type CreatedWithIdResponse } from './CreatedWithIdResponse';
2023-02-04 15:09:13 -06:00
import { type FileInfo } from './FileInfo';
2023-02-06 21:28:11 -06:00
import { type File } from './File';
2023-02-07 11:52:46 -06:00
import { type ListItemResponse } from './ListItemResponse';
import { type ListItemRequest } from './ListItemRequest';
import { type GetPagedReportsResponse } from './GetPagedReportsResponse';
2023-02-09 18:23:49 -06:00
import { type ReportData } from './ReportData';
import { type Record } from './Record';
import { type GetRecordRequest } from './GetRecordRequest';
2023-02-11 00:16:33 -06:00
import { type GetRecordsByAppIdRequest } from './GetRecordsByAppIdRequest';
import { type GetPagedRecordsResponse } from './GetPagedRecordsResponse';
import { type GetRecordsRequest } from './GetRecordsRequest';
2023-02-12 19:11:42 -06:00
import { type QueryRecordsRequest } from './QueryRecordsRequest';
2023-01-25 17:08:19 -06:00
/**
* @class OnspringClient - A client that can communicate with the Onspring API.
*/
export class OnspringClient {
/**
2023-01-26 14:20:30 -06:00
* @readonly {AxiosInstance} client - The axios instance that will be used to make requests to the Onspring API.
*/
2023-01-26 22:40:13 -06:00
private readonly _client: AxiosInstance;
/**
* @constructor - Creates a new instance of the OnspringClient class.
* @param {string} baseUrl - The base url that will be used to make requests to the Onspring API.
* @param {string} apiKey - The api key that will be used to authorize requests made by this client.
* @throws {Error} - Thrown when the baseUrl is not a valid url.
* @throws {Error} - Thrown when the apiKey is null/undefined/empty/whitespace.
* @returns {OnspringClient} - A new instance of the OnspringClient class.
*/
constructor(
baseUrl: string | undefined | null,
apiKey: string | undefined | null
) {
if (ArgumentValidator.isValidUrl(baseUrl) === false || baseUrl === null) {
throw new Error('baseUrl must be an absolute and well-formed URI.');
}
if (ArgumentValidator.isNullOrWhiteSpace(apiKey)) {
throw new Error('apiKey cannot be null/empty/whitespace.');
}
2023-02-01 23:45:14 -06:00
this._client = axios.create({
baseURL: baseUrl,
headers: { 'x-apikey': apiKey, 'x-api-version': '2' },
});
2023-01-25 17:08:19 -06:00
}
2023-01-27 11:28:45 -06:00
/**
* @method canConnect - Determines if the client can connect to the Onspring API.
* @returns {Promise<boolean>} - A promise that resolves to a boolean indicating if the client can connect to the Onspring API.
*/
2023-01-27 11:28:45 -06:00
public async canConnect(): Promise<boolean> {
const endpoint = EndpointFactory.getPingEndpoint();
2023-01-27 22:53:14 -06:00
const response = await this.get<any>(endpoint);
2023-01-27 22:53:14 -06:00
return response.isSuccessful;
}
/**
2023-02-02 20:24:20 -06:00
* @method getApps - Gets a paged list of apps.
2023-02-02 22:52:14 -06:00
* @param {PagingRequest} pagingRequest - The paging request that will be used to get the apps.
* @returns {Promise<ApiResponse<GetPagedAppsResponse>>} - A promise that resolves to an ApiResponse of type GetPagedAppsResponse.
2023-01-27 22:53:14 -06:00
*/
public async getApps(
pagingRequest: PagingRequest = new PagingRequest(1, 50)
): Promise<ApiResponse<GetPagedAppsResponse>> {
const endpoint = EndpointFactory.getAppsEndpoint();
const params = { ...pagingRequest };
const apiResponse = await this.get<any>(endpoint, { params });
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.asGetPagedAppsResponseType();
2023-01-27 11:28:45 -06:00
}
2023-02-02 20:24:20 -06:00
/**
* @method getAppById - Gets an app by its id.
2023-02-02 22:52:14 -06:00
* @param {number} appId - The id of the app to get.
* @returns {Promise<ApiResponse<App>>} - A promise that resolves to an ApiResponse of type App.
2023-02-02 20:24:20 -06:00
*/
public async getAppById(appId: number): Promise<ApiResponse<App>> {
const endpoint = EndpointFactory.getAppByIdEndpoint(appId);
2023-02-03 00:16:38 -06:00
const apiResponse = await this.get<any>(endpoint);
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.asAppType();
}
2023-02-02 20:24:20 -06:00
/**
* @method getAppsByIds - Gets a list of apps by their ids.
2023-02-02 22:52:14 -06:00
* @param {number[]} appIds - The ids of the apps to get.
* @returns {Promise<ApiResponse<CollectionResponse<App>>>} - A promise that resolves to an ApiResponse of type CollectionResponse<App>.
2023-02-02 20:24:20 -06:00
*/
public async getAppsByIds(
2023-02-02 22:52:14 -06:00
appIds: number[]
2023-02-02 20:24:20 -06:00
): Promise<ApiResponse<CollectionResponse<App>>> {
const endpoint = EndpointFactory.getAppsByIdsEndpoint();
2023-02-04 15:09:13 -06:00
const uniqueIds = [...new Set(appIds)];
const apiResponse = await this.post<any>(endpoint, uniqueIds);
2023-02-02 20:24:20 -06:00
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.asAppCollectionType();
2023-02-02 20:24:20 -06:00
}
2023-02-02 22:52:14 -06:00
/**
2023-02-02 22:59:07 -06:00
* @method getFieldById - Gets a field by its id.
* @param {number} fieldId - The id of the field to get.
2023-02-02 22:52:14 -06:00
* @returns {Promise<ApiResponse<Field>>} - A promise that resolves to an ApiResponse of type Field.
*/
2023-02-02 21:37:40 -06:00
public async getFieldById(fieldId: number): Promise<ApiResponse<Field>> {
const endpoint = EndpointFactory.getFieldByIdEndpoint(fieldId);
const apiResponse = await this.get<any>(endpoint);
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.asFieldType();
2023-02-02 21:37:40 -06:00
}
/**
* @method getFieldsByIds - Gets a list of fields by their ids.
* @param {number[]} fieldIds - The ids of the fields to get.
* @returns {Promise<ApiResponse<CollectionResponse<Field>>>} - A promise that resolves to an ApiResponse of type CollectionResponse<Field>.
*/
2023-02-03 00:16:38 -06:00
public async getFieldsByIds(
fieldIds: number[]
): Promise<ApiResponse<CollectionResponse<Field>>> {
const endpoint = EndpointFactory.getFieldsByIdsEndpoint();
2023-02-03 11:17:53 -06:00
const uniqueIds = [...new Set(fieldIds)];
const apiResponse = await this.post<any>(endpoint, uniqueIds);
2023-02-03 00:16:38 -06:00
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.asFieldCollectionType();
2023-02-03 00:16:38 -06:00
}
/**
* @method getFieldsByAppId - Gets a paged list of fields by an app id.
* @param {number} appId - The id of the app to get the fields for.
* @param {PagingRequest} pagingRequest - The paging request that will be used to get the fields.
* @returns {Promise<ApiResponse<GetPagedFieldsResponse>>} - A promise that resolves to an ApiResponse of type GetPagedFieldsResponse.
*/
2023-02-03 11:45:15 -06:00
public async getFieldsByAppId(
appId: number,
pagingRequest: PagingRequest = new PagingRequest(1, 50)
): Promise<ApiResponse<GetPagedFieldsResponse>> {
const endpoint = EndpointFactory.getFieldsByAppIdEndpoint(appId);
const params = { ...pagingRequest };
const apiResponse = await this.get<any>(endpoint, { params });
2023-02-03 11:45:15 -06:00
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.asGetPagedFieldsResponseType();
2023-02-03 11:45:15 -06:00
}
2023-02-04 15:09:13 -06:00
/**
* @method getFileInfoById - Gets a file's information by its id.
* @param {number} recordId - The id of the record that the file is attached to.
* @param {number} fieldId - The id of the field that the file is attached to.
* @param {number} fileId - The id of the file to get the information for.
* @returns {Promise<ApiResponse<FileInfo>>} - A promise that resolves to an ApiResponse of type FileInfo.
*/
public async getFileInfoById(
recordId: number,
fieldId: number,
fileId: number
): Promise<ApiResponse<FileInfo>> {
const endpoint = EndpointFactory.getFileInfoByIdEndpoint(
recordId,
fieldId,
fileId
);
const apiResponse = await this.get<any>(endpoint);
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.asFileInfoType();
}
2023-02-06 21:28:11 -06:00
/**
* @method getFileById - Gets a file by its id.
* @param {number} recordId - The id of the record that the file is attached to.
* @param {number} fieldId - The id of the field that the file is attached to.
* @param {number} fileId - The id of the file to get.
* @returns {Promise<ApiResponse<File>>} - A promise that resolves to an ApiResponse of type File.
*/
public async getFileById(
recordId: number,
fieldId: number,
fileId: number
): Promise<ApiResponse<File>> {
const endpoint = EndpointFactory.getFileByIdEndpoint(
recordId,
fieldId,
fileId
);
const response = await this._client.get(endpoint, {
responseType: 'stream',
});
const apiResponse = ApiResponseFactory.getApiResponse<any>(response);
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.asFileType(response);
}
2023-02-03 16:01:20 -06:00
/**
* @method saveFile - Saves a file to a record in Onspring.
* @param {SaveFileRequest} request - The request that will be used to save the file.
* @returns {Promise<ApiResponse<CreatedWithIdResponse>>} - A promise that resolves to an ApiResponse of type CreatedWithIdResponse.
*/
public async saveFile(
request: SaveFileRequest
): Promise<ApiResponse<CreatedWithIdResponse>> {
const endpoint = EndpointFactory.getSaveFileEndpoint();
const formData = request.asFormData();
2023-02-03 16:01:20 -06:00
const apiResponse = await this.post<any>(endpoint, formData, {
headers: { 'Content-Type': 'multipart/form-data' },
2023-02-03 16:01:20 -06:00
});
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.asCreatedWithIdResponseType();
2023-02-03 16:01:20 -06:00
}
2023-02-06 22:16:43 -06:00
public async deleteFileById(
recordId: number,
fieldId: number,
fileId: number
): Promise<ApiResponse<any>> {
const endpoint = EndpointFactory.getDeleteFileByIdEndpoint(
recordId,
fieldId,
fileId
);
const apiResponse = await this.delete<any>(endpoint);
return apiResponse;
}
2023-02-07 22:43:06 -06:00
/**
* @method addOrUpdateListItem - Adds or updates a list item depending on if an id is provided or not.
* @param {ListItemRequest} listItemRequest - The request that will be used to add or update the list item.
* @returns {Promise<ApiResponse<ListItemResponse>>} - A promise that resolves to an ApiResponse of type ListItemResponse.
*/
2023-02-07 11:52:46 -06:00
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();
}
2023-02-08 14:19:37 +00:00
/**
* @method deleteListItemById - Deletes a list item by its id.
* @param {number} listId - The id of the list that the list item belongs to.
* @param {string} itemId - The id of the list item to delete.
* @returns {Promise<ApiResponse<any>>} - A promise that resolves to an ApiResponse of type any.
*/
2023-02-07 23:00:55 -06:00
public async deleteListItemById(
listId: number,
itemId: string
): Promise<ApiResponse<any>> {
const endpoint = EndpointFactory.getDeleteListItemEndpoint(listId, itemId);
const apiResponse = await this.delete<any>(endpoint);
return apiResponse;
}
2023-02-11 00:16:33 -06:00
/**
* @method getRecordsByAppId - Gets records by an app id.
2023-02-11 00:16:33 -06:00
* @param {GetRecordsByAppIdRequest} request - The request that will be used to get the records.
* @returns {Promise<ApiResponse<GetPagedRecordsResponse>>} - A promise that resolves to an ApiResponse of type GetPagedRecordsResponse.
*/
public async getRecordsByAppId(
2023-02-11 00:16:33 -06:00
request: GetRecordsByAppIdRequest
): Promise<ApiResponse<GetPagedRecordsResponse>> {
const { appId, pagingRequest, fieldIds, dataFormat } = request;
const endpoint = EndpointFactory.getRecordsByAppIdEndpoint(appId);
const params = {
...pagingRequest,
fieldIds: fieldIds.join(','),
dataFormat,
};
const apiResponse = await this.get<any>(endpoint, { params });
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.asGetPagedRecordsResponseType();
}
2023-02-10 23:29:56 -06:00
/**
* @method getRecordById - Gets a record by its id.
* @param {GetRecordRequest} request - The request that will be used to get the record.
* @returns {Promise<ApiResponse<Record>>} - A promise that resolves to an ApiResponse of type Record.
*/
public async getRecordById(
request: GetRecordRequest
): Promise<ApiResponse<Record>> {
const { appId, recordId, fieldIds, dataFormat } = request;
const endpoint = EndpointFactory.getRecordByIdEndpoint(appId, recordId);
2023-02-11 00:16:33 -06:00
const params = { fieldIds: fieldIds.join(','), dataFormat };
const apiResponse = await this.get<any>(endpoint, { params });
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.asRecordType();
}
/**
* @method getRecordsByIds - Gets records by their ids.
* @param {GetRecordsRequest} request - The request that will be used to get the records.
* @returns {Promise<ApiResponse<CollectionResponse<Record>>>} - A promise that resolves to an ApiResponse of type CollectionResponse of type Record.
*/
public async getRecordsByIds(
request: GetRecordsRequest
): Promise<ApiResponse<CollectionResponse<Record>>> {
const endpoint = EndpointFactory.getRecordsByIdsEndpoint();
const apiResponse = await this.post<any>(endpoint, request);
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.asRecordCollectionType();
}
2023-02-12 19:11:42 -06:00
public async queryRecords(
request: QueryRecordsRequest
): Promise<ApiResponse<GetPagedRecordsResponse>> {
const endpoint = EndpointFactory.getQueryRecordsEndpoint();
const { pagingRequest, ...data } = request;
const params = { ...pagingRequest };
const apiResponse = await this.post<any>(endpoint, data, { params });
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.asGetPagedRecordsResponseType();
}
2023-02-08 23:42:19 -06:00
/**
* @method getReportsByAppId - Gets a paged list of reports by the app id.
* @param {number} appId - The id of the app to get the reports for.
* @param {PagingRequest} pagingRequest - The paging request that will be used to get the reports.
* @returns {Promise<ApiResponse<GetPagedReportsResponse>>} - A promise that resolves to an ApiResponse of type GetPagedReportsResponse.
*/
public async getReportsByAppId(
appId: number,
pagingRequest: PagingRequest = new PagingRequest(1, 50)
): Promise<ApiResponse<GetPagedReportsResponse>> {
const endpoint = EndpointFactory.getReportsByAppIdEndpoint(appId);
const params = { ...pagingRequest };
const apiResponse = await this.get<any>(endpoint, { params });
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.asGetPagedReportsResponseType();
}
/**
* @method getReportById - Gets a report by its id.
* @param {number} reportId - The id of the report to get.
* @param {DataFormat} apiDataFormat - The data format that the report data will be returned in.
* @param {ReportDataType} dataType - The type of data that will be returned.
* @returns {Promise<ApiResponse<ReportData>>} - A promise that resolves to an ApiResponse of type ReportData.
*/
2023-02-09 18:23:49 -06:00
public async getReportById(
reportId: number,
apiDataFormat: DataFormat = DataFormat.Raw,
dataType: ReportDataType = ReportDataType.ReportData
2023-02-09 18:23:49 -06:00
): Promise<ApiResponse<ReportData>> {
const endpoint = EndpointFactory.getReportByIdEndpoint(reportId);
const params = {
2023-02-09 18:23:49 -06:00
apiDataFormat,
dataType,
};
2023-02-09 18:23:49 -06:00
const apiResponse = await this.get<any>(endpoint, { params });
2023-02-09 18:23:49 -06:00
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.asReportDataType();
}
/**
* @method get - Makes a GET request to the specified endpoint.
* @param {string} endpoint - The endpoint that will be used to make the request.
2023-02-02 20:24:20 -06:00
* @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.
*/
2023-02-01 23:45:14 -06:00
private async get<T>(
endpoint: string,
config: AxiosRequestConfig = {}
): Promise<ApiResponse<T>> {
2023-02-01 23:50:20 -06:00
const response = await this._client.get(endpoint, config);
const apiResponse = ApiResponseFactory.getApiResponse<T>(response);
2023-02-01 23:45:14 -06:00
return apiResponse;
2023-01-27 11:28:45 -06:00
}
2023-02-02 20:24:20 -06:00
/**
2023-02-02 22:52:14 -06:00
* @method post - Makes a POST 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.
2023-02-02 20:24:20 -06:00
*/
private async post<T>(
endpoint: string,
data: any,
config: AxiosRequestConfig = {}
): Promise<ApiResponse<T>> {
const response = await this._client.post(endpoint, data, config);
const apiResponse = ApiResponseFactory.getApiResponse<T>(response);
return apiResponse;
}
2023-02-06 22:16:43 -06:00
2023-02-07 11:52:46 -06:00
/**
* @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.
*/
2023-02-06 22:16:43 -06:00
private async delete<T>(
endpoint: string,
config: AxiosRequestConfig = {}
): Promise<ApiResponse<T>> {
const response = await this._client.delete(endpoint, config);
const apiResponse = ApiResponseFactory.getApiResponse<T>(response);
return apiResponse;
}
}