media-rip/frontend/src/api/client.ts
xpltd efc2ead796 M001: media.rip() v1.0 — complete application
Full-featured self-hosted yt-dlp web frontend:
- Python 3.12+ / FastAPI backend with async SQLite, SSE transport, session isolation
- Vue 3 / TypeScript / Pinia frontend with real-time progress, theme picker
- 3 built-in themes (cyberpunk/dark/light) + drop-in custom theme system
- Admin auth (bcrypt), purge system, cookie upload, file serving
- Docker multi-stage build, GitHub Actions CI/CD
- 179 backend tests, 29 frontend tests (208 total)

Slices: S01 (Foundation), S02 (SSE+Sessions), S03 (Frontend),
        S04 (Admin+Auth), S05 (Themes), S06 (Docker+CI)
2026-03-18 20:00:17 -05:00

82 lines
2 KiB
TypeScript

/**
* Fetch-based API client for the media.rip() backend.
*
* All routes are relative — the Vite dev proxy handles /api → backend.
* In production, the SPA is served by the same FastAPI process, so
* relative paths work without configuration.
*/
import type { Job, JobCreate, FormatInfo, PublicConfig, HealthStatus } from './types'
class ApiError extends Error {
constructor(
public status: number,
public statusText: string,
public body: string,
) {
super(`API error ${status}: ${statusText}`)
this.name = 'ApiError'
}
}
async function request<T>(url: string, options?: RequestInit): Promise<T> {
const res = await fetch(url, {
...options,
headers: {
'Content-Type': 'application/json',
...options?.headers,
},
})
if (!res.ok) {
const body = await res.text()
throw new ApiError(res.status, res.statusText, body)
}
// 204 No Content
if (res.status === 204) {
return undefined as T
}
return res.json()
}
export const api = {
/** Fetch all downloads for the current session. */
async getDownloads(): Promise<Job[]> {
return request<Job[]>('/api/downloads')
},
/** Submit a new download. */
async createDownload(payload: JobCreate): Promise<Job> {
return request<Job>('/api/downloads', {
method: 'POST',
body: JSON.stringify(payload),
})
},
/** Cancel / remove a download. */
async deleteDownload(id: string): Promise<void> {
return request<void>(`/api/downloads/${id}`, {
method: 'DELETE',
})
},
/** Extract available formats for a URL. */
async getFormats(url: string): Promise<FormatInfo[]> {
const encoded = encodeURIComponent(url)
return request<FormatInfo[]>(`/api/formats?url=${encoded}`)
},
/** Load public (non-sensitive) configuration. */
async getPublicConfig(): Promise<PublicConfig> {
return request<PublicConfig>('/api/config/public')
},
/** Health check. */
async getHealth(): Promise<HealthStatus> {
return request<HealthStatus>('/api/health')
},
}
export { ApiError }