search

tcmts.registry.http.search · #azwg23ky · term · runs anywhere

GET /api/search?q=… (tcm lib search asks it): every library whose newest version has definitions matching q, as the search page finds them, each with its author, version, description, how many use and starred it, the tcm lib add line, and what matched (at most 20 a library).

import { addLine, everyone, libraryKey, searchLibraries, starCounts } from "#codebase/tcmts/registry";

export async function search(request: Request) {
  const q = new URL(request.url).searchParams.get("q") ?? "";
  const who = await everyone();
  const stars = await starCounts();
  return {
    libraries: (await searchLibraries(q)).map(({ l, hits }) => {
      const w = who.get(l.did) ?? { did: l.did };
      return {
        did: l.did,
        ...(w.handle ? { handle: w.handle } : {}),
        package: l.package,
        version: l.latest.version,
        ...(l.latest.facts?.description ? { description: l.latest.facts.description } : {}),
        dependents: l.dependents,
        stars: stars.get(libraryKey(l)) ?? 0,
        add: addLine(w, l.package, l.latest.version),
        hits: hits
          .slice(0, 20)
          .map((h) => ({ name: h.name, signature: h.signature, detail: h.detail })),
        more: Math.max(0, hits.length - 20),
      };
    }),
  };
}

uses