deno.land / x / gesso@v0.1.2 / src / BaseApi.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import { Configuration } from "./Configuration.ts";
// type Resolvable = | { [k: string]: Resolvable }// | string | null// | number | Resolvable[];
export class BaseApi { public configuration: Configuration;
constructor(config: Configuration) { this.configuration = config; }
public async get(route: URL, body?: string) { return await fetch(route, this.makeRequest("GET", body)); }
public async post(route: URL, body?: string) { return await fetch(route, this.makeRequest("POST", body)); }
public async patch(route: URL, body?: string) { return await fetch(route, this.makeRequest("PATCH", body)); }
public async put(route: URL, body?: string) { return await fetch(route, this.makeRequest("PUT", body)); }
public async delete(route: URL, body?: string) { return await fetch(route, this.makeRequest("DELETE", body)); }
private makeRequest(method: string, body?: string): RequestInit { return { method, headers: new Headers({ Accept: "application/json", "Content-Type": "application/json", Authorization: this.resolveAuth(), }), body: body ? JSON.stringify(body) : null, }; }
private resolveAuth() { if (this.configuration.apiKey) { return "Bearer " + this.configuration.apiKey; } else if (this.configuration.tokens) { return "Bearer " + this.configuration.tokens.accessToken; } else { // Make custom auth error throw new Error(); } }}
Version Info