Skip to content

Commit 9eac472

Browse files
committed
refactor: client 메소드 분리
1 parent 6062ca8 commit 9eac472

File tree

3 files changed

+69
-46
lines changed

3 files changed

+69
-46
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Client } from '@notionhq/client';
2+
import { isPropertyFileType } from '../file/isPropertyFileType';
3+
/**
4+
* Retrieves the URL of a file attached to a specific property of a Notion page.
5+
*
6+
* @async
7+
* @param {string} pageId - The ID of the Notion page.
8+
* @param {string} propertyKey - The key of the property containing the file.
9+
* @returns {Promise<string | undefined>} The URL of the file if found, otherwise undefined.
10+
* @throws {Error} If there's an issue retrieving the page or if the property is not of file type.
11+
*/
12+
export async function getFileUrl(
13+
client: Client,
14+
pageId: string,
15+
propertyKey: string
16+
) {
17+
const page = await client.pages.retrieve({
18+
page_id: pageId,
19+
});
20+
21+
if (!isPropertyFileType(page, propertyKey)) return undefined;
22+
23+
const property = page.properties[propertyKey]!;
24+
const fileObj = property.files[0];
25+
return 'file' in fileObj ? fileObj.file.url : undefined;
26+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Client } from '@notionhq/client';
2+
import { isPageObjectResponse } from '../page/isPageObjectResponse';
3+
4+
/**
5+
* Retrieves properties of a Notion page.
6+
*
7+
* @param {Client} client - The Notion client instance.
8+
* @param {string} pageId - The ID of the Notion page.
9+
* @param {string[]} [keys=[]] - Optional array of property keys to filter. If empty, all properties are returned.
10+
* @returns {Promise<PageObjectResponse['properties'] | undefined>} The requested page properties or undefined if the page is not found or is not a PageObjectResponse.
11+
*/
12+
export async function getPageProperties(
13+
client: Client,
14+
pageId: string,
15+
keys: string[] = []
16+
) {
17+
const page = await client.pages.retrieve({
18+
page_id: pageId,
19+
});
20+
21+
if (!isPageObjectResponse(page)) return;
22+
const { properties } = page;
23+
24+
if (keys.length === 0) {
25+
return properties;
26+
}
27+
28+
const filteredProperties: Record<string, any> = {};
29+
30+
keys.forEach((key) => {
31+
if (key in properties) {
32+
filteredProperties[key] = properties[key];
33+
}
34+
});
35+
36+
return filteredProperties;
37+
}
Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,17 @@
11
import { Client as NotionClient } from '@notionhq/client';
2-
32
import type { ClientOptions } from '@notionhq/client/build/src/Client';
4-
import { isPageObjectResponse } from '../page/isPageObjectResponse';
5-
import { isPropertyFileType } from '../file/isPropertyFileType';
3+
import { getPageProperties as getPagePropertiesFunc } from './getPageProperties';
4+
import { getFileUrl as getFileUrlFunc } from './getFileUrl';
65

76
export class Client extends NotionClient {
87
constructor(options: ClientOptions = {}) {
98
super(options);
109
}
1110

12-
async getPageProperties(pageId: string, keys: string[] = []) {
13-
const res = await this.pages.retrieve({
14-
page_id: pageId,
15-
});
16-
17-
if (isPageObjectResponse(res)) {
18-
const properties = res.properties;
19-
20-
// keys가 빈 배열이면 모든 properties를 반환
21-
if (keys.length === 0) {
22-
return properties;
23-
}
24-
25-
const filteredProperties: Record<string, any> = {};
26-
27-
// keys 배열을 순회하면서 해당하는 properties 값을 추출
28-
keys.forEach((key) => {
29-
if (key in properties) {
30-
filteredProperties[key] = properties[key];
31-
}
32-
});
33-
34-
return filteredProperties; // 추출된 properties 반환
35-
}
36-
37-
return;
38-
}
39-
40-
async getFileUrl(pageId: string, propertyKey: string) {
41-
const res = await this.pages.retrieve({
42-
page_id: pageId,
43-
});
44-
45-
if (isPropertyFileType(res, propertyKey)) {
46-
const property = res.properties[propertyKey]!;
47-
const fileObj = property.files[0];
48-
if ('file' in fileObj) {
49-
const fileUrl = fileObj.file.url;
50-
return fileUrl;
51-
}
52-
}
53-
return;
54-
}
11+
getPageProperties = (pageId: string, keys?: string[]) =>
12+
getPagePropertiesFunc(this, pageId, keys);
13+
getFileUrl = (pageId: string, propertyKey: string) =>
14+
getFileUrlFunc(this, pageId, propertyKey);
5515
}
5616

5717
export default Client;

0 commit comments

Comments
 (0)