Skip to content

Commit 62bbe85

Browse files
committed
feat: getPageBlocks func, test 추가
1 parent 86a9b78 commit 62bbe85

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
2+
import Client from '.';
3+
4+
let notionClient: Client;
5+
6+
beforeAll(() => {
7+
notionClient = new Client();
8+
});
9+
10+
afterEach(() => {
11+
vi.restoreAllMocks();
12+
});
13+
14+
const TEST_ID = 'TEST_PAGE_ID';
15+
16+
// Mock response data
17+
const mockBlockResponse1 = {
18+
object: 'list',
19+
results: [
20+
{
21+
object: 'block',
22+
id: 'block1',
23+
type: 'paragraph',
24+
paragraph: { text: [{ text: { content: 'Test content 1' } }] },
25+
},
26+
{
27+
object: 'block',
28+
id: 'block2',
29+
type: 'heading_1',
30+
heading_1: { text: [{ text: { content: 'Test heading' } }] },
31+
},
32+
],
33+
next_cursor: 'cursor1',
34+
has_more: true,
35+
};
36+
37+
const mockBlockResponse2 = {
38+
object: 'list',
39+
results: [
40+
{
41+
object: 'block',
42+
id: 'block3',
43+
type: 'numbered_list_item',
44+
numbered_list_item: { text: [{ text: { content: 'Test list item' } }] },
45+
},
46+
],
47+
next_cursor: null,
48+
has_more: false,
49+
};
50+
51+
describe('getPageBlocks', () => {
52+
it('페이지의 모든 블록을 가져온다', async () => {
53+
const listMock = vi
54+
.fn()
55+
.mockResolvedValueOnce(mockBlockResponse1)
56+
.mockResolvedValueOnce(mockBlockResponse2);
57+
58+
notionClient.blocks.children.list = listMock;
59+
60+
const blocks = await notionClient.getPageBlocks(TEST_ID);
61+
expect(blocks).toHaveLength(3); // Total blocks from both responses
62+
expect(blocks[0]!.id).toBe('block1');
63+
expect(blocks[1]!.id).toBe('block2');
64+
expect(blocks[2]!.id).toBe('block3');
65+
66+
// Verify pagination handling
67+
expect(listMock).toHaveBeenCalledTimes(2);
68+
expect(listMock).toHaveBeenNthCalledWith(1, {
69+
block_id: TEST_ID,
70+
start_cursor: undefined,
71+
});
72+
expect(listMock).toHaveBeenNthCalledWith(2, {
73+
block_id: TEST_ID,
74+
start_cursor: 'cursor1',
75+
});
76+
});
77+
78+
it('API 호출 실패시 빈 배열을 반환한다', async () => {
79+
notionClient.blocks.children.list = vi
80+
.fn()
81+
.mockRejectedValue(new Error('API Error'));
82+
83+
const blocks = await notionClient.getPageBlocks(TEST_ID);
84+
85+
expect(blocks).toEqual([]);
86+
expect(notionClient.blocks.children.list).toHaveBeenCalledWith({
87+
block_id: TEST_ID,
88+
start_cursor: undefined,
89+
});
90+
});
91+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Client } from '@notionhq/client';
2+
import { ListBlockChildrenResponse } from '@notionhq/client/build/src/api-endpoints';
3+
4+
type BlockAttributes = {
5+
numbered_list_item?: {
6+
number?: number;
7+
};
8+
};
9+
10+
type ListBlockChildrenResponseResults = ListBlockChildrenResponse['results'] &
11+
BlockAttributes;
12+
13+
export async function getPageBlocks(client: Client, pageId: string) {
14+
try {
15+
// Get all children blocks with pagination
16+
let allBlocks: ListBlockChildrenResponseResults = [];
17+
let hasMore = true;
18+
let nextCursor = undefined;
19+
20+
while (hasMore) {
21+
const response = (await client.blocks.children.list({
22+
block_id: pageId,
23+
start_cursor: nextCursor,
24+
})) as ListBlockChildrenResponse;
25+
26+
allBlocks = [...allBlocks, ...response.results];
27+
hasMore = response.has_more;
28+
nextCursor = response.next_cursor || undefined;
29+
}
30+
31+
return allBlocks;
32+
} catch (error) {
33+
console.error('Error:', error);
34+
return [];
35+
}
36+
}

packages/notion-to-utils/src/client/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Client as NotionClient } from '@notionhq/client';
22
import type { ClientOptions } from '@notionhq/client/build/src/Client';
33
import { getPageProperties as getPagePropertiesFunc } from './getPageProperties';
44
import { getFileUrl as getFileUrlFunc } from './getFileUrl';
5+
import { getPageBlocks as getPageBlocksFunc } from './getPageBlocks';
56

67
export class Client extends NotionClient {
78
constructor(options: ClientOptions = {}) {
@@ -12,6 +13,7 @@ export class Client extends NotionClient {
1213
getPagePropertiesFunc(this, pageId, keys);
1314
getFileUrl = (pageId: string, propertyKey: string) =>
1415
getFileUrlFunc(this, pageId, propertyKey);
16+
getPageBlocks = (pageId: string) => getPageBlocksFunc(this, pageId);
1517
}
1618

1719
export default Client;

0 commit comments

Comments
 (0)