import { LogLevel, Method } from "./models"; export default class Utils { apiKey: string; host: string; token: string; debug: boolean; constructor(apiKey: string, host: string, debug: boolean = false) { this.apiKey = apiKey; this.host = host; this.debug = debug; } async request(method: Method, path: string, data?: unknown | null): Promise { const req: Response = await fetch(`${this.host}${path}`, { method: Method[method], mode: "cors", credentials: "same-origin", headers: { "Content-Type": "application/json", "api-key": this.apiKey, Authorization: `Bearer ${this.token}`, }, redirect: "follow", referrerPolicy: "no-referrer", body: data ? JSON.stringify(data) : null, }); return req.json(); } setToken(token: string) { this.token = token; } log(message: string, level = LogLevel.INFO) { if (this.debug) { switch (level) { case 0: console.log(`%cⓘ SDK INFO: ${message}`, "background: #183fdb; color: #ffffff"); break; case 1: console.log(`%c❗SDK WARNING: ${message}`, "background: #c46a10; color: #ffffff"); break; case 2: console.log(`%c⛔SDK ERROR: ${message}`, "background: #c41010; color: #ffffff"); break; default: console.log("default"); } } } }