0

Retrieve Block Children

by
Published Apr 25, 2023
Script notion Verified

The script

Submitted by hugo989 Bun
Verified 6 days ago
1
import { Client } from "@notionhq/client";
2

3
/**
4
 * Learn more at
5
 * https://developers.notion.com/reference/get-block-children
6
 */
7
type Notion = {
8
  token: string;
9
};
10
export async function main(
11
  auth: Notion,
12
  block_id: string,
13
  start_cursor?: string,
14
  page_size?: number,
15
) {
16
  const client = new Client({ auth: auth.token });
17
  const args = removeObjectEmptyFields({
18
    block_id,
19
    start_cursor,
20
    page_size,
21
  });
22
  return await client.blocks.children.list(args);
23
}
24

25
function removeObjectEmptyFields(
26
  object?: Record<string, any>,
27
  removeEmptyArraysAndObjects = true,
28
  createNewObject = true,
29
) {
30
  if (!object || typeof object !== "object") return {}
31
  const obj = createNewObject ? { ...object } : object
32
  const emptyValues = [undefined, null, ""]
33
  for (const key in obj) {
34
    const value = obj[key]
35
    if (emptyValues.includes(value)) {
36
      delete obj[key]
37
    } else if (typeof value === "object") {
38
      if (Object.keys(value).length) {
39
        obj[key] = removeObjectEmptyFields(value, removeEmptyArraysAndObjects, false)
40
      }
41
      if (!Object.keys(value).length && removeEmptyArraysAndObjects) {
42
        delete obj[key]
43
      }
44
    }
45
  }
46
  return obj
47
}
48

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
     * Learn more at
    6
     * https://developers.notion.com/reference/get-block-children
    7
     */
    8
    type Notion = {
    9
      token: string;
    10
    };
    11
    export async function main(
    12
      auth: Notion,
    13
      block_id: string,
    14
      start_cursor?: string,
    15
      page_size?: number,
    16
    ) {
    17
      const client = new Client({ auth: auth.token });
    18
      const args = removeObjectEmptyFields({
    19
        block_id,
    20
        start_cursor,
    21
        page_size,
    22
      });
    23
      return await client.blocks.children.list(args);
    24
    }
    25