|
@@ -1,13 +1,16 @@
|
1
|
1
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
2
|
2
|
import type { NextApiRequest, NextApiResponse } from "next";
|
3
|
|
-import { LAWS_BLOCK_ID } from "../../config";
|
4
|
|
-import { notion } from "../../lib/notion";
|
|
3
|
+import { DatabaseType, getDatabaseId, notion } from "../../lib/notion";
|
5
|
4
|
|
6
|
5
|
interface Topic {
|
7
|
6
|
blockId: string;
|
8
|
7
|
name: string;
|
9
|
8
|
}
|
10
|
9
|
|
|
10
|
+interface Error {
|
|
11
|
+ status: string;
|
|
12
|
+}
|
|
13
|
+
|
11
|
14
|
function getTopicsInDatabase(notionResp: any): Topic[] {
|
12
|
15
|
return notionResp.results.map((block: any) => ({
|
13
|
16
|
blockId: block.id,
|
|
@@ -17,16 +20,24 @@ function getTopicsInDatabase(notionResp: any): Topic[] {
|
17
|
20
|
|
18
|
21
|
export default async function handler(
|
19
|
22
|
req: NextApiRequest,
|
20
|
|
- res: NextApiResponse<Topic[]>
|
|
23
|
+ res: NextApiResponse<Topic[] | Error>
|
21
|
24
|
) {
|
22
|
|
- const resp = await notion.databases.query({
|
23
|
|
- database_id: LAWS_BLOCK_ID,
|
24
|
|
- sorts: [
|
25
|
|
- {
|
26
|
|
- property: "Created",
|
27
|
|
- direction: "ascending",
|
28
|
|
- },
|
29
|
|
- ],
|
30
|
|
- });
|
31
|
|
- res.status(200).json(getTopicsInDatabase(resp));
|
|
25
|
+ const {
|
|
26
|
+ query: { type },
|
|
27
|
+ } = req;
|
|
28
|
+ const blockId = getDatabaseId(type as DatabaseType);
|
|
29
|
+ if (!blockId) {
|
|
30
|
+ res.status(404).json({ status: "Not found." });
|
|
31
|
+ } else {
|
|
32
|
+ const resp = await notion.databases.query({
|
|
33
|
+ database_id: blockId,
|
|
34
|
+ sorts: [
|
|
35
|
+ {
|
|
36
|
+ property: "Created",
|
|
37
|
+ direction: "ascending",
|
|
38
|
+ },
|
|
39
|
+ ],
|
|
40
|
+ });
|
|
41
|
+ res.status(200).json(getTopicsInDatabase(resp));
|
|
42
|
+ }
|
32
|
43
|
}
|