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

18 lines
667 B
TypeScript
Raw Normal View History

import type { RequestHandler } from './$types';
import { json } from '@sveltejs/kit';
import { getDb } from '$lib/server/db';
import { listRecentRecipes, searchLocal } from '$lib/server/recipes/search-local';
export const GET: RequestHandler = async ({ url }) => {
const q = url.searchParams.get('q')?.trim() ?? '';
const limit = Math.min(Number(url.searchParams.get('limit') ?? 30), 100);
const offset = Math.max(0, Number(url.searchParams.get('offset') ?? 0));
const hits =
q.length >= 1
? searchLocal(getDb(), q, limit, offset)
: offset === 0
? listRecentRecipes(getDb(), limit)
: [];
return json({ query: q, hits });
};