import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=42";
const randomId = () => Math.random().toString(36).substring(2, 8);
const createResult = await fetchJSON(`https://api.val.town/v1/vals`, {
method: "POST",
bearer: Deno.env.get("valtown"),
body: JSON.stringify({
code: `console.log(42);`,
name: `api_test_${randomId()}`,
}),
});
console.log("Created with name:", createResult.name);
const newName = `api_test_updated_${randomId()}`;
const updateResult = await fetch(`https://api.val.town/v1/vals/${createResult.id}`, {
method: "PUT",
headers: {
Authorization: `Bearer ${Deno.env.get("valtown")}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
name: newName,
privacy: "unlisted",
}),
});
console.log("Updated val:", `https://val.town/v/${createResult.author.username.replace(/^@/, "")}/${newName}`);