:api added explore auto-complete feature
This commit is contained in:
24
apis/autoCompleteApi.ts
Normal file
24
apis/autoCompleteApi.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
export type AutoCompleteItemType = "SHOW" | "PERSON" | "SEASON" | string;
|
||||
|
||||
export type AutoCompleteItem = {
|
||||
type: AutoCompleteItemType;
|
||||
text: string;
|
||||
};
|
||||
|
||||
const DISCOVER_BASE = "http://45.157.177.99:8080/discover";
|
||||
|
||||
export async function getAutoComplete(
|
||||
query: string,
|
||||
limit = 10,
|
||||
signal?: AbortSignal
|
||||
): Promise<AutoCompleteItem[]> {
|
||||
if (!query.trim()) return [];
|
||||
const url = `${DISCOVER_BASE}/autoComplete?q=${encodeURIComponent(
|
||||
query
|
||||
)}&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 AutoCompleteItem[];
|
||||
}
|
||||
Reference in New Issue
Block a user