diff --git a/src/article.ts b/src/article.ts index f74ba60..80d0953 100644 --- a/src/article.ts +++ b/src/article.ts @@ -1,5 +1,6 @@ import db from "./db.js"; import { z } from "zod"; +import { fixDates } from "./util.js"; export const slugRegex = /^[a-zA-Z0-9_-]+$/; @@ -20,20 +21,22 @@ export const getBySlug = async (slug: string): Promise
=> { return db
("articles") .where("slug", slug) .first() - .then((rec) => !!rec ? rec : null) + .then((rec) => !!rec ? fixDates
(rec) : null) }; export const getById = async (articleId: number): Promise
=> { return db
("articles") .where("id", articleId) .first() - .then((rec) => !!rec ? rec : null) + .then((rec) => !!rec ? fixDates
(rec) : null) }; export const getByUserId = async (userId: number): Promise => db
("articles") .where("users_id", userId) - .orderBy("created_at", "desc"); + .orderBy("created_at", "desc") + .then((articles) => articles.map((a) => fixDates
(a))) + ; export const insert = async (userId: number, slug: string, title: string): Promise
=> { const data: Record = { @@ -58,4 +61,5 @@ export const getAll = async (count: number, lastId = Number.MAX_SAFE_INTEGER) => .where("id", "<", lastId) .orderBy("created_at", "desc") .limit(count) + .then((articles) => articles.map((a) => fixDates
(a))) ; diff --git a/src/index.ts b/src/index.ts index 5e804f5..8958367 100644 --- a/src/index.ts +++ b/src/index.ts @@ -32,7 +32,7 @@ app.get("/", async (req, res) => { const renderedArticles = articles.map((a) => `
  • - ${escapeHtml(a.title)} - ${a.created_at.toDateString()} + ${escapeHtml(a.title)} - ${a.created_at.toISOString()}
  • `).join("\n"); @@ -45,6 +45,8 @@ app.get("/", async (req, res) => { ${renderedArticles} `; + + res.set("Content-Type", "text/html").send(payload); }); app.post(Routes.inbox, handleInboxPost); diff --git a/src/util.ts b/src/util.ts index 07ddeaf..60e4f19 100644 --- a/src/util.ts +++ b/src/util.ts @@ -10,3 +10,11 @@ export const streamToString = async (stream: ReadableStream) => { const arr = new Uint8Array(await new Response(stream).arrayBuffer()); return new TextDecoder("utf-8").decode(arr); }; + +export const fixDates = (record: Record, columns: string[] = ["created_at", "updated_at"]) => { + columns.forEach((column) => { + const value = record[column] + if (value && typeof value === "number") record[column] = new Date(record[column]); + }); + return record as T; +};