//native
type Discourse = {
apiKey: string;
defaultHost: string;
apiUsername: string;
};
/**
* Creates a new topic, a new post, or a private message
*
*/
export async function main(
auth: Discourse,
body: {
title?: string;
raw: string;
topic_id?: number;
category?: number;
target_recipients?: string;
target_usernames?: string;
archetype?: string;
created_at?: string;
reply_to_post_number?: number;
embed_url?: string;
external_id?: string;
},
) {
const url = new URL(`https://${auth.defaultHost}/posts.json`);
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"API-KEY": auth.apiKey,
"API-USERNAME": auth.apiUsername,
},
body: JSON.stringify(body),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 235 days ago