fix: distinguish failed sources from empty results
CI (SIAX Cloud) / integration (push) Skipped
CI (SIAX Cloud) / sonar (push) Skipped
masterplan-lock / check (push) Successful in 4s
CI (SIAX Cloud) / sonar (pull_request) Skipped
CI (SIAX Cloud) / security (push) Successful in 15s
masterplan-lock / check (pull_request) Successful in 7s
Security Scan (SIAX Cloud) / security (push) Successful in 18s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 17s
CI (SIAX Cloud) / security (pull_request) Successful in 20s
CI (SIAX Cloud) / integration (pull_request) Successful in 35s
CI (SIAX Cloud) / quality (pull_request) Successful in 39s
CI (SIAX Cloud) / quality (push) Successful in 48s

This commit is contained in:
2026-11-02 06:58:15 +01:00
parent 4f5a2ea865
commit 5137bf87a1
6 changed files with 156 additions and 56 deletions
+23 -20
View File
@@ -1,27 +1,30 @@
import { NextRequest, NextResponse } from "next/server";
import { engineFetch } from "@/lib/engine";
import { engineFetch, hasEngine } from "@/lib/engine";
// GET /api/screen?q=Name — OpenSanctions screening (sanctions/PEP). VERIFY
// commercial terms before production volume. Defers to engine when running.
// An unavailable or malformed screening source is never evidence of no match.
export async function GET(req: NextRequest) {
const q = req.nextUrl.searchParams.get("q") ?? "";
if (!q) return NextResponse.json({ matches: [] });
const eng = await engineFetch(`/screen?q=${encodeURIComponent(q)}`);
if (eng && eng.ok) return NextResponse.json(await eng.json());
const q = req.nextUrl.searchParams.get("q")?.trim() ?? "";
if (!q) return NextResponse.json({ error: { code: "bad_request", message: "q krävs" } }, { status: 400 });
const unavailable = () => NextResponse.json({ error: { code: "source_unavailable", message: "Screeningkällan är inte tillgänglig" } }, { status: 503 });
try {
if (hasEngine()) {
const eng = await engineFetch(`/screen?q=${encodeURIComponent(q)}`);
if (!eng?.ok) return unavailable();
const data: unknown = await eng.json();
if (!data || typeof data !== "object" || (!Array.isArray((data as { matches?: unknown }).matches) || (data as { source_status?: unknown }).source_status !== "ok")) return unavailable();
return NextResponse.json(data);
}
const r = await fetch(`https://api.opensanctions.org/search/default?q=${encodeURIComponent(q)}&limit=5`, {
headers: { "User-Agent": "0pt/0.1 (+https://0pt.app; SIAX)" },
cache: "no-store",
headers: { "User-Agent": "0pt/0.1 (+https://0pt.app; SIAX)" }, cache: "no-store",
});
if (!r.ok) return NextResponse.json({ matches: [], source: "opensanctions" });
const data = (await r.json()) as { results?: { caption?: string; score?: number; schema?: string }[] };
return NextResponse.json({
source: "opensanctions",
matches: (data.results ?? []).map((x) => ({ name: x.caption ?? null, score: x.score ?? null, schema: x.schema ?? null })),
});
} catch {
return NextResponse.json({ matches: [], source: "opensanctions" });
}
if (!r.ok) return unavailable();
const data: unknown = await r.json();
if (!data || typeof data !== "object" || !Array.isArray((data as { results?: unknown }).results)) return unavailable();
const results = (data as { results: unknown[] }).results;
if (results.some((item) => !item || typeof item !== "object" || Array.isArray(item))) return unavailable();
return NextResponse.json({ source: "opensanctions", matches: results.map((item) => {
const x = item as { caption?: unknown; score?: unknown; schema?: unknown };
return { name: x.caption ?? null, score: x.score ?? null, schema: x.schema ?? null };
}) });
} catch { return unavailable(); }
}
+21 -25
View File
@@ -1,34 +1,30 @@
import { NextRequest, NextResponse } from "next/server";
import { engineFetch } from "@/lib/engine";
import { engineFetch, hasEngine } from "@/lib/engine";
// GET /api/vehicles?plate=XX-99-YY — RDW (NL) open vehicle data (technical/fleet
// only, no owner PII). Defers to the engine when running; else calls RDW directly.
// A missing record is distinct from an unavailable or malformed source.
export async function GET(req: NextRequest) {
const plate = (req.nextUrl.searchParams.get("plate") ?? "").toUpperCase().replace(/[-\s]/g, "").replace(/[^A-Z0-9]/g, "");
if (!plate) return NextResponse.json({ error: { code: "bad_request", message: "plate krävs" } }, { status: 400 });
const eng = await engineFetch(`/vehicles/${plate}`);
if (eng && eng.ok) return NextResponse.json(await eng.json());
const unavailable = () => NextResponse.json({ error: { code: "source_unavailable", message: "Fordonskällan är inte tillgänglig" } }, { status: 503 });
try {
if (hasEngine()) {
const eng = await engineFetch(`/vehicles/${plate}`);
if (!eng?.ok) return unavailable();
const data: unknown = await eng.json();
if (!data || typeof data !== "object" || Array.isArray(data) || (data as { source_status?: unknown }).source_status !== "ok") return unavailable();
return NextResponse.json(data);
}
const r = await fetch(`https://opendata.rdw.nl/resource/m9d7-ebf2.json?kenteken=${encodeURIComponent(plate)}`, {
headers: { "User-Agent": "0pt/0.1 (+https://0pt.app; SIAX)" },
cache: "no-store",
headers: { "User-Agent": "0pt/0.1 (+https://0pt.app; SIAX)" }, cache: "no-store",
});
if (!r.ok) return NextResponse.json({ vehicle: null }, { status: 200 });
const arr = (await r.json()) as Record<string, string>[];
if (!arr.length) return NextResponse.json({ vehicle: null });
const d = arr[0];
return NextResponse.json({
source: "rdw",
vehicle: {
reg_plate: plate, market: "NL",
make: d.merk ?? null, model: d.handelsbenaming ?? null,
year: (d.datum_eerste_toelating ?? "").slice(0, 4) || null,
},
provenance: { level: 3, source: "rdw" },
});
} catch {
return NextResponse.json({ vehicle: null, error: "fetch_failed" }, { status: 200 });
}
if (!r.ok) return unavailable();
const data: unknown = await r.json();
if (!Array.isArray(data) || data.some((item) => !item || typeof item !== "object" || Array.isArray(item))) return unavailable();
if (!data.length) return NextResponse.json({ vehicle: null });
const d = data[0] as Record<string, string>;
if (d.datum_eerste_toelating != null && typeof d.datum_eerste_toelating !== "string") return unavailable();
return NextResponse.json({ source: "rdw", vehicle: { reg_plate: plate, market: "NL", make: d.merk ?? null,
model: d.handelsbenaming ?? null, year: (d.datum_eerste_toelating ?? "").slice(0, 4) || null },
provenance: { level: 3, source: "rdw" } });
} catch { return unavailable(); }
}