Skip to content

Commit 9c5f47b

Browse files
committed
version: notion-utils v0.4.7
1 parent 29c22e0 commit 9c5f47b

File tree

11 files changed

+445
-4
lines changed

11 files changed

+445
-4
lines changed

packages/notion-to-utils/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# notion-to-utils
22

3+
## 0.4.7
4+
5+
### Patch Changes
6+
7+
- makePreviewImage 추가
8+
39
## 0.4.6
410

511
### Patch Changes

packages/notion-to-utils/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "notion-to-utils",
3-
"version": "0.4.6",
3+
"version": "0.4.7",
44
"description": "",
55
"repository": {
66
"type": "git",
@@ -40,6 +40,8 @@
4040
"vitest": "^2.1.2"
4141
},
4242
"dependencies": {
43-
"@notionhq/client": "^2.2.15"
43+
"@notionhq/client": "^2.2.15",
44+
"@types/lqip-modern": "^1.1.7",
45+
"lqip-modern": "^2.1.0"
4446
}
4547
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Client } from '@notionhq/client';
2-
import { isPropertyFileType } from '../file/isPropertyFileType';
2+
import { isPropertyFileType } from './utils/isPropertyFileType';
33
/**
44
* Retrieves the URL of a file attached to a specific property of a Notion page.
55
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Client } from '@notionhq/client';
2-
import { isPageObjectResponse } from '../page/isPageObjectResponse';
2+
import { isPageObjectResponse } from './utils/isPageObjectResponse';
33

44
/**
55
* Retrieves properties of a Notion page.

packages/notion-to-utils/src/page/isPageObjectResponse.ts renamed to packages/notion-to-utils/src/client/utils/isPageObjectResponse.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import {
33
GetPageResponse,
44
} from '@notionhq/client/build/src/api-endpoints';
55

6+
/**
7+
* Type guard function to check if the response is a PageObjectResponse
8+
* @param {GetPageResponse} obj - The response object from Notion API
9+
* @returns {boolean} True if the object is a PageObjectResponse
10+
*/
611
export const isPageObjectResponse = (
712
obj: GetPageResponse
813
): obj is PageObjectResponse => {

packages/notion-to-utils/src/file/isPropertyFileType.ts renamed to packages/notion-to-utils/src/client/utils/isPropertyFileType.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import {
55

66
type NonEmptyArray<T> = [T, ...T[]];
77

8+
/**
9+
* Type guard function to check if a property of a Notion page is a files property and contains at least one file
10+
* @param {GetPageResponse} obj - The response object from Notion API
11+
* @param {keyof PageObjectResponse['properties']} propertyKey - The key of the property to check
12+
* @returns {boolean} True if the property exists, is of type 'files', and contains at least one file
13+
*/
814
export const isPropertyFileType = (
915
obj: GetPageResponse,
1016
propertyKey: keyof PageObjectResponse['properties']

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './client';
2+
export * from './utils';
23

34
import type {
45
PageObjectResponse,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './makePreviewImage';
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import {
2+
describe,
3+
it,
4+
expect,
5+
beforeEach,
6+
vi,
7+
beforeAll,
8+
afterEach,
9+
} from 'vitest';
10+
import { makePreviewImage } from './makePreviewImage';
11+
import lqip from 'lqip-modern';
12+
13+
beforeAll(() => {
14+
// lqip-modern 모듈 모킹
15+
vi.mock('lqip-modern');
16+
// fetch 모킹
17+
global.fetch = vi.fn();
18+
});
19+
20+
afterEach(() => {
21+
vi.clearAllMocks();
22+
});
23+
24+
describe('makePreviewImage', () => {
25+
it('이미지 URL이 주어졌을 때 PreviewImage 객체를 반환한다', async () => {
26+
// 테스트 데이터 준비
27+
const mockImageUrl = 'https://example.com/image.jpg';
28+
const mockBuffer = Buffer.from('test');
29+
const mockLqipResult = {
30+
metadata: {
31+
dataURIBase64: 'data:image/jpeg;base64,test',
32+
originalHeight: 100,
33+
originalWidth: 200,
34+
},
35+
};
36+
37+
// fetch 모의 구현
38+
(global.fetch as unknown as ReturnType<typeof vi.fn>).mockResolvedValue({
39+
arrayBuffer: () => Promise.resolve(mockBuffer),
40+
});
41+
42+
// lqip 모의 구현
43+
(lqip as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(
44+
mockLqipResult
45+
);
46+
47+
// 함수 실행
48+
const result = await makePreviewImage(mockImageUrl);
49+
50+
// 결과 검증
51+
expect(result).toEqual({
52+
dataURIBase64: mockLqipResult.metadata.dataURIBase64,
53+
originalHeight: mockLqipResult.metadata.originalHeight,
54+
originalWidth: mockLqipResult.metadata.originalWidth,
55+
});
56+
57+
// fetch가 올바른 URL로 호출되었는지 확인
58+
expect(fetch).toHaveBeenCalledWith(mockImageUrl);
59+
// lqip가 올바른 버퍼로 호출되었는지 확인
60+
expect(lqip).toHaveBeenCalledWith(mockBuffer);
61+
});
62+
63+
it('에러가 발생했을 때 undefined를 반환한다', async () => {
64+
const mockImageUrl = 'https://example.com/invalid-image.jpg';
65+
66+
// fetch 에러 시뮬레이션
67+
(global.fetch as unknown as ReturnType<typeof vi.fn>).mockRejectedValue(
68+
new Error('Failed to fetch')
69+
);
70+
71+
const result = await makePreviewImage(mockImageUrl);
72+
73+
expect(result).toBeUndefined();
74+
});
75+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import lqip from 'lqip-modern';
2+
3+
export interface PreviewImage {
4+
originalWidth: number;
5+
originalHeight: number;
6+
dataURIBase64: string;
7+
}
8+
9+
/**
10+
* Generates a low-quality preview image from a given URL
11+
*
12+
* @param url - The URL of the image to generate a preview for
13+
* @returns A Promise that resolves to a PreviewImage object containing the base64 data URI
14+
* and original dimensions, or undefined if the operation fails
15+
*
16+
* @example
17+
* const preview = await makePreviewImage('https://example.com/image.jpg');
18+
* if (preview) {
19+
* console.log(preview.dataURIBase64);
20+
* }
21+
*/
22+
export const makePreviewImage = async (url: string) => {
23+
try {
24+
const response = await fetch(url);
25+
const buffer = Buffer.from(await response.arrayBuffer());
26+
27+
const {
28+
metadata: { dataURIBase64, originalHeight, originalWidth },
29+
} = await lqip(buffer);
30+
const result: PreviewImage = {
31+
dataURIBase64,
32+
originalHeight,
33+
originalWidth,
34+
};
35+
return result;
36+
} catch (error) {
37+
return undefined;
38+
}
39+
};

0 commit comments

Comments
 (0)