Skip to content

Commit 8fe7d42

Browse files
committed
refactor: http to server. request
1 parent fbbdf5d commit 8fe7d42

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

apps/web/const/notion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export const SEO_MAP: Record<string, string> = {
77
}
88

99
export const databaseList = [
10+
'd093839d-1d66-4880-811c-bb060627dd5d',// 博文列表
1011
'd8423c93-e14c-4089-97c0-eaa23eda1d71',// 地图数据源
11-
'20a2d306f3b04b4da32dfa0ddf68ed4f',// 博文列表
1212
// 'fd2ef32a46bb42a69711f826cb70a267', // 地图
1313
// '5c0aa83127014f3791e9c66ce70687dc', // 发布历史
1414
]

apps/web/pages/api/doc.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ async function fetchDocByPath(path: string): Promise<string | null> {
5454
const queryResult = await queryIdByPathInDatabase(id,path);
5555
if(queryResult){
5656
console.log('从遍历数据源中获取ID',path,id)
57+
return queryResult;
5758
}
58-
return queryResult;
5959
}
60-
6160
return null
6261
}
6362

@@ -68,7 +67,7 @@ async function queryIdByPathInDatabase(databaseId: string, path: string) {
6867
}
6968
try{
7069
const queryResultFromDataSource = await officialNotion.dataSources.query({
71-
data_source_id: databaseId,
70+
data_source_id: parsePageId(databaseId),
7271
filter: {
7372
or: [
7473
{
@@ -104,22 +103,20 @@ async function queryIdByPathInDatabase(databaseId: string, path: string) {
104103
}
105104
}
106105

107-
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
108-
const notionIdOrUrlPath = (req.query.id || '').toString()
109-
// 基于文章ID 或 path 查询详情
106+
export async function getNotionDocByIdOrPathFromServer(notionIdOrUrlPath: string) {
110107
let notionId = notionIdOrUrlPath
111108
/**
112109
* 如果是 URL path,需要查询对应的 notion id
113110
* */
114111
if (!/^(\w|\d){8}/.test(notionIdOrUrlPath) || notionIdOrUrlPath.length < 20) {
115112
notionId =
116-
SEO_REVERT_MAP[notionIdOrUrlPath] ||
117-
(await fetchDocByPath(notionIdOrUrlPath)) ||
118-
''
113+
SEO_REVERT_MAP[notionIdOrUrlPath] ||
114+
(await fetchDocByPath(notionIdOrUrlPath)) ||
115+
''
119116
if (!notionId) {
120117
console.error(notionIdOrUrlPath, 'no page')
121118
// throw Error('找不到 notion 页面')
122-
return res.status(404).json(null)
119+
return null;
123120
}
124121
}
125122

@@ -138,7 +135,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
138135
// }
139136
} catch (e) {
140137
console.error(e,'fetch recordMap error')
141-
return res.status(200).json(null)
138+
return null
142139
}
143140
/**
144141
* 为了通过获取 page 的property属性 title path description keywords
@@ -147,22 +144,22 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
147144
let notionPage = null
148145
if (recordMap && recordMap.block[notionId]?.value.type === 'page' && getOfficialNotion()) {
149146
notionPage = await getOfficialNotion()
150-
?.pages.retrieve({
151-
page_id: notionId,
152-
})
153-
.catch(function (e) {
154-
console.warn(e, '获取文章详情失败')
155-
})
147+
?.pages.retrieve({
148+
page_id: notionId,
149+
})
150+
.catch(function (e) {
151+
console.warn(e, '获取文章详情失败')
152+
})
156153
}
157154
const properties = recordMap?.block[notionId]?.value?.properties
158155
const title = get(properties, 'title.0.0') || null
159156
// const description = get(properties, 'description.0.0') || null;
160157
// console.log(notionPage.properties,'notionPage')
161158
const path = get(notionPage, 'properties.path.url') || null
162159
const description =
163-
get(notionPage, 'properties.description.rich_text[0].plain_text') || null
160+
get(notionPage, 'properties.description.rich_text[0].plain_text') || null
164161
const keywords = (
165-
get(notionPage, 'properties.keywords.multi_select') || []
162+
get(notionPage, 'properties.keywords.multi_select') || []
166163
).map(function (item) {
167164
//@ts-ignore
168165
return item.name || ''
@@ -181,6 +178,15 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
181178
if(recordMap){
182179
writeCacheFile(notionIdOrUrlPath, responseData)
183180
}
181+
}
182+
183+
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
184+
const notionIdOrUrlPath = (req.query.id || '').toString()
185+
// 基于文章ID 或 path 查询详情
186+
const responseData = await getNotionDocByIdOrPathFromServer(notionIdOrUrlPath);
187+
if(!responseData){
188+
return res.status(404);
189+
}
184190

185191
// 增加缓存相应头
186192
const cacheTime = 60 * 30;

apps/web/service/server/doc.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { getNotionDetailFromServer, WEB_HOST} from "./api";
55
import {NotionDocProp} from "../../components/notion/NotionDoc";
66
import {getOfficialNotion} from "./notion";
77
import {GetStaticPathsResult} from "next/types";
8+
import {getNotionDocByIdOrPathFromServer} from "../../pages/api/doc";
9+
import {parsePageId} from "notion-utils";
810
export interface DocNotion {
911
notFound?: boolean,
1012
props?: NotionDocProp,
@@ -21,8 +23,8 @@ export async function getNotionDocDetail(id: string, notFound: boolean = true):P
2123
let result = getCacheContent(id);
2224
if(!result){
2325
try{
24-
result = await getNotionDetailFromServer(id);
25-
console.log(id,'get from server ',WEB_HOST, result)
26+
result = await getNotionDocByIdOrPathFromServer(id);
27+
console.log(id,'get from server ::', result)
2628
}catch (e) {
2729
console.error(id,'fetch doc detail error')
2830
}
@@ -61,7 +63,7 @@ export async function computeStaticPaths() {
6163
// 从数据库中获取文章列表
6264
for(let i=0; i<databaseList.length; i++){
6365
const dataSource = await getOfficialNotion()?.dataSources.query({
64-
data_source_id: databaseList[0],
66+
data_source_id: parsePageId(databaseList[0]),
6567
})
6668

6769
dataSource?.results.forEach(function (item) {

0 commit comments

Comments
 (0)