List Users

Script notion Verified

by adam186 ยท 4/25/2023

The script

Submitted by hugo989 Bun
Verified 6 days ago
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

Other submissions
  • Submitted by adam186 Deno
    Created 398 days ago
    1
    import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
    2
    import { Client } from "npm:@notionhq/client";
    3
    
    
    4
    /**
    5
     * @param start_cursor If supplied, only results starting after the cursor will be returned.
    6
     *
    7
     * @param page_size The number of items in the response. Maximum is `100`.
    8
     *
    9
     * Learn more at
    10
     * https://developers.notion.com/reference/get-users
    11
     */
    12
    type Notion = {
    13
      token: string;
    14
    };
    15
    export async function main(
    16
      auth: Notion,
    17
      start_cursor?: string | undefined,
    18
      page_size?: number | undefined,
    19
    ) {
    20
      const client = new Client({ auth: auth.token });
    21
      const args = removeObjectEmptyFields({
    22
        page_size,
    23
        start_cursor,
    24
      });
    25
      return await client.users.list(args);
    26
    }
    27