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);
}