import { json, error } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; import { env } from '$env/dynamic/private'; import { log } from '$lib/server/logger'; const SCRAPER_URL = env.SCRAPER_API_URL ?? 'http://localhost:8080'; /** * GET /api/browse-page?page=2&genre=all&sort=popular&status=all * * Thin proxy to the Go scraper's /api/browse endpoint. * Used by the infinite-scroll browse page to append subsequent pages * without a full SSR navigation. */ export const GET: RequestHandler = async ({ url }) => { const page = url.searchParams.get('page') ?? '1'; const genre = url.searchParams.get('genre') ?? 'all'; const sort = url.searchParams.get('sort') ?? 'popular'; const status = url.searchParams.get('status') ?? 'all'; const params = new URLSearchParams({ page, genre, sort, status }); const apiURL = `${SCRAPER_URL}/api/browse?${params.toString()}`; try { const res = await fetch(apiURL); if (!res.ok) { log.error('browse-page', 'scraper returned error', { status: res.status }); throw error(502, `Browse fetch failed: ${res.status}`); } const data = await res.json(); return json(data); } catch (e) { if (e instanceof Error && 'status' in e) throw e; log.error('browse-page', 'network error', { err: String(e) }); throw error(502, 'Could not reach browse service'); } };