import PQueue from 'p-queue' ; import { ExtendedRecordMap , PageMap } from 'notion-types' ; import { parsePageId } from './parse-page-id' ; param rootPageId param rootSpaceId param getPage param opts export async function getAllPagesInSpace ( rootPageId , rootSpaceId , getPage , { concurrency = 4 , traverseCollections = true , targetPageId = null } = { } ) { const pages = { } ; const pendingPageIds = new Set < string > ( ) ; const queue = new PQueue ( { concurrency } ) ; async function processPage ( pageId ) { if ( targetPageId && pendingPageIds . has ( targetPageId ) ) { return ; } pageId = parsePageId ( pageId ) as string ; if ( pageId && ! pages [ pageId ] && ! pendingPageIds . has ( pageId ) ) { pendingPageIds . add ( pageId ) ; queue . add ( async ( ) => { try { if ( targetPageId && pendingPageIds . has ( targetPageId ) && pageId !== targetPageId ) { return ; } const page = await getPage ( pageId ) ; if ( ! page ) { return ; } const spaceId = page . block [ pageId ] ?. value ?. space_id ; if ( ! rootSpaceId ) { rootSpaceId = spaceId ; } else if ( rootSpaceId !== spaceId ) { return ; } Object . keys ( page . block ) . filter ( ( key ) => { const block = page . block [ key ] ?. value ; if ( ! block ) return false ; const isPage = block . type === 'page' || block . type === 'collection_view_page' ; return isPage && block . space_id === rootSpaceId ; } ) . forEach ( ( subPageId ) => processPage ( subPageId ) ) ; if ( traverseCollections ) { for ( const collectionViews of Object . values ( page . collection_query ) ) { for ( const collectionData of Object . values ( collectionViews ) ) { const { blockIds } = collectionData ; if ( blockIds ) { for ( const collectionItemId of blockIds ) { processPage ( collectionItemId ) ; } } } } } pages [ pageId ] = page ; } catch ( err ) { console . warn ( 'page load error' , { pageId , spaceId : rootSpaceId } , err . statusCode , err . message ) ; pages [ pageId ] = null ; } pendingPageIds . delete ( pageId ) ; } ) ; } } await processPage ( rootPageId ) ; await queue . onIdle ( ) ; return pages ; }