Edits history of script submission #22525 for ' List Users (slack)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import { WebClient } from "@slack/web-api";
    
    /**
     * @param cursor Paginate the list of users by setting the cursor parameter
     * to a `next_cursor` attribute returned by a previous request's
     * `response_metadata`. Default value fetches the first "page" of the users.
     * Used in conjunction with `limit`.
     *
     * @param limit The maximum number of users to return. Fewer than the
     * requested number of users may be returned, even if the end of the result
     * list hasn't been reached.
     * Used in conjunction with `cursor`.
     */
    type Slack = {
      token: string;
    };
    export async function main(auth: Slack, cursor?: string, limit?: number) {
      const client = new WebClient(auth.token);
      const params = removeObjectEmptyFields({ cursor, limit });
      return await client.users.list(params);
    }
    
    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 7 days ago