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
import { object, optional, parse, record, string, url } from "https://deno.land/x/valibot@v0.22.0/mod.ts";
import ky, { HTTPError } from "npm:ky@1.1.3";
const REGISTRY_URL = "https://registry.npmjs.org/";
const VersionSchema = object({
version: string(),
dist: object({
tarball: string([url()]),
}),
deprecated: optional(string()),
});
const PkgInfoSchema = object({
name: string(),
"dist-tags": record(string()),
versions: record(VersionSchema),
});
export async function getPkgInfo(pkgName: string, version = "latest") {
const pkgURL = new URL(encodeURIComponent(pkgName).replace(/^%40/, "@"), REGISTRY_URL);
const headers = {
accept: "application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*",
};
const pkgInfo = parse(PkgInfoSchema, await ky.get(pkgURL, { headers }).json());
if (pkgInfo["dist-tags"][version]) {
version = pkgInfo["dist-tags"][version];
}
return pkgInfo.versions[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.