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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { fetch } from "https://esm.town/v/std/fetch";
export function sunbeamValTownFn(valtownToken: string) {
return async (req: Request) => {
const { Hono } = await import("npm:hono");
const app = new Hono();
// extension manifest
app.get("/", async (c) => {
const resp = await fetch("https://api.val.town/v1/me", {
headers: {
Authorization: `Bearer ${valtownToken}`,
},
});
const { id: userID } = await resp.json();
return c.json({
title: "Val Town",
commands: [
{
name: "search-vals",
title: "Seach Vals",
mode: "view",
params: [
{
name: "user",
type: "string",
description: "User ID",
default: userID,
},
],
},
{
name: "view-val",
title: "view-val",
mode: "view",
params: [
{
name: "val",
type: "string",
description: "Val ID",
required: true,
},
],
},
],
});
});
// search-val command handler
app.post("/search-vals", async (c) => {
const { params, query } = await c.req.json();
const { user } = params;
const endpoint = query
? `https://api.val.town/v1/search/vals?query=${
encodeURIComponent(query)
}`
: `https://api.val.town/v1/users/${user}/vals`;
const resp = await fetch(endpoint, {
headers: {
Authorization: `Bearer ${valtownToken}`,
},
});
const { data: vals } = await resp.json();
return c.json({
type: "list",
dynamic: true,
items: vals.map((val) => {
const fullName = `${val.author.username}.${val.name}`;
return {
title: fullName,
accessories: [val.privacy],
actions: [
{
title: "View Val",
onAction: {
type: "run",
command: "view-val",
params: {
val: val.id,
},
},
},
{
title: "Open Val",
key: "o",
onAction: {
type: "open",
exit: true,
target: `https://val.town/v/${fullName}`,
},
},
],
};
}),
});
});
// view-val command handler
app.post("/view-val", async (c) => {
const { params } = await c.req.json();
const resp = await fetch(`https://api.val.town/v1/vals/${params.val}`, {
headers: {
Authorization: `Bearer ${valtownToken}`,