mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 17:12:43 +00:00
committed by
Rainer Killinger
parent
42c7350c36
commit
8b457c9911
@@ -14,10 +14,14 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {SCBulkAddRequest, SCBulkAddResponse, SCBulkAddRoute, SCNotFoundErrorResponse} from '@openstapps/core';
|
||||
import {isTestEnvironment, logger} from '../common';
|
||||
import {BulkStorage} from '../storage/BulkStorage';
|
||||
import {createRoute} from './Route';
|
||||
import {Logger} from '@openstapps/logger';
|
||||
import {isTestEnvironment} from '../common';
|
||||
import {BulkStorage} from '../storage/bulk-storage';
|
||||
import {createRoute} from './route';
|
||||
|
||||
/**
|
||||
* Contains information for using the route for adding bulks
|
||||
*/
|
||||
const bulkRouteModel = new SCBulkAddRoute();
|
||||
|
||||
/**
|
||||
@@ -27,7 +31,7 @@ export const bulkAddRouter = createRoute<SCBulkAddResponse>(
|
||||
bulkRouteModel,
|
||||
async (request: SCBulkAddRequest, app, params) => {
|
||||
|
||||
if (!params || typeof params.UID !== 'string') {
|
||||
if (typeof params === 'undefined' || typeof params.UID !== 'string') {
|
||||
throw new Error('UID of Bulk was not given, but route with obligatory parameter was called');
|
||||
}
|
||||
|
||||
@@ -35,7 +39,7 @@ export const bulkAddRouter = createRoute<SCBulkAddResponse>(
|
||||
const bulk = await bulkMemory.read(params.UID);
|
||||
|
||||
if (typeof bulk === 'undefined') {
|
||||
logger.warn(`Bulk with ${params.UID} not found.`);
|
||||
Logger.warn(`Bulk with ${params.UID} not found.`);
|
||||
throw new SCNotFoundErrorResponse(isTestEnvironment);
|
||||
}
|
||||
|
||||
@@ -14,10 +14,14 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {SCBulkDoneRequest, SCBulkDoneResponse, SCBulkDoneRoute, SCNotFoundErrorResponse} from '@openstapps/core';
|
||||
import {isTestEnvironment, logger} from '../common';
|
||||
import {BulkStorage} from '../storage/BulkStorage';
|
||||
import {createRoute} from './Route';
|
||||
import {Logger} from '@openstapps/logger';
|
||||
import {isTestEnvironment} from '../common';
|
||||
import {BulkStorage} from '../storage/bulk-storage';
|
||||
import {createRoute} from './route';
|
||||
|
||||
/**
|
||||
* Contains information for using the route for closing bulks
|
||||
*/
|
||||
const bulkDoneRouteModel = new SCBulkDoneRoute();
|
||||
|
||||
/**
|
||||
@@ -27,7 +31,7 @@ export const bulkDoneRouter = createRoute<SCBulkDoneResponse>(
|
||||
bulkDoneRouteModel,
|
||||
async (_request: SCBulkDoneRequest, app, params) => {
|
||||
|
||||
if (!params || typeof params.UID !== 'string') {
|
||||
if (typeof params === 'undefined' || typeof params.UID !== 'string') {
|
||||
throw new Error('UID of Bulk was not given, but route with obligatory parameter was called');
|
||||
}
|
||||
|
||||
@@ -35,12 +39,13 @@ export const bulkDoneRouter = createRoute<SCBulkDoneResponse>(
|
||||
const bulk = await bulkMemory.read(params.UID);
|
||||
|
||||
if (typeof bulk === 'undefined') {
|
||||
logger.warn(`Bulk with ${params.UID} not found.`);
|
||||
Logger.warn(`Bulk with ${params.UID} not found.`);
|
||||
throw new SCNotFoundErrorResponse(isTestEnvironment);
|
||||
}
|
||||
|
||||
bulk.state = 'done';
|
||||
await bulkMemory.markAsDone(bulk);
|
||||
|
||||
return {};
|
||||
},
|
||||
);
|
||||
@@ -14,9 +14,12 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {SCBulkRequest, SCBulkResponse, SCBulkRoute} from '@openstapps/core';
|
||||
import {BulkStorage} from '../storage/BulkStorage';
|
||||
import {createRoute} from './Route';
|
||||
import {BulkStorage} from '../storage/bulk-storage';
|
||||
import {createRoute} from './route';
|
||||
|
||||
/**
|
||||
* Contains information for using the route for creating bulks
|
||||
*/
|
||||
const bulkRouteModel = new SCBulkRoute();
|
||||
|
||||
/**
|
||||
@@ -26,6 +29,7 @@ export const bulkRouter = createRoute<SCBulkResponse>(
|
||||
bulkRouteModel,
|
||||
async (request: SCBulkRequest, app) => {
|
||||
const bulkMemory: BulkStorage = app.get('bulk');
|
||||
return await bulkMemory.create(request);
|
||||
|
||||
return bulkMemory.create(request);
|
||||
},
|
||||
);
|
||||
@@ -13,6 +13,9 @@
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/**
|
||||
* Strings that can be used as HTTP verbs (e.g. in requests)
|
||||
*/
|
||||
export type HTTPVerb = 'all' |
|
||||
'get' |
|
||||
'post' |
|
||||
@@ -38,6 +41,11 @@ export type HTTPVerb = 'all' |
|
||||
'unlock' |
|
||||
'unsubscribe';
|
||||
|
||||
/**
|
||||
* Provides information if a text (representing a method) is an HTTP verb
|
||||
*
|
||||
* @param method A text (representing a method) to check
|
||||
*/
|
||||
export function isHttpMethod(method: string): method is HTTPVerb {
|
||||
return ['get', 'post', 'put'].indexOf(method) > -1;
|
||||
}
|
||||
@@ -15,8 +15,11 @@
|
||||
*/
|
||||
import {SCIndexResponse, SCIndexRoute} from '@openstapps/core';
|
||||
import {configFile} from '../common';
|
||||
import {createRoute} from './Route';
|
||||
import {createRoute} from './route';
|
||||
|
||||
/**
|
||||
* Contains information for using the index route
|
||||
*/
|
||||
const indexRouteModel = new SCIndexRoute();
|
||||
|
||||
/**
|
||||
@@ -21,9 +21,11 @@ import {
|
||||
SCTooManyRequestsErrorResponse,
|
||||
} from '@openstapps/core';
|
||||
import {configFile, isTestEnvironment} from '../common';
|
||||
import {BulkStorage} from '../storage/BulkStorage';
|
||||
import {createRoute} from './Route';
|
||||
|
||||
import {BulkStorage} from '../storage/bulk-storage';
|
||||
import {createRoute} from './route';
|
||||
/**
|
||||
* Contains information for using the multi search route
|
||||
*/
|
||||
const multiSearchRouteModel = new SCMultiSearchRoute();
|
||||
|
||||
/**
|
||||
@@ -41,13 +43,13 @@ export const multiSearchRouter = createRoute<SCMultiSearchResponse | SCTooManyRe
|
||||
}
|
||||
|
||||
// get a map of promises for each query
|
||||
const searchRequests = queryNames.map((queryName) => {
|
||||
const searchRequests = queryNames.map(async (queryName) => {
|
||||
return bulkMemory.database.search(request[queryName]);
|
||||
});
|
||||
|
||||
const listOfSearchResponses = await Promise.all(searchRequests);
|
||||
|
||||
const response: { [queryName: string]: SCSearchResponse } = {};
|
||||
const response: { [queryName: string]: SCSearchResponse; } = {};
|
||||
queryNames.forEach((queryName, index) => {
|
||||
response[queryName] = listOfSearchResponses[index];
|
||||
});
|
||||
@@ -19,24 +19,26 @@ import {
|
||||
SCRoute,
|
||||
SCValidationErrorResponse,
|
||||
} from '@openstapps/core';
|
||||
import {Logger} from '@openstapps/logger';
|
||||
import {Application, Router} from 'express';
|
||||
import PromiseRouter from 'express-promise-router';
|
||||
import {ValidationError} from 'jsonschema';
|
||||
import {isTestEnvironment, logger, validator} from '../common';
|
||||
import {isHttpMethod} from './HTTPTypes';
|
||||
import {isTestEnvironment, validator} from '../common';
|
||||
import {isHttpMethod} from './http-types';
|
||||
|
||||
/**
|
||||
* Creates a router from a route class (model of a route) and a handler function which implements the logic
|
||||
* Creates a router from a route class and a handler function which implements the logic
|
||||
*
|
||||
* The given router performs a request and respone validation, sets status codes and checks if the given handler
|
||||
* only returns errors that are allowed for the client to see
|
||||
*
|
||||
* @param routeClass
|
||||
* @param handler
|
||||
* @param routeClass Model of a route
|
||||
* @param handler Implements the logic of the route
|
||||
*/
|
||||
export function createRoute<RETURNTYPE>(
|
||||
routeClass: SCRoute,
|
||||
handler: (validatedBody: any, app: Application, params?: { [parameterName: string]: string }) => Promise<RETURNTYPE>,
|
||||
// tslint:disable-next-line: no-any
|
||||
handler: (validatedBody: any, app: Application, params?: { [parameterName: string]: string; }) => Promise<RETURNTYPE>,
|
||||
): Router {
|
||||
// create router
|
||||
const router = PromiseRouter({mergeParams: true});
|
||||
@@ -80,7 +82,8 @@ export function createRoute<RETURNTYPE>(
|
||||
);
|
||||
res.status(error.statusCode);
|
||||
res.json(error);
|
||||
logger.warn(error);
|
||||
Logger.warn(error);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -103,7 +106,8 @@ export function createRoute<RETURNTYPE>(
|
||||
);
|
||||
res.status(internalServerError.statusCode);
|
||||
res.json(internalServerError);
|
||||
logger.warn(internalServerError);
|
||||
Logger.warn(internalServerError);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -118,7 +122,7 @@ export function createRoute<RETURNTYPE>(
|
||||
// respond with the error from the handler
|
||||
res.status(error.statusCode);
|
||||
res.json(error);
|
||||
logger.warn(error);
|
||||
Logger.warn(error);
|
||||
} else {
|
||||
// the error is not allowed so something went wrong
|
||||
const internalServerError = new SCInternalServerErrorResponse(
|
||||
@@ -127,7 +131,7 @@ export function createRoute<RETURNTYPE>(
|
||||
);
|
||||
res.status(internalServerError.statusCode);
|
||||
res.json(internalServerError);
|
||||
logger.error(error);
|
||||
await Logger.error(error);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -140,7 +144,7 @@ export function createRoute<RETURNTYPE>(
|
||||
const error = new SCMethodNotAllowedErrorResponse(isTestEnvironment);
|
||||
res.status(error.statusCode);
|
||||
res.json(error);
|
||||
logger.warn(error);
|
||||
Logger.warn(error);
|
||||
});
|
||||
|
||||
// return router
|
||||
@@ -14,9 +14,12 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {SCSearchRequest, SCSearchResponse, SCSearchRoute} from '@openstapps/core';
|
||||
import {BulkStorage} from '../storage/BulkStorage';
|
||||
import {createRoute} from './Route';
|
||||
import {BulkStorage} from '../storage/bulk-storage';
|
||||
import {createRoute} from './route';
|
||||
|
||||
/**
|
||||
* Contains information for using the search route
|
||||
*/
|
||||
const searchRouteModel = new SCSearchRoute();
|
||||
|
||||
/**
|
||||
@@ -24,5 +27,6 @@ const searchRouteModel = new SCSearchRoute();
|
||||
*/
|
||||
export const searchRouter = createRoute<SCSearchResponse>(searchRouteModel, async ( request: SCSearchRequest, app) => {
|
||||
const bulkMemory: BulkStorage = app.get('bulk');
|
||||
return await bulkMemory.database.search(request);
|
||||
|
||||
return bulkMemory.database.search(request);
|
||||
});
|
||||
@@ -14,9 +14,12 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {SCThingUpdateRequest, SCThingUpdateResponse, SCThingUpdateRoute} from '@openstapps/core';
|
||||
import {BulkStorage} from '../storage/BulkStorage';
|
||||
import {createRoute} from './Route';
|
||||
import {BulkStorage} from '../storage/bulk-storage';
|
||||
import {createRoute} from './route';
|
||||
|
||||
/**
|
||||
* Contains information for using the route for updating single things
|
||||
*/
|
||||
const thingUpdateRouteModel = new SCThingUpdateRoute();
|
||||
|
||||
/**
|
||||
@@ -27,6 +30,7 @@ export const thingUpdateRouter = createRoute<SCThingUpdateResponse>(
|
||||
async (request: SCThingUpdateRequest, app) => {
|
||||
const bulkMemory: BulkStorage = app.get('bulk');
|
||||
await bulkMemory.database.put(request);
|
||||
|
||||
return {};
|
||||
},
|
||||
);
|
||||
Reference in New Issue
Block a user