mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 17:12:43 +00:00
refactor: use export global variables instead of express
This commit is contained in:
committed by
Rainer Killinger
parent
8c48552abf
commit
59e4009c5d
@@ -19,11 +19,10 @@ import {
|
||||
SCRoute,
|
||||
SCValidationErrorResponse,
|
||||
} from '@openstapps/core';
|
||||
import {Validator} from '@openstapps/core-tools/lib/validate';
|
||||
import {Application, Router} from 'express';
|
||||
import PromiseRouter from 'express-promise-router';
|
||||
import {ValidationError} from 'jsonschema';
|
||||
import {logger} from '../common';
|
||||
import {isTestEnvironment, logger, validator} from '../common';
|
||||
import {isHttpMethod} from './HTTPTypes';
|
||||
|
||||
/**
|
||||
@@ -71,16 +70,13 @@ export function createRoute<RETURNTYPE>(
|
||||
route[verb](async (req, res) => {
|
||||
|
||||
try {
|
||||
// get the core validator from the app
|
||||
const validator: Validator = req.app.get('validator');
|
||||
|
||||
// validate request
|
||||
const requestValidation = validator.validate(req.body, routeClass.requestBodyName);
|
||||
|
||||
if (requestValidation.errors.length > 0) {
|
||||
const error = new SCValidationErrorResponse(
|
||||
requestValidation.errors,
|
||||
req.app.get('isTestEnvironment'),
|
||||
isTestEnvironment,
|
||||
);
|
||||
res.status(error.statusCode);
|
||||
res.json(error);
|
||||
@@ -97,11 +93,11 @@ export function createRoute<RETURNTYPE>(
|
||||
if (responseErrors.length > 0) {
|
||||
const validationError = new SCValidationErrorResponse(
|
||||
responseErrors,
|
||||
req.app.get('isTestEnvironment'),
|
||||
isTestEnvironment,
|
||||
);
|
||||
const internalServerError = new SCInternalServerErrorResponse(
|
||||
validationError,
|
||||
req.app.get('isTestEnvironment'),
|
||||
isTestEnvironment,
|
||||
);
|
||||
res.status(internalServerError.statusCode);
|
||||
res.json(internalServerError);
|
||||
@@ -125,7 +121,7 @@ export function createRoute<RETURNTYPE>(
|
||||
// the error is not allowed so something went wrong
|
||||
const internalServerError = new SCInternalServerErrorResponse(
|
||||
error,
|
||||
req.app.get('isTestEnvironment'),
|
||||
isTestEnvironment,
|
||||
);
|
||||
res.status(internalServerError.statusCode);
|
||||
res.json(internalServerError);
|
||||
@@ -138,8 +134,8 @@ export function createRoute<RETURNTYPE>(
|
||||
}
|
||||
|
||||
// return a SCMethodNotAllowedErrorResponse on all other HTTP methods
|
||||
route.all((req, res) => {
|
||||
const error = new SCMethodNotAllowedErrorResponse(req.app.get('isTestEnvironment'));
|
||||
route.all((_req, res) => {
|
||||
const error = new SCMethodNotAllowedErrorResponse(isTestEnvironment);
|
||||
res.status(error.statusCode);
|
||||
res.json(error);
|
||||
logger.warn(error);
|
||||
|
||||
Reference in New Issue
Block a user