0

List latest posts across topics

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