Edits history of script submission #22482 for ' Retrieve Block Children (notion)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import { Client } from "@notionhq/client";
    
    /**
     * Learn more at
     * https://developers.notion.com/reference/get-block-children
     */
    type Notion = {
      token: string;
    };
    export async function main(
      auth: Notion,
      block_id: string,
      start_cursor?: string,
      page_size?: number,
    ) {
      const client = new Client({ auth: auth.token });
      const args = removeObjectEmptyFields({
        block_id,
        start_cursor,
        page_size,
      });
      return await client.blocks.children.list(args);
    }
    
    function removeObjectEmptyFields(
      object?: Record<string, any>,
      removeEmptyArraysAndObjects = true,
      createNewObject = true,
    ) {
      if (!object || typeof object !== "object") return {}
      const obj = createNewObject ? { ...object } : object
      const emptyValues = [undefined, null, ""]
      for (const key in obj) {
        const value = obj[key]
        if (emptyValues.includes(value)) {
          delete obj[key]
        } else if (typeof value === "object") {
          if (Object.keys(value).length) {
            obj[key] = removeObjectEmptyFields(value, removeEmptyArraysAndObjects, false)
          }
          if (!Object.keys(value).length && removeEmptyArraysAndObjects) {
            delete obj[key]
          }
        }
      }
      return obj
    }
    

    Submitted by hugo989 6 days ago