update
This commit is contained in:
29
apis/searchApi.ts
Normal file
29
apis/searchApi.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
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[];
|
||||
}
|
||||
Reference in New Issue
Block a user