serveFunction = (fn, { name = "default" } = {}) => {
const server = endpoint(name || "default", async (req, res) => {
const args = req.query.args ? JSON.parse(req.query.args) : [];
const response = await fn(...args);
if (!response) res.status(204).end();
else res.json(response);
});
const client = htl.html`<span>${server}`;
client.value = async (...args) => {
const response = await fetch(
server.href + "?args=" + encodeURIComponent(JSON.stringify(args))
);
if (response.status === 200) {
return response.json();
} else if (response.status === 204) {
return undefined;
} else {
throw new Error(`Status: ${response.status}, ${await response.text()}`);
}
};
return client;
}