refactor(api): parsePositiveIntParam fuer view-endpoint
Code-Review-Finding zu commit 82d4348: das Sibling-Endpoint
recipes/[id]/+server.ts nutzt schon parsePositiveIntParam aus
api-helpers.ts. Der neue View-Endpoint hatte die Logik inline
nachgebaut — jetzt aufgeraeumt fuer Konsistenz.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -2,7 +2,7 @@ import type { RequestHandler } from './$types';
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { error } from '@sveltejs/kit';
|
import { error } from '@sveltejs/kit';
|
||||||
import { getDb } from '$lib/server/db';
|
import { getDb } from '$lib/server/db';
|
||||||
import { validateBody } from '$lib/server/api-helpers';
|
import { validateBody, parsePositiveIntParam } from '$lib/server/api-helpers';
|
||||||
import { recordView } from '$lib/server/recipes/views';
|
import { recordView } from '$lib/server/recipes/views';
|
||||||
|
|
||||||
const Schema = z.object({
|
const Schema = z.object({
|
||||||
@@ -10,10 +10,7 @@ const Schema = z.object({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export const POST: RequestHandler = async ({ params, request }) => {
|
export const POST: RequestHandler = async ({ params, request }) => {
|
||||||
const recipeId = Number(params.id);
|
const recipeId = parsePositiveIntParam(params.id, 'id');
|
||||||
if (!Number.isInteger(recipeId) || recipeId <= 0) {
|
|
||||||
error(400, { message: 'Invalid recipe id' });
|
|
||||||
}
|
|
||||||
const body = validateBody(await request.json().catch(() => null), Schema);
|
const body = validateBody(await request.json().catch(() => null), Schema);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user