forked from NotionX/react-notion-x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotion.ts
38 lines (31 loc) · 1.2 KB
/
notion.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { Client } from '@notionhq/client'
import { NotionAPI } from 'notion-client'
import { NotionCompatAPI } from 'notion-compat'
import { ExtendedRecordMap, SearchParams, SearchResults } from 'notion-types'
import { previewImagesEnabled, useOfficialNotionAPI } from './config'
import { getPreviewImageMap } from './preview-images'
const notion = useOfficialNotionAPI
? new NotionCompatAPI(new Client({ auth: process.env.NOTION_TOKEN }))
: new NotionAPI()
if (useOfficialNotionAPI) {
console.warn(
'Using the official Notion API. Note that many blocks only include partial support for formatting and layout. Use at your own risk.'
)
}
export async function getPage(pageId: string): Promise<ExtendedRecordMap> {
const recordMap = await notion.getPage(pageId)
if (previewImagesEnabled) {
const previewImageMap = await getPreviewImageMap(recordMap)
;(recordMap as any).preview_images = previewImageMap
}
return recordMap
}
export async function search(params: SearchParams): Promise<SearchResults> {
if (notion instanceof NotionAPI) {
return notion.search(params)
} else {
console.error(
'NotionCompatAPI does not have a search method. Use NotionAPI instead.'
)
}
}