| 19 | } |
| 20 | |
| 21 | async do<T>(method: string, path: string, body?: Body): Promise<ApiResult<T>> { |
| 22 | let base = this.base; |
| 23 | |
| 24 | let headers = {}; |
| 25 | if (this.token) { |
| 26 | headers = { |
| 27 | Authorization: this.token, |
| 28 | ...headers, |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | let sendingBody = body?.body |
| 33 | if (body && body.kind === "json") { |
| 34 | headers = { |
| 35 | "Content-Type": "application/json", |
| 36 | ...headers, |
| 37 | }; |
| 38 | sendingBody = JSON.stringify(body.body) |
| 39 | } |
| 40 | |
| 41 | let response = await this.fetcher.fetch(base + path, { |
| 42 | headers: headers, |
| 43 | method: method, |
| 44 | body: sendingBody, |
| 45 | }); |
| 46 | |
| 47 | console.log(`Response status for ${path}: ${response.status}`); |
| 48 | if (response.status === 204) { |
| 49 | return {} as ApiResult<T>; |
| 50 | } |
| 51 | |
| 52 | if (response.status !== 200) { |
| 53 | let decoded: ApiErrorResponse = await response.json() as ApiErrorResponse; |
| 54 | return new ApiError(response.status, decoded); |
| 55 | } |
| 56 | |
| 57 | return await response.json() as ApiResult<T>; |
| 58 | } |
| 59 | |
| 60 | async get<T>(path: string,): Promise<ApiResult<T>> { |
| 61 | return await this.do("GET", path); |