Reviewed-on: #14 Co-authored-by: Nicholas Ward <nicholaspward@outlook.com>
37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
import type { APIRoute } from "astro";
|
|
import { openDatabase } from "../../../../lib/database";
|
|
import { createMcpTools } from "../../../../mcp/tools";
|
|
|
|
export const prerender = false;
|
|
|
|
export const POST: APIRoute = async ({ request }) => {
|
|
const database = openDatabase({ readOnly: true });
|
|
if (!database) return Response.json({ success: false, error: "Database unavailable." }, { status: 503 });
|
|
|
|
try {
|
|
const body = await request.json();
|
|
if (!body.quantity || !body.from_unit || !body.to_unit) {
|
|
return Response.json(
|
|
{ success: false, error: "Required fields: quantity, from_unit, to_unit" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const tools = createMcpTools(() => database);
|
|
const result = tools.convertUnits({
|
|
ingredient_id: body.ingredient_id,
|
|
quantity: Number(body.quantity),
|
|
from_unit: body.from_unit,
|
|
to_unit: body.to_unit,
|
|
});
|
|
return Response.json({ success: true, data: result });
|
|
} catch (error) {
|
|
return Response.json(
|
|
{ success: false, error: error instanceof Error ? error.message : "Conversion failed." },
|
|
{ status: 400 }
|
|
);
|
|
} finally {
|
|
database.close();
|
|
}
|
|
};
|