36 lines
963 B
TypeScript
36 lines
963 B
TypeScript
export type SearchItemType = "SHOW" | "PERSON" | "SEASON" | string;
|
|
|
|
export type SearchResultItem = {
|
|
type: SearchItemType;
|
|
data: any;
|
|
};
|
|
|
|
const DISCOVER_BASE = "https://fltr-app.de/api/discover/search";
|
|
|
|
export async function discoverSearch(
|
|
tags: string[],
|
|
signal?: AbortSignal,
|
|
): Promise<SearchResultItem[]> {
|
|
const filteredTags = tags.map((t) => t.trim()).filter(Boolean);
|
|
if (!filteredTags.length) return [];
|
|
|
|
const params = filteredTags
|
|
.map((tag) => `tags=${encodeURIComponent(tag)}`)
|
|
.join("&");
|
|
const url = `${DISCOVER_BASE}?${params}`;
|
|
|
|
const apiKey = process.env.EXPO_PUBLIC_API_KEY;
|
|
const res = await fetch(url, {
|
|
signal,
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"X-API-Key": apiKey ?? "",
|
|
},
|
|
});
|
|
if (!res.ok) throw new Error("Discover search failed " + res.status);
|
|
|
|
const data: unknown = await res.json();
|
|
if (!Array.isArray(data)) return [];
|
|
return data as SearchResultItem[];
|
|
}
|