2026-09-03 23:43:37 +00:00
|
|
|
export class ApiError extends Error {
|
|
|
|
|
status:number;
|
|
|
|
|
constructor(status:number,message:string){super(message);this.name="ApiError";this.status=status}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-03 16:16:34 +00:00
|
|
|
export async function api<T>(path:string, options:RequestInit={}):Promise<T>{
|
|
|
|
|
const response=await fetch(path,{...options,headers:{"content-type":"application/json",...options.headers}});
|
|
|
|
|
const text=await response.text(); let body:unknown=null;
|
|
|
|
|
try{body=text?JSON.parse(text):null}catch{body={message:text}}
|
2026-09-03 23:43:37 +00:00
|
|
|
if(!response.ok){const message=typeof body==="object"&&body&&"message" in body?String((body as {message:unknown}).message):`${response.status} ${response.statusText}`;throw new ApiError(response.status,message)}
|
2026-09-03 16:16:34 +00:00
|
|
|
return body as T;
|
|
|
|
|
}
|