fix: add eslint and configure it

This commit is contained in:
StevanFreeborn
2023-02-03 00:16:38 -06:00
parent 54b0668e4a
commit bd55381dc9
16 changed files with 4259 additions and 70 deletions
+5
View File
@@ -0,0 +1,5 @@
#
.nyc_output/
coverage/
dist/
node_modules/
+34
View File
@@ -0,0 +1,34 @@
{
"env": {
"browser": true,
"es2021": true
},
"plugins": ["@typescript-eslint", "mocha"],
"extends": [
"standard-with-typescript",
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:mocha/recommended",
"prettier"
],
"overrides": [
{
"files": ["tests/**/*.spec.ts"],
"rules": {
"@typescript-eslint/no-unused-expressions": "off",
"@typescript-eslint/consistent-type-assertions": "off"
}
}
],
"parserOptions": {
"project": "./tsconfig.eslint.json",
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unnecessary-boolean-literal-compare": "off",
"@typescript-eslint/no-useless-constructor": "off",
"@typescript-eslint/no-extraneous-class": "off"
}
}
+4132 -7
View File
File diff suppressed because it is too large Load Diff
+13 -3
View File
@@ -2,9 +2,10 @@
"name": "onspring-api-sdk", "name": "onspring-api-sdk",
"version": "1.0.0", "version": "1.0.0",
"description": "A javascript SDK for interacting with version 2 of the Onspring API.", "description": "A javascript SDK for interacting with version 2 of the Onspring API.",
"main": "dist/onspringApiSdk.js", "main": "dist/index.js",
"types": "dist/onspringApiSdk.d.ts", "types": "dist/index.d.ts",
"scripts": { "scripts": {
"lint": "eslint --ignore-path .eslintignore --ext .js,.ts .",
"format-staged": "pretty-quick --staged", "format-staged": "pretty-quick --staged",
"format": "pretty-quick", "format": "pretty-quick",
"build": "npm run tests && npm run format && tsc", "build": "npm run tests && npm run format && tsc",
@@ -20,7 +21,16 @@
"@types/chai": "^4.3.4", "@types/chai": "^4.3.4",
"@types/mocha": "^10.0.1", "@types/mocha": "^10.0.1",
"@types/sinon": "^10.0.13", "@types/sinon": "^10.0.13",
"@typescript-eslint/eslint-plugin": "^5.50.0",
"@typescript-eslint/parser": "^5.50.0",
"chai": "^4.3.7", "chai": "^4.3.7",
"eslint": "^8.33.0",
"eslint-config-prettier": "^8.6.0",
"eslint-config-standard-with-typescript": "^34.0.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-mocha": "^10.1.0",
"eslint-plugin-n": "^15.6.1",
"eslint-plugin-promise": "^6.1.1",
"husky": "^8.0.3", "husky": "^8.0.3",
"mocha": "^10.2.0", "mocha": "^10.2.0",
"nyc": "^15.1.0", "nyc": "^15.1.0",
@@ -28,7 +38,7 @@
"pretty-quick": "^3.1.3", "pretty-quick": "^3.1.3",
"sinon": "^15.0.1", "sinon": "^15.0.1",
"ts-node": "^10.9.1", "ts-node": "^10.9.1",
"typescript": "^4.9.4" "typescript": "^4.9.5"
}, },
"dependencies": { "dependencies": {
"axios": "^1.2.4" "axios": "^1.2.4"
+4
View File
@@ -133,4 +133,8 @@ export class ApiResponse<T> {
field field
); );
} }
public AsFieldCollectionType(): ApiResponse<CollectionResponse<Field>> {
throw new Error('Method not implemented.');
}
} }
+1 -1
View File
@@ -1,4 +1,4 @@
import { AxiosResponse } from 'axios'; import { type AxiosResponse } from 'axios';
import { HttpStatusCode } from '../enums/HttpStatusCode'; import { HttpStatusCode } from '../enums/HttpStatusCode';
import { ApiResponse } from './ApiResponse'; import { ApiResponse } from './ApiResponse';
+1 -1
View File
@@ -1,4 +1,4 @@
import { PagingRequest } from './PagingRequest'; import { type PagingRequest } from './PagingRequest';
/** /**
* @class EndpointFactory - A factory class for creating endpoints. * @class EndpointFactory - A factory class for creating endpoints.
+1 -1
View File
@@ -1,5 +1,5 @@
import { PagedResponse } from './PagedResponse'; import { PagedResponse } from './PagedResponse';
import { App } from './App'; import { type App } from './App';
/** /**
* @class GetPagedAppsResponse - A paged response model for the GetApps method. * @class GetPagedAppsResponse - A paged response model for the GetApps method.
+21 -8
View File
@@ -1,14 +1,14 @@
import axios from 'axios'; import axios from 'axios';
import { AxiosInstance, AxiosRequestConfig } from 'axios'; import { type AxiosInstance, type AxiosRequestConfig } from 'axios';
import { ArgumentValidator } from './ArgumentValidator'; import { ArgumentValidator } from './ArgumentValidator';
import { EndpointFactory } from './EndpointFactory'; import { EndpointFactory } from './EndpointFactory';
import { ApiResponseFactory } from './ApiResponseFactory'; import { ApiResponseFactory } from './ApiResponseFactory';
import { ApiResponse } from './ApiResponse'; import { type ApiResponse } from './ApiResponse';
import { PagingRequest } from './PagingRequest'; import { PagingRequest } from './PagingRequest';
import { GetPagedAppsResponse } from './GetPagedAppsResponse'; import { type GetPagedAppsResponse } from './GetPagedAppsResponse';
import { App } from './App'; import { type App } from './App';
import { CollectionResponse } from './CollectionResponse'; import { type CollectionResponse } from './CollectionResponse';
import { Field } from './Field'; import { type Field } from './Field';
/** /**
* @class OnspringClient - A client that can communicate with the Onspring API. * @class OnspringClient - A client that can communicate with the Onspring API.
@@ -66,7 +66,7 @@ export class OnspringClient {
): Promise<ApiResponse<GetPagedAppsResponse>> { ): Promise<ApiResponse<GetPagedAppsResponse>> {
const endpoint = EndpointFactory.getAppsEndpoint(pagingRequest); const endpoint = EndpointFactory.getAppsEndpoint(pagingRequest);
var apiResponse = await this.get<any>(endpoint); const apiResponse = await this.get<any>(endpoint);
if (apiResponse.isSuccessful === false) { if (apiResponse.isSuccessful === false) {
return apiResponse; return apiResponse;
@@ -83,7 +83,7 @@ export class OnspringClient {
public async getAppById(appId: number): Promise<ApiResponse<App>> { public async getAppById(appId: number): Promise<ApiResponse<App>> {
const endpoint = EndpointFactory.getAppByIdEndpoint(appId); const endpoint = EndpointFactory.getAppByIdEndpoint(appId);
var apiResponse = await this.get<any>(endpoint); const apiResponse = await this.get<any>(endpoint);
if (apiResponse.isSuccessful === false) { if (apiResponse.isSuccessful === false) {
return apiResponse; return apiResponse;
@@ -125,6 +125,19 @@ export class OnspringClient {
return apiResponse.AsFieldType(); return apiResponse.AsFieldType();
} }
public async getFieldsByIds(
fieldIds: number[]
): Promise<ApiResponse<CollectionResponse<Field>>> {
const endpoint = EndpointFactory.getFieldsByIdsEndpoint();
const apiResponse = await this.post<any>(endpoint, fieldIds);
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.AsFieldCollectionType();
}
/** /**
* @method get - Makes a GET request to the specified endpoint. * @method get - Makes a GET request to the specified endpoint.
* @param {string} endpoint - The endpoint that will be used to make the request. * @param {string} endpoint - The endpoint that will be used to make the request.
+7 -6
View File
@@ -9,6 +9,7 @@ import { FieldType } from '../src/enums/FieldType';
describe('ApiResponse', function () { describe('ApiResponse', function () {
it('should be defined', function () { it('should be defined', function () {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
expect(ApiResponse).to.not.be.undefined; expect(ApiResponse).to.not.be.undefined;
}); });
@@ -108,7 +109,7 @@ describe('ApiResponse', function () {
], ],
}; };
const apiResponse = new ApiResponse<any>(200, 'OK', mockResponseData); const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const appsPagedResponse = apiResponse.AsGetPagedAppsResponseType(); const appsPagedResponse = apiResponse.AsGetPagedAppsResponseType();
expect(appsPagedResponse).to.be.instanceOf(ApiResponse); expect(appsPagedResponse).to.be.instanceOf(ApiResponse);
@@ -126,7 +127,7 @@ describe('ApiResponse', function () {
} }
}); });
it('should return an ApiResponse<GetPagedAppsResponse> when data contains app items', function () { it('should return an ApiResponse<GetPagedAppsResponse> when data contains no app items', function () {
const mockResponseData = { const mockResponseData = {
pageNumber: 0, pageNumber: 0,
pageSize: 0, pageSize: 0,
@@ -135,7 +136,7 @@ describe('ApiResponse', function () {
items: [], items: [],
}; };
const apiResponse = new ApiResponse<any>(200, 'OK', mockResponseData); const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const appsPagedResponse = apiResponse.AsGetPagedAppsResponseType(); const appsPagedResponse = apiResponse.AsGetPagedAppsResponseType();
expect(appsPagedResponse).to.be.instanceOf(ApiResponse); expect(appsPagedResponse).to.be.instanceOf(ApiResponse);
@@ -168,7 +169,7 @@ describe('ApiResponse', function () {
name: 'Test App', name: 'Test App',
}; };
const apiResponse = new ApiResponse<any>(200, 'OK', mockResponseData); const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const appResponse = apiResponse.AsAppType(); const appResponse = apiResponse.AsAppType();
expect(appResponse).to.be.instanceOf(ApiResponse); expect(appResponse).to.be.instanceOf(ApiResponse);
@@ -209,7 +210,7 @@ describe('ApiResponse', function () {
], ],
}; };
const apiResponse = new ApiResponse<any>(200, 'OK', mockResponseData); const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const appCollectionResponse = apiResponse.AsAppCollectionType(); const appCollectionResponse = apiResponse.AsAppCollectionType();
expect(appCollectionResponse).to.be.instanceOf( expect(appCollectionResponse).to.be.instanceOf(
@@ -252,7 +253,7 @@ describe('ApiResponse', function () {
isUnique: false, isUnique: false,
}; };
const apiResponse = new ApiResponse<any>(200, 'OK', mockResponseData); const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const fieldResponse = apiResponse.AsFieldType(); const fieldResponse = apiResponse.AsFieldType();
expect(fieldResponse).to.be.instanceOf(ApiResponse); expect(fieldResponse).to.be.instanceOf(ApiResponse);
+1 -1
View File
@@ -1,5 +1,5 @@
import { ApiResponseFactory } from '../src/models/ApiResponseFactory'; import { ApiResponseFactory } from '../src/models/ApiResponseFactory';
import { AxiosResponse, InternalAxiosRequestConfig } from 'axios'; import { type AxiosResponse, type InternalAxiosRequestConfig } from 'axios';
import { expect } from 'chai'; import { expect } from 'chai';
describe('ApiResponseFactory', function () { describe('ApiResponseFactory', function () {
-4
View File
@@ -153,10 +153,6 @@ describe('ArgumentValidator', function () {
expect(ArgumentValidator.isValidUrl('www.google.com')).to.be.false; 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 () { 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; expect(ArgumentValidator.isValidUrl('ftp://www.google.com')).to.be.false;
}); });
+31 -31
View File
@@ -1,107 +1,107 @@
import { HttpStatusCode } from '../src/enums/HttpStatusCode'; import { HttpStatusCode } from '../src/enums/HttpStatusCode';
import { expect } from 'chai'; import { expect } from 'chai';
describe('HttpStatusCode', () => { describe('HttpStatusCode', function () {
describe('OK', () => { describe('OK', function () {
it('should return the correct value', () => { it('should return the correct value', function () {
const result = HttpStatusCode.OK; const result = HttpStatusCode.OK;
expect(result).to.equal(200); expect(result).to.equal(200);
}); });
}); });
describe('Created', () => { describe('Created', function () {
it('should return the correct value', () => { it('should return the correct value', function () {
const result = HttpStatusCode.Created; const result = HttpStatusCode.Created;
expect(result).to.equal(201); expect(result).to.equal(201);
}); });
}); });
describe('Accepted', () => { describe('Accepted', function () {
it('should return the correct value', () => { it('should return the correct value', function () {
const result = HttpStatusCode.Accepted; const result = HttpStatusCode.Accepted;
expect(result).to.equal(202); expect(result).to.equal(202);
}); });
}); });
describe('NoContent', () => { describe('NoContent', function () {
it('should return the correct value', () => { it('should return the correct value', function () {
const result = HttpStatusCode.NoContent; const result = HttpStatusCode.NoContent;
expect(result).to.equal(204); expect(result).to.equal(204);
}); });
}); });
describe('BadRequest', () => { describe('BadRequest', function () {
it('should return the correct value', () => { it('should return the correct value', function () {
const result = HttpStatusCode.BadRequest; const result = HttpStatusCode.BadRequest;
expect(result).to.equal(400); expect(result).to.equal(400);
}); });
}); });
describe('Unauthorized', () => { describe('Unauthorized', function () {
it('should return the correct value', () => { it('should return the correct value', function () {
const result = HttpStatusCode.Unauthorized; const result = HttpStatusCode.Unauthorized;
expect(result).to.equal(401); expect(result).to.equal(401);
}); });
}); });
describe('Forbidden', () => { describe('Forbidden', function () {
it('should return the correct value', () => { it('should return the correct value', function () {
const result = HttpStatusCode.Forbidden; const result = HttpStatusCode.Forbidden;
expect(result).to.equal(403); expect(result).to.equal(403);
}); });
}); });
describe('NotFound', () => { describe('NotFound', function () {
it('should return the correct value', () => { it('should return the correct value', function () {
const result = HttpStatusCode.NotFound; const result = HttpStatusCode.NotFound;
expect(result).to.equal(404); expect(result).to.equal(404);
}); });
}); });
describe('MethodNotAllowed', () => { describe('MethodNotAllowed', function () {
it('should return the correct value', () => { it('should return the correct value', function () {
const result = HttpStatusCode.MethodNotAllowed; const result = HttpStatusCode.MethodNotAllowed;
expect(result).to.equal(405); expect(result).to.equal(405);
}); });
}); });
describe('Conflict', () => { describe('Conflict', function () {
it('should return the correct value', () => { it('should return the correct value', function () {
const result = HttpStatusCode.Conflict; const result = HttpStatusCode.Conflict;
expect(result).to.equal(409); expect(result).to.equal(409);
}); });
}); });
describe('InternalServerError', () => { describe('InternalServerError', function () {
it('should return the correct value', () => { it('should return the correct value', function () {
const result = HttpStatusCode.InternalServerError; const result = HttpStatusCode.InternalServerError;
expect(result).to.equal(500); expect(result).to.equal(500);
}); });
}); });
describe('NotImplemented', () => { describe('NotImplemented', function () {
it('should return the correct value', () => { it('should return the correct value', function () {
const result = HttpStatusCode.NotImplemented; const result = HttpStatusCode.NotImplemented;
expect(result).to.equal(501); expect(result).to.equal(501);
}); });
}); });
describe('BadGateway', () => { describe('BadGateway', function () {
it('should return the correct value', () => { it('should return the correct value', function () {
const result = HttpStatusCode.BadGateway; const result = HttpStatusCode.BadGateway;
expect(result).to.equal(502); expect(result).to.equal(502);
}); });
}); });
describe('ServiceUnavailable', () => { describe('ServiceUnavailable', function () {
it('should return the correct value', () => { it('should return the correct value', function () {
const result = HttpStatusCode.ServiceUnavailable; const result = HttpStatusCode.ServiceUnavailable;
expect(result).to.equal(503); expect(result).to.equal(503);
}); });
}); });
describe('GatewayTimeout', () => { describe('GatewayTimeout', function () {
it('should return the correct value', () => { it('should return the correct value', function () {
const result = HttpStatusCode.GatewayTimeout; const result = HttpStatusCode.GatewayTimeout;
expect(result).to.equal(504); expect(result).to.equal(504);
}); });
+4 -7
View File
@@ -1,6 +1,9 @@
import { OnspringClient } from '../src/models/OnspringClient'; import { OnspringClient } from '../src/models/OnspringClient';
import { ApiResponse } from '../src/models/ApiResponse'; import { ApiResponse } from '../src/models/ApiResponse';
import axios, { AxiosResponse, InternalAxiosRequestConfig } from 'axios'; import axios, {
type AxiosResponse,
type InternalAxiosRequestConfig,
} from 'axios';
import { expect } from 'chai'; import { expect } from 'chai';
import * as sinon from 'sinon'; import * as sinon from 'sinon';
import { GetPagedAppsResponse } from '../src/models/GetPagedAppsResponse'; import { GetPagedAppsResponse } from '../src/models/GetPagedAppsResponse';
@@ -84,12 +87,6 @@ describe('OnspringClient', function () {
expect(() => new OnspringClient(baseUrl, apiKey)).to.not.throw(); expect(() => new OnspringClient(baseUrl, 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 () { it('should not throw an error when the apiKey is a valid string', function () {
expect(() => new OnspringClient(baseUrl, apiKey)).to.not.throw(); expect(() => new OnspringClient(baseUrl, apiKey)).to.not.throw();
}); });
+4
View File
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"include": ["tests/**/*.ts", "src/index.ts"]
}