@@ -2,7 +2,21 @@ const fetch = require("node-fetch")
22const { errorMessage } = require ( "../error-message" )
33const { getBlocks } = require ( "./get-blocks" )
44
5- exports . getPages = async ( { token, databaseId, notionVersion = "2021-05-13" } , reporter ) => {
5+ async function fetchPageChildren ( { page, token, notionVersion } , reporter , cache ) {
6+ let cacheKey = `notionApiPageChildren:${ page . id } :${ page . last_edited_time } `
7+
8+ let children = await cache . get ( cacheKey )
9+
10+ if ( children ) {
11+ return children
12+ }
13+
14+ children = await getBlocks ( { id : page . id , token, notionVersion } , reporter )
15+ await cache . set ( cacheKey , children )
16+ return children
17+ }
18+
19+ exports . getPages = async ( { token, databaseId, notionVersion = "2021-05-13" } , reporter , cache ) => {
620 let hasMore = true
721 let startCursor = ""
822 const url = `https://api.notion.com/v1/databases/${ databaseId } /query`
@@ -32,8 +46,7 @@ exports.getPages = async ({ token, databaseId, notionVersion = "2021-05-13" }, r
3246 hasMore = result . has_more
3347
3448 for ( let page of result . results ) {
35- page . children = await getBlocks ( { id : page . id , token, notionVersion } , reporter )
36-
49+ page . children = await fetchPageChildren ( { page, token, notionVersion } , reporter , cache )
3750 pages . push ( page )
3851 }
3952 } catch ( e ) {
0 commit comments