@unito/integration-sdk
Version:
Integration SDK
26 lines (25 loc) • 1.12 kB
JavaScript
import { OperatorTypes } from '@unito/integration-api';
// The operators are ordered by their symbol length, in descending order.
// This is necessary because the symbol of an operator can be
// a subset of the symbol of another operator.
//
// For example, the symbol "=" (EQUAL) is a subset of the symbol "!=" (NOT_EQUAL).
const ORDERED_OPERATORS = Object.values(OperatorTypes).sort((o1, o2) => o2.length - o1.length);
function extractFilters(req, res, next) {
const rawFilters = req.query.filter;
res.locals.filters = [];
if (typeof rawFilters === 'string') {
for (const rawFilter of rawFilters.split(',')) {
for (const operator of ORDERED_OPERATORS) {
if (rawFilter.includes(operator)) {
const [field, valuesRaw] = rawFilter.split(operator, 2);
const values = valuesRaw ? valuesRaw.split('|').map(decodeURIComponent) : [];
res.locals.filters.push({ field: field, operator, values });
break;
}
}
}
}
next();
}
export default extractFilters;