1+
12import {
23 JSONData ,
34 NotionUserType ,
@@ -22,23 +23,34 @@ const loadPageChunkBody = {
2223 verticalColumns : false ,
2324} ;
2425
26+ let ctr = 0
27+
28+ const delay = ms => new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
29+
2530const fetchNotionData = async < T extends any > ( {
2631 resource,
2732 body,
2833 notionToken,
2934} : INotionParams ) : Promise < T > => {
30- const res = await fetch ( `${ NOTION_API } /${ resource } ` , {
31- method : "POST" ,
32- headers : {
33- "content-type" : "application/json" ,
34- ...( notionToken && { cookie : `token_v2=${ notionToken } ` } ) ,
35- } ,
36-
37- body : JSON . stringify ( body ) ,
38- } ) ;
35+ try {
36+ console . log ( '------ fetching notion data?' , resource , body , ctr ++ )
37+ const res = await fetch ( `${ NOTION_API } /${ resource } ` , {
38+ method : "POST" ,
39+ headers : {
40+ "content-type" : "application/json" ,
41+ ...( notionToken && { cookie : `token_v2=${ notionToken } ` } ) ,
42+ } ,
3943
40- let json = await res . json ( )
41- return json ;
44+ body : JSON . stringify ( body ) ,
45+ } ) ;
46+
47+ let json = await res . json ( )
48+ // await delay(1000);
49+ return json ;
50+ } catch ( e ) {
51+ console . error ( 'fetchNotionData error:' , e , e . message )
52+ throw new Error ( 'Failed to pull data from Notion' )
53+ }
4254} ;
4355
4456export const fetchPageById = async ( pageId : string , notionToken ?: string ) => {
@@ -102,25 +114,25 @@ export const fetchNotionUsers = async (
102114 userIds : string [ ] ,
103115 notionToken ?: string
104116) => {
105- const users = await fetchNotionData < { results : NotionUserType [ ] } > ( {
106- resource : "getRecordValues" ,
107- body : {
108- requests : userIds . map ( ( id ) => ( { id, table : "notion_user" } ) ) ,
109- } ,
110- notionToken,
111- } ) ;
112- if ( users && users . results ) {
113- return users . results . map ( ( u ) => {
114- const user = {
115- id : u . value . id ,
116- firstName : u . value . given_name ,
117- lastLame : u . value . family_name ,
118- fullName : u . value . given_name + " " + u . value . family_name ,
119- profilePhoto : u . value . profile_photo ,
120- } ;
121- return user ;
122- } ) ;
123- }
117+ // const users = await fetchNotionData<{ results: NotionUserType[] }>({
118+ // resource: "getRecordValues",
119+ // body: {
120+ // requests: userIds.map((id) => ({ id, table: "notion_user" })),
121+ // },
122+ // notionToken,
123+ // });
124+ // if (users && users.results) {
125+ // return users.results.map((u) => {
126+ // const user = {
127+ // id: u.value.id,
128+ // firstName: u.value.given_name,
129+ // lastLame: u.value.family_name,
130+ // fullName: u.value.given_name + " " + u.value.family_name,
131+ // profilePhoto: u.value.profile_photo,
132+ // };
133+ // return user;
134+ // });
135+ // }
124136 return [ ] ;
125137} ;
126138
@@ -131,12 +143,18 @@ export const fetchBlocks = async (
131143 return await fetchNotionData < LoadPageChunkData > ( {
132144 resource : "syncRecordValues" ,
133145 body : {
134- recordVersionMap : {
135- block : blockList . reduce ( ( obj , blockId ) => {
136- obj [ blockId ] = - 1 ;
137- return obj ;
138- } , { } as { [ key : string ] : - 1 } ) ,
139- } ,
146+ // recordVersionMap: {
147+ // block: blockList.reduce((obj, blockId) => {
148+ // obj[blockId] = -1;
149+ // return obj;
150+ // }, {} as { [key: string]: -1 }),
151+ // },
152+
153+ requests : blockList . map ( ( id ) => ( {
154+ id,
155+ table : "block" ,
156+ version : - 1 ,
157+ } ) ) ,
140158 } ,
141159 notionToken,
142160 } ) ;
0 commit comments