0

Get a list of user actions

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 list of user actions
9
 *
10
 */
11
export async function main(
12
  auth: Discourse,
13
  offset: string | undefined,
14
  username: string | undefined,
15
  filter: string | undefined,
16
) {
17
  const url = new URL(`https://${auth.defaultHost}/user_actions.json`);
18
  for (const [k, v] of [
19
    ["offset", offset],
20
    ["username", username],
21
    ["filter", filter],
22
  ]) {
23
    if (v !== undefined && v !== "" && k !== undefined) {
24
      url.searchParams.append(k, v);
25
    }
26
  }
27
  const response = await fetch(url, {
28
    method: "GET",
29
    headers: {
30
      "API-KEY": auth.apiKey,
31
      "API-USERNAME": auth.apiUsername,
32
    },
33
    body: undefined,
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41