freshter-logs/routes/api/msg/index.ts

29 lines
875 B
TypeScript
Raw Permalink Normal View History

2023-04-28 19:48:34 -04:00
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<Response> {
if (req.method == "PUT") {
return await put(req);
} else {
return new Response(JSON.stringify({ err: "expected a PUT" }));
}
}
async function put(req: Request): Promise<Response> {
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}