Kaynağa Gözat

[feat] Add getTopic functionality

Sergio Mattei 2 yıl önce
ebeveyn
işleme
59569e1e7c
6 değiştirilmiş dosya ile 891 ekleme ve 1 silme
  1. 2
    0
      config.ts
  2. 6
    1
      lib/notion.ts
  3. 848
    0
      package-lock.json
  4. 2
    0
      package.json
  5. 26
    0
      pages/api/getTopic.ts
  6. 7
    0
      pages/api/getTopicList.ts

+ 2
- 0
config.ts Dosyayı Görüntüle

@@ -1 +1,3 @@
1 1
 export const NOTION_KEY = process.env.NOTION_KEY;
2
+export const NOTION_USER_ID = process.env.NOTION_USER_ID;
3
+export const NOTION_USER_TOKEN = process.env.NOTION_USER_TOKEN;

+ 6
- 1
lib/notion.ts Dosyayı Görüntüle

@@ -1,9 +1,14 @@
1
-import { NOTION_KEY } from "../config";
1
+import { NotionAPI } from "notion-client";
2
+import { NOTION_KEY, NOTION_USER_ID, NOTION_USER_TOKEN } from "../config";
2 3
 import { DatabaseType } from "../types";
3 4
 
4 5
 const { Client } = require("@notionhq/client");
5 6
 
6 7
 export const notion = new Client({ auth: NOTION_KEY });
8
+export const renderClient = new NotionAPI({
9
+  authToken: NOTION_USER_TOKEN,
10
+  activeUser: NOTION_USER_ID,
11
+});
7 12
 
8 13
 /**
9 14
  * Translates incoming GET parameters into a valid block ID.

+ 848
- 0
package-lock.json
Dosya farkı çok büyük olduğundan ihmal edildi
Dosyayı Görüntüle


+ 2
- 0
package.json Dosyayı Görüntüle

@@ -16,6 +16,8 @@
16 16
     "eslint": "8.29.0",
17 17
     "eslint-config-next": "13.0.6",
18 18
     "next": "13.0.6",
19
+    "nextjs-cors": "^2.1.2",
20
+    "notion-client": "^6.15.6",
19 21
     "react": "18.2.0",
20 22
     "react-dom": "18.2.0",
21 23
     "typescript": "4.9.3"

+ 26
- 0
pages/api/getTopic.ts Dosyayı Görüntüle

@@ -0,0 +1,26 @@
1
+// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
2
+import type { NextApiRequest, NextApiResponse } from "next";
3
+import NextCors from "nextjs-cors";
4
+import { getDatabaseId, notion, renderClient } from "../../lib/notion";
5
+import { DatabaseType, Error, Topic } from "../../types";
6
+
7
+export default async function handler(
8
+  req: NextApiRequest,
9
+  res: NextApiResponse<any>
10
+) {
11
+  await NextCors(req, res, {
12
+    methods: ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE"],
13
+    origin: "*",
14
+    optionsSuccessStatus: 200,
15
+  });
16
+
17
+  const {
18
+    query: { id },
19
+  } = req;
20
+  if (!id) {
21
+    res.status(404).json({ status: "ID not supplied." });
22
+  } else {
23
+    const recordMap = await renderClient.getPage(id as string);
24
+    res.status(200).json(recordMap);
25
+  }
26
+}

+ 7
- 0
pages/api/getTopicList.ts Dosyayı Görüntüle

@@ -1,5 +1,6 @@
1 1
 // Next.js API route support: https://nextjs.org/docs/api-routes/introduction
2 2
 import type { NextApiRequest, NextApiResponse } from "next";
3
+import NextCors from "nextjs-cors";
3 4
 import { getDatabaseId, notion } from "../../lib/notion";
4 5
 import { DatabaseType, Error, Topic } from "../../types";
5 6
 
@@ -14,6 +15,12 @@ export default async function handler(
14 15
   req: NextApiRequest,
15 16
   res: NextApiResponse<Topic[] | Error>
16 17
 ) {
18
+  await NextCors(req, res, {
19
+    methods: ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE"],
20
+    origin: "*",
21
+    optionsSuccessStatus: 200,
22
+  });
23
+
17 24
   const {
18 25
     query: { type },
19 26
   } = req;