refactor: use constructor for checking error type in SCRoute

change SCRouteHttpVerbs from type to enum
This commit is contained in:
Wieland Schöbl
2019-02-13 16:20:31 +01:00
parent f9c357fccf
commit e2ff4f4ec6
10 changed files with 152 additions and 74 deletions

View File

@@ -15,6 +15,7 @@
/** /**
* Possible HTTP verbs for routes * Possible HTTP verbs for routes
*/ */
import {SCErrorResponse} from './protocol/errors/ErrorResponse';
import {SCBookAvailabilityRequest} from './protocol/routes/bookAvailability/BookAvailabilityRequest'; import {SCBookAvailabilityRequest} from './protocol/routes/bookAvailability/BookAvailabilityRequest';
import {SCBookAvailabilityResponse} from './protocol/routes/bookAvailability/BookAvailabilityResponse'; import {SCBookAvailabilityResponse} from './protocol/routes/bookAvailability/BookAvailabilityResponse';
import {SCBulkRequest} from './protocol/routes/bulk/BulkRequest'; import {SCBulkRequest} from './protocol/routes/bulk/BulkRequest';
@@ -35,7 +36,19 @@ import {SCThingUpdateRequest} from './protocol/routes/TYPE/UID/ThingUpdateReques
import {SCThingUpdateResponse} from './protocol/routes/TYPE/UID/ThingUpdateResponse'; import {SCThingUpdateResponse} from './protocol/routes/TYPE/UID/ThingUpdateResponse';
import {SCMap} from './types/Map'; import {SCMap} from './types/Map';
export type SCRouteHttpVerbs = 'GET' | 'POST' | 'PUT'; /**
* Possible Verbs for HTTP requests
*/
export enum SCRouteHttpVerbs {
GET = 'GET',
POST = 'POST',
PUT = 'PUT',
}
/**
* The constructor of an error response
*/
export type SCErrorResponseConstructor = new (...args: any) => SCErrorResponse;
/** /**
* A description of a route * A description of a route
@@ -44,7 +57,7 @@ export interface SCRoute {
/** /**
* A map of names of possible errors that can be returned by the route with their appropriate status codes * A map of names of possible errors that can be returned by the route with their appropriate status codes
*/ */
errorNames: string[]; errorNames: SCErrorResponseConstructor[];
/** /**
* HTTP verb to use to request the route * HTTP verb to use to request the route
@@ -81,10 +94,8 @@ export interface SCRoute {
* An abstract route * An abstract route
*/ */
export abstract class SCAbstractRoute implements SCRoute { export abstract class SCAbstractRoute implements SCRoute {
errorNames = [ errorNames: SCErrorResponseConstructor[] = [];
'SCErrorResponse', method: SCRouteHttpVerbs = SCRouteHttpVerbs.GET;
];
method: SCRouteHttpVerbs = 'GET';
obligatoryParameters?: SCMap<string>; obligatoryParameters?: SCMap<string>;
requestBodyName = 'any'; requestBodyName = 'any';
responseBodyName = 'any'; responseBodyName = 'any';

View File

@@ -12,7 +12,14 @@
* You should have received a copy of the GNU General Public License along with * You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>. * this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import {SCAbstractRoute} from '../../../Route'; import {SCAbstractRoute, SCRouteHttpVerbs} from '../../../Route';
import {
SCInternalServerErrorResponse,
SCMethodNotAllowedErrorResponse,
SCSyntaxErrorResponse,
SCUnsupportedMediaTypeErrorResponse,
SCValidationErrorResponse,
} from '../../errors/ErrorResponse';
/** /**
* Index request * Index request
@@ -29,13 +36,13 @@ export class SCIndexRoute extends SCAbstractRoute {
constructor() { constructor() {
super(); super();
this.errorNames = [ this.errorNames = [
'SCInternalServerErrorResponse', SCInternalServerErrorResponse,
'SCMethodNotAllowedErrorResponse', SCMethodNotAllowedErrorResponse,
'SCSyntaxError', SCSyntaxErrorResponse,
'SCUnsupportedMediaTypeErrorResponse', SCUnsupportedMediaTypeErrorResponse,
'SCValidationErrorResponse', SCValidationErrorResponse,
]; ];
this.method = 'POST'; this.method = SCRouteHttpVerbs.POST;
this.requestBodyName = 'SCIndexRequest'; this.requestBodyName = 'SCIndexRequest';
this.responseBodyName = 'SCIndexResponse'; this.responseBodyName = 'SCIndexResponse';
this.statusCodeSuccess = 200; this.statusCodeSuccess = 200;

View File

@@ -13,7 +13,14 @@
* this program. If not, see <https://www.gnu.org/licenses/>. * this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import {SCThings} from '../../../../Classes'; import {SCThings} from '../../../../Classes';
import {SCAbstractRoute} from '../../../../Route'; import {SCAbstractRoute, SCRouteHttpVerbs} from '../../../../Route';
import {
SCInternalServerErrorResponse,
SCMethodNotAllowedErrorResponse,
SCNotFoundErrorResponse, SCSyntaxErrorResponse,
SCUnsupportedMediaTypeErrorResponse,
SCValidationErrorResponse,
} from '../../../errors/ErrorResponse';
/** /**
* Request to update an existing thing * Request to update an existing thing
@@ -29,14 +36,14 @@ export class SCThingUpdateRoute extends SCAbstractRoute {
constructor() { constructor() {
super(); super();
this.errorNames = [ this.errorNames = [
'SCInternalServerErrorResponse', SCInternalServerErrorResponse,
'SCMethodNotAllowedErrorResponse', SCMethodNotAllowedErrorResponse,
'SCNotFoundErrorResponse', SCNotFoundErrorResponse,
'SCSyntaxError', SCSyntaxErrorResponse,
'SCUnsupportedMediaTypeErrorResponse', SCUnsupportedMediaTypeErrorResponse,
'SCValidationErrorResponse', SCValidationErrorResponse,
]; ];
this.method = 'PUT'; this.method = SCRouteHttpVerbs.PUT;
this.obligatoryParameters = { this.obligatoryParameters = {
TYPE: 'SCThingTypes', TYPE: 'SCThingTypes',
UID: 'SCUuid', UID: 'SCUuid',

View File

@@ -12,8 +12,16 @@
* You should have received a copy of the GNU General Public License along with * You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>. * this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import {SCAbstractRoute} from '../../../Route'; import {SCAbstractRoute, SCRouteHttpVerbs} from '../../../Route';
import {SCUuid} from '../../../types/UUID'; import {SCUuid} from '../../../types/UUID';
import {
SCInternalServerErrorResponse,
SCMethodNotAllowedErrorResponse,
SCNotFoundErrorResponse,
SCSyntaxErrorResponse,
SCUnsupportedMediaTypeErrorResponse,
SCValidationErrorResponse,
} from '../../errors/ErrorResponse';
/** /**
* Request to check the availability of books * Request to check the availability of books
@@ -60,14 +68,14 @@ export class SCBookAvailabilityRoute extends SCAbstractRoute {
constructor() { constructor() {
super(); super();
this.errorNames = [ this.errorNames = [
'SCInternalServerErrorResponse', SCInternalServerErrorResponse,
'SCMethodNotAllowedErrorResponse', SCMethodNotAllowedErrorResponse,
'SCNotFoundErrorResponse', SCNotFoundErrorResponse,
'SCSyntaxError', SCSyntaxErrorResponse,
'SCUnsupportedMediaTypeErrorResponse', SCUnsupportedMediaTypeErrorResponse,
'SCValidationErrorResponse', SCValidationErrorResponse,
]; ];
this.method = 'POST'; this.method = SCRouteHttpVerbs.POST;
this.requestBodyName = 'SCBookAvailabilityRequest'; this.requestBodyName = 'SCBookAvailabilityRequest';
this.responseBodyName = 'SCBookAvailabilityResponse'; this.responseBodyName = 'SCBookAvailabilityResponse';
this.statusCodeSuccess = 200; this.statusCodeSuccess = 200;

View File

@@ -12,9 +12,16 @@
* You should have received a copy of the GNU General Public License along with * You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>. * this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import {SCAbstractRoute} from '../../../Route'; import {SCAbstractRoute, SCRouteHttpVerbs} from '../../../Route';
import {SCThingType} from '../../../Thing'; import {SCThingType} from '../../../Thing';
import {SCISO8601Date} from '../../../types/Time'; import {SCISO8601Date} from '../../../types/Time';
import {
SCInternalServerErrorResponse,
SCMethodNotAllowedErrorResponse,
SCSyntaxErrorResponse,
SCUnsupportedMediaTypeErrorResponse,
SCValidationErrorResponse,
} from '../../errors/ErrorResponse';
/** /**
* A bulk request * A bulk request
@@ -60,13 +67,13 @@ export class SCBulkRoute extends SCAbstractRoute {
constructor() { constructor() {
super(); super();
this.errorNames = [ this.errorNames = [
'SCInternalServerErrorResponse', SCInternalServerErrorResponse,
'SCMethodNotAllowedErrorResponse', SCMethodNotAllowedErrorResponse,
'SCSyntaxError', SCSyntaxErrorResponse,
'SCUnsupportedMediaTypeErrorResponse', SCUnsupportedMediaTypeErrorResponse,
'SCValidationErrorResponse', SCValidationErrorResponse,
]; ];
this.method = 'POST'; this.method = SCRouteHttpVerbs.POST;
this.requestBodyName = 'SCBulkRequest'; this.requestBodyName = 'SCBulkRequest';
this.responseBodyName = 'SCBulkResponse'; this.responseBodyName = 'SCBulkResponse';
this.statusCodeSuccess = 200; this.statusCodeSuccess = 200;

View File

@@ -13,7 +13,15 @@
* this program. If not, see <https://www.gnu.org/licenses/>. * this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import {SCThings} from '../../../../Classes'; import {SCThings} from '../../../../Classes';
import {SCAbstractRoute} from '../../../../Route'; import {SCAbstractRoute, SCRouteHttpVerbs} from '../../../../Route';
import {
SCInternalServerErrorResponse,
SCMethodNotAllowedErrorResponse,
SCNotFoundErrorResponse,
SCSyntaxErrorResponse,
SCUnsupportedMediaTypeErrorResponse,
SCValidationErrorResponse,
} from '../../../errors/ErrorResponse';
/** /**
* Request to add a thing to a bulk * Request to add a thing to a bulk
@@ -28,14 +36,14 @@ export class SCBulkAddRoute extends SCAbstractRoute {
constructor() { constructor() {
super(); super();
this.errorNames = [ this.errorNames = [
'SCInternalServerErrorResponse', SCInternalServerErrorResponse,
'SCMethodNotAllowedErrorResponse', SCMethodNotAllowedErrorResponse,
'SCNotFoundErrorResponse', SCNotFoundErrorResponse,
'SCSyntaxError', SCSyntaxErrorResponse,
'SCUnsupportedMediaTypeErrorResponse', SCUnsupportedMediaTypeErrorResponse,
'SCValidationErrorResponse', SCValidationErrorResponse,
]; ];
this.method = 'POST'; this.method = SCRouteHttpVerbs.POST;
this.obligatoryParameters = { this.obligatoryParameters = {
UID: 'SCUuid', UID: 'SCUuid',
}; };

View File

@@ -12,7 +12,15 @@
* You should have received a copy of the GNU General Public License along with * You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>. * this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import {SCAbstractRoute} from '../../../../Route'; import {SCAbstractRoute, SCRouteHttpVerbs} from '../../../../Route';
import {
SCInternalServerErrorResponse,
SCMethodNotAllowedErrorResponse,
SCNotFoundErrorResponse,
SCSyntaxErrorResponse,
SCUnsupportedMediaTypeErrorResponse,
SCValidationErrorResponse,
} from '../../../errors/ErrorResponse';
/** /**
* Request to change the bulk state to done (close the bulk process) * Request to change the bulk state to done (close the bulk process)
@@ -28,14 +36,14 @@ export class SCBulkDoneRoute extends SCAbstractRoute {
constructor() { constructor() {
super(); super();
this.errorNames = [ this.errorNames = [
'SCInternalServerErrorResponse', SCInternalServerErrorResponse,
'SCMethodNotAllowedErrorResponse', SCMethodNotAllowedErrorResponse,
'SCNotFoundErrorResponse', SCNotFoundErrorResponse,
'SCSyntaxError', SCSyntaxErrorResponse,
'SCUnsupportedMediaTypeErrorResponse', SCUnsupportedMediaTypeErrorResponse,
'SCValidationErrorResponse', SCValidationErrorResponse,
]; ];
this.method = 'POST'; this.method = SCRouteHttpVerbs.POST;
this.obligatoryParameters = { this.obligatoryParameters = {
UID: 'SCUuid', UID: 'SCUuid',
}; };

View File

@@ -12,8 +12,15 @@
* You should have received a copy of the GNU General Public License along with * You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>. * this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import {SCAbstractRoute} from '../../../Route'; import {SCAbstractRoute, SCRouteHttpVerbs} from '../../../Route';
import {SCMessage} from '../../../things/Message'; import {SCMessage} from '../../../things/Message';
import {
SCInternalServerErrorResponse,
SCMethodNotAllowedErrorResponse,
SCSyntaxErrorResponse,
SCUnsupportedMediaTypeErrorResponse,
SCValidationErrorResponse,
} from '../../errors/ErrorResponse';
/** /**
* User feedback * User feedback
@@ -34,13 +41,13 @@ export class SCFeedbackRoute extends SCAbstractRoute {
constructor() { constructor() {
super(); super();
this.errorNames = [ this.errorNames = [
'SCInternalServerErrorResponse', SCInternalServerErrorResponse,
'SCMethodNotAllowedErrorResponse', SCMethodNotAllowedErrorResponse,
'SCSyntaxError', SCSyntaxErrorResponse,
'SCUnsupportedMediaTypeErrorResponse', SCUnsupportedMediaTypeErrorResponse,
'SCValidationErrorResponse', SCValidationErrorResponse,
]; ];
this.method = 'POST'; this.method = SCRouteHttpVerbs.POST;
this.requestBodyName = 'SCFeedbackRequest'; this.requestBodyName = 'SCFeedbackRequest';
this.responseBodyName = 'SCFeedbackResponse'; this.responseBodyName = 'SCFeedbackResponse';
this.statusCodeSuccess = 204; this.statusCodeSuccess = 204;

View File

@@ -12,7 +12,15 @@
* You should have received a copy of the GNU General Public License along with * You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>. * this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import {SCAbstractRoute} from '../../../Route'; import {SCAbstractRoute, SCRouteHttpVerbs} from '../../../Route';
import {
SCInternalServerErrorResponse,
SCMethodNotAllowedErrorResponse,
SCSyntaxErrorResponse,
SCTooManyRequestsErrorResponse,
SCUnsupportedMediaTypeErrorResponse,
SCValidationErrorResponse,
} from '../../errors/ErrorResponse';
import {SCSearchQuery} from './SearchRequest'; import {SCSearchQuery} from './SearchRequest';
/** /**
@@ -35,14 +43,14 @@ export class SCMultiSearchRoute extends SCAbstractRoute {
constructor() { constructor() {
super(); super();
this.errorNames = [ this.errorNames = [
'SCInternalServerErrorResponse', SCInternalServerErrorResponse,
'SCMethodNotAllowedErrorResponse', SCMethodNotAllowedErrorResponse,
'SCSyntaxError', SCSyntaxErrorResponse,
'SCTooManyRequestsErrorResponse', SCTooManyRequestsErrorResponse,
'SCUnsupportedMediaTypeErrorResponse', SCUnsupportedMediaTypeErrorResponse,
'SCValidationErrorResponse', SCValidationErrorResponse,
]; ];
this.method = 'POST'; this.method = SCRouteHttpVerbs.POST;
this.requestBodyName = 'SCMultiSearchRequest'; this.requestBodyName = 'SCMultiSearchRequest';
this.responseBodyName = 'SCMultiSearchResponse'; this.responseBodyName = 'SCMultiSearchResponse';
this.statusCodeSuccess = 200; this.statusCodeSuccess = 200;

View File

@@ -12,9 +12,16 @@
* You should have received a copy of the GNU General Public License along with * You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>. * this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import {SCAbstractRoute} from '../../../Route'; import {SCAbstractRoute, SCRouteHttpVerbs} from '../../../Route';
import {SCSearchFilter} from '../../../types/filters/Abstract'; import {SCSearchFilter} from '../../../types/filters/Abstract';
import {SCSearchSort} from '../../../types/sorts/Abstract'; import {SCSearchSort} from '../../../types/sorts/Abstract';
import {
SCInternalServerErrorResponse,
SCMethodNotAllowedErrorResponse,
SCSyntaxErrorResponse,
SCUnsupportedMediaTypeErrorResponse,
SCValidationErrorResponse,
} from '../../errors/ErrorResponse';
/** /**
* A search request * A search request
@@ -61,13 +68,13 @@ export class SCSearchRoute extends SCAbstractRoute {
constructor() { constructor() {
super(); super();
this.errorNames = [ this.errorNames = [
'SCInternalServerErrorResponse', SCInternalServerErrorResponse,
'SCMethodNotAllowedErrorResponse', SCMethodNotAllowedErrorResponse,
'SCSyntaxError', SCSyntaxErrorResponse,
'SCUnsupportedMediaTypeErrorResponse', SCUnsupportedMediaTypeErrorResponse,
'SCValidationErrorResponse', SCValidationErrorResponse,
]; ];
this.method = 'POST'; this.method = SCRouteHttpVerbs.POST;
this.requestBodyName = 'SCSearchRequest'; this.requestBodyName = 'SCSearchRequest';
this.responseBodyName = 'SCSearchResponse'; this.responseBodyName = 'SCSearchResponse';
this.statusCodeSuccess = 200; this.statusCodeSuccess = 200;