Skip to content

Commit 6646947

Browse files
committed
feat: utils - getFileUrl add
1 parent 2036f33 commit 6646947

File tree

4 files changed

+140
-3
lines changed

4 files changed

+140
-3
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 KEY = 'media';
15+
const TEST_ID = 'TEMP';
16+
17+
// Mock response data
18+
const getFileUrlValidMock = {
19+
object: 'page',
20+
properties: {
21+
[KEY]: {
22+
type: 'files',
23+
files: [
24+
{
25+
file: {
26+
url: 'https://~~',
27+
expiry_time: 'string',
28+
},
29+
name: 'test',
30+
type: 'file',
31+
},
32+
],
33+
id: '1',
34+
},
35+
},
36+
};
37+
38+
const getFileUrlInValidMock = {
39+
object: 'page',
40+
properties: {
41+
[KEY]: {
42+
type: 'files',
43+
files: [],
44+
id: '1',
45+
},
46+
},
47+
};
48+
49+
describe('getFileUrl', () => {
50+
it('should return fileUrl when valid pageId, valid propertyKey are provided', async () => {
51+
// Mock the notionClient.pages.retrieve method
52+
notionClient.pages.retrieve = vi
53+
.fn()
54+
.mockResolvedValue(getFileUrlValidMock);
55+
56+
const fileUrl = await notionClient.getFileUrl(TEST_ID, KEY);
57+
expect(fileUrl).toEqual(
58+
getFileUrlValidMock.properties[KEY].files[0]?.file.url
59+
);
60+
});
61+
62+
it('should return undefined when invalid pageId, valid propertyKey are provided', async () => {
63+
notionClient.pages.retrieve = vi
64+
.fn()
65+
.mockResolvedValue(getFileUrlInValidMock);
66+
67+
const fileUrl = await notionClient.getFileUrl(TEST_ID, KEY);
68+
expect(fileUrl).toBeUndefined();
69+
});
70+
71+
it('should return undefined when valid pageId, invalid propertyKey are provided', async () => {
72+
notionClient.pages.retrieve = vi
73+
.fn()
74+
.mockResolvedValue(getFileUrlValidMock);
75+
76+
const fileUrl = await notionClient.getFileUrl(TEST_ID, 'temp');
77+
expect(fileUrl).toBeUndefined();
78+
});
79+
});

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Client as NotionClient } from '@notionhq/client';
22

33
import type { ClientOptions } from '@notionhq/client/build/src/Client';
44
import { isPageObjectResponse } from '../page/isPageObjectResponse';
5+
import { isPropertyFileType } from '../file/isPropertyFileType';
56

67
export class Client extends NotionClient {
78
constructor(options: ClientOptions = {}) {
@@ -35,6 +36,22 @@ export class Client extends NotionClient {
3536

3637
return;
3738
}
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+
}
3855
}
3956

4057
export default Client;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {
2+
GetPageResponse,
3+
PageObjectResponse,
4+
} from '@notionhq/client/build/src/api-endpoints';
5+
6+
type NonEmptyArray<T> = [T, ...T[]];
7+
8+
export const isPropertyFileType = (
9+
obj: GetPageResponse,
10+
propertyKey: keyof PageObjectResponse['properties']
11+
): obj is PageObjectResponse & {
12+
properties: {
13+
[K in typeof propertyKey]: {
14+
type: 'files';
15+
files: NonEmptyArray<
16+
| {
17+
file: {
18+
url: string;
19+
expiry_time: string;
20+
};
21+
name: string;
22+
type?: 'file';
23+
}
24+
| {
25+
external: {
26+
url: string;
27+
};
28+
name: string;
29+
type?: 'external';
30+
}
31+
>;
32+
id: string;
33+
};
34+
};
35+
} => {
36+
return (
37+
obj &&
38+
'properties' in obj &&
39+
obj.properties[propertyKey] !== undefined && // key가 존재하는지 체크
40+
obj.properties[propertyKey].type === 'files' &&
41+
Array.isArray(obj.properties[propertyKey].files) &&
42+
obj.properties[propertyKey].files.length > 0
43+
);
44+
};

packages/notion-to-utils/src/preview/_internal.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)