(method: "GET" | "POST" | "PUT" | "DELETE", path: string, init?: RequestInit)
| 53 | } |
| 54 | |
| 55 | private async request<T>(method: "GET" | "POST" | "PUT" | "DELETE", path: string, init?: RequestInit): Promise<T> { |
| 56 | const resp = await fetch(`${this.oasstApiUrl}${path}`, { |
| 57 | method, |
| 58 | ...init, |
| 59 | headers: { |
| 60 | ...init?.headers, |
| 61 | ...this.userHeaders, |
| 62 | "X-API-Key": this.oasstApiKey, |
| 63 | "Content-Type": "application/json", |
| 64 | }, |
| 65 | }); |
| 66 | |
| 67 | if (resp.status === 204) { |
| 68 | return null as T; |
| 69 | } |
| 70 | |
| 71 | if (resp.status >= 300) { |
| 72 | const errorText = await resp.text(); |
| 73 | let error; |
| 74 | try { |
| 75 | error = JSON.parse(errorText); |
| 76 | } catch (e) { |
| 77 | throw new OasstError({ |
| 78 | message: errorText, |
| 79 | errorCode: 0, |
| 80 | httpStatusCode: resp.status, |
| 81 | path, |
| 82 | method, |
| 83 | }); |
| 84 | } |
| 85 | throw new OasstError({ |
| 86 | message: error.message ?? error, |
| 87 | errorCode: error.error_code, |
| 88 | httpStatusCode: resp.status, |
| 89 | path, |
| 90 | method, |
| 91 | }); |
| 92 | } |
| 93 | |
| 94 | return resp.json(); |
| 95 | } |
| 96 | |
| 97 | private async post<T>(path: string, body: unknown) { |
| 98 | return this.request<T>("POST", path, { |
no test coverage detected