0.1.4•Updated 6 months ago
export const HOST = (Deno.env.get('DRACO_HOST') || Deno.env.get('DRACO_URL'))!;
const API_KEY = Deno.env.get('DRACO_API_KEY');
export const DracoRequest = async <T = any>(api_path: string, data: Record<string, any> = {}) => {
const full_route = `${HOST}/api/v1/${api_path}`.replace(/\/{2,}/g, '/');
try {
const response = await fetch(full_route, {
method: 'POST',
body: JSON.stringify(data),
headers: new Headers({
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json"
})
});
if(response.ok) {
try {
return (await response.json()) as T;
} catch(_) {
return {} as T;
}
}
throw new Error(await response.text());
} catch(e: any) {
console.error(`API error! [${e.message}]`);
return null;
}
};