File size: 957 Bytes
10ea63e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import type { GenerateRequest, HealthResponse } from '../types'

export async function generateAudio(
  req: GenerateRequest,
  signal: AbortSignal,
): Promise<Blob> {
  const res = await fetch('/api/generate', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(req),
    signal,
  })
  if (!res.ok) {
    const ct = res.headers.get('content-type') ?? ''
    if (ct.includes('json')) {
      const body = await res.json() as { detail: string | Array<{ msg: string }> }
      const detail = Array.isArray(body.detail)
        ? body.detail.map(e => e.msg).join('; ')
        : body.detail
      throw new Error(detail)
    }
    throw new Error(await res.text())
  }
  return res.blob()
}

export async function fetchHealth(): Promise<HealthResponse> {
  const res = await fetch('/health')
  if (!res.ok) throw new Error(`Health check returned ${res.status}`)
  return res.json() as Promise<HealthResponse>
}