* One request, any method. The writing routes of the projects panel * (POST /api/index, POST /api/adr, DELETE /api/project) go through here * with a JSON body; the error carries the status so a caller can tell * "busy, try later" (423) from "refused" (403) from "not there" (404).
(method: 'GET' | 'POST' | 'DELETE', route: string, payload?: unknown)
| 121 | * "busy, try later" (423) from "refused" (403) from "not there" (404). |
| 122 | */ |
| 123 | private async request(method: 'GET' | 'POST' | 'DELETE', route: string, payload?: unknown): Promise<unknown> { |
| 124 | const url = `${this.options.base ?? ''}${route}`; |
| 125 | const doFetch = this.options.fetch ?? globalThis.fetch; |
| 126 | let response: Response; |
| 127 | try { |
| 128 | const init: RequestInit = { method, headers: { Accept: 'application/json' } }; |
| 129 | if (payload !== undefined) { |
| 130 | init.headers = { Accept: 'application/json', 'Content-Type': 'application/json' }; |
| 131 | init.body = JSON.stringify(payload); |
| 132 | } |
| 133 | if (this.options.signal !== undefined) init.signal = this.options.signal; |
| 134 | response = await doFetch(url, init); |
| 135 | } catch (error) { |
| 136 | const message = error instanceof Error ? error.message : String(error); |
| 137 | const failure = new AtlasApiError(route, 0, `${route} war nicht erreichbar: ${message}`); |
| 138 | // Ein abgebrochener Aufruf ist die Sache des Aufrufers und kein Fehler des Servers. |
| 139 | if (this.options.signal?.aborted) throw failure; |
| 140 | throw announce(failure); |
| 141 | } |
| 142 | const body = await response.text(); |
| 143 | if (!response.ok) { |
| 144 | throw announce( |
| 145 | new AtlasApiError(route, response.status, `${route} antwortete mit HTTP ${response.status}: ${body.slice(0, 200)}`), |
| 146 | body.slice(0, 400), |
| 147 | ); |
| 148 | } |
| 149 | try { |
| 150 | return JSON.parse(body) as unknown; |
| 151 | } catch { |
| 152 | throw announce( |
| 153 | new AtlasApiError(route, response.status, `${route} lieferte kein JSON: ${body.slice(0, 200)}`), |
| 154 | body.slice(0, 400), |
| 155 | ); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Eine Ebene des Datei-Baums. `path` leer heisst Projektwurzel. |
no test coverage detected