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
// SPDX-License-Identifier: 0BSD
import ky from "npm:ky";
import * as v from "npm:valibot";
export { HTTPError } from "npm:ky";
const PositiveIntegerSchema = v.number([v.integer(), v.minValue(0)]);
const ValSchema = v.transform(
v.object({
initialData: v.object({
id: v.string([v.uuid()]),
user: v.object({
handle: v.string(),
}),
name: v.string(),
code: v.string(),
version: PositiveIntegerSchema,
runEndAt: v.nullish(v.coerce(v.date(), (d) => new Date(d))),
readme: v.nullish(v.string()),
likeCount: PositiveIntegerSchema,
referenceCount: PositiveIntegerSchema,
commentCount: PositiveIntegerSchema,
forkCount: PositiveIntegerSchema,
prCount: PositiveIntegerSchema,
forkedVal: v.nullish(v.unknown()),
}),
}),
(input) => input.initialData,
);
const PageSchema = v.object({
page: PositiveIntegerSchema,
hasNext: v.boolean(),
vals: v.array(ValSchema),
});
export async function fetchNewestVals({ page = 0 } = {}) {
const url = new URL("https://www.val.town/newest");
url.searchParams.set("_data", "routes/_app.newest");
if (page) url.searchParams.set("page", `${page}`);
const resp = await ky.get(url).json();
return v.parse(PageSchema, resp);
}