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 { parsePositiveIntParam, validateBody } from '$lib/server/api-helpers';
|
||||
import {
|
||||
removeDomain,
|
||||
updateDomain,
|
||||
@@ -16,20 +17,12 @@ const UpdateSchema = z.object({
|
||||
display_name: z.string().max(100).nullable().optional()
|
||||
});
|
||||
|
||||
function parseId(raw: string): number {
|
||||
const id = Number(raw);
|
||||
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' });
|
||||
return id;
|
||||
}
|
||||
|
||||
export const PATCH: RequestHandler = async ({ params, request }) => {
|
||||
const id = parseId(params.id!);
|
||||
const body = await request.json().catch(() => null);
|
||||
const parsed = UpdateSchema.safeParse(body);
|
||||
if (!parsed.success) error(400, { message: 'Invalid body' });
|
||||
const id = parsePositiveIntParam(params.id, 'id');
|
||||
const data = validateBody(await request.json().catch(() => null), UpdateSchema);
|
||||
try {
|
||||
const db = getDb();
|
||||
const updated = updateDomain(db, id, parsed.data);
|
||||
const updated = updateDomain(db, id, data);
|
||||
if (!updated) error(404, { message: 'Not found' });
|
||||
// Wenn updateDomain favicon_path genullt hat (Domain geändert), frisch laden.
|
||||
if (updated.favicon_path === null) {
|
||||
@@ -41,12 +34,14 @@ export const PATCH: RequestHandler = async ({ params, request }) => {
|
||||
}
|
||||
return json(updated);
|
||||
} catch (e) {
|
||||
// HTTP-Errors aus error() durchreichen, sonst landet ein 404 als 409.
|
||||
if (isHttpError(e)) throw e;
|
||||
error(409, { message: (e as Error).message });
|
||||
}
|
||||
};
|
||||
|
||||
export const DELETE: RequestHandler = async ({ params }) => {
|
||||
const id = parseId(params.id!);
|
||||
const id = parsePositiveIntParam(params.id, 'id');
|
||||
removeDomain(getDb(), id);
|
||||
return json({ ok: true });
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user