// 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 ) { 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)); } }