File size: 875 Bytes
c5ced83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { Catalog, SearchResponse } from "./types";

export const REPO = "https://github.com/openai/math";
export const blob = (path: string) => `${REPO}/blob/main/${path}`;
export const tree = (path: string) => `${REPO}/tree/main/${path}`;

export async function fetchCatalog(): Promise<Catalog> {
	const res = await fetch("/api/catalog");
	if (!res.ok) throw new Error(`catalog: ${res.status}`);
	return res.json();
}

const cache = new Map<string, SearchResponse>();

export async function search(q: string, signal?: AbortSignal): Promise<SearchResponse> {
	const key = q.trim().toLowerCase();
	const hit = cache.get(key);
	if (hit) return hit;
	const res = await fetch(`/api/search?q=${encodeURIComponent(q)}`, { signal });
	if (!res.ok) throw new Error(`search: ${res.status}`);
	const data: SearchResponse = await res.json();
	cache.set(key, data);
	return data;
}