mirror of
https://github.com/ReCloudStudio/WebHooker.git
synced 2026-09-23 00:21:28 +00:00
Add a field filter type reading any payload value by JSONPath with array expansion, 12 comparison operators (eq/ne/contains/startsWith/endsWith/regex/gt/gte/lt/lte/in/exists), a visual AST builder (all/any/not) in the route editor, chip-based multi-value input, a stateless POST /admin/api/test-match dry-run, and named filter fragments stored in D1 (d1_fragments, migration 0010) inlined into route ASTs on insert.
55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
import type { FilterNode } from "./types";
|
|
import { log } from "./lib/log";
|
|
|
|
export interface NamedFragment {
|
|
id: string;
|
|
groupId?: string;
|
|
name: string;
|
|
node: FilterNode;
|
|
}
|
|
|
|
interface D1FragmentRow {
|
|
id: string;
|
|
group_id: string;
|
|
name: string;
|
|
node: string;
|
|
}
|
|
|
|
export async function loadFragments(db: D1Database): Promise<NamedFragment[]> {
|
|
try {
|
|
const stmt = db.prepare(
|
|
"SELECT id, group_id, name, node FROM d1_fragments ORDER BY id",
|
|
);
|
|
if (typeof stmt.all !== "function") return [];
|
|
const { results } = await stmt.all<D1FragmentRow>();
|
|
if (!results || results.length === 0) return [];
|
|
return results.map((r) => ({
|
|
id: r.id,
|
|
groupId: r.group_id || undefined,
|
|
name: r.name,
|
|
node: JSON.parse(r.node) as FilterNode,
|
|
}));
|
|
} catch (err) {
|
|
log.warn({ err }, "Failed to load fragments from D1");
|
|
return [];
|
|
}
|
|
}
|
|
|
|
export async function saveFragments(
|
|
db: D1Database,
|
|
fragments: NamedFragment[],
|
|
): Promise<void> {
|
|
const now = Date.now();
|
|
const statements: D1PreparedStatement[] = [
|
|
db.prepare("DELETE FROM d1_fragments"),
|
|
...fragments.map((f) =>
|
|
db
|
|
.prepare(
|
|
`INSERT INTO d1_fragments (id, group_id, name, node, version, created_at, updated_at)
|
|
VALUES (?, ?, ?, ?, 1, ?, ?)`,
|
|
)
|
|
.bind(f.id, f.groupId ?? "", f.name, JSON.stringify(f.node), now, now),
|
|
),
|
|
];
|
|
await db.batch(statements);
|
|
}
|