| 310 | globalThis.fetch(...args)); |
| 311 | |
| 312 | async function call<T>(path: string, init: RequestInit, signal?: AbortSignal): Promise<T> { |
| 313 | let response: Response; |
| 314 | try { |
| 315 | response = await doFetch(`${base}${path}`, { ...init, signal }); |
| 316 | } catch (cause) { |
| 317 | if (signal?.aborted) throw cause; |
| 318 | throw new ApiFailure( |
| 319 | 0, |
| 320 | 'unreachable', |
| 321 | 'The codegraph ui server is not answering.', |
| 322 | 'It may have been stopped — restart it with `codegraph ui` and reload this page.' |
| 323 | ); |
| 324 | } |
| 325 | |
| 326 | const body = (await response.json().catch(() => null)) as unknown; |
| 327 | if (!response.ok) { |
| 328 | const failure = (body as ApiErrorBody | null) ?? {}; |
| 329 | throw new ApiFailure( |
| 330 | response.status, |
| 331 | failure.code ?? 'error', |
| 332 | failure.error ?? `The server answered ${response.status}.`, |
| 333 | failure.hint ?? null |
| 334 | ); |
| 335 | } |
| 336 | return body as T; |
| 337 | } |
| 338 | |
| 339 | function getJson<T>(path: string, signal?: AbortSignal): Promise<T> { |
| 340 | return call<T>(path, { headers: { accept: 'application/json' } }, signal); |