refactor(api): alle handler auf api-helpers umstellen
13 +server.ts handler nutzen jetzt parsePositiveIntParam und
validateBody statt jeweils lokaler parseId/safeParse-Bloecke.
Konsequenzen:
- 9 lokale parseId/parsePositiveInt Definitionen geloescht
- 11 safeParse + manueller error()-Throws ersetzt
- domains/[id], domains, profiles: catch-Block reicht jetzt HttpError
durch (isHttpError) — vorher wurde ein 404 vom updateDomain als 409
re-emittiert
- recipes/[id]/image: kein function-clutter mehr neben den FormData-Pfaden
- Konsistente Error-Bodies: validateBody schickt {message, issues},
parsePositiveIntParam {message: 'Missing X' / 'Invalid X'}
Findings aus REVIEW-2026-04-18.md (Refactor A) und redundancy.md
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import type { RequestHandler } from './$types';
|
||||
import { json, error } from '@sveltejs/kit';
|
||||
import { json, error, isHttpError } from '@sveltejs/kit';
|
||||
import { z } from 'zod';
|
||||
import { getDb } from '$lib/server/db';
|
||||
import { validateBody } from '$lib/server/api-helpers';
|
||||
import { addDomain, listDomains, setDomainFavicon } from '$lib/server/domains/repository';
|
||||
import { ensureFavicons, fetchAndStoreFavicon } from '$lib/server/domains/favicons';
|
||||
|
||||
@@ -21,16 +22,14 @@ export const GET: RequestHandler = async () => {
|
||||
};
|
||||
|
||||
export const POST: RequestHandler = async ({ request }) => {
|
||||
const body = await request.json().catch(() => null);
|
||||
const parsed = CreateSchema.safeParse(body);
|
||||
if (!parsed.success) error(400, { message: 'Invalid body' });
|
||||
const data = validateBody(await request.json().catch(() => null), CreateSchema);
|
||||
try {
|
||||
const db = getDb();
|
||||
const d = addDomain(
|
||||
db,
|
||||
parsed.data.domain,
|
||||
parsed.data.display_name ?? null,
|
||||
parsed.data.added_by_profile_id ?? null
|
||||
data.domain,
|
||||
data.display_name ?? null,
|
||||
data.added_by_profile_id ?? null
|
||||
);
|
||||
// Favicon direkt nach dem Insert mitziehen, damit die Antwort schon das
|
||||
// Icon enthält — der POST ist eh ein interaktiver Admin-Vorgang.
|
||||
@@ -41,6 +40,7 @@ export const POST: RequestHandler = async ({ request }) => {
|
||||
}
|
||||
return json(d, { status: 201 });
|
||||
} catch (e) {
|
||||
if (isHttpError(e)) throw e;
|
||||
error(409, { message: (e as Error).message });
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user