Skip to content

Commit 208b1b8

Browse files
committed
feat: utils: getPageProperties add
1 parent ac961c0 commit 208b1b8

File tree

6 files changed

+222
-9
lines changed

6 files changed

+222
-9
lines changed

packages/notion-to-utils/package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
"author": "Jinsoo Lee",
1212
"license": "MIT",
1313
"devDependencies": {
14-
"typescript": "^5.6.3",
15-
"vitest": "^2.1.2",
1614
"@repo/eslint-config": "workspace:*",
17-
"@repo/typescript-config": "workspace:*"
15+
"@repo/typescript-config": "workspace:*",
16+
"typescript": "^5.6.3",
17+
"vitest": "^2.1.2"
18+
},
19+
"dependencies": {
20+
"@notionhq/client": "^2.2.15"
1821
}
1922
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 = 'TEMP';
15+
16+
// Mock response data
17+
const getPagePropertiesMock = {
18+
object: 'page',
19+
id: TEST_ID,
20+
properties: {
21+
Category: { id: '1', type: 'text', text: { content: 'Category A' } },
22+
Slug: { id: '2', type: 'text', text: { content: 'slug-value' } },
23+
},
24+
};
25+
26+
describe('getPageProperties', () => {
27+
it('should return properties when valid pageId is provided', async () => {
28+
// Mock the notionClient.pages.retrieve method
29+
notionClient.pages.retrieve = vi
30+
.fn()
31+
.mockResolvedValue(getPagePropertiesMock);
32+
33+
// Test when no keys are provided (should return all properties)
34+
const properties = await notionClient.getPageProperties(TEST_ID);
35+
expect(properties).toEqual(getPagePropertiesMock.properties);
36+
});
37+
38+
it('should return properties when valid pageId, keys are provided', async () => {
39+
// Mock the notionClient.pages.retrieve method
40+
notionClient.pages.retrieve = vi
41+
.fn()
42+
.mockResolvedValue(getPagePropertiesMock);
43+
44+
const filteredProperties = await notionClient.getPageProperties(TEST_ID, [
45+
'Category',
46+
]);
47+
48+
expect(filteredProperties).toEqual({
49+
Category: getPagePropertiesMock.properties.Category,
50+
});
51+
});
52+
53+
it('should return undefined when invalid pageId is provided', async () => {
54+
notionClient.pages.retrieve = vi.fn().mockResolvedValue(null);
55+
56+
const filteredProperties = await notionClient.getPageProperties(TEST_ID, [
57+
'Category',
58+
]);
59+
60+
expect(filteredProperties).toBeUndefined();
61+
});
62+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Client as NotionClient } from '@notionhq/client';
2+
3+
import type { ClientOptions } from '@notionhq/client/build/src/Client';
4+
import { isPageObjectResponse } from '../page/isPageObjectResponse';
5+
6+
export class Client extends NotionClient {
7+
constructor(options: ClientOptions = {}) {
8+
super(options);
9+
}
10+
11+
async getPageProperties(pageId: string, keys: string[] = []) {
12+
const res = await this.pages.retrieve({
13+
page_id: pageId,
14+
});
15+
16+
if (isPageObjectResponse(res)) {
17+
const properties = res.properties;
18+
19+
// keys가 빈 배열이면 모든 properties를 반환
20+
if (keys.length === 0) {
21+
return properties;
22+
}
23+
24+
const filteredProperties: Record<string, any> = {};
25+
26+
// keys 배열을 순회하면서 해당하는 properties 값을 추출
27+
keys.forEach((key) => {
28+
if (key in properties) {
29+
filteredProperties[key] = properties[key];
30+
}
31+
});
32+
33+
return filteredProperties; // 추출된 properties 반환
34+
}
35+
36+
return;
37+
}
38+
}
39+
40+
export default Client;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './client';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {
2+
PageObjectResponse,
3+
GetPageResponse,
4+
} from '@notionhq/client/build/src/api-endpoints';
5+
6+
export const isPageObjectResponse = (
7+
obj: GetPageResponse
8+
): obj is PageObjectResponse => {
9+
return obj && 'properties' in obj;
10+
};

0 commit comments

Comments
 (0)