Files
kochwas/src/routes/api/recipes/import/+server.ts

21 lines
764 B
TypeScript
Raw Normal View History

import type { RequestHandler } from './$types';
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';
import { IMAGE_DIR } from '$lib/server/paths';
const ImportSchema = z.object({ url: z.string().url() });
export const POST: RequestHandler = async ({ request }) => {
const data = validateBody(await request.json().catch(() => null), ImportSchema);
try {
const result = await importRecipe(getDb(), IMAGE_DIR, data.url);
return json({ id: result.id, duplicate: result.duplicate });
} catch (e) {
mapImporterError(e);
}
};