1 | |
2 |
|
3 | |
4 | * Search |
5 | * Search the pages and data sources (databases) shared with the integration by title. Leave `query` empty to list everything. Paginate with `next_cursor` as `start_cursor`. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Notion, |
9 | query: string | undefined, |
10 | object_type: "page" | "data_source" | undefined, |
11 | sort_direction: "ascending" | "descending" | undefined, |
12 | start_cursor: string | undefined, |
13 | page_size: number | undefined |
14 | ) { |
15 | const body: { [key: string]: any } = {} |
16 | if (query !== undefined && query !== "") body.query = query |
17 | if (object_type) body.filter = { property: "object", value: object_type } |
18 | if (sort_direction) { |
19 | body.sort = { timestamp: "last_edited_time", direction: sort_direction } |
20 | } |
21 | if (start_cursor !== undefined && start_cursor !== "") { |
22 | body.start_cursor = start_cursor |
23 | } |
24 | if (page_size !== undefined) body.page_size = page_size |
25 |
|
26 | const response = await fetch("https://api.notion.com/v1/search", { |
27 | method: "POST", |
28 | headers: { |
29 | Authorization: `Bearer ${auth.token}`, |
30 | "Content-Type": "application/json", |
31 | Accept: "application/json", |
32 | "Notion-Version": "2026-03-11", |
33 | }, |
34 | body: JSON.stringify(body), |
35 | }) |
36 |
|
37 | if (!response.ok) { |
38 | throw new Error(`${response.status} ${await response.text()}`) |
39 | } |
40 |
|
41 | return await response.json() |
42 | } |
43 |
|