Files
Flight-Ops/app/api/packages/[id]/load/route.ts
T

35 lines
1.4 KiB
TypeScript

import { loadPackage, performAction } from '@/lib/game/actions';
// POST /api/packages/:id/load (v0.12.0) — body { planeId: string }. Loads an OPEN
// package onto a GROUNDED plane at the package's origin with a free slot. 400
// missing/non-string planeId or unknown fields; 404 unknown package/plane id; 409
// wrong package status, plane flying, plane not at the origin, no free slots. On
// success returns the full state snapshot (package now "loaded" on that plane).
export async function POST(req: Request, ctx: { params: Promise<{ id: string }> }) {
const { id } = await ctx.params;
let body: unknown;
try {
const text = await req.text();
body = text ? JSON.parse(text) : {};
} catch {
return Response.json({ error: 'invalid JSON body' }, { status: 400 });
}
if (typeof body !== 'object' || body === null || Array.isArray(body)) {
return Response.json({ error: 'body must be a JSON object' }, { status: 400 });
}
const input = body as Record<string, unknown>;
for (const key of Object.keys(input)) {
if (key !== 'planeId') {
return Response.json({ error: `unknown field "${key}"` }, { status: 400 });
}
}
if (typeof input.planeId !== 'string' || input.planeId.length === 0) {
return Response.json({ error: 'planeId must be a non-empty string' }, { status: 400 });
}
const planeId = input.planeId;
return performAction((inst) => loadPackage(inst, id, planeId));
}