1
//native
2
type Discourse = {
3
apiKey: string;
4
defaultHost: string;
5
apiUsername: string;
6
};
7
/**
8
* Update badge
9
*
10
*/
11
export async function main(
12
auth: Discourse,
13
id: string,
14
body: { name: string; badge_type_id: number },
15
) {
16
const url = new URL(`https://${auth.defaultHost}/admin/badges/${id}.json`);
17
18
const response = await fetch(url, {
19
method: "PUT",
20
headers: {
21
"Content-Type": "application/json",
22
"API-KEY": auth.apiKey,
23
"API-USERNAME": auth.apiUsername,
24
},
25
body: JSON.stringify(body),
26
});
27
if (!response.ok) {
28
const text = await response.text();
29
throw new Error(`${response.status} ${text}`);
30
}
31
return await response.json();
32
33