0.1.3Updated 10 days ago
// deno-lint-ignore-file no-explicit-any

import type { Draco } from "../classes/draco.ts";

export class RequestManager {
  constructor(private wrapper: typeof Draco) {}

  async Send<T = any>(path: string, data: Record<string, any> = {}) {
    if(!this.wrapper.HOST) throw new Error(`Draco host not set`);
    if(!this.wrapper.API_KEY) throw new Error(`Draco api key not set`);

    const full_route = new URL(`/api/v1/${path}`, this.wrapper.HOST).href;

    try {
      const response = await fetch(full_route, {
        method: 'POST',
        body: JSON.stringify(data),
        headers: new Headers({
          Authorization: `Bearer ${this.wrapper.API_KEY}`,
          "Content-Type": "application/json"
        })
      });

      if(response.ok) {
        try {
          return (await response.json() as T)
        } catch(_) {
          return {} as T;
        }
      }
    } catch(e: any) {
      console.error(`API error! [${e.message}]`);
      return null;
    }
  }
}