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
import {
array,
boolean,
object,
parse,
string,
toCustom,
transform,
url,
} from "https://deno.land/x/valibot@v0.22.0/mod.ts";
import ky, { HTTPError } from "npm:ky@1.1.3";
import { joinURL } from "npm:ufo@1.3.2";
const API_URL = "https://crates.io/api/v1/";
const VersionSchema = transform(
object({
num: string(),
dl_path: string([toCustom(input => (new URL(input, API_URL)).href)]),
yanked: boolean(),
}),
({ num: version, dl_path: downloadUrl, ...other }) => ({ version, downloadUrl, ...other }),
);
const CrateInfoSchema = object({
versions: array(VersionSchema),
});
export async function getCrateInfo(crateName: string, version?: string) {
const createURL = joinURL(API_URL, `/crates/${crateName}`);
const crateInfo = parse(CrateInfoSchema, await ky.get(createURL).json());
const versions = crateInfo.versions.filter(it => !it.yanked);
if (!version) return versions.at(0);
return versions.find(it => it.version === version);
}
export { HTTPError };
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.