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:
hsiegeln
2026-04-18 22:19:12 +02:00
parent 739cc2d058
commit ff293e9db8
13 changed files with 75 additions and 142 deletions

View File

@@ -2,6 +2,7 @@ import type { RequestHandler } from './$types';
import { json, error } from '@sveltejs/kit';
import { z } from 'zod';
import { getDb } from '$lib/server/db';
import { parsePositiveIntParam, validateBody } from '$lib/server/api-helpers';
import {
deleteRecipe,
getRecipeById,
@@ -48,14 +49,8 @@ const PatchSchema = z
})
.refine((v) => Object.keys(v).length > 0, { message: 'Empty patch' });
function parseId(raw: string): number {
const id = Number(raw);
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' });
return id;
}
export const GET: RequestHandler = async ({ params }) => {
const id = parseId(params.id!);
const id = parsePositiveIntParam(params.id, 'id');
const db = getDb();
const recipe = getRecipeById(db, id);
if (!recipe) error(404, { message: 'Recipe not found' });
@@ -68,12 +63,10 @@ export const GET: RequestHandler = async ({ params }) => {
};
export const PATCH: RequestHandler = async ({ params, request }) => {
const id = parseId(params.id!);
const id = parsePositiveIntParam(params.id, 'id');
const body = await request.json().catch(() => null);
const parsed = PatchSchema.safeParse(body);
if (!parsed.success) error(400, { message: 'Invalid body' });
const p = validateBody(body, PatchSchema);
const db = getDb();
const p = parsed.data;
// Spezielle Kurz-Updates (bleiben als Sonderfall, weil sie FTS triggern
// bzw. andere Tabellen mitpflegen).
if (p.title !== undefined && Object.keys(p).length === 1) {
@@ -121,7 +114,7 @@ export const PATCH: RequestHandler = async ({ params, request }) => {
};
export const DELETE: RequestHandler = async ({ params }) => {
const id = parseId(params.id!);
const id = parsePositiveIntParam(params.id, 'id');
deleteRecipe(getDb(), id);
return json({ ok: true });
};

View File

@@ -1,7 +1,8 @@
import type { RequestHandler } from './$types';
import { json, error } from '@sveltejs/kit';
import { json } from '@sveltejs/kit';
import { z } from 'zod';
import { getDb } from '$lib/server/db';
import { parsePositiveIntParam, validateBody } from '$lib/server/api-helpers';
import { addComment, deleteComment, listComments } from '$lib/server/recipes/actions';
const Schema = z.object({
@@ -11,30 +12,20 @@ const Schema = z.object({
const DeleteSchema = z.object({ comment_id: z.number().int().positive() });
function parseId(raw: string): number {
const id = Number(raw);
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' });
return id;
}
export const GET: RequestHandler = async ({ params }) => {
const id = parseId(params.id!);
const id = parsePositiveIntParam(params.id, 'id');
return json(listComments(getDb(), id));
};
export const POST: RequestHandler = async ({ params, request }) => {
const id = parseId(params.id!);
const body = await request.json().catch(() => null);
const parsed = Schema.safeParse(body);
if (!parsed.success) error(400, { message: 'Invalid body' });
const cid = addComment(getDb(), id, parsed.data.profile_id, parsed.data.text);
const id = parsePositiveIntParam(params.id, 'id');
const data = validateBody(await request.json().catch(() => null), Schema);
const cid = addComment(getDb(), id, data.profile_id, data.text);
return json({ id: cid }, { status: 201 });
};
export const DELETE: RequestHandler = async ({ request }) => {
const body = await request.json().catch(() => null);
const parsed = DeleteSchema.safeParse(body);
if (!parsed.success) error(400, { message: 'Invalid body' });
deleteComment(getDb(), parsed.data.comment_id);
const data = validateBody(await request.json().catch(() => null), DeleteSchema);
deleteComment(getDb(), data.comment_id);
return json({ ok: true });
};

View File

@@ -1,25 +1,18 @@
import type { RequestHandler } from './$types';
import { json, error } from '@sveltejs/kit';
import { json } from '@sveltejs/kit';
import { z } from 'zod';
import { getDb } from '$lib/server/db';
import { parsePositiveIntParam, validateBody } from '$lib/server/api-helpers';
import { logCooked } from '$lib/server/recipes/actions';
import { removeFromWishlistForAll } from '$lib/server/wishlist/repository';
const Schema = z.object({ profile_id: z.number().int().positive() });
function parseId(raw: string): number {
const id = Number(raw);
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' });
return id;
}
export const POST: RequestHandler = async ({ params, request }) => {
const id = parseId(params.id!);
const body = await request.json().catch(() => null);
const parsed = Schema.safeParse(body);
if (!parsed.success) error(400, { message: 'Invalid body' });
const id = parsePositiveIntParam(params.id, 'id');
const data = validateBody(await request.json().catch(() => null), Schema);
const db = getDb();
const entry = logCooked(db, id, parsed.data.profile_id);
const entry = logCooked(db, id, data.profile_id);
// Wenn das Rezept heute gekocht wurde, ist der Wunsch erfüllt — für alle
// Profile raus aus der Wunschliste. Client nutzt den removed_from_wishlist-
// Flag, um den lokalen State (Badge, Button) ohne Reload zu aktualisieren.

View File

@@ -1,31 +1,22 @@
import type { RequestHandler } from './$types';
import { json, error } from '@sveltejs/kit';
import { json } from '@sveltejs/kit';
import { z } from 'zod';
import { getDb } from '$lib/server/db';
import { parsePositiveIntParam, validateBody } from '$lib/server/api-helpers';
import { addFavorite, removeFavorite } from '$lib/server/recipes/actions';
const Schema = z.object({ profile_id: z.number().int().positive() });
function parseId(raw: string): number {
const id = Number(raw);
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' });
return id;
}
export const PUT: RequestHandler = async ({ params, request }) => {
const id = parseId(params.id!);
const body = await request.json().catch(() => null);
const parsed = Schema.safeParse(body);
if (!parsed.success) error(400, { message: 'Invalid body' });
addFavorite(getDb(), id, parsed.data.profile_id);
const id = parsePositiveIntParam(params.id, 'id');
const data = validateBody(await request.json().catch(() => null), Schema);
addFavorite(getDb(), id, data.profile_id);
return json({ ok: true });
};
export const DELETE: RequestHandler = async ({ params, request }) => {
const id = parseId(params.id!);
const body = await request.json().catch(() => null);
const parsed = Schema.safeParse(body);
if (!parsed.success) error(400, { message: 'Invalid body' });
removeFavorite(getDb(), id, parsed.data.profile_id);
const id = parsePositiveIntParam(params.id, 'id');
const data = validateBody(await request.json().catch(() => null), Schema);
removeFavorite(getDb(), id, data.profile_id);
return json({ ok: true });
};

View File

@@ -5,6 +5,7 @@ import { existsSync } from 'node:fs';
import { mkdir, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { getDb } from '$lib/server/db';
import { parsePositiveIntParam } from '$lib/server/api-helpers';
import { getRecipeById, updateImagePath } from '$lib/server/recipes/repository';
const IMAGE_DIR = process.env.IMAGE_DIR ?? './data/images';
@@ -19,14 +20,8 @@ const EXT_BY_MIME: Record<string, string> = {
'image/avif': '.avif'
};
function parseId(raw: string): number {
const id = Number(raw);
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' });
return id;
}
export const POST: RequestHandler = async ({ params, request }) => {
const id = parseId(params.id!);
const id = parsePositiveIntParam(params.id, 'id');
const db = getDb();
if (!getRecipeById(db, id)) error(404, { message: 'Recipe not found' });
@@ -53,7 +48,7 @@ export const POST: RequestHandler = async ({ params, request }) => {
};
export const DELETE: RequestHandler = ({ params }) => {
const id = parseId(params.id!);
const id = parsePositiveIntParam(params.id, 'id');
const db = getDb();
if (!getRecipeById(db, id)) error(404, { message: 'Recipe not found' });
updateImagePath(db, id, null);

View File

@@ -1,7 +1,8 @@
import type { RequestHandler } from './$types';
import { json, error } from '@sveltejs/kit';
import { json } from '@sveltejs/kit';
import { z } from 'zod';
import { getDb } from '$lib/server/db';
import { parsePositiveIntParam, validateBody } from '$lib/server/api-helpers';
import { clearRating, setRating } from '$lib/server/recipes/actions';
const Schema = z.object({
@@ -11,26 +12,16 @@ const Schema = z.object({
const DeleteSchema = z.object({ profile_id: z.number().int().positive() });
function parseId(raw: string): number {
const id = Number(raw);
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' });
return id;
}
export const PUT: RequestHandler = async ({ params, request }) => {
const id = parseId(params.id!);
const body = await request.json().catch(() => null);
const parsed = Schema.safeParse(body);
if (!parsed.success) error(400, { message: 'Invalid body' });
setRating(getDb(), id, parsed.data.profile_id, parsed.data.stars);
const id = parsePositiveIntParam(params.id, 'id');
const data = validateBody(await request.json().catch(() => null), Schema);
setRating(getDb(), id, data.profile_id, data.stars);
return json({ ok: true });
};
export const DELETE: RequestHandler = async ({ params, request }) => {
const id = parseId(params.id!);
const body = await request.json().catch(() => null);
const parsed = DeleteSchema.safeParse(body);
if (!parsed.success) error(400, { message: 'Invalid body' });
clearRating(getDb(), id, parsed.data.profile_id);
const id = parsePositiveIntParam(params.id, 'id');
const data = validateBody(await request.json().catch(() => null), DeleteSchema);
clearRating(getDb(), id, data.profile_id);
return json({ ok: true });
};

View File

@@ -1,7 +1,8 @@
import type { RequestHandler } from './$types';
import { json, error } from '@sveltejs/kit';
import { json } from '@sveltejs/kit';
import { z } from 'zod';
import { getDb } from '$lib/server/db';
import { validateBody } from '$lib/server/api-helpers';
import { importRecipe } from '$lib/server/recipes/importer';
import { mapImporterError } from '$lib/server/errors';
@@ -10,11 +11,9 @@ const ImportSchema = z.object({ url: z.string().url() });
const IMAGE_DIR = process.env.IMAGE_DIR ?? './data/images';
export const POST: RequestHandler = async ({ request }) => {
const body = await request.json().catch(() => null);
const parsed = ImportSchema.safeParse(body);
if (!parsed.success) error(400, { message: 'Invalid body' });
const data = validateBody(await request.json().catch(() => null), ImportSchema);
try {
const result = await importRecipe(getDb(), IMAGE_DIR, parsed.data.url);
const result = await importRecipe(getDb(), IMAGE_DIR, data.url);
return json({ id: result.id, duplicate: result.duplicate });
} catch (e) {
mapImporterError(e);