12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- // Next.js API route support: https://nextjs.org/docs/api-routes/introduction
- import type { NextApiRequest, NextApiResponse } from "next";
- import { DatabaseType, getDatabaseId, notion } from "../../lib/notion";
-
- interface Topic {
- blockId: string;
- name: string;
- }
-
- interface Error {
- status: string;
- }
-
- function getTopicsInDatabase(notionResp: any): Topic[] {
- return notionResp.results.map((block: any) => ({
- blockId: block.id,
- name: block.properties.Name.title[0].plain_text,
- }));
- }
-
- export default async function handler(
- req: NextApiRequest,
- res: NextApiResponse<Topic[] | Error>
- ) {
- const {
- query: { type },
- } = req;
- const blockId = getDatabaseId(type as DatabaseType);
- if (!blockId) {
- res.status(404).json({ status: "Not found." });
- } else {
- const resp = await notion.databases.query({
- database_id: blockId,
- sorts: [
- {
- property: "Created",
- direction: "ascending",
- },
- ],
- });
- res.status(200).json(getTopicsInDatabase(resp));
- }
- }
|