refactor: tidy structure of source files

Fixes #79
This commit is contained in:
Karl-Philipp Wulfert
2019-06-19 15:04:59 +02:00
parent 5de9bf3794
commit ceab7cc7ef
99 changed files with 1395 additions and 1511 deletions

176
src/protocol/route.ts Normal file
View File

@@ -0,0 +1,176 @@
/*
* Copyright (C) 2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {SCMap} from '../general/map';
import {SCErrorResponse} from './error';
import {SCIndexRequest, SCIndexResponse} from './routes';
import {SCBookAvailabilityRequest, SCBookAvailabilityResponse} from './routes/book-availability';
import {SCBulkAddRequest, SCBulkAddResponse} from './routes/bulk-add';
import {SCBulkDoneRequest, SCBulkDoneResponse} from './routes/bulk-done';
import {SCBulkRequest, SCBulkResponse} from './routes/bulk-request';
import {SCFeedbackRequest, SCFeedbackResponse} from './routes/feedback';
import {SCSearchRequest, SCSearchResponse} from './routes/search';
import {SCMultiSearchRequest, SCMultiSearchResponse} from './routes/search-multi';
import {SCThingUpdateRequest, SCThingUpdateResponse} from './routes/thing-update';
/**
* Possible Verbs for HTTP requests
*/
export enum SCRouteHttpVerbs {
GET = 'GET',
POST = 'POST',
PUT = 'PUT',
}
/**
* The constructor of an error response
*/
// tslint:disable-next-line:no-any
export type SCErrorResponseConstructor = new (...args: any[]) => SCErrorResponse;
/**
* A description of a route
*/
export interface SCRoute {
/**
* A map of names of possible errors that can be returned by the route with their appropriate status codes
*/
errorNames: SCErrorResponseConstructor[];
/**
* HTTP verb to use to request the route
*/
method: SCRouteHttpVerbs;
/**
* Map of obligatory parameters and their type that have to be set via the requested path
*/
obligatoryParameters?: SCMap<string>;
/**
* Name of the type of the request body
*/
requestBodyName: string;
/**
* Name of the type of the response body
*/
responseBodyName: string;
/**
* Status code for success
*/
statusCodeSuccess: number;
/**
* URL fragment of the route
*/
urlFragment: string;
}
/**
* An abstract route
*/
export abstract class SCAbstractRoute implements SCRoute {
/**
* @see SCRoute.errorNames
*/
errorNames: SCErrorResponseConstructor[] = [];
/**
* @see SCRoute.method
*/
method: SCRouteHttpVerbs = SCRouteHttpVerbs.GET;
/**
* @see SCRoute.obligatoryParameters
*/
obligatoryParameters?: SCMap<string>;
/**
* @see SCRoute.requestBodyName
*/
requestBodyName = 'any';
/**
* @see SCRoute.responseBodyName
*/
responseBodyName = 'any';
/**
* @see SCRoute.statusCodeSuccess
*/
statusCodeSuccess = 200;
/**
* @see SCRoute.urlFragment
*/
urlFragment = '/';
/**
* Get "compiled" URL fragment
*
* @param parameters Parameters to compile URL fragment with
*/
public getUrlFragment(parameters: SCMap<string> = {}): string {
let obligatoryParameters: string[] = [];
if (typeof this.obligatoryParameters === 'object') {
obligatoryParameters = Object.keys(this.obligatoryParameters);
}
if (Object.keys(parameters).length > obligatoryParameters.length) {
throw new Error('Extraneous parameters provided.');
}
return this.urlFragment
.split('/')
.map((part) => {
if (part.indexOf(':') !== 0) {
return part;
}
const parameter = part.substr(1);
if (typeof parameters[parameter] === 'undefined') {
throw new Error(`Parameter '${parameter}' not provided.`);
}
return parameters[parameter];
})
.join('/');
}
}
/**
* Possible requests
*/
export type SCRequests =
SCBookAvailabilityRequest
| SCBulkRequest
| SCBulkAddRequest
| SCBulkDoneRequest
| SCFeedbackRequest
| SCIndexRequest
| SCMultiSearchRequest
| SCSearchRequest
| SCThingUpdateRequest;
/**
* Possible responses
*/
export type SCResponses =
SCBookAvailabilityResponse
| SCBulkResponse
| SCBulkAddResponse
| SCBulkDoneResponse
| SCFeedbackResponse
| SCIndexResponse
| SCMultiSearchResponse
| SCSearchResponse
| SCThingUpdateResponse;