feat: added nyc for test coverage reporting. fix: add jsdoc comments

This commit is contained in:
StevanFreeborn
2023-01-26 13:58:25 -06:00
parent b36d9445bd
commit 8b6e5ccefc
10 changed files with 2993 additions and 27 deletions
+19
View File
@@ -0,0 +1,19 @@
{
"extends": "@istanbuljs/nyc-config-typescript",
"check-coverage": true,
"all": true,
"include": [
"src/**/*.ts"
],
"exclude": [
"src/index.ts",
"**/*.spec.ts"
],
"reporter": [
"html",
"lcov",
"text",
"text-summary"
],
"report-dir": "coverage"
}
+2801
View File
File diff suppressed because it is too large Load Diff
+4 -1
View File
@@ -7,15 +7,18 @@
"scripts": {
"build": "npm tests && tsc",
"tests": "mocha -r ts-node/register ./tests/**/*.spec.ts",
"test": "mocha -r ts-node/register"
"test": "mocha -r ts-node/register",
"test-coverage": "nyc npm run tests"
},
"author": "Stevan Freeborn",
"license": "MIT",
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@types/chai": "^4.3.4",
"@types/mocha": "^10.0.1",
"chai": "^4.3.7",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
},
+25 -8
View File
@@ -1,24 +1,41 @@
import { AxiosInstance } from "axios";
import axios from "axios";
import { ArgumentValidator } from "./models/ArgumentValidator";
import { AxiosInstance } from 'axios';
import axios from 'axios';
import { ArgumentValidator } from './models/ArgumentValidator';
/**
* @class OnspringClient - A client that can communicate with the Onspring API.
*/
export class OnspringClient {
/**
* @property {AxiosInstance} client - The axios instance that will be used to make requests to the Onspring API.
*/
protected readonly client: AxiosInstance;
constructor(baseUrl: string | undefined | null, apiKey: string | undefined | null) {
/**
* @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) {
throw new Error("baseUrl must be an absolute and well-formed URI.");
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.");
throw new Error('apiKey cannot be null/empty/whitespace.');
}
this.client = axios.create({
baseURL: baseUrl,
headers: {
"x-apikey": apiKey,
"x-api-version": "2",
'x-apikey': apiKey,
'x-api-version': '2',
},
});
}
+26 -1
View File
@@ -1,10 +1,35 @@
/**
* @class ApiResponse - A generic response object for API requests.
*/
export class ApiResponse<T> {
/**
* @property {number} statusCode - The status code of the response.
*/
readonly statusCode: number;
/**
* @property {boolean} isSuccessful - True if the status code is less than 400; otherwise, false.
*/
readonly isSuccessful: boolean;
/**
* @property {string} message - The message of the response.
*/
readonly message: string;
/**
* @property {T} data - The data of the response.
*/
readonly data: T;
constructor(statusCode: number, isSuccessful: boolean, message: string, data: T) {
/**
* @constructor - Creates a new instance of the ApiResponse class.
* @param {number} statusCode - The status code of the response.
* @param {string} message - The message of the response.
* @param {T} data - The data of the response.
* @returns {ApiResponse<T>} - A new instance of the ApiResponse class.
*/
constructor(statusCode: number, message: string, data: T) {
this.statusCode = statusCode;
this.isSuccessful = statusCode < 400;
this.message = message;
+14
View File
@@ -1,5 +1,19 @@
/**
* @class App - Model for the App object
*/
class App {
/**
* @property {string} href - The URL to the app
*/
href: string;
/**
* @property {number} id - The id of the app
*/
id: number;
/**
* @property {string} name - The name of the app
*/
name: string;
}
+30 -1
View File
@@ -1,8 +1,19 @@
/**
* @class ArgumentValidator - A class that validates arguments.
*/
export class ArgumentValidator {
/**
* @param {string} value - The value to validate.
* @returns {boolean} - True if the value is null, undefined, or a string that contains only whitespace characters; otherwise, false.
*/
public static isNullOrWhiteSpace(value: string | null | undefined): boolean {
return value === null || value === undefined || (/^\s*$/).test(value);
return value === null || value === undefined || /^\s*$/.test(value);
}
/**
* @param {string} value - The value to validate.
* @returns {boolean} - True if the value is a valid URL; otherwise, false.
*/
public static isValidUrl(value: string | null | undefined): boolean {
let url: URL;
@@ -14,4 +25,22 @@ export class ArgumentValidator {
return url.protocol === 'http:' || url.protocol === 'https:';
}
/**
* @param {number} value - The value to validate.
* @returns {boolean} - True if the value is a valid page size; otherwise, false.
* @remarks - A valid page size is a number greater than 0 and less than or equal to 1000.
*/
public static isValidPageSize(value: number): boolean {
return value > 0 && value <= 1000;
}
/**
* @param {number} value - The value to validate.
* @returns {boolean} - True if the value is a valid page number; otherwise, false.
* @remarks - A valid page number is a number greater than 0.
*/
public static isValidPageNumber(value: number): boolean {
return value > 0;
}
}
+29
View File
@@ -1,24 +1,53 @@
/**
* @class EndpointFactory - A factory class for creating endpoints.
*/
export class EndpointFactory {
/**
* @param {string} baseUrl - The base url that will be used to create the ping endpoint.
* @returns {string} - The ping endpoint.
*/
public static getPingEndpoint(baseUrl: string): string {
return `${baseUrl}/Ping`;
}
/**
* @param {string} baseUrl - The base url that will be used to create the apps endpoint.
* @returns {string} - The apps endpoint.
*/
public static getAppsEndpoint(baseUrl: string): string {
return `${baseUrl}/Apps`;
}
/**
* @param {string} baseUrl - The base url that will be used to create the app by id endpoint.
* @param {number} id - The id of the app.
* @returns {string} - The app by id endpoint.
*/
public static getAppByIdEndpoint(baseUrl: string, id: number): string {
return `${baseUrl}/Apps/id/${id}`;
}
/**
* @param {string} baseUrl - The base url that will be used to create the apps by ids endpoint.
* @returns {string} - The apps by ids endpoint.
*/
public static getAppsByIdsEndpoint(baseUrl: string): string {
return `${baseUrl}/Apps/batch-get`;
}
/**
* @param {string} baseUrl - The base url that will be used to create the field by id endpoint.
* @param {number} id - The id of the field.
* @returns {string} - The field by id endpoint.
*/
public static getFieldByIdEndpoint(baseUrl: string, id: number): string {
return `${baseUrl}/Fields/id/${id}`;
}
/**
* @param {string} baseUrl - The base url that will be used to create the fields by ids endpoint.
* @returns {string} - The fields by ids endpoint.
*/
public static getFieldsByIdsEndpoint(baseUrl: string): string {
return `${baseUrl}/Fields/batch-get`;
}
+29
View File
@@ -1,8 +1,37 @@
import { ArgumentValidator } from "./ArgumentValidator";
/**
* @class PagingRequest - Paging request model
*/
export class PagingRequest {
/**
* @property {number} pageNumber - The page number of the request.
*/
pageNumber: number;
/**
* @property {number} pageSize - The page size of the request.
*/
pageSize: number;
/**
* @constructor - Creates a new instance of the PagingRequest class.
* @param {number} pageNumber - The page number of the request. Must be greater than 0.
* @param {number} pageSize - The page size of the request. Must be greater than 0 and less than 1001.
* @returns {PagingRequest} - A new instance of the PagingRequest class.
* @throws {Error} - Thrown when the pageNumber is less than 1.
* @throws {Error} - Thrown when the pageSize is less than 1.
* @throws {Error} - Thrown when the pageSize is greater than 1000.
*/
constructor(pageNumber: number, pageSize: number) {
if (ArgumentValidator.isValidPageNumber(pageNumber) === false) {
throw new Error("pageNumber must be greater than 0.");
}
if (ArgumentValidator.isValidPageSize(pageSize) === false) {
throw new Error("pageSize must be greater than 0 and less than 1001.");
}
this.pageNumber = pageNumber;
this.pageSize = pageSize;
}
+1 -1
View File
@@ -1,4 +1,4 @@
import { EndpointFactory } from '../src/Models/EndpointFactory';
import { EndpointFactory } from '../src/models/EndpointFactory';
import { expect } from 'chai';
describe('EndpointFactory', function () {