File size: 964 Bytes
729628a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { StatewideQuery, StatewideResponse } from '../types';

async function handle<T>(res: Response): Promise<T> {
  if (!res.ok) {
    const body = await res.text().catch(() => '');
    throw new Error(`API ${res.status}: ${body || res.statusText}`);
  }
  return res.json() as Promise<T>;
}

export async function fetchStatewide(query: StatewideQuery, signal?: AbortSignal): Promise<StatewideResponse> {
  const res = await fetch('/api/ssam/statewide', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(query),
    signal,
  });
  return handle<StatewideResponse>(res);
}

export async function annotateStreet(streetName: string, annotation: string): Promise<void> {
  const res = await fetch('/api/ssam/annotate', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ street_name: streetName, manual_annotation: annotation }),
  });
  await handle(res);
}