0

Get the top topics filtered by period

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