From 6d70105f873c8d4229a1d4f988503f0c6bdfc587 Mon Sep 17 00:00:00 2001 From: thierryverse Date: Mon, 17 Nov 2025 07:52:02 +0200 Subject: [PATCH] fix build error --- surfsense_web/lib/apis/podcasts.api.ts | 74 ++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 surfsense_web/lib/apis/podcasts.api.ts diff --git a/surfsense_web/lib/apis/podcasts.api.ts b/surfsense_web/lib/apis/podcasts.api.ts new file mode 100644 index 000000000..beaa475ca --- /dev/null +++ b/surfsense_web/lib/apis/podcasts.api.ts @@ -0,0 +1,74 @@ +import type { PodcastItem } from "@/app/dashboard/[search_space_id]/podcasts/podcasts-client"; +import type { GeneratePodcastRequest } from "@/components/chat/ChatPanel/ChatPanelContainer"; + +export const getPodcastByChatId = async (chatId: string, authToken: string) => { + const response = await fetch( + `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/podcasts/by-chat/${Number(chatId)}`, + { + headers: { + Authorization: `Bearer ${authToken}`, + }, + method: "GET", + } + ); + + if (!response.ok) { + const errorData = await response.json().catch(() => ({})); + throw new Error(errorData.detail || "Failed to fetch podcast"); + } + + return (await response.json()) as PodcastItem | null; +}; + +export const generatePodcast = async (request: GeneratePodcastRequest, authToken: string) => { + const response = await fetch( + `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/podcasts/generate/`, + { + method: "POST", + headers: { + Authorization: `Bearer ${authToken}`, + "Content-Type": "application/json", + }, + body: JSON.stringify(request), + } + ); + + if (!response.ok) { + const errorData = await response.json().catch(() => ({})); + throw new Error(errorData.detail || "Failed to generate podcast"); + } + + return await response.json(); +}; + +export const loadPodcast = async (podcast: PodcastItem, authToken: string) => { + const controller = new AbortController(); + const timeoutId = setTimeout(() => controller.abort(), 30000); + + try { + const response = await fetch( + `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/podcasts/${podcast.id}/stream`, + { + headers: { + Authorization: `Bearer ${authToken}`, + }, + signal: controller.signal, + } + ); + + if (!response.ok) { + throw new Error(`Failed to fetch audio stream: ${response.statusText}`); + } + + const blob = await response.blob(); + const objectUrl = URL.createObjectURL(blob); + return objectUrl; + } catch (error) { + if (error instanceof DOMException && error.name === "AbortError") { + throw new Error("Request timed out. Please try again."); + } + throw error; + } finally { + clearTimeout(timeoutId); + } +};