File size: 1,599 Bytes
1e3df84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { ExampleImage, ProcessingResponse } from './types';

const BASE = '';  // same-origin; Vite proxy handles /api in dev

export async function fetchExamples(): Promise<ExampleImage[]> {
  const res = await fetch(`${BASE}/api/examples`);
  if (!res.ok) throw new Error('Failed to fetch examples');
  return res.json();
}

export async function processImage(file: File): Promise<ProcessingResponse> {
  const form = new FormData();
  form.append('file', file);
  const res = await fetch(`${BASE}/api/process`, { method: 'POST', body: form });
  if (!res.ok) {
    const err = await res.json().catch(() => ({ detail: res.statusText }));
    throw new Error(err.detail || 'Processing failed');
  }
  return res.json();
}

export async function processExample(name: string): Promise<ProcessingResponse> {
  const res = await fetch(`${BASE}/api/process?example=${encodeURIComponent(name)}`, {
    method: 'POST',
  });
  if (!res.ok) {
    const err = await res.json().catch(() => ({ detail: res.statusText }));
    throw new Error(err.detail || 'Processing failed');
  }
  return res.json();
}

export async function fetchCachedResult(name: string): Promise<ProcessingResponse> {
  const res = await fetch(`${BASE}/api/cached-result/${encodeURIComponent(name)}`);
  if (!res.ok) throw new Error('No cached result');
  return res.json();
}

export function exampleImageUrl(name: string): string {
  return `${BASE}/api/example-image/${encodeURIComponent(name)}`;
}

export function floorplanImageUrl(name: string): string {
  return `${BASE}/api/floorplan-image/${encodeURIComponent(name)}`;
}