diff options
Diffstat (limited to 'packages/tool-youtube-transcript/src/client.ts')
| -rw-r--r-- | packages/tool-youtube-transcript/src/client.ts | 92 |
1 files changed, 46 insertions, 46 deletions
diff --git a/packages/tool-youtube-transcript/src/client.ts b/packages/tool-youtube-transcript/src/client.ts index a088d7d..576eb3a 100644 --- a/packages/tool-youtube-transcript/src/client.ts +++ b/packages/tool-youtube-transcript/src/client.ts @@ -18,13 +18,13 @@ export const DEFAULT_BASE_URL = "http://100.102.55.49:41090"; export const DEFAULT_TIMEOUT_MS = 30_000; export interface TranscriptClient { - readonly getTranscript: (url: string, signal: AbortSignal) => Promise<TranscriptResponse>; + readonly getTranscript: (url: string, signal: AbortSignal) => Promise<TranscriptResponse>; } export interface TranscriptClientDeps { - readonly baseUrl: string; - readonly fetchFn: FetchLike; - readonly timeoutMs?: number; + readonly baseUrl: string; + readonly fetchFn: FetchLike; + readonly timeoutMs?: number; } /** @@ -34,47 +34,47 @@ export interface TranscriptClientDeps { * is combined with the caller's cancellation signal via `AbortSignal.any`. */ export function createTranscriptClient(deps: TranscriptClientDeps): TranscriptClient { - const baseUrl = deps.baseUrl; - const fetchFn = deps.fetchFn; - const timeoutMs = deps.timeoutMs ?? DEFAULT_TIMEOUT_MS; + const baseUrl = deps.baseUrl; + const fetchFn = deps.fetchFn; + const timeoutMs = deps.timeoutMs ?? DEFAULT_TIMEOUT_MS; - return { - async getTranscript(url: string, signal: AbortSignal): Promise<TranscriptResponse> { - const endpoint = `${baseUrl}/api/transcript?url=${encodeURIComponent(url)}`; - const controller = new AbortController(); - const timeout = setTimeout(() => controller.abort(), timeoutMs); - const combined = AbortSignal.any([signal, controller.signal]); - try { - let response: Response; - try { - response = await fetchFn(endpoint, { - method: "GET", - headers: { Accept: "application/json" }, - signal: combined, - }); - } catch (err) { - if (signal.aborted) { - throw new Error("Request aborted."); - } - if (controller.signal.aborted) { - throw new Error(`Transcriber request timed out after ${timeoutMs / 1000} seconds.`); - } - throw err; - } - if (!response.ok) { - const text = await response.text().catch(() => ""); - throw new Error( - `HTTP ${response.status} ${response.statusText}${text ? `: ${text}` : ""}`, - ); - } - try { - return (await response.json()) as TranscriptResponse; - } catch { - throw new Error("Failed to parse transcriber response as JSON"); - } - } finally { - clearTimeout(timeout); - } - }, - }; + return { + async getTranscript(url: string, signal: AbortSignal): Promise<TranscriptResponse> { + const endpoint = `${baseUrl}/api/transcript?url=${encodeURIComponent(url)}`; + const controller = new AbortController(); + const timeout = setTimeout(() => controller.abort(), timeoutMs); + const combined = AbortSignal.any([signal, controller.signal]); + try { + let response: Response; + try { + response = await fetchFn(endpoint, { + method: "GET", + headers: { Accept: "application/json" }, + signal: combined, + }); + } catch (err) { + if (signal.aborted) { + throw new Error("Request aborted."); + } + if (controller.signal.aborted) { + throw new Error(`Transcriber request timed out after ${timeoutMs / 1000} seconds.`); + } + throw err; + } + if (!response.ok) { + const text = await response.text().catch(() => ""); + throw new Error( + `HTTP ${response.status} ${response.statusText}${text ? `: ${text}` : ""}`, + ); + } + try { + return (await response.json()) as TranscriptResponse; + } catch { + throw new Error("Failed to parse transcriber response as JSON"); + } + } finally { + clearTimeout(timeout); + } + }, + }; } |
