Skip to content

Commit 52f2271

Browse files
Merge pull request #7 from meshcloud/feature/block-children-paging
feat: implement support for block children paging
2 parents 3c95e7c + c166183 commit 52f2271

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

src/NotionApiFacade.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ export class NotionApiFacade {
4949
...query,
5050
start_cursor: next_cursor || undefined,
5151
})
52-
);
52+
);
5353

5454
results.push(...response.results);
55-
55+
5656
next_cursor = response.next_cursor;
5757
} while (next_cursor);
5858

@@ -66,20 +66,25 @@ export class NotionApiFacade {
6666
}
6767

6868
async listBlockChildren(blockId: string) {
69-
const result = await this.withRetry(
70-
async () =>
71-
await this.client.blocks.children.list({
72-
block_id: blockId,
73-
})
74-
); // todo: paging here?
75-
76-
if (result.next_cursor) {
77-
throw new Error(
78-
`Paging not implemented, block ${blockId} has more children than returned in a single request`
69+
const results = [];
70+
71+
let next_cursor: string | null = null;
72+
73+
do {
74+
const response = await this.withRetry(
75+
async () =>
76+
await this.client.blocks.children.list({
77+
block_id: blockId,
78+
start_cursor: next_cursor || undefined,
79+
})
7980
);
80-
}
8181

82-
return result;
82+
results.push(...response.results);
83+
84+
next_cursor = response.next_cursor;
85+
} while (next_cursor);
86+
87+
return results;
8388
}
8489

8590
printStats() {

src/RecursiveBodyRenderer.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ export class RecursiveBodyRenderer {
2020

2121
const childs = await this.publicApi.listBlockChildren(page.id);
2222

23-
// todo: paging
24-
const renderChilds = childs.results.map(
23+
const renderChilds = childs.map(
2524
async (x) => await this.renderBlock(x, "", context)
2625
);
2726
const blocks = await Promise.all(renderChilds);
@@ -47,7 +46,7 @@ export class RecursiveBodyRenderer {
4746
// blocks, see https://developers.notion.com/reference/retrieve-a-block
4847
// "If a block contains the key has_children: true, use the Retrieve block children endpoint to get the list of children"
4948
const children = block.has_children
50-
? (await this.publicApi.listBlockChildren(block.id)).results
49+
? (await this.publicApi.listBlockChildren(block.id))
5150
: [];
5251

5352
const childIndent = indent + " ".repeat(parentBlock?.childIndent || 0);

0 commit comments

Comments
 (0)