Edits history of script submission #22480 for ' List Users (notion)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import { Client } from "@notionhq/client";
    
    /**
     * @param start_cursor If supplied, only results starting after the cursor will be returned.
     *
     * @param page_size The number of items in the response. Maximum is `100`.
     *
     * Learn more at
     * https://developers.notion.com/reference/get-users
     */
    type Notion = {
      token: string;
    };
    export async function main(
      auth: Notion,
      start_cursor?: string | undefined,
      page_size?: number | undefined,
    ) {
      const client = new Client({ auth: auth.token });
      const args = removeObjectEmptyFields({
        page_size,
        start_cursor,
      });
      return await client.users.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