import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
export let callPrivateAPI = async ({ handle, val, method = "GET", secret }) => {
let endpoint;
if (handle && val) {
endpoint = `${handle}.${val}`;
}
else {
endpoint = "galligan.simpleTest";
}
const valTownUrl = "https://api.val.town/v1/run/" + endpoint;
const response = await fetchJSON(valTownUrl, {
method,
headers: {
Authorization: "Bearer " + secret,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
});
if (response.status !== 200) {
throw new Error(`Request failed with status code ${response.status}`);
}
else if (endpoint === "@galligan.simpleTest") {
return "Success calling @galligan.simpleTest. To test a specific endpoint, please set `handle` and `val` as arguments";
}
return response;
};