1
//native
2
type Discourse = {
3
apiKey: string;
4
defaultHost: string;
5
apiUsername: string;
6
};
7
/**
8
* Download backup
9
*
10
*/
11
export async function main(
12
auth: Discourse,
13
filename: string,
14
token: string | undefined,
15
) {
16
const url = new URL(`https://${auth.defaultHost}/admin/backups/${filename}`);
17
for (const [k, v] of [["token", token]]) {
18
if (v !== undefined && v !== "" && k !== undefined) {
19
url.searchParams.append(k, v);
20
}
21
22
const response = await fetch(url, {
23
method: "GET",
24
headers: {
25
"API-KEY": auth.apiKey,
26
"API-USERNAME": auth.apiUsername,
27
},
28
body: undefined,
29
});
30
if (!response.ok) {
31
const text = await response.text();
32
throw new Error(`${response.status} ${text}`);
33
34
return await response.text();
35
36