1 | import { WebClient } from "@slack/web-api"; |
2 |
|
3 | |
4 | * @param cursor Paginate the list of users by setting the cursor parameter |
5 | * to a `next_cursor` attribute returned by a previous request's |
6 | * `response_metadata`. Default value fetches the first "page" of the users. |
7 | * Used in conjunction with `limit`. |
8 | * |
9 | * @param limit The maximum number of users to return. Fewer than the |
10 | * requested number of users may be returned, even if the end of the result |
11 | * list hasn't been reached. |
12 | * Used in conjunction with `cursor`. |
13 | */ |
14 | type Slack = { |
15 | token: string; |
16 | }; |
17 | export async function main(auth: Slack, cursor?: string, limit?: number) { |
18 | const client = new WebClient(auth.token); |
19 | const params = removeObjectEmptyFields({ cursor, limit }); |
20 | return await client.users.list(params); |
21 | } |
22 |
|
23 | function removeObjectEmptyFields( |
24 | object?: Record<string, any>, |
25 | removeEmptyArraysAndObjects = true, |
26 | createNewObject = true, |
27 | ) { |
28 | if (!object || typeof object !== "object") return {} |
29 | const obj = createNewObject ? { ...object } : object |
30 | const emptyValues = [undefined, null, ""] |
31 | for (const key in obj) { |
32 | const value = obj[key] |
33 | if (emptyValues.includes(value)) { |
34 | delete obj[key] |
35 | } else if (typeof value === "object") { |
36 | if (Object.keys(value).length) { |
37 | obj[key] = removeObjectEmptyFields(value, removeEmptyArraysAndObjects, false) |
38 | } |
39 | if (!Object.keys(value).length && removeEmptyArraysAndObjects) { |
40 | delete obj[key] |
41 | } |
42 | } |
43 | } |
44 | return obj |
45 | } |
46 |
|