-
-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e9bbaac
commit a7b89aa
Showing
7 changed files
with
107 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,59 @@ | ||
import { VercelRequest, VercelResponse } from "@vercel/node"; | ||
import { dateAsNumber, dateString, niceDate } from "../_lib/_dates"; | ||
import { notion, getNestedProperties } from "../_lib/_notion"; | ||
import { niceDate } from "../_lib/_dates"; | ||
import { notion } from "../_lib/_notion"; | ||
import { z } from "zod"; | ||
import { BlogPost, NotionPost } from "shared"; | ||
|
||
export default async function handler(req: VercelRequest, res: VercelResponse) { | ||
const response = await notion.databases.query({ | ||
database_id: "b7a09b10aa83485b94092269239a8b38", | ||
}); | ||
const posts = response.results.map((page) => { | ||
if (!("properties" in page)) throw new Error("No properties"); | ||
const { properties = {}, id } = page; | ||
const { date, ...props } = getNestedProperties(properties); | ||
if (!("title" in props)) throw new Error("No title"); | ||
if (!("status" in props)) throw new Error("No status"); | ||
if (!("slug" in props)) throw new Error("No slug"); | ||
if (!("description" in props)) throw new Error("No description"); | ||
|
||
if (!date) throw new Error("No date"); | ||
|
||
const sanitizedDate = dateString(date); | ||
const publishDate = niceDate(date); | ||
const rawDate = dateAsNumber(date); | ||
|
||
return { id, rawDate, date: sanitizedDate, publishDate, ...props }; | ||
}); | ||
|
||
const notionPosts = response.results.filter( | ||
isValidNotionPost | ||
) as unknown as NotionPost[]; | ||
|
||
const posts = notionPosts.map(notionPostToBlogPost); | ||
|
||
// Cache for 1 week, stale-while-revalidate | ||
res.setHeader("Cache-Control", "s-maxage=1, stale-while-revalidate"); | ||
|
||
res.json(posts); | ||
} | ||
|
||
const postSchema: z.ZodType<NotionPost> = z.object({ | ||
id: z.string(), | ||
created_time: z.string(), | ||
properties: z.object({ | ||
title: z.object({ | ||
title: z.array(z.object({ plain_text: z.string() })).min(1), | ||
}), | ||
description: z.object({ | ||
rich_text: z.array(z.object({ plain_text: z.string() })).min(1), | ||
}), | ||
status: z.object({ | ||
status: z.object({ name: z.string() }), | ||
}), | ||
slug: z.object({ | ||
rich_text: z.array(z.object({ plain_text: z.string() })).min(1), | ||
}), | ||
}), | ||
}); | ||
|
||
function isValidNotionPost(post: unknown): post is NotionPost { | ||
const parsed = postSchema.safeParse(post); | ||
if (parsed.success) return true; | ||
return false; | ||
} | ||
|
||
function notionPostToBlogPost(post: NotionPost): BlogPost { | ||
return { | ||
id: post.id, | ||
publishDate: new Date(post.created_time).getTime(), | ||
niceDate: niceDate(new Date(post.created_time)), | ||
description: post.properties.description.rich_text[0].plain_text, | ||
slug: post.properties.slug.rich_text[0].plain_text, | ||
status: post.properties.status.status.name, | ||
title: post.properties.title.title[0].plain_text, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
export type NotionPost = { | ||
id: string; | ||
created_time: string; | ||
properties: NotionPostProperties; | ||
}; | ||
|
||
export type NotionPostProperties = { | ||
title: { | ||
title: { | ||
plain_text: string; | ||
}[]; | ||
}; | ||
description: { | ||
rich_text: { | ||
plain_text: string; | ||
}[]; | ||
}; | ||
status: { | ||
status: { | ||
name: string; | ||
}; | ||
}; | ||
slug: { | ||
rich_text: { | ||
plain_text: string; | ||
}[]; | ||
}; | ||
}; | ||
|
||
export type BlogPost = { | ||
id: string; | ||
publishDate: number; | ||
niceDate: string; | ||
description: string; | ||
slug: string; | ||
status: string; | ||
title: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./ImportDataFormType"; | ||
export * from "./templates"; | ||
export * from "./blog"; |