refactor: nested $json/$select syntax, migrate stored queries
This commit is contained in:
@@ -48,10 +48,10 @@ class QueryBuilder {
|
||||
return { [field]: { [operator]: parsedVal } };
|
||||
}
|
||||
if (field === '$select') {
|
||||
return { '$select': rule.selectorValue || '', [operator]: parsedVal };
|
||||
return { '$select': { [rule.selectorValue || '*']: { [operator]: parsedVal } } };
|
||||
}
|
||||
if (field === '$json') {
|
||||
return { '$json': rule.jsonPath || '', [operator]: parsedVal };
|
||||
return { '$json': { [rule.jsonPath || '$']: { [operator]: parsedVal } } };
|
||||
}
|
||||
if (field === 'headers.*') {
|
||||
const headerField = `headers.${rule.headerName || 'content-type'}`;
|
||||
@@ -117,19 +117,23 @@ class QueryBuilder {
|
||||
}
|
||||
|
||||
if ('$select' in clause) {
|
||||
const selectorValue = clause.$select;
|
||||
for (const [op, val] of Object.entries(clause)) {
|
||||
if (op !== '$select') {
|
||||
return { field: '$select', operator: op, value: String(val), headerName: '', selectorValue, jsonPath: '' };
|
||||
const selMap = clause.$select;
|
||||
if (selMap && typeof selMap === 'object') {
|
||||
const [selectorValue, condition] = Object.entries(selMap)[0] || ['*', {}];
|
||||
if (condition && typeof condition === 'object') {
|
||||
const [operator, value] = Object.entries(condition)[0] || ['$eq', ''];
|
||||
return { field: '$select', operator, value: String(value), headerName: '', selectorValue, jsonPath: '' };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ('$json' in clause) {
|
||||
const jsonPath = clause.$json;
|
||||
for (const [op, val] of Object.entries(clause)) {
|
||||
if (op !== '$json') {
|
||||
return { field: '$json', operator: op, value: String(val), headerName: '', selectorValue: '', jsonPath };
|
||||
const pathMap = clause.$json;
|
||||
if (pathMap && typeof pathMap === 'object') {
|
||||
const [jsonPath, condition] = Object.entries(pathMap)[0] || ['$', {}];
|
||||
if (condition && typeof condition === 'object') {
|
||||
const [operator, value] = Object.entries(condition)[0] || ['$eq', ''];
|
||||
return { field: '$json', operator, value: String(value), headerName: '', selectorValue: '', jsonPath };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+11
-11
@@ -117,22 +117,22 @@ export function evaluate(query: unknown, ctx: EvalContext): boolean {
|
||||
return evalCondition(q.$certExpiry, val);
|
||||
}
|
||||
|
||||
// $select — CSS selector shorthand
|
||||
// $select — { "$select": { "css.selector": { "$op": val } } }
|
||||
if ("$select" in q) {
|
||||
// Server-side: we don't have a DOM parser here, so we just validate structure.
|
||||
// Actual evaluation happens in Rust runner. This returns true for validation purposes.
|
||||
// Server-side: no DOM parser available, pass through (Rust runner evaluates)
|
||||
// Validate structure only
|
||||
const selMap = q.$select as Record<string, unknown>;
|
||||
if (typeof selMap !== "object" || Array.isArray(selMap)) throw new Error("$select expects { selector: condition }");
|
||||
return true;
|
||||
}
|
||||
|
||||
// $json — JSONPath shorthand
|
||||
// $json — { "$json": { "$.path": { "$op": val } } }
|
||||
if ("$json" in q) {
|
||||
const expr = q.$json as string;
|
||||
const cond = q as Record<string, unknown>;
|
||||
const resolved = resolveJsonPath(ctx.body, expr);
|
||||
// Apply operators from remaining keys
|
||||
for (const [op, opVal] of Object.entries(cond)) {
|
||||
if (op === "$json") continue;
|
||||
if (!evalOp(op, resolved, opVal)) return false;
|
||||
const pathMap = q.$json as Record<string, unknown>;
|
||||
if (typeof pathMap !== "object" || Array.isArray(pathMap)) throw new Error("$json expects { path: condition }");
|
||||
for (const [path, condition] of Object.entries(pathMap)) {
|
||||
const resolved = resolveJsonPath(ctx.body, path);
|
||||
if (!evalCondition(condition, resolved)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user