1 | import { Client } from "@notionhq/client"; |
2 |
|
3 | |
4 | * @param start_cursor If supplied, only results starting after the cursor will be returned. |
5 | * |
6 | * @param page_size The number of items in the response. Maximum is `100`. |
7 | * |
8 | * Learn more at |
9 | * https://developers.notion.com/reference/get-users |
10 | */ |
11 | type Notion = { |
12 | token: string; |
13 | }; |
14 | export async function main( |
15 | auth: Notion, |
16 | start_cursor?: string | undefined, |
17 | page_size?: number | undefined, |
18 | ) { |
19 | const client = new Client({ auth: auth.token }); |
20 | const args = removeObjectEmptyFields({ |
21 | page_size, |
22 | start_cursor, |
23 | }); |
24 | return await client.users.list(args); |
25 | } |
26 |
|
27 | function removeObjectEmptyFields( |
28 | object?: Record<string, any>, |
29 | removeEmptyArraysAndObjects = true, |
30 | createNewObject = true, |
31 | ) { |
32 | if (!object || typeof object !== "object") return {} |
33 | const obj = createNewObject ? { ...object } : object |
34 | const emptyValues = [undefined, null, ""] |
35 | for (const key in obj) { |
36 | const value = obj[key] |
37 | if (emptyValues.includes(value)) { |
38 | delete obj[key] |
39 | } else if (typeof value === "object") { |
40 | if (Object.keys(value).length) { |
41 | obj[key] = removeObjectEmptyFields(value, removeEmptyArraysAndObjects, false) |
42 | } |
43 | if (!Object.keys(value).length && removeEmptyArraysAndObjects) { |
44 | delete obj[key] |
45 | } |
46 | } |
47 | } |
48 | return obj |
49 | } |
50 |
|