2020-04-09 01:41:45 +02:00
|
|
|
import _ from "lodash";
|
|
|
|
|
|
|
|
import CONSTRAINTS from "../validation/constraints";
|
|
|
|
import ErrorTypes from "../utils/errorTypes";
|
|
|
|
import Logger from "../logger";
|
|
|
|
import {Constraints, forConstraints, isConstraints} from "../validation/validator";
|
|
|
|
import {Request, Response} from "express";
|
2022-07-21 18:39:33 +02:00
|
|
|
import {
|
|
|
|
EnumTypeGuard,
|
|
|
|
EnumValue,
|
|
|
|
type GenericSortField,
|
2022-07-22 17:11:08 +02:00
|
|
|
isJSONObject, isNumber, isString, isUndefined,
|
2022-07-21 18:39:33 +02:00
|
|
|
JSONObject,
|
|
|
|
SortDirection,
|
|
|
|
TypeGuard
|
|
|
|
} from "../types";
|
|
|
|
|
|
|
|
export type RequestData = JSONObject;
|
|
|
|
export type RequestHandler = (request: Request, response: Response) => void;
|
2020-04-09 01:41:45 +02:00
|
|
|
|
2022-06-23 14:26:15 +02:00
|
|
|
export type Entity = { [key: string]: any };
|
2020-04-09 01:41:45 +02:00
|
|
|
|
|
|
|
export type RestParams = {
|
|
|
|
q?: string;
|
|
|
|
|
2022-06-23 14:26:15 +02:00
|
|
|
_sortField?: GenericSortField;
|
|
|
|
_sortDir?: SortDirection;
|
2020-04-09 01:41:45 +02:00
|
|
|
|
|
|
|
_page: number;
|
|
|
|
_perPage: number;
|
|
|
|
|
|
|
|
filters?: FilterClause;
|
|
|
|
};
|
|
|
|
|
2022-06-23 14:26:15 +02:00
|
|
|
export type OrderByClause = { query: string, params: any[] };
|
|
|
|
export type LimitOffsetClause = { query: string, params: any[] };
|
|
|
|
export type FilterClause = { query: string, params: any[] };
|
2020-04-09 01:41:45 +02:00
|
|
|
|
|
|
|
function respond(res: Response, httpCode: number, data: any, type: string): void {
|
|
|
|
switch (type) {
|
|
|
|
case 'html':
|
|
|
|
res.writeHead(httpCode, {'Content-Type': 'text/html'});
|
|
|
|
res.end(data);
|
2022-06-23 14:26:15 +02:00
|
|
|
break;
|
2020-04-09 01:41:45 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
res.writeHead(httpCode, {'Content-Type': 'application/json'});
|
|
|
|
res.end(JSON.stringify(data));
|
2022-06-23 14:26:15 +02:00
|
|
|
break;
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-23 14:26:15 +02:00
|
|
|
function orderByClause<S>(
|
2020-04-09 01:41:45 +02:00
|
|
|
restParams: RestParams,
|
2022-06-23 14:26:15 +02:00
|
|
|
defaultSortField: EnumValue<S>,
|
|
|
|
isSortField: EnumTypeGuard<S>,
|
2020-04-09 01:41:45 +02:00
|
|
|
): OrderByClause {
|
2022-06-23 14:26:15 +02:00
|
|
|
let sortField: EnumValue<S> | undefined = isSortField(restParams._sortField) ? restParams._sortField : undefined;
|
2020-04-09 01:41:45 +02:00
|
|
|
if (!sortField) {
|
|
|
|
sortField = defaultSortField;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2022-06-23 14:26:15 +02:00
|
|
|
query: 'ORDER BY LOWER(' + sortField + ') ' + (restParams._sortDir === SortDirection.ASCENDING ? 'ASC' : 'DESC'),
|
2020-04-09 01:41:45 +02:00
|
|
|
params: []
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function limitOffsetClause(restParams: RestParams): LimitOffsetClause {
|
|
|
|
const page = restParams._page;
|
|
|
|
const perPage = restParams._perPage;
|
|
|
|
|
|
|
|
return {
|
|
|
|
query: 'LIMIT ? OFFSET ?',
|
|
|
|
params: [perPage, ((page - 1) * perPage)]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function escapeForLikePattern(str: string): string {
|
|
|
|
return str
|
|
|
|
.replace(/\\/g, '\\\\')
|
|
|
|
.replace(/%/g, '\\%')
|
|
|
|
.replace(/_/g, '\\_');
|
|
|
|
}
|
|
|
|
|
|
|
|
function filterCondition(restParams: RestParams, filterFields: string[]): FilterClause {
|
|
|
|
if (_.isEmpty(filterFields)) {
|
|
|
|
return {
|
|
|
|
query: '1 = 1',
|
|
|
|
params: []
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-22 17:11:08 +02:00
|
|
|
let query = filterFields
|
|
|
|
.map(field => 'LOWER(' + field + ') LIKE ?')
|
|
|
|
.join(' OR ');
|
2020-04-09 01:41:45 +02:00
|
|
|
|
|
|
|
query += ' ESCAPE \'\\\'';
|
|
|
|
|
2022-07-22 17:11:08 +02:00
|
|
|
const search = '%' + (isString(restParams.q) ? escapeForLikePattern(restParams.q.trim().toLowerCase()) : '') + '%';
|
|
|
|
const params = _.times(filterFields.length, () => search);
|
2020-04-09 01:41:45 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
query: query,
|
|
|
|
params: params
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-06-23 14:26:15 +02:00
|
|
|
function getConstrainedValues(data: { [key: string]: any }, constraints: Constraints): { [key: string]: any } {
|
|
|
|
const values: { [key: string]: any } = {};
|
2022-07-22 17:11:08 +02:00
|
|
|
for (const key of Object.keys(constraints)) {
|
2020-04-09 01:41:45 +02:00
|
|
|
const value = data[key];
|
|
|
|
values[key] =
|
2022-07-22 17:11:08 +02:00
|
|
|
isUndefined(value) && key in constraints && !isUndefined(constraints[key].default)
|
2020-04-09 01:41:45 +02:00
|
|
|
? constraints[key].default
|
|
|
|
: value;
|
2022-07-22 17:11:08 +02:00
|
|
|
|
|
|
|
}
|
2020-04-09 01:41:45 +02:00
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
function normalize(data: any): JSONObject {
|
|
|
|
return isJSONObject(data) ? data : {};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getData(req: Request): RequestData {
|
|
|
|
const body = normalize(req.body);
|
|
|
|
const params = normalize(req.params);
|
|
|
|
const query = normalize(req.query);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...body,
|
|
|
|
...params,
|
|
|
|
...query,
|
|
|
|
};
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
|
|
|
|
2020-04-10 00:43:15 +02:00
|
|
|
export async function getValidRestParams(
|
2020-04-09 01:41:45 +02:00
|
|
|
type: string,
|
2020-04-09 20:18:13 +02:00
|
|
|
subtype: string | null,
|
2020-04-09 01:41:45 +02:00
|
|
|
req: Request,
|
2020-04-10 00:43:15 +02:00
|
|
|
): Promise<RestParams> {
|
2022-06-23 14:26:15 +02:00
|
|
|
const restConstraints = CONSTRAINTS.rest as { [key: string]: any };
|
2020-04-09 01:41:45 +02:00
|
|
|
let constraints: Constraints;
|
|
|
|
if (!(type in restConstraints) || !isConstraints(restConstraints[type])) {
|
|
|
|
Logger.tag('validation', 'rest').error('Unknown REST resource type: {}', type);
|
2020-04-10 00:43:15 +02:00
|
|
|
throw {data: 'Internal error.', type: ErrorTypes.internalError};
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
|
|
|
constraints = restConstraints[type];
|
|
|
|
|
|
|
|
let filterConstraints: Constraints = {};
|
|
|
|
if (subtype) {
|
|
|
|
const subtypeFilters = subtype + 'Filters';
|
2022-06-23 14:26:15 +02:00
|
|
|
const constraintsObj = CONSTRAINTS as { [key: string]: any };
|
2020-04-09 01:41:45 +02:00
|
|
|
if (!(subtypeFilters in constraintsObj) || !isConstraints(constraintsObj[subtypeFilters])) {
|
|
|
|
Logger.tag('validation', 'rest').error('Unknown REST resource subtype: {}', subtype);
|
2020-04-10 00:43:15 +02:00
|
|
|
throw {data: 'Internal error.', type: ErrorTypes.internalError};
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
|
|
|
filterConstraints = constraintsObj[subtypeFilters];
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = getData(req);
|
|
|
|
|
|
|
|
const restParams = getConstrainedValues(data, constraints);
|
|
|
|
const filterParams = getConstrainedValues(data, filterConstraints);
|
|
|
|
|
|
|
|
const areValidParams = forConstraints(constraints, false);
|
|
|
|
const areValidFilters = forConstraints(filterConstraints, false);
|
|
|
|
if (!areValidParams(restParams) || !areValidFilters(filterParams)) {
|
2020-04-10 00:43:15 +02:00
|
|
|
throw {data: 'Invalid REST parameters.', type: ErrorTypes.badRequest};
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
restParams.filters = filterParams;
|
2020-04-10 00:43:15 +02:00
|
|
|
return restParams as RestParams;
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
|
|
|
|
2022-07-22 17:11:08 +02:00
|
|
|
export function filter<E>(entities: E[], allowedFilterFields: string[], restParams: RestParams): E[] {
|
2020-04-09 01:41:45 +02:00
|
|
|
let query = restParams.q;
|
|
|
|
if (query) {
|
2022-07-22 17:11:08 +02:00
|
|
|
query = query.trim().toLowerCase();
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function queryMatches(entity: Entity): boolean {
|
|
|
|
if (!query) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-07-28 13:16:13 +02:00
|
|
|
return allowedFilterFields.some((field: string): boolean => {
|
2020-04-09 01:41:45 +02:00
|
|
|
if (!query) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
let value = entity[field];
|
2022-07-22 17:11:08 +02:00
|
|
|
if (isNumber(value)) {
|
2020-04-09 01:41:45 +02:00
|
|
|
value = value.toString();
|
|
|
|
}
|
|
|
|
|
2022-07-22 17:11:08 +02:00
|
|
|
if (!isString(value) || _.isEmpty(value)) {
|
2020-04-09 01:41:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-22 17:11:08 +02:00
|
|
|
value = value.toLowerCase();
|
2020-04-09 01:41:45 +02:00
|
|
|
if (field === 'mac') {
|
|
|
|
return _.includes(value.replace(/:/g, ''), query.replace(/:/g, ''));
|
|
|
|
}
|
|
|
|
|
|
|
|
return _.includes(value, query);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const filters = restParams.filters;
|
|
|
|
|
|
|
|
function filtersMatch(entity: Entity): boolean {
|
2022-07-28 13:16:13 +02:00
|
|
|
if (isUndefined(filters) || _.isEmpty(filters)) {
|
2020-04-09 01:41:45 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:16:13 +02:00
|
|
|
return Object.entries(filters).every(([key, value]) => {
|
2022-07-22 17:11:08 +02:00
|
|
|
if (isUndefined(value)) {
|
2020-04-09 01:41:45 +02:00
|
|
|
return true;
|
|
|
|
}
|
2022-07-28 13:16:13 +02:00
|
|
|
if (key.startsWith('has')) {
|
2022-07-21 18:39:33 +02:00
|
|
|
const entityKey = key.substring(3, 4).toLowerCase() + key.substring(4);
|
2020-04-09 01:41:45 +02:00
|
|
|
return _.isEmpty(entity[entityKey]).toString() !== value;
|
|
|
|
}
|
|
|
|
return entity[key] === value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:16:13 +02:00
|
|
|
return entities.filter(entity => queryMatches(entity) && filtersMatch(entity));
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
|
|
|
|
2022-07-22 17:11:08 +02:00
|
|
|
export function sort<T extends Record<S, any>, S extends string>(entities: T[], isSortField: TypeGuard<S>, restParams: RestParams): T[] {
|
2022-06-23 14:26:15 +02:00
|
|
|
const sortField: S | undefined = isSortField(restParams._sortField) ? restParams._sortField : undefined;
|
2020-04-09 01:41:45 +02:00
|
|
|
if (!sortField) {
|
|
|
|
return entities;
|
|
|
|
}
|
|
|
|
|
2022-07-22 17:11:08 +02:00
|
|
|
const sorted = entities.slice(0);
|
|
|
|
sorted.sort((a, b) => {
|
|
|
|
let as = a[sortField];
|
|
|
|
let bs = b[sortField];
|
|
|
|
|
|
|
|
if (isString(as)) {
|
|
|
|
as = as.toLowerCase();
|
|
|
|
}
|
|
|
|
if (isString(bs)) {
|
|
|
|
bs = bs.toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
let order = 0;
|
|
|
|
if (as < bs) {
|
|
|
|
order = -1;
|
|
|
|
}
|
|
|
|
else if (bs > as) {
|
|
|
|
order = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return restParams._sortDir === SortDirection.DESCENDING ? -order : order;
|
|
|
|
});
|
|
|
|
|
|
|
|
return sorted;
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:16:13 +02:00
|
|
|
export function getPageEntities<Entity>(entities: Entity[], restParams: RestParams): Entity[] {
|
2020-04-09 01:41:45 +02:00
|
|
|
const page = restParams._page;
|
|
|
|
const perPage = restParams._perPage;
|
|
|
|
|
2022-07-22 17:11:08 +02:00
|
|
|
return entities.slice((page - 1) * perPage, page * perPage);
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export {filterCondition as whereCondition};
|
|
|
|
|
2022-06-23 14:26:15 +02:00
|
|
|
export function filterClause<S>(
|
2020-04-09 01:41:45 +02:00
|
|
|
restParams: RestParams,
|
2022-06-23 14:26:15 +02:00
|
|
|
defaultSortField: EnumValue<S>,
|
|
|
|
isSortField: EnumTypeGuard<S>,
|
2020-04-09 01:41:45 +02:00
|
|
|
filterFields: string[],
|
|
|
|
): FilterClause {
|
2022-06-23 14:26:15 +02:00
|
|
|
const orderBy = orderByClause<S>(
|
2020-04-09 01:41:45 +02:00
|
|
|
restParams,
|
|
|
|
defaultSortField,
|
2022-06-23 14:26:15 +02:00
|
|
|
isSortField,
|
2020-04-09 01:41:45 +02:00
|
|
|
);
|
|
|
|
const limitOffset = limitOffsetClause(restParams);
|
|
|
|
|
|
|
|
const filter = filterCondition(
|
|
|
|
restParams,
|
|
|
|
filterFields
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
query: filter.query + ' ' + orderBy.query + ' ' + limitOffset.query,
|
2022-07-28 13:16:13 +02:00
|
|
|
params: [...filter.params, ...orderBy.params, ...limitOffset.params]
|
2020-04-09 01:41:45 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-06-23 14:26:15 +02:00
|
|
|
export function success(res: Response, data: any) {
|
2020-04-09 01:41:45 +02:00
|
|
|
respond(res, 200, data, 'json');
|
|
|
|
}
|
|
|
|
|
2022-06-23 14:26:15 +02:00
|
|
|
export function successHtml(res: Response, html: string) {
|
2020-04-09 01:41:45 +02:00
|
|
|
respond(res, 200, html, 'html');
|
|
|
|
}
|
|
|
|
|
2022-06-23 14:26:15 +02:00
|
|
|
export function error(res: Response, err: { data: any, type: { code: number } }) {
|
2020-04-09 01:41:45 +02:00
|
|
|
respond(res, err.type.code, err.data, 'json');
|
|
|
|
}
|
2022-07-21 18:39:33 +02:00
|
|
|
|
|
|
|
export function handleJSON<Response>(handler: () => Promise<Response>): RequestHandler {
|
|
|
|
return (request, response) => {
|
|
|
|
handler()
|
|
|
|
.then(data => success(response, data || {}))
|
|
|
|
.catch(error => error(response, error));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function handleJSONWithData<Response>(handler: (data: RequestData) => Promise<Response>): RequestHandler {
|
|
|
|
return (request, response) => {
|
|
|
|
handler(getData(request))
|
|
|
|
.then(data => success(response, data || {}))
|
|
|
|
.catch(error => error(response, error));
|
|
|
|
};
|
|
|
|
}
|