import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo";
import { BlobPreset } from "https://esm.town/v/pomdtr/lowdb";
type Post = {
id: number;
title: string;
created: string;
};
type Data = {
posts: Post[];
};
const { key } = extractValInfo(import.meta.url);
export const db = await BlobPreset<Data>(key, { posts: [] });
export default async (req: Request): Promise<Response> => {
if (req.method === "GET") {
return new Response(JSON.stringify({ ok: "false, use Post" }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
const { title } = await req.json();
const date = new Date();
const created = date.toLocaleString("en-US", { timeZone: "America/Chicago" });
db.data
.posts
.push({ id: db.data.posts.length + 1, title, created });
await db.write();
return new Response(JSON.stringify(db.data), { headers: { "Content-Type": "application/json" } });
};