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
import { api } from "https://esm.town/v/pomdtr/api";
type ValRef = {
author: string;
name: string;
};
export function extractDependencies(code: string) {
const esmUrlRegex = /https:\/\/esm\.town\/v\/([a-zA-Z_$][0-9a-zA-Z_$]*)\/([a-zA-Z_$][0-9a-zA-Z_$]*)/g;
const matches = [...code.matchAll(esmUrlRegex)];
if (matches.length === 0) {
return [];
}
const deps: ValRef[] = matches.map((match) => ({
author: match[1],
name: match[2],
}));
return deps;
}
export async function searchReferences(val: ValRef) {
const esmUrl = `https://esm.town/v/${val.author}/${val.name}`;
const { data: vals } = await api(`/v1/search/vals?query=${encodeURIComponent(esmUrl)}`, {
authenticated: true,
paginate: true,
});
const refs: ValRef[] = vals.filter(ref => ref.author.username != val.author || ref.name != val.name).map(
ref => ({
author: ref.author.username,
name: ref.name,
}),
);
return refs;
}