1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { extractArgs } from "https://esm.town/v/pomdtr/extractArgs?v=30";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
export async function valForm(req: express.Request, resp: express.Response) {
try {
const [author, name] = req.path.slice(1).split(".");
const { code } = await fetchJSON(
`https://api.val.town/v1/alias/${author}/${name}`,
);
const args = await extractArgs(code);
if (req.method == "POST") {
const payload = {
args: args.map((arg) =>
arg.type == "number"
? parseInt(req.body[arg.name])
: req.body[arg.name]
),
};
const res = await fetchJSON(
`https://api.val.town/v1/run/${author}.${name}`,
{
method: "POST",
body: JSON.stringify(payload),
},
);
resp.json(res);
return;
}
const inputs = [];
for (const arg of args) {
switch (arg.type) {
case "boolean":
inputs.push({ name: arg.name, title: arg.name, type: "checkbox" });
break;
case "number":
inputs.push({ name: arg.name, title: arg.name, type: "textfield" });
break;
case "string":
inputs.push({ name: arg.name, title: arg.name, type: "textfield" });
break;
default:
resp.status(500);
resp.send(`Unsupported arg type: ${arg.type}`);
return;
}
}
return resp.json({
type: "form",
title: `${author}.${name}`,
submitAction: {
type: "fetch",
request: {
url: `https://pomdtr-valForm.express.val.run/${author}.${name}`,
headers: {
"Content-Type": "application/json",
},
method: "POST",
body: "{{inputs:json}}",
},
inputs,
},
});
}
catch (e) {
resp.status(500);
resp.send(`An error occured: ${e}`);
}
}
// Forked from @pomdtr.valForm
1
Next