1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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" } });
};