mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 00:52:55 +00:00
refactor: use export global variables instead of express
This commit is contained in:
committed by
Rainer Killinger
parent
8c48552abf
commit
59e4009c5d
@@ -14,7 +14,7 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {SCBulkAddRequest, SCBulkAddResponse, SCBulkAddRoute, SCNotFoundErrorResponse} from '@openstapps/core';
|
||||
import {logger} from '../common';
|
||||
import {isTestEnvironment, logger} from '../common';
|
||||
import {BulkStorage} from '../storage/BulkStorage';
|
||||
import {createRoute} from './Route';
|
||||
|
||||
@@ -36,7 +36,7 @@ export const bulkAddRouter = createRoute<SCBulkAddResponse>(
|
||||
|
||||
if (typeof bulk === 'undefined') {
|
||||
logger.warn(`Bulk with ${params.UID} not found.`);
|
||||
throw new SCNotFoundErrorResponse(app.get('isTestEnvironment'));
|
||||
throw new SCNotFoundErrorResponse(isTestEnvironment);
|
||||
}
|
||||
|
||||
await bulkMemory.database.post(request, bulk);
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {SCBulkDoneRequest, SCBulkDoneResponse, SCBulkDoneRoute, SCNotFoundErrorResponse} from '@openstapps/core';
|
||||
import {logger} from '../common';
|
||||
import {isTestEnvironment, logger} from '../common';
|
||||
import {BulkStorage} from '../storage/BulkStorage';
|
||||
import {createRoute} from './Route';
|
||||
|
||||
@@ -36,7 +36,7 @@ export const bulkDoneRouter = createRoute<SCBulkDoneResponse>(
|
||||
|
||||
if (typeof bulk === 'undefined') {
|
||||
logger.warn(`Bulk with ${params.UID} not found.`);
|
||||
throw new SCNotFoundErrorResponse(app.get('isTestEnvironment'));
|
||||
throw new SCNotFoundErrorResponse(isTestEnvironment);
|
||||
}
|
||||
|
||||
bulk.state = 'done';
|
||||
|
||||
@@ -20,7 +20,7 @@ import {
|
||||
SCSearchResponse,
|
||||
SCTooManyRequestsErrorResponse,
|
||||
} from '@openstapps/core';
|
||||
import {configFile} from '../common';
|
||||
import {configFile, isTestEnvironment} from '../common';
|
||||
import {BulkStorage} from '../storage/BulkStorage';
|
||||
import {createRoute} from './Route';
|
||||
|
||||
@@ -37,7 +37,7 @@ export const multiSearchRouter = createRoute<SCMultiSearchResponse | SCTooManyRe
|
||||
const queryNames = Object.keys(request);
|
||||
|
||||
if (queryNames.length > configFile.backend.maxMultiSearchRouteQueries) {
|
||||
throw new SCTooManyRequestsErrorResponse(app.get('isTestEnvironment'));
|
||||
throw new SCTooManyRequestsErrorResponse(isTestEnvironment);
|
||||
}
|
||||
|
||||
// get a map of promises for each query
|
||||
|
||||
@@ -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