refactor: extract signals filter builder, add ESLint 10 config, fix low-hanging issues

This commit is contained in:
Sebastian Seedorf
2026-06-04 14:09:12 +02:00
parent cf9bb33ecb
commit 4b05f2968e
34 changed files with 2145 additions and 188 deletions

View File

@@ -1,5 +1,8 @@
import { NextRequest, NextResponse } from 'next/server';
import { type NextRequest, NextResponse } from 'next/server';
import { isAuthorized, unauthorized } from './auth';
import { getServerLocaleMap } from './localeServer';
import { matchKeys } from './localization';
type RouteContext = { params: Promise<Record<string, string>> };
type Handler = (req: NextRequest, ctx: RouteContext) => Promise<NextResponse>;
@@ -24,3 +27,53 @@ export function withAuth(handler: Handler): Handler {
}
};
}
/**
* Builds a SQL WHERE clause fragment for item whitelist/blacklist filtering.
*
* - `isWhitelist=true`: `item_key ~* $N OR item_key = ANY($N+1)` (regex), `item_key = ANY($N)` (exact)
* - `isWhitelist=false`: `item_key !~* $N AND item_key != ALL($N+1)` (regex), `item_key != ALL($N)` (exact)
*
* Mutates `values` and `param.current` to track parameter bindings.
*/
export function buildItemFilter(
items: string[],
useRegex: boolean,
isWhitelist: boolean,
values: unknown[],
param: { current: number },
): string | null {
if (items.length === 0) return null;
if (useRegex) {
const localeMap = getServerLocaleMap();
const localeKeys = [...new Set(items.flatMap((p) => matchKeys(p, localeMap)))];
const sqlPattern = items.map((p) => `(${p})`).join('|');
if (isWhitelist) {
const orConds: string[] = [`item_key ~* $${param.current++}`];
values.push(sqlPattern);
if (localeKeys.length > 0) {
orConds.push(`item_key = ANY($${param.current++})`);
values.push(localeKeys);
}
return `(${orConds.join(' OR ')})`;
} else {
const andConds: string[] = [`item_key !~* $${param.current++}`];
values.push(sqlPattern);
if (localeKeys.length > 0) {
andConds.push(`item_key != ALL($${param.current++})`);
values.push(localeKeys);
}
return `(${andConds.join(' AND ')})`;
}
}
if (isWhitelist) {
values.push(items);
return `item_key = ANY($${param.current++})`;
} else {
values.push(items);
return `item_key != ALL($${param.current++})`;
}
}