feat: added sinon for stubbing in tests. fix: added jsdoc comments to enums. feat: begin working on implementing OnspringClient methods
This commit is contained in:
+20
-6
@@ -2,6 +2,8 @@ import { AxiosInstance } from 'axios';
|
||||
import axios from 'axios';
|
||||
import { ArgumentValidator } from './models/ArgumentValidator';
|
||||
import { EndpointFactory } from './models/EndpointFactory';
|
||||
import { ApiResponseFactory } from './models/ApiResponseFactory';
|
||||
import { ApiResponse } from './models/ApiResponse';
|
||||
|
||||
/**
|
||||
* @class OnspringClient - A client that can communicate with the Onspring API.
|
||||
@@ -41,18 +43,30 @@ export class OnspringClient {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
public async canConnect(): Promise<boolean> {
|
||||
const endpoint = EndpointFactory.getPingEndpoint(this._client.defaults.baseURL);
|
||||
const endpoint = EndpointFactory.getPingEndpoint(
|
||||
this._client.defaults.baseURL
|
||||
);
|
||||
try {
|
||||
var response = await this._client.get(endpoint);
|
||||
return response.status === 200;
|
||||
const response = await this.get<ApiResponse<boolean>>(endpoint);
|
||||
return response.isSuccessful;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async get<T>(endpoint: string): Promise<T> {
|
||||
const response = await this._client.get<T>(endpoint);
|
||||
return response.data;
|
||||
/**
|
||||
* @method get - Makes a GET request to the specified endpoint.
|
||||
* @param {string} endpoint - The endpoint that will be used to make the request.
|
||||
* @returns {Promise<ApiResponse<T>>} - A promise that resolves to an ApiResponse of type T.
|
||||
*/
|
||||
private async get<T>(endpoint: string): Promise<ApiResponse<T>> {
|
||||
const response = await this._client.get(endpoint);
|
||||
const apiResponse = ApiResponseFactory.getApiResponse<T>(response);
|
||||
return apiResponse;
|
||||
}
|
||||
}
|
||||
|
||||
+13
-1
@@ -1,4 +1,16 @@
|
||||
/**
|
||||
* @enum DataFormat - The format of the data to be returned.
|
||||
*/
|
||||
export enum DataFormat {
|
||||
/**
|
||||
* @constant Raw - The raw data will be returned.
|
||||
* @type {string}
|
||||
*/
|
||||
Raw = 'Raw',
|
||||
|
||||
/**
|
||||
* @constant Formatted - The formatted data will be returned.
|
||||
* @type {string}
|
||||
*/
|
||||
Formatted = 'Formatted',
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,22 @@
|
||||
/**
|
||||
* @enum FieldStatus - The status of a field.
|
||||
*/
|
||||
export enum FieldStatus {
|
||||
/**
|
||||
* @constant Enabled - The field is enabled.
|
||||
* @type {string}
|
||||
*/
|
||||
Enabled = 'Enabled',
|
||||
|
||||
/**
|
||||
* @constant Disabled - The field is disabled.
|
||||
* @type {string}
|
||||
*/
|
||||
Disabled = 'Disabled',
|
||||
|
||||
/**
|
||||
* @constant Invalid - The field is invalid.
|
||||
* @type {string}
|
||||
*/
|
||||
Invalid = 'Invalid',
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,88 @@
|
||||
/**
|
||||
* @enum FieldType - The type of field.
|
||||
*/
|
||||
export enum FieldType {
|
||||
/**
|
||||
* @constant Text - The field is a text field.
|
||||
* @type {string}
|
||||
*/
|
||||
Text = 'Text',
|
||||
|
||||
/**
|
||||
* @constant Number - The field is a number field.
|
||||
* @type {string}
|
||||
*/
|
||||
Number = 'Number',
|
||||
|
||||
/**
|
||||
* @constant AutoNumber - The field is a autonumber field.
|
||||
* @type {string}
|
||||
*/
|
||||
AutoNumber = 'AutoNumber',
|
||||
|
||||
/**
|
||||
* @constant Date - The field is a date field.
|
||||
* @type {string}
|
||||
*/
|
||||
Date = 'Date',
|
||||
|
||||
/**
|
||||
* @constant TimeSpan - The field is a timespan field.
|
||||
* @type {string}
|
||||
*/
|
||||
TimeSpan = 'TimeSpan',
|
||||
|
||||
/**
|
||||
* @constant List - The field is a list field.
|
||||
* @type {string}
|
||||
*/
|
||||
List = 'List',
|
||||
|
||||
/**
|
||||
* @constant Reference - The field is a reference field.
|
||||
* @type {string}
|
||||
*/
|
||||
Reference = 'Reference',
|
||||
|
||||
/**
|
||||
* @constant SurveyReference - The field is a survey reference field.
|
||||
* @type {string}
|
||||
*/
|
||||
SurveyReference = 'SurveyReference',
|
||||
|
||||
/**
|
||||
* @constant SurveyGroupScoring - The field is a survey group scoring field.
|
||||
* @type {string}
|
||||
*/
|
||||
SurveyGroupScoring = 'SurveyGroupScoring',
|
||||
|
||||
/**
|
||||
* @constant SurveyCampaign - The field is a survey campaign field.
|
||||
* @type {string}
|
||||
*/
|
||||
SurveyCampaign = 'SurveyCampaign',
|
||||
|
||||
/**
|
||||
* @constant SurveyUnifiedAnswer - The field is a survey unified answer field.
|
||||
* @type {string}
|
||||
*/
|
||||
SurveyUnifiedAnswer = 'SurveyUnifiedAnswer',
|
||||
|
||||
/**
|
||||
* @constant Attachment - The field is a attachment field.
|
||||
* @type {string}
|
||||
*/
|
||||
Attachment = 'Attachment',
|
||||
|
||||
/**
|
||||
* @constant Image - The field is a image field.
|
||||
* @type {string}
|
||||
*/
|
||||
Image = 'Image',
|
||||
|
||||
/**
|
||||
* @constant Formula - The field is a formula field.
|
||||
* @type {string}
|
||||
*/
|
||||
Formula = 'Formula',
|
||||
}
|
||||
|
||||
@@ -1,5 +1,19 @@
|
||||
export enum FileStorageSite {
|
||||
/**
|
||||
* @constant Internal - The file is stored in the internal file storage.
|
||||
* @type {string}
|
||||
*/
|
||||
Internal = 'Internal',
|
||||
|
||||
/**
|
||||
* @constant OneDrive - The file is stored in OneDrive.
|
||||
* @type {string}
|
||||
*/
|
||||
OneDrive = 'OneDrive',
|
||||
|
||||
/**
|
||||
* @constant GoogleDrive - The file is stored in Google Drive.
|
||||
* @type {string}
|
||||
*/
|
||||
GoogleDrive = 'GoogleDrive',
|
||||
}
|
||||
|
||||
@@ -1,6 +1,25 @@
|
||||
export enum FormulaOutputType {
|
||||
/**
|
||||
* @constant Text - The formula output is a text value.
|
||||
* @type {string}
|
||||
*/
|
||||
Text = 'Text',
|
||||
|
||||
/**
|
||||
* @constant Numeric - The formula output is a numeric value.
|
||||
* @type {string}
|
||||
*/
|
||||
Numeric = 'Numeric',
|
||||
|
||||
/**
|
||||
* @constant DateAndTime - The formula output is a date and time value.
|
||||
* @type {string}
|
||||
*/
|
||||
DateAndTime = 'DateAndTime',
|
||||
|
||||
/**
|
||||
* @constant ListValue - The formula output is a list value.
|
||||
* @type {string}
|
||||
*/
|
||||
ListValue = 'ListValue',
|
||||
}
|
||||
|
||||
@@ -1,17 +1,94 @@
|
||||
/**
|
||||
* @enum HttpStatusCode - Enum for HTTP status codes
|
||||
*/
|
||||
export enum HttpStatusCode {
|
||||
/**
|
||||
* @constant OK - The request was successful.
|
||||
* @type {number}
|
||||
*/
|
||||
OK = 200,
|
||||
|
||||
/**
|
||||
* @constant Created - The request was successful and a new resource was created.
|
||||
* @type {number}
|
||||
*/
|
||||
Created = 201,
|
||||
|
||||
/**
|
||||
* @constant Accepted - The request was accepted for processing.
|
||||
* @type {number}
|
||||
*/
|
||||
Accepted = 202,
|
||||
|
||||
/**
|
||||
* @constant NoContent - The request was successful but there is no content to return.
|
||||
* @type {number}
|
||||
*/
|
||||
NoContent = 204,
|
||||
|
||||
/**
|
||||
* @constant BadRequest - The request was invalid.
|
||||
* @type {number}
|
||||
*/
|
||||
BadRequest = 400,
|
||||
|
||||
/**
|
||||
* @constant Unauthorized - The request did not include an authentication token.
|
||||
* @type {number}
|
||||
*/
|
||||
Unauthorized = 401,
|
||||
|
||||
/**
|
||||
* @constant Forbidden - The request included an authentication token but the token was not valid.
|
||||
* @type {number}
|
||||
*/
|
||||
Forbidden = 403,
|
||||
|
||||
/**
|
||||
* @constant NotFound - The requested resource does not exist.
|
||||
* @type {number}
|
||||
*/
|
||||
NotFound = 404,
|
||||
|
||||
/**
|
||||
* @constant MethodNotAllowed - The requested method is not supported for the resource.
|
||||
* @type {number}
|
||||
*/
|
||||
MethodNotAllowed = 405,
|
||||
|
||||
/**
|
||||
* @constant Conflict - The request could not be completed due to a conflict with the current state of the resource.
|
||||
* @type {number}
|
||||
*/
|
||||
Conflict = 409,
|
||||
|
||||
/**
|
||||
* @constant InternalServerError - An unexpected error occurred on the server.
|
||||
* @type {number}
|
||||
*/
|
||||
InternalServerError = 500,
|
||||
|
||||
/**
|
||||
* @constant NotImplemented - The requested method is not implemented.
|
||||
* @type {number}
|
||||
*/
|
||||
NotImplemented = 501,
|
||||
|
||||
/**
|
||||
* @constant BadGateway - The server received an invalid response from an upstream server.
|
||||
* @type {number}
|
||||
*/
|
||||
BadGateway = 502,
|
||||
|
||||
/**
|
||||
* @constant ServiceUnavailable - The server is currently unavailable.
|
||||
* @type {number}
|
||||
*/
|
||||
ServiceUnavailable = 503,
|
||||
|
||||
/**
|
||||
* @constant GatewayTimeout - The server did not receive a response from an upstream server in a timely manner.
|
||||
* @type {number}
|
||||
*/
|
||||
GatewayTimeout = 504,
|
||||
}
|
||||
|
||||
@@ -1,4 +1,16 @@
|
||||
/**
|
||||
* @enum Multiplicity - The multiplicity type of the object.
|
||||
*/
|
||||
export enum Multiplicity {
|
||||
/**
|
||||
* @constant SingleSelect - Single select multiplicity.
|
||||
* @type {string}
|
||||
*/
|
||||
SingleSelect = 'SingleSelect',
|
||||
|
||||
/**
|
||||
* @constant MultiSelect - Multi select multiplicity.
|
||||
* @type {string}
|
||||
*/
|
||||
MultiSelect = 'MultiSelect',
|
||||
}
|
||||
|
||||
@@ -1,4 +1,15 @@
|
||||
/**
|
||||
* @enum ReportDataType - Enum for report data type
|
||||
*/
|
||||
export enum ReportDataType {
|
||||
/**
|
||||
* @constant ReportData - Report data
|
||||
* @type {string}
|
||||
*/
|
||||
ReportData = 'ReportData',
|
||||
/**
|
||||
* @constant ChartData - Chart data
|
||||
* @type {string}
|
||||
*/
|
||||
ChartData = 'ChartData',
|
||||
}
|
||||
|
||||
@@ -1,14 +1,75 @@
|
||||
/**
|
||||
* @enum ResultValueType - The type of the result value.
|
||||
*/
|
||||
export enum ResultValueType {
|
||||
/**
|
||||
* @constant String - The result value is a string.
|
||||
* @type {string}
|
||||
*/
|
||||
String = 'String',
|
||||
|
||||
/**
|
||||
* @constant Integer - The result value is an integer.
|
||||
* @type {string}
|
||||
*/
|
||||
Integer = 'Integer',
|
||||
|
||||
/**
|
||||
* @constant Decimal - The result value is a decimal.
|
||||
* @type {string}
|
||||
*/
|
||||
Decimal = 'Decimal',
|
||||
|
||||
/**
|
||||
* @constant Date - The result value is a Date.
|
||||
* @type {string}
|
||||
*/
|
||||
Date = 'Date',
|
||||
|
||||
/**
|
||||
* @constant TimeSpan - The result value is a TimeSpan.
|
||||
* @type {string}
|
||||
*/
|
||||
TimeSpan = 'TimeSpan',
|
||||
|
||||
/**
|
||||
* @constant Guid - The result value is a Guid.
|
||||
*/
|
||||
Guid = 'Guid',
|
||||
|
||||
/**
|
||||
* @constant StringList - The result value is a list of strings.
|
||||
* @type {string}
|
||||
*/
|
||||
StringList = 'StringList',
|
||||
|
||||
/**
|
||||
* @constant IntegerList - The result value is a list of integers.
|
||||
* @type {string}
|
||||
*/
|
||||
IntegerList = 'IntegerList',
|
||||
|
||||
/**
|
||||
* @constant GuidList - The result value is a list of Guids.
|
||||
* @type {string}
|
||||
*/
|
||||
GuidList = 'GuidList',
|
||||
|
||||
/**
|
||||
* @constant AttachmentList - The result value is a list of attachments.
|
||||
* @type {string}
|
||||
*/
|
||||
AttachmentList = 'AttachmentList',
|
||||
|
||||
/**
|
||||
* @constant ScoringGroupList - The result value is a list of scoring groups.
|
||||
* @type {string}
|
||||
*/
|
||||
ScoringGroupList = 'ScoringGroupList',
|
||||
|
||||
/**
|
||||
* @constant FileList - The result value is a list of files.
|
||||
* @type {string}
|
||||
*/
|
||||
FileList = 'FileList',
|
||||
}
|
||||
|
||||
@@ -1,9 +1,46 @@
|
||||
/**
|
||||
* @enum TimeSpanIncrement - The increment values for a time span field.
|
||||
*/
|
||||
export enum TimeSpanIncrement {
|
||||
/**
|
||||
* @constant Seconds - The time span increment is seconds.
|
||||
* @type {string}
|
||||
*/
|
||||
Seconds = 'Seconds',
|
||||
|
||||
/**
|
||||
* @constant Minutes - The time span increment is minutes.
|
||||
* @type {string}
|
||||
*/
|
||||
Minutes = 'Minutes',
|
||||
|
||||
/**
|
||||
* @constant Hours - The time span increment is hours.
|
||||
* @type {string}
|
||||
*/
|
||||
Hours = 'Hours',
|
||||
|
||||
/**
|
||||
* @constant Days - The time span increment is days.
|
||||
* @type {string}
|
||||
*/
|
||||
Days = 'Days',
|
||||
|
||||
/**
|
||||
* @constant Weeks - The time span increment is weeks.
|
||||
* @type {string}
|
||||
*/
|
||||
Weeks = 'Weeks',
|
||||
|
||||
/**
|
||||
* @constant Months - The time span increment is months.
|
||||
* @type {string}
|
||||
*/
|
||||
Months = 'Months',
|
||||
|
||||
/**
|
||||
* @constant Years - The time span increment is years.
|
||||
* @type {string}
|
||||
*/
|
||||
Years = 'Years',
|
||||
}
|
||||
|
||||
@@ -1,5 +1,23 @@
|
||||
/**
|
||||
* @enum TimeSpanRecurrenceType - The type of recurrence for a time span.
|
||||
* @type {string}
|
||||
*/
|
||||
export enum TimeSpanRecurrenceType {
|
||||
/**
|
||||
* @constant None - The time span does not recur.
|
||||
* @type {string}
|
||||
*/
|
||||
None = 'None',
|
||||
|
||||
/**
|
||||
* @constant EndByDate - The time span recurs until a specific date.
|
||||
* @type {string}
|
||||
*/
|
||||
EndByDate = 'EndByDate',
|
||||
|
||||
/**
|
||||
* @constant EndAfterOccurrences - The time span recurs for a specific number of occurrences.
|
||||
* @type {string}
|
||||
*/
|
||||
EndAfterOccurrences = 'EndAfterOccurrences',
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import { AxiosResponse } from "axios";
|
||||
import { HttpStatusCode } from "../enums/HttpStatusCode";
|
||||
import { ApiResponse } from "./ApiResponse";
|
||||
|
||||
/**
|
||||
* @class ApiResponseFactory - Factory class for creating ApiResponse objects
|
||||
*/
|
||||
export class ApiResponseFactory {
|
||||
/**
|
||||
* @method getApiResponse - Creates an ApiResponse object from an AxiosResponse object
|
||||
* @param {AxiosResponse} response - The AxiosResponse object that will be used to create the ApiResponse object
|
||||
* @returns {ApiResponse<T>} - An ApiResponse object of type T
|
||||
*/
|
||||
public static getApiResponse<T>(response: AxiosResponse): ApiResponse<T> {
|
||||
const message = this.TryToGetMessage(response);
|
||||
|
||||
if (this.isSuccessStatusCode(response.status) === true)
|
||||
{
|
||||
const data = response.data as T;
|
||||
return new ApiResponse<T>(response.status, message, data);
|
||||
}
|
||||
|
||||
return new ApiResponse<T>(response.status, message, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @method TryToGetMessage - Attempts to get the message from the response data
|
||||
* @param {AxiosResponse} response - The AxiosResponse object that will be used to get the message
|
||||
* @returns {string} - The message from the response data
|
||||
*/
|
||||
private static TryToGetMessage(response: AxiosResponse): string {
|
||||
|
||||
if (
|
||||
response.status == HttpStatusCode.Unauthorized ||
|
||||
response.status == HttpStatusCode.Forbidden ||
|
||||
response.status == HttpStatusCode.NotFound
|
||||
)
|
||||
{
|
||||
return response.data?.message;
|
||||
}
|
||||
|
||||
if (this.isSuccessStatusCode(response.status) === false)
|
||||
{
|
||||
return JSON.stringify(response.data);
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* @method isSuccessStatusCode - Determines if the specified status code is a success status code
|
||||
* @param {number} statusCode - The status code that will be used to determine if it is a success status code
|
||||
* @returns {boolean} - A boolean indicating if the specified status code is a success status code
|
||||
*/
|
||||
private static isSuccessStatusCode(statusCode: number): boolean {
|
||||
return statusCode >= HttpStatusCode.OK && statusCode <= 299;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user