Latest game events, newest first; auto-initializes like the other reads. ?limit= defaults to 50 (cap 200), non-integer/out-of-range → 400. OpenAPI spec gains the path + Event schema (14 -> 15 paths).
22 lines
677 B
TypeScript
22 lines
677 B
TypeScript
import { getGameDb } from '@/lib/db/client'
|
|
import { ensureSave } from '@/lib/game/save'
|
|
import { EventError, getEvents, parseEventLimit } from '@/lib/game/events'
|
|
|
|
// The latest game events, newest first; auto-initializes on first hit.
|
|
export async function GET(req: Request) {
|
|
const db = await getGameDb()
|
|
await ensureSave(db) // getEvents assumes a save exists
|
|
|
|
let limit: number
|
|
try {
|
|
limit = parseEventLimit(new URL(req.url).searchParams.get('limit'))
|
|
} catch (err) {
|
|
if (err instanceof EventError) {
|
|
return Response.json({ error: err.message }, { status: err.status })
|
|
}
|
|
throw err
|
|
}
|
|
|
|
return Response.json(await getEvents(db, limit))
|
|
}
|