feat(api): expose preview/import/profile/domain endpoints
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
22
src/routes/api/recipes/import/+server.ts
Normal file
22
src/routes/api/recipes/import/+server.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import type { RequestHandler } from './$types';
|
||||
import { json, error } from '@sveltejs/kit';
|
||||
import { z } from 'zod';
|
||||
import { getDb } from '$lib/server/db';
|
||||
import { importRecipe } from '$lib/server/recipes/importer';
|
||||
import { mapImporterError } from '$lib/server/errors';
|
||||
|
||||
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' });
|
||||
try {
|
||||
const result = await importRecipe(getDb(), IMAGE_DIR, parsed.data.url);
|
||||
return json({ id: result.id, duplicate: result.duplicate });
|
||||
} catch (e) {
|
||||
mapImporterError(e);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user