import type { Molecule, Protein } from './visualization-types'; const API_BASE = '/api'; // Generic fetch helper with error handling async function fetchApi(url: string): Promise { const response = await fetch(url); if (!response.ok) { const error = await response.json().catch(() => ({ error: 'Unknown error' })); throw new Error(error.error || `HTTP ${response.status}`); } return response.json(); } async function fetchText(url: string): Promise { const response = await fetch(url); if (!response.ok) { const error = await response.json().catch(() => ({ error: 'Unknown error' })); throw new Error(error.error || `HTTP ${response.status}`); } return response.text(); } // Molecule API export async function getMolecules(): Promise { return fetchApi(`${API_BASE}/molecules`); } export async function getMolecule(id: string): Promise { return fetchApi(`${API_BASE}/molecules/${id}`); } export async function getMoleculeSDF(id: string): Promise { return fetchText(`${API_BASE}/molecules/${id}/sdf`); } // Protein API export async function getProteins(): Promise { return fetchApi(`${API_BASE}/proteins`); } export async function getProtein(id: string): Promise { return fetchApi(`${API_BASE}/proteins/${id}`); } export async function getProteinPDB(id: string): Promise { return fetchText(`${API_BASE}/proteins/${id}/pdb`); } // URL builders for direct links export function getMoleculeSdfUrl(id: string): string { return `${API_BASE}/molecules/${id}/sdf`; } export function getProteinPdbUrl(pdbId: string): string { // Fetch directly from RCSB PDB for now return `https://files.rcsb.org/download/${pdbId.toUpperCase()}.pdb`; }