0

Get a public list of users

by
Published Oct 17, 2025
Script discourse Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Discourse = {
3
  apiKey: string;
4
  defaultHost: string;
5
  apiUsername: string;
6
};
7
/**
8
 * Get a public list of users
9
 *
10
 */
11
export async function main(
12
  auth: Discourse,
13
  period:
14
    | "daily"
15
    | "weekly"
16
    | "monthly"
17
    | "quarterly"
18
    | "yearly"
19
    | "all"
20
    | undefined,
21
  order:
22
    | "likes_received"
23
    | "likes_given"
24
    | "topic_count"
25
    | "post_count"
26
    | "topics_entered"
27
    | "posts_read"
28
    | "days_visited"
29
    | undefined,
30
  asc: "true" | undefined,
31
  page: string | undefined,
32
) {
33
  const url = new URL(`https://${auth.defaultHost}/directory_items.json`);
34
  for (const [k, v] of [
35
    ["period", period],
36
    ["order", order],
37
    ["asc", asc],
38
    ["page", page],
39
  ]) {
40
    if (v !== undefined && v !== "" && k !== undefined) {
41
      url.searchParams.append(k, v);
42
    }
43
  }
44
  const response = await fetch(url, {
45
    method: "GET",
46
    headers: {
47
      "API-KEY": auth.apiKey,
48
      "API-USERNAME": auth.apiUsername,
49
    },
50
    body: undefined,
51
  });
52
  if (!response.ok) {
53
    const text = await response.text();
54
    throw new Error(`${response.status} ${text}`);
55
  }
56
  return await response.json();
57
}
58