deno.land / x / gesso@v0.1.2 / src / BaseApi.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { 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(); } }}
gesso

Version Info

Tagged at
a year ago