mapdash/frontend/src/api.ts

27 lines
905 B
TypeScript

// Typed wrappers around the mapdash HTTP API. All network access for the app
// goes through here, so response shapes stay in one typed place.
import type { Meta, Board, Booking, Filters } from './types';
function buildQuery(f: Filters): string {
const params = new URLSearchParams({
city: f.cities.join(','),
dimension: f.dimensions.join(','),
search: f.search,
});
return params.toString();
}
async function getJson<T>(url: string): Promise<T> {
const res = await fetch(url);
if (!res.ok) {
throw new Error(`Request failed: ${url} -> HTTP ${res.status}`);
}
return (await res.json()) as T;
}
export const api = {
meta: (): Promise<Meta> => getJson<Meta>('/api/meta'),
boards: (f: Filters): Promise<Board[]> => getJson<Board[]>('/api/boards?' + buildQuery(f)),
bookings: (f: Filters): Promise<Booking[]> => getJson<Booking[]>('/api/bookings?' + buildQuery(f)),
};