@@ -2,7 +2,21 @@ const fetch = require("node-fetch")
2
2
const { errorMessage } = require ( "../error-message" )
3
3
const { getBlocks } = require ( "./get-blocks" )
4
4
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 ) => {
6
20
let hasMore = true
7
21
let startCursor = ""
8
22
const url = `https://api.notion.com/v1/databases/${ databaseId } /query`
@@ -32,8 +46,7 @@ exports.getPages = async ({ token, databaseId, notionVersion = "2021-05-13" }, r
32
46
hasMore = result . has_more
33
47
34
48
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 )
37
50
pages . push ( page )
38
51
}
39
52
} catch ( e ) {
0 commit comments