import type { APIResponse, Vod } from "../types.ts"; import { API_URL, AuthError } from "./client.ts"; const getVods = async (): Promise => { const response = await fetch(API_URL + '/api/v1/vods', { credentials: 'include' }); if (!response.ok) { const errorResult: APIResponse = await response.json(); throw new Error(`Failed to fetch vods: ${errorResult.message}`); } const result: APIResponse = await response.json(); return result.data; }; const getVodById = async (id: string): Promise => { const response = await fetch(API_URL + `/api/v1/vods/${id}`, { credentials: 'include' }); if (!response.ok) { if (response.status === 401 || response.status === 403) throw new AuthError(); throw new Error(`Failed to fetch vod: ${response.status}`); } const result: APIResponse = await response.json(); return result.data; }; const getVodBlob = async (id: string): Promise => { const response = await fetch(API_URL + `/api/v1/vods/${id}/media`, { credentials: 'include' }); if (!response.ok) { if (response.status === 401 || response.status === 403) throw new AuthError(); throw new Error(`Failed to fetch vod ${id}: ${response.status}`); } return response.blob(); }; const patchVod = async (id: number, data: { title?: string; description?: string }): Promise => { const response = await fetch(API_URL + `/api/v1/vods/${id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify(data), }); if (!response.ok) throw new Error(`Failed to update vod: ${response.status}`); const result: APIResponse = await response.json(); if (result.status === 'error') throw new Error(`Failed to update vod: ${result.message}`); return result.data; }; const deleteVod = async (id: number): Promise => { const response = await fetch(API_URL + `/api/v1/vods/${id}`, { method: 'DELETE', credentials: 'include', }); if (!response.ok) throw new Error(`Failed to delete vod: ${response.status}`); const result: APIResponse = await response.json(); if (result.status === 'error') throw new Error(`Failed to delete vod: ${result.message}`); }; export { getVods, getVodById, getVodBlob, patchVod, deleteVod };