1
//native
2
type Discourse = {
3
apiKey: string;
4
defaultHost: string;
5
apiUsername: string;
6
};
7
/**
8
* Retrieves a list of categories
9
*
10
*/
11
export async function main(
12
auth: Discourse,
13
include_subcategories: "true" | undefined,
14
) {
15
const url = new URL(`https://${auth.defaultHost}/categories.json`);
16
for (const [k, v] of [["include_subcategories", include_subcategories]]) {
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