query.ts
This file
Path
packages/search/src/query.ts
29 lines · 783 bytes · TypeScript
Last changed by
History of this fileTake it
1import { API, legacyFetch } from "@burrow/kit";2import type { Transport } from "@burrow/kit";34/** A search result: what matched, and how well. */5export interface Hit {6 readonly id: string;7 readonly score: number;8}910// The query run last, for "search again".11let lastQuery = "";1213/** Searches the index; the best hit comes first. */14export const search = async (15 query: string,16 transport?: Transport17): Promise<readonly Hit[]> => {18 lastQuery = query;1920 const hits: readonly Hit[] = JSON.parse(21 await legacyFetch(`${API}/search?q=${encodeURIComponent(query)}`, transport)22 );2324 return hits.toSorted((one, other) => other.score - one.score);25};2627/** Runs the last query again. */28export const searchAgain = (transport?: Transport) =>29 search(lastQuery, transport);