Resolve "Transition to ESLint"

This commit is contained in:
Thea Schöbl
2022-06-27 14:40:09 +00:00
committed by Rainer Killinger
parent ca1d2444e0
commit 418ba67d15
47 changed files with 1854 additions and 1634 deletions

View File

@@ -24,7 +24,7 @@ import config from 'config';
import cors from 'cors';
import {Express} from 'express';
import morgan from 'morgan';
import {join} from 'path';
import path from 'path';
import {configFile, DEFAULT_TIMEOUT, isTestEnvironment, mailer, plugins, validator} from './common';
import {getPrometheusMiddleware} from './middleware/prometheus';
import {MailQueue} from './notification/mail-queue';
@@ -43,26 +43,26 @@ import {DatabaseConstructor} from './storage/database';
/**
* Configure the backend
*/
export async function configureApp(app: Express, databases: {[name: string]: DatabaseConstructor; }) {
export async function configureApp(app: Express, databases: {[name: string]: DatabaseConstructor}) {
let integrationTestTimeout: NodeJS.Timeout;
// request loggers have to be the first middleware to be set in express
app.use(morgan('dev', {
skip: (_req, res) => {
if (process.env.NODE_ENV === 'integration-test') {
clearTimeout(integrationTestTimeout);
integrationTestTimeout = setTimeout(() => {
process.exit(1);
},
DEFAULT_TIMEOUT);
app.use(
morgan('dev', {
skip: (_request, response) => {
if (process.env.NODE_ENV === 'integration-test') {
clearTimeout(integrationTestTimeout);
integrationTestTimeout = setTimeout(() => {
process.exit(1);
}, DEFAULT_TIMEOUT);
return false;
}
return false;
}
// tslint:disable-next-line: no-magic-numbers
return res.statusCode < 400;
}, stream: process.stdout,
}));
return response.statusCode < 400;
},
stream: process.stdout,
}),
);
if (process.env.PROMETHEUS_MIDDLEWARE === 'true') {
app.use(getPrometheusMiddleware());
@@ -80,7 +80,7 @@ export async function configureApp(app: Express, databases: {[name: string]: Dat
'X-StApps-Version',
],
credentials: true,
maxAge: 1728000,
maxAge: 1_728_000,
methods: ['GET', 'POST', 'PUT', 'OPTIONS'],
optionsSuccessStatus: 204,
};
@@ -93,13 +93,13 @@ export async function configureApp(app: Express, databases: {[name: string]: Dat
app.options('*', [cors(corsOptions)]);
// only accept json as content type for all requests
app.use((req, res, next) => {
app.use((request, response, next) => {
// Only accept json as content type
if (req.is('application/json') !== 'application/json') {
if (request.is('application/json') !== 'application/json') {
// return an error in the response
const err = new SCUnsupportedMediaTypeErrorResponse(isTestEnvironment);
res.status(err.statusCode);
res.json(err);
const error = new SCUnsupportedMediaTypeErrorResponse(isTestEnvironment);
response.status(error.statusCode);
response.json(error);
return;
}
@@ -111,12 +111,12 @@ export async function configureApp(app: Express, databases: {[name: string]: Dat
bodySize += chunk.byteLength;
// when adding each chunk size to the total size, check how large it now is.
if (bodySize > configFile.backend.maxRequestBodySize) {
req.off('data', chunkGatherer);
req.off('end', endCallback);
request.off('data', chunkGatherer);
request.off('end', endCallback);
// return an error in the response
const err = new SCRequestBodyTooLargeErrorResponse(isTestEnvironment);
res.status(err.statusCode);
res.json(err);
const error = new SCRequestBodyTooLargeErrorResponse(isTestEnvironment);
response.status(error.statusCode);
response.json(error);
return;
}
@@ -125,26 +125,24 @@ export async function configureApp(app: Express, databases: {[name: string]: Dat
};
const endCallback = () => {
req.body = Buffer.concat(bodyBuffer)
.toString();
request.body = Buffer.concat(bodyBuffer).toString();
try {
req.body = JSON.parse(req.body);
request.body = JSON.parse(request.body);
next();
} catch (catchErr) {
const err = new SCSyntaxErrorResponse(catchErr.message, isTestEnvironment);
res.status(err.statusCode);
res.json(err);
} catch (error) {
const error_ = new SCSyntaxErrorResponse(error.message, isTestEnvironment);
response.status(error_.statusCode);
response.json(error_);
return;
}
};
req.on('data', chunkGatherer)
.on('end', endCallback);
request.on('data', chunkGatherer).on('end', endCallback);
});
// validate config file
await validator.addSchemas(join('node_modules', '@openstapps', 'core', 'lib', 'schema'));
await validator.addSchemas(path.join('node_modules', '@openstapps', 'core', 'lib', 'schema'));
// validate the config file
const configValidation = validator.validate(configFile, 'SCConfigFile');
@@ -161,17 +159,16 @@ export async function configureApp(app: Express, databases: {[name: string]: Dat
throw new Error('You have to configure a database');
}
const database =
new databases[config.get<string>('internal.database.name')](
configFile,
// mailQueue
typeof mailer !== 'undefined' && config.has('internal.monitoring') ? new MailQueue(mailer) : undefined,
);
const database = new databases[config.get<string>('internal.database.name')](
configFile,
// mailQueue
typeof mailer !== 'undefined' && config.has('internal.monitoring') ? new MailQueue(mailer) : undefined,
);
await database.init();
if (typeof database === 'undefined') {
throw new Error('No implementation for configured database found. Please check your configuration.');
throw new TypeError('No implementation for configured database found. Please check your configuration.');
}
Logger.ok('Validated config file successfully');
@@ -181,10 +178,7 @@ export async function configureApp(app: Express, databases: {[name: string]: Dat
app.enable('strict routing');
// make the bulk storage available to all http middlewares/routes
app.set(
'bulk',
new BulkStorage(database),
);
app.set('bulk', new BulkStorage(database));
app.set('env', process.env.NODE_ENV);
@@ -202,15 +196,15 @@ export async function configureApp(app: Express, databases: {[name: string]: Dat
);
// for plugins, as Express doesn't really want you to unregister routes (and doesn't offer any method to do so at all)
app.all('*', async (req, res, next) => {
app.all('*', async (request, response, next) => {
// if the route exists then call virtual route on the plugin that registered that route
if (plugins.has(req.originalUrl)) {
if (plugins.has(request.originalUrl)) {
try {
res.json(await virtualPluginRoute(req, plugins.get(req.originalUrl)!));
} catch (e) {
response.json(await virtualPluginRoute(request, plugins.get(request.originalUrl)!));
} catch (error) {
// in case of error send an error response
res.status(e.statusCode);
res.json(e);
response.status(error.statusCode);
response.json(error);
}
} else {
// pass to the next matching route (which is 404)
@@ -219,9 +213,9 @@ export async function configureApp(app: Express, databases: {[name: string]: Dat
});
// add a route for a missing resource (404)
app.use((_req, res) => {
app.use((_request, response) => {
const errorResponse = new SCNotFoundErrorResponse(isTestEnvironment);
res.status(errorResponse.statusCode);
res.json(errorResponse);
response.status(errorResponse.statusCode);
response.json(errorResponse);
});
}