fix: added dependencies for development. feat: added axios as dependency. feat: added some initial modeals. feat: setup unit testing
This commit is contained in:
Generated
+2213
File diff suppressed because it is too large
Load Diff
+14
-2
@@ -5,8 +5,20 @@
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"scripts": {
|
||||
"test": ""
|
||||
"build": "npm test && tsc",
|
||||
"test": "mocha -r ts-node/register ./tests/**/*.spec.ts"
|
||||
},
|
||||
"author": "Stevan Freeborn",
|
||||
"license": "MIT"
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/chai": "^4.3.4",
|
||||
"@types/mocha": "^10.0.1",
|
||||
"chai": "^4.3.7",
|
||||
"mocha": "^10.2.0",
|
||||
"ts-node": "^10.9.1",
|
||||
"typescript": "^4.9.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^1.2.4"
|
||||
}
|
||||
}
|
||||
|
||||
+22
-6
@@ -1,9 +1,25 @@
|
||||
class OnspringClient {
|
||||
private readonly _baseUrl: string;
|
||||
private readonly _apiKey: string;
|
||||
import { AxiosInstance } from "axios";
|
||||
import axios from "axios";
|
||||
import { ArgumentValidator } from "./models/ArgumentValidator";
|
||||
|
||||
constructor(baseUrl: string, apiKey: string) {
|
||||
this._baseUrl = baseUrl;
|
||||
this._apiKey = apiKey;
|
||||
export class OnspringClient {
|
||||
protected readonly client: AxiosInstance;
|
||||
|
||||
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.");
|
||||
}
|
||||
|
||||
if (ArgumentValidator.isNullOrWhiteSpace(apiKey)) {
|
||||
throw new Error("apiKey cannot be null/empty/whitespace.");
|
||||
}
|
||||
|
||||
this.client = axios.create({
|
||||
baseURL: baseUrl,
|
||||
headers: {
|
||||
"x-apikey": apiKey,
|
||||
"x-api-version": "2",
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
export class ApiResponse<T> {
|
||||
readonly statusCode: number;
|
||||
readonly isSuccessful: boolean;
|
||||
readonly message: string;
|
||||
readonly data: T;
|
||||
|
||||
constructor(statusCode: number, isSuccessful: boolean, message: string, data: T) {
|
||||
this.statusCode = statusCode;
|
||||
this.isSuccessful = statusCode < 400;
|
||||
this.message = message;
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class App {
|
||||
href: string;
|
||||
id: number;
|
||||
name: string;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
export class ArgumentValidator {
|
||||
public static isNullOrWhiteSpace(value: string | null | undefined): boolean {
|
||||
return value === null || value === undefined || (/^\s*$/).test(value);
|
||||
}
|
||||
|
||||
public static isValidUrl(value: string | null | undefined): boolean {
|
||||
let url: URL;
|
||||
|
||||
try {
|
||||
url = new URL(value);
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return url.protocol === 'http:' || url.protocol === 'https:';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
export class PagingRequest {
|
||||
pageNumber: number;
|
||||
pageSize: number;
|
||||
|
||||
constructor(pageNumber: number, pageSize: number) {
|
||||
this.pageNumber = pageNumber;
|
||||
this.pageSize = pageSize;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
import { ArgumentValidator } from '../src/models/ArgumentValidator';
|
||||
import { expect } from 'chai';
|
||||
|
||||
describe('ArgumentValidator', function () {
|
||||
describe('isNullOrWhiteSpace', function () {
|
||||
it('should be defined', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace).to.not.be.undefined;
|
||||
});
|
||||
|
||||
it('should have 1 parameter of type string', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace).to.have.lengthOf(1);
|
||||
});
|
||||
|
||||
it('should return true when the value is null', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace(null)).to.be.true;
|
||||
});
|
||||
|
||||
it('should return true when the value is undefined', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace(undefined)).to.be.true;
|
||||
});
|
||||
|
||||
it('should return true when the value is an empty string', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace('')).to.be.true;
|
||||
});
|
||||
|
||||
it('should return true when the value is a string with only spaces', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace(' ')).to.be.true;
|
||||
});
|
||||
|
||||
it('should return true when the value is a string with only tabs', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace('\t')).to.be.true;
|
||||
});
|
||||
|
||||
it('should return true when the value is a string with only newlines', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace('\n')).to.be.true;
|
||||
});
|
||||
|
||||
it('should return true when the value is a string with only spaces, tabs, and newlines', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace(' \t\n')).to.be.true;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with a character', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace('a')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with a number', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace('1')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with a symbol', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace('@')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with a character and spaces', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace('a ')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with a number and spaces', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace('1 ')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with a symbol and spaces', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace('@ ')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with a character and tabs', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace('a\t')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with a number and tabs', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace('1\t')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with a symbol and tabs', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace('@\t')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with a character and newlines', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace('a\n')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with a number and newlines', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace('1\n')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with a symbol and newlines', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace('@\n')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with a character and spaces, tabs, and newlines', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace('a \t \n')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with a number and spaces, tabs, and newlines', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace('1 \t \n')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with a symbol and spaces, tabs, and newlines', function () {
|
||||
expect(ArgumentValidator.isNullOrWhiteSpace('@ \t \n')).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('isValidUrl', function () {
|
||||
it('should be defined', function () {
|
||||
expect(ArgumentValidator.isValidUrl).to.not.be.undefined;
|
||||
});
|
||||
|
||||
it('should have 1 parameter of type string', function () {
|
||||
expect(ArgumentValidator.isValidUrl).to.have.lengthOf(1);
|
||||
});
|
||||
|
||||
it('should return false when the value is null', function () {
|
||||
expect(ArgumentValidator.isValidUrl(null)).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is undefined', function () {
|
||||
expect(ArgumentValidator.isValidUrl(undefined)).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is an empty string', function () {
|
||||
expect(ArgumentValidator.isValidUrl('')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with only spaces', function () {
|
||||
expect(ArgumentValidator.isValidUrl(' ')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with only tabs', function () {
|
||||
expect(ArgumentValidator.isValidUrl('\t')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with only newlines', function () {
|
||||
expect(ArgumentValidator.isValidUrl('\n')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with only spaces, tabs, and newlines', function () {
|
||||
expect(ArgumentValidator.isValidUrl(' \t \n')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with a character', function () {
|
||||
expect(ArgumentValidator.isValidUrl('a')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with a number', function () {
|
||||
expect(ArgumentValidator.isValidUrl('1')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when the value is a string with a symbol', function () {
|
||||
expect(ArgumentValidator.isValidUrl('@')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when invalid url is passed', function () {
|
||||
expect(ArgumentValidator.isValidUrl('www.google.com')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when invalid url is passed', function () {
|
||||
expect(ArgumentValidator.isValidUrl('ht://www.google')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false when url with non http or https protocol is passed', function () {
|
||||
expect(ArgumentValidator.isValidUrl('ftp://www.google.com')).to.be.false;
|
||||
});
|
||||
|
||||
it('should return true when the value is a string with a valid http url', function () {
|
||||
expect(ArgumentValidator.isValidUrl('http://www.google.com')).to.be.true;
|
||||
});
|
||||
|
||||
it('should return true when the value is a string with a valid https url', function () {
|
||||
expect(ArgumentValidator.isValidUrl('https://www.google.com')).to.be.true;
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,87 @@
|
||||
import { OnspringClient } from '../src/OnspringClient';
|
||||
import { expect } from 'chai';
|
||||
|
||||
describe('OnspringClient Tests', function () {
|
||||
it('should be defined', function () {
|
||||
expect(OnspringClient).to.not.be.undefined;
|
||||
});
|
||||
|
||||
it('should have a constructor', function () {
|
||||
expect(OnspringClient).to.have.property('constructor');
|
||||
});
|
||||
|
||||
it('should have 2 parameters', function () {
|
||||
expect(OnspringClient).to.have.lengthOf(2);
|
||||
});
|
||||
|
||||
it('should throw an error when the baseUrl is null', function () {
|
||||
expect(() => new OnspringClient(null, 'apiKey')).to.throw(
|
||||
'baseUrl must be an absolute and well-formed URI.'
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw an error when the baseUrl is undefined', function () {
|
||||
expect(() => new OnspringClient(undefined, 'apiKey')).to.throw(
|
||||
'baseUrl must be an absolute and well-formed URI.'
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw an error when the baseUrl is an empty string', function () {
|
||||
expect(() => new OnspringClient('', 'apiKey')).to.throw(
|
||||
'baseUrl must be an absolute and well-formed URI.'
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw an error when the baseUrl is a string with only spaces', function () {
|
||||
expect(() => new OnspringClient(' ', 'apiKey')).to.throw(
|
||||
'baseUrl must be an absolute and well-formed URI.'
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw an error when the baseUrl is not a valid url', function () {
|
||||
expect(() => new OnspringClient('api.onspring.com', 'apiKey')).to.throw(
|
||||
'baseUrl must be an absolute and well-formed URI.'
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw an error when the apiKey is null', function () {
|
||||
expect(() => new OnspringClient('https://api.onspring.com', null)).to.throw(
|
||||
'apiKey cannot be null/empty/whitespace.'
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw an error when the apiKey is undefined', function () {
|
||||
expect(() => new OnspringClient('https://api.onspring.com', undefined)).to.throw(
|
||||
'apiKey cannot be null/empty/whitespace.'
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw an error when the apiKey is an empty string', function () {
|
||||
expect(() => new OnspringClient('https://api.onspring.com', '')).to.throw(
|
||||
'apiKey cannot be null/empty/whitespace.'
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw an error when the apiKey is a string with only spaces', function () {
|
||||
expect(() => new OnspringClient('https://api.onspring.com', ' ')).to.throw(
|
||||
'apiKey cannot be null/empty/whitespace.'
|
||||
);
|
||||
});
|
||||
|
||||
it('should not throw an error when the baseUrl is a valid url', function () {
|
||||
expect(() => new OnspringClient('https://api.onspring.com', 'apiKey')).to.not.throw();
|
||||
});
|
||||
|
||||
it('should not throw an error when the baseUrl is a valid url', function () {
|
||||
expect(() => new OnspringClient('http://api.onspring.com', 'apiKey')).to.not.throw();
|
||||
});
|
||||
|
||||
it('should not throw an error when the apiKey is a valid string', function () {
|
||||
expect(() => new OnspringClient('https://api.onspring.com', 'apiKey')).to.not.throw();
|
||||
});
|
||||
|
||||
it('should have a client property', function () {
|
||||
const client = new OnspringClient('https://api.onspring.com', 'apiKey');
|
||||
expect(client).to.have.property('client');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user