30 lines
855 B
TypeScript
30 lines
855 B
TypeScript
export type SearchItemType = "SHOW" | "PERSON" | "SEASON" | string;
|
|
|
|
export type SearchResultItem = {
|
|
type: SearchItemType;
|
|
data: any;
|
|
};
|
|
|
|
const DISCOVER_BASE = "http://45.157.177.99:8080/discover/search";
|
|
|
|
export async function getSearchResults(
|
|
tags: string[] | string,
|
|
limit = 10,
|
|
signal?: AbortSignal
|
|
): Promise<SearchResultItem[]> {
|
|
const tagList = Array.isArray(tags) ? tags : [tags];
|
|
const filteredTags = tagList.map((t) => t.trim()).filter(Boolean);
|
|
if (!filteredTags.length) return [];
|
|
|
|
const url = `${DISCOVER_BASE}?tags=${encodeURIComponent(
|
|
filteredTags.join(",")
|
|
)}&limit=${limit}`;
|
|
|
|
const res = await fetch(url, { signal });
|
|
if (!res.ok) throw new Error("AutoComplete failed " + res.status);
|
|
|
|
const data: unknown = await res.json();
|
|
if (!Array.isArray(data)) return [];
|
|
return data as SearchResultItem[];
|
|
}
|