//native
type Discourse = {
apiKey: string;
defaultHost: string;
apiUsername: string;
};
/**
* Search for a term
*
*/
export async function main(
auth: Discourse,
q: string | undefined,
page: string | undefined,
) {
const url = new URL(`https://${auth.defaultHost}/search.json`);
for (const [k, v] of [
["q", q],
["page", page],
]) {
if (v !== undefined && v !== "" && k !== undefined) {
url.searchParams.append(k, v);
}
}
const response = await fetch(url, {
method: "GET",
headers: {
"API-KEY": auth.apiKey,
"API-USERNAME": auth.apiUsername,
},
body: undefined,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 235 days ago