import { CONFIG } from '../config'; export class Exception extends Error { constructor( public response: { message: string; error: string; statusCode: number; } ) { super(response.error); this.name = 'Exception'; } } export async function fetcher(key: string): Promise { const resp = await fetch(CONFIG.API_URL + key, { method: 'GET', headers: { 'Content-Type': 'application/json', Accept: 'application/json', }, }); const json = await resp.json(); if (!resp.ok) { throw new Exception(json); } return json; } export async function post( path: string, body: Record, headers?: Record ): Promise { const resp = await fetch(CONFIG.API_URL + path, { method: 'POST', headers: { 'Content-Type': 'application/json', Accept: 'application/json', ...headers, }, body: JSON.stringify(body), }); const json = await resp.json(); if (!resp.ok) { throw new Exception(json); } return json; }