import { HandlerContext } from "$fresh/server.ts"; import { catcher_async, json_response } from "utils"; import { orphan_message } from "db"; export async function handler( req: Request, _ctx: HandlerContext ): Promise { if (req.method == "PUT") { return await put(req); } else { return new Response(JSON.stringify({ err: "expected a PUT" })); } } async function put(req: Request): Promise { const json = await catcher_async(() => req.json()); if (json.isNone()) { return json_response({ err: "expected JSON :(" }, { status: 400 }); } const json_ = json.unwrap(); const new_id = await orphan_message(json_); return new Response(new_id.unwrapOr("fail")); } //TODO: validate that they give a valid message object //TODO: put under some id //TODO: e.g. {under: string, message: Message}