2020-04-09 01:41:45 +02:00
|
|
|
import _ from "lodash";
|
|
|
|
|
2022-08-04 15:31:01 +02:00
|
|
|
import CONSTRAINTS from "../shared/validation/constraints";
|
2022-08-23 20:08:53 +02:00
|
|
|
import ErrorTypes from "./errorTypes";
|
2020-04-09 01:41:45 +02:00
|
|
|
import Logger from "../logger";
|
2022-08-23 20:08:53 +02:00
|
|
|
import {
|
|
|
|
Constraints,
|
|
|
|
forConstraints,
|
|
|
|
isConstraints,
|
2022-08-23 21:38:37 +02:00
|
|
|
NestedConstraints,
|
2022-08-23 20:08:53 +02:00
|
|
|
} from "../shared/validation/validator";
|
2022-09-14 16:33:43 +02:00
|
|
|
import type { Request, Response } from "express";
|
2022-07-21 18:39:33 +02:00
|
|
|
import {
|
|
|
|
type GenericSortField,
|
2022-07-28 13:49:22 +02:00
|
|
|
isJSONObject,
|
|
|
|
isNumber,
|
|
|
|
isString,
|
|
|
|
isUndefined,
|
2022-07-21 18:39:33 +02:00
|
|
|
JSONObject,
|
2022-07-28 13:49:22 +02:00
|
|
|
JSONValue,
|
2022-07-21 18:39:33 +02:00
|
|
|
SortDirection,
|
2022-08-23 20:08:53 +02:00
|
|
|
TypeGuard,
|
2022-07-21 18:39:33 +02:00
|
|
|
} from "../types";
|
2022-09-06 19:09:25 +02:00
|
|
|
import { getFieldIfExists } from "../shared/utils/objects";
|
2022-07-21 18:39:33 +02:00
|
|
|
|
|
|
|
export type RequestData = JSONObject;
|
|
|
|
export type RequestHandler = (request: Request, response: Response) => void;
|
2020-04-09 01:41:45 +02:00
|
|
|
|
2022-08-23 21:38:37 +02:00
|
|
|
export type Entity = { [key: string]: unknown };
|
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-08-23 20:08:53 +02:00
|
|
|
export type OrderByClause = { query: string; params: unknown[] };
|
|
|
|
export type LimitOffsetClause = { query: string; params: unknown[] };
|
|
|
|
export type FilterClause = { query: string; params: unknown[] };
|
|
|
|
|
|
|
|
function respond(
|
|
|
|
res: Response,
|
|
|
|
httpCode: number,
|
|
|
|
data: string,
|
|
|
|
type: "html"
|
|
|
|
): void;
|
|
|
|
function respond(
|
|
|
|
res: Response,
|
|
|
|
httpCode: number,
|
|
|
|
data: JSONValue,
|
|
|
|
type: "json"
|
|
|
|
): void;
|
|
|
|
function respond(
|
|
|
|
res: Response,
|
|
|
|
httpCode: number,
|
|
|
|
data: JSONValue,
|
|
|
|
type: "html" | "json"
|
|
|
|
): void {
|
2020-04-09 01:41:45 +02:00
|
|
|
switch (type) {
|
2022-08-23 20:08:53 +02:00
|
|
|
case "html":
|
|
|
|
res.writeHead(httpCode, { "Content-Type": "text/html" });
|
2020-04-09 01:41:45 +02:00
|
|
|
res.end(data);
|
2022-06-23 14:26:15 +02:00
|
|
|
break;
|
2020-04-09 01:41:45 +02:00
|
|
|
|
|
|
|
default:
|
2022-08-23 20:08:53 +02:00
|
|
|
res.writeHead(httpCode, { "Content-Type": "application/json" });
|
2020-04-09 01:41:45 +02:00
|
|
|
res.end(JSON.stringify(data));
|
2022-06-23 14:26:15 +02:00
|
|
|
break;
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 19:09:25 +02:00
|
|
|
function orderByClause<SortField>(
|
2020-04-09 01:41:45 +02:00
|
|
|
restParams: RestParams,
|
2022-09-06 19:09:25 +02:00
|
|
|
defaultSortField: SortField,
|
|
|
|
isSortField: TypeGuard<SortField>
|
2020-04-09 01:41:45 +02:00
|
|
|
): OrderByClause {
|
2022-09-06 19:09:25 +02:00
|
|
|
let sortField: SortField | undefined = isSortField(restParams._sortField)
|
2022-08-23 20:08:53 +02:00
|
|
|
? restParams._sortField
|
|
|
|
: undefined;
|
2020-04-09 01:41:45 +02:00
|
|
|
if (!sortField) {
|
|
|
|
sortField = defaultSortField;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2022-08-23 20:08:53 +02:00
|
|
|
query:
|
|
|
|
"ORDER BY LOWER(" +
|
|
|
|
sortField +
|
|
|
|
") " +
|
|
|
|
(restParams._sortDir === SortDirection.ASCENDING ? "ASC" : "DESC"),
|
|
|
|
params: [],
|
2020-04-09 01:41:45 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function limitOffsetClause(restParams: RestParams): LimitOffsetClause {
|
|
|
|
const page = restParams._page;
|
|
|
|
const perPage = restParams._perPage;
|
|
|
|
|
|
|
|
return {
|
2022-08-23 20:08:53 +02:00
|
|
|
query: "LIMIT ? OFFSET ?",
|
|
|
|
params: [perPage, (page - 1) * perPage],
|
2020-04-09 01:41:45 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function escapeForLikePattern(str: string): string {
|
2022-08-23 20:08:53 +02:00
|
|
|
return str.replace(/\\/g, "\\\\").replace(/%/g, "\\%").replace(/_/g, "\\_");
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
function filterCondition(
|
|
|
|
restParams: RestParams,
|
|
|
|
filterFields: string[]
|
|
|
|
): FilterClause {
|
2020-04-09 01:41:45 +02:00
|
|
|
if (_.isEmpty(filterFields)) {
|
|
|
|
return {
|
2022-08-23 20:08:53 +02:00
|
|
|
query: "1 = 1",
|
|
|
|
params: [],
|
2020-04-09 01:41:45 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-22 17:11:08 +02:00
|
|
|
let query = filterFields
|
2022-08-23 20:08:53 +02:00
|
|
|
.map((field) => "LOWER(" + field + ") LIKE ?")
|
|
|
|
.join(" OR ");
|
2020-04-09 01:41:45 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
query += " ESCAPE '\\'";
|
2020-04-09 01:41:45 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const search =
|
|
|
|
"%" +
|
|
|
|
(isString(restParams.q)
|
|
|
|
? escapeForLikePattern(restParams.q.trim().toLowerCase())
|
|
|
|
: "") +
|
|
|
|
"%";
|
2022-07-22 17:11:08 +02:00
|
|
|
const params = _.times(filterFields.length, () => search);
|
2020-04-09 01:41:45 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
query: query,
|
2022-08-23 20:08:53 +02:00
|
|
|
params: params,
|
2020-04-09 01:41:45 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
function getConstrainedValues(
|
|
|
|
data: { [key: string]: unknown },
|
|
|
|
constraints: Constraints
|
|
|
|
): { [key: string]: unknown } {
|
|
|
|
const values: { [key: string]: unknown } = {};
|
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-08-23 20:08:53 +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-08-23 20:08:53 +02:00
|
|
|
function normalize(data: unknown): JSONObject {
|
2022-07-21 18:39:33 +02:00
|
|
|
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,
|
2022-08-23 20:08:53 +02:00
|
|
|
req: Request
|
2020-04-10 00:43:15 +02:00
|
|
|
): Promise<RestParams> {
|
2022-08-23 21:38:37 +02:00
|
|
|
const restConstraints = CONSTRAINTS.rest as { [key: string]: Constraints };
|
2020-04-09 01:41:45 +02:00
|
|
|
if (!(type in restConstraints) || !isConstraints(restConstraints[type])) {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("validation", "rest").error(
|
|
|
|
"Unknown REST resource type: {}",
|
|
|
|
type
|
|
|
|
);
|
|
|
|
throw { data: "Internal error.", type: ErrorTypes.internalError };
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
2022-08-23 20:08:53 +02:00
|
|
|
const constraints: Constraints = restConstraints[type];
|
2020-04-09 01:41:45 +02:00
|
|
|
|
|
|
|
let filterConstraints: Constraints = {};
|
|
|
|
if (subtype) {
|
2022-08-23 20:08:53 +02:00
|
|
|
const subtypeFilters = subtype + "Filters";
|
2022-08-23 21:38:37 +02:00
|
|
|
const nestedConstraints = CONSTRAINTS as NestedConstraints;
|
|
|
|
const subConstraints = nestedConstraints[subtypeFilters];
|
|
|
|
if (!isConstraints(subConstraints)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("validation", "rest").error(
|
|
|
|
"Unknown REST resource subtype: {}",
|
|
|
|
subtype
|
|
|
|
);
|
|
|
|
throw { data: "Internal error.", type: ErrorTypes.internalError };
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
2022-08-23 21:38:37 +02:00
|
|
|
filterConstraints = subConstraints;
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
2022-08-23 20:08:53 +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-08-23 20:08:53 +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
|
|
|
}
|
|
|
|
|
2022-08-23 21:38:37 +02:00
|
|
|
function queryMatches(entity: E): boolean {
|
2020-04-09 01:41:45 +02:00
|
|
|
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;
|
|
|
|
}
|
2022-08-23 21:38:37 +02:00
|
|
|
let value = getFieldIfExists(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-08-23 21:38:37 +02:00
|
|
|
const lowerCaseValue = value.toLowerCase();
|
2022-08-23 20:08:53 +02:00
|
|
|
if (field === "mac") {
|
|
|
|
return _.includes(
|
2022-08-23 21:38:37 +02:00
|
|
|
lowerCaseValue.replace(/:/g, ""),
|
2022-08-23 20:08:53 +02:00
|
|
|
query.replace(/:/g, "")
|
|
|
|
);
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 21:38:37 +02:00
|
|
|
return _.includes(lowerCaseValue, query);
|
2020-04-09 01:41:45 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const filters = restParams.filters;
|
|
|
|
|
2022-08-23 21:38:37 +02:00
|
|
|
function filtersMatch(entity: E): 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-08-23 20:08:53 +02:00
|
|
|
if (key.startsWith("has")) {
|
|
|
|
const entityKey =
|
|
|
|
key.substring(3, 4).toLowerCase() + key.substring(4);
|
2022-08-23 21:38:37 +02:00
|
|
|
return (
|
|
|
|
_.isEmpty(
|
|
|
|
getFieldIfExists(entity, entityKey)
|
|
|
|
).toString() !== value
|
|
|
|
);
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
2022-08-23 21:38:37 +02:00
|
|
|
return getFieldIfExists(entity, key) === value;
|
2020-04-09 01:41:45 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
return entities.filter(
|
|
|
|
(entity) => queryMatches(entity) && filtersMatch(entity)
|
|
|
|
);
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 21:01:58 +02:00
|
|
|
export function sort<
|
2022-08-23 21:38:37 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
Type extends { [Key in SortField]: any },
|
2022-08-23 21:01:58 +02:00
|
|
|
SortField extends string
|
|
|
|
>(
|
|
|
|
entities: Type[],
|
|
|
|
isSortField: TypeGuard<SortField>,
|
2022-08-23 20:08:53 +02:00
|
|
|
restParams: RestParams
|
2022-08-23 21:01:58 +02:00
|
|
|
): Type[] {
|
|
|
|
const sortField: SortField | undefined = isSortField(restParams._sortField)
|
2022-08-23 20:08:53 +02:00
|
|
|
? 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) => {
|
2022-08-23 21:38:37 +02:00
|
|
|
let as = a[sortField];
|
|
|
|
let bs = b[sortField];
|
2022-07-22 17:11:08 +02:00
|
|
|
|
|
|
|
if (isString(as)) {
|
|
|
|
as = as.toLowerCase();
|
|
|
|
}
|
|
|
|
if (isString(bs)) {
|
|
|
|
bs = bs.toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
let order = 0;
|
|
|
|
if (as < bs) {
|
|
|
|
order = -1;
|
2022-08-24 17:32:36 +02:00
|
|
|
} else if (as > bs) {
|
2022-07-22 17:11:08 +02:00
|
|
|
order = 1;
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
return restParams._sortDir === SortDirection.DESCENDING
|
|
|
|
? -order
|
|
|
|
: order;
|
2022-07-22 17:11:08 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return sorted;
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +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
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export { filterCondition as whereCondition };
|
2020-04-09 01:41:45 +02:00
|
|
|
|
2022-09-06 19:09:25 +02:00
|
|
|
export function filterClause<SortField>(
|
2020-04-09 01:41:45 +02:00
|
|
|
restParams: RestParams,
|
2022-09-06 19:09:25 +02:00
|
|
|
defaultSortField: SortField,
|
|
|
|
isSortField: TypeGuard<SortField>,
|
2022-08-23 20:08:53 +02:00
|
|
|
filterFields: string[]
|
2020-04-09 01:41:45 +02:00
|
|
|
): FilterClause {
|
2022-09-06 19:09:25 +02:00
|
|
|
const orderBy = orderByClause<SortField>(
|
|
|
|
restParams,
|
|
|
|
defaultSortField,
|
|
|
|
isSortField
|
|
|
|
);
|
2020-04-09 01:41:45 +02:00
|
|
|
const limitOffset = limitOffsetClause(restParams);
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const filter = filterCondition(restParams, filterFields);
|
2020-04-09 01:41:45 +02:00
|
|
|
|
|
|
|
return {
|
2022-08-23 20:08:53 +02:00
|
|
|
query: filter.query + " " + orderBy.query + " " + limitOffset.query,
|
|
|
|
params: [...filter.params, ...orderBy.params, ...limitOffset.params],
|
2020-04-09 01:41:45 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:49:22 +02:00
|
|
|
export function success(res: Response, data: JSONValue) {
|
2022-08-23 20:08:53 +02:00
|
|
|
respond(res, 200, data, "json");
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
|
|
|
|
2022-06-23 14:26:15 +02:00
|
|
|
export function successHtml(res: Response, html: string) {
|
2022-08-23 20:08:53 +02:00
|
|
|
respond(res, 200, html, "html");
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export function error(
|
|
|
|
res: Response,
|
|
|
|
err: { data: JSONValue; type: { code: number } }
|
|
|
|
) {
|
|
|
|
respond(res, err.type.code, err.data, "json");
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
2022-07-21 18:39:33 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export function handleJSON<Response>(
|
|
|
|
handler: () => Promise<Response>
|
|
|
|
): RequestHandler {
|
2022-07-21 18:39:33 +02:00
|
|
|
return (request, response) => {
|
|
|
|
handler()
|
2022-08-23 20:08:53 +02:00
|
|
|
.then((data) => success(response, data || {}))
|
|
|
|
.catch((e) => error(response, e));
|
2022-07-21 18:39:33 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export function handleJSONWithData<Response>(
|
|
|
|
handler: (data: RequestData) => Promise<Response>
|
|
|
|
): RequestHandler {
|
2022-07-21 18:39:33 +02:00
|
|
|
return (request, response) => {
|
|
|
|
handler(getData(request))
|
2022-08-23 20:08:53 +02:00
|
|
|
.then((data) => success(response, data || {}))
|
|
|
|
.catch((e) => error(response, e));
|
2022-07-21 18:39:33 +02:00
|
|
|
};
|
|
|
|
}
|