@@ -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 ;
0 commit comments