File size: 1,802 Bytes
3a0ae3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af68ace
 
 
3a0ae3e
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import type { Molecule, Protein } from './visualization-types';

const API_BASE = '/api';

// Generic fetch helper with error handling
async function fetchApi<T>(url: string): Promise<T> {
  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<string> {
  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<Molecule[]> {
  return fetchApi<Molecule[]>(`${API_BASE}/molecules`);
}

export async function getMolecule(id: string): Promise<Molecule> {
  return fetchApi<Molecule>(`${API_BASE}/molecules/${id}`);
}

export async function getMoleculeSDF(id: string): Promise<string> {
  return fetchText(`${API_BASE}/molecules/${id}/sdf`);
}

// Protein API
export async function getProteins(): Promise<Protein[]> {
  return fetchApi<Protein[]>(`${API_BASE}/proteins`);
}

export async function getProtein(id: string): Promise<Protein> {
  return fetchApi<Protein>(`${API_BASE}/proteins/${id}`);
}

export async function getProteinPDB(id: string): Promise<string> {
  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`;
}