Skip to content

Commit aa6ed63

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

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
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: 27 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 || ''
@@ -182,6 +179,17 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
182179
writeCacheFile(notionIdOrUrlPath, responseData)
183180
}
184181

182+
return responseData
183+
}
184+
185+
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
186+
const notionIdOrUrlPath = (req.query.id || '').toString()
187+
// 基于文章ID 或 path 查询详情
188+
const responseData = await getNotionDocByIdOrPathFromServer(notionIdOrUrlPath);
189+
if(!responseData){
190+
return res.status(404);
191+
}
192+
185193
// 增加缓存相应头
186194
const cacheTime = 60 * 30;
187195
res.setHeader('Cache-Control', `max-age=${cacheTime},public`);

apps/web/service/server/doc.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {isDev} from '../../const/env'
22
import {databaseList, DEFAULT_BASE_DOC_PATH} from '../../const/notion'
33
import {getCacheContent} from './cache'
4-
import { getNotionDetailFromServer, WEB_HOST} from "./api";
54
import {NotionDocProp} from "../../components/notion/NotionDoc";
65
import {getOfficialNotion} from "./notion";
76
import {GetStaticPathsResult} from "next/types";
7+
import {getNotionDocByIdOrPathFromServer} from "../../pages/api/doc";
8+
import {parsePageId} from "notion-utils";
89
export interface DocNotion {
910
notFound?: boolean,
1011
props?: NotionDocProp,
@@ -21,8 +22,8 @@ export async function getNotionDocDetail(id: string, notFound: boolean = true):P
2122
let result = getCacheContent(id);
2223
if(!result){
2324
try{
24-
result = await getNotionDetailFromServer(id);
25-
console.log(id,'get from server ',WEB_HOST, result)
25+
result = await getNotionDocByIdOrPathFromServer(id);
26+
console.log(id,'get from server ::', result)
2627
}catch (e) {
2728
console.error(id,'fetch doc detail error')
2829
}
@@ -61,7 +62,7 @@ export async function computeStaticPaths() {
6162
// 从数据库中获取文章列表
6263
for(let i=0; i<databaseList.length; i++){
6364
const dataSource = await getOfficialNotion()?.dataSources.query({
64-
data_source_id: databaseList[0],
65+
data_source_id: parsePageId(databaseList[0]),
6566
})
6667

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

0 commit comments

Comments
 (0)