~bigbes/lethe

ref: 321125b29b8dcde4046cf019b588f2bfe4e96d2a lethe/web/src/api/client.ts -rw-r--r-- 1.2 KiB
321125b2 — Eugene Blikh stats: add /api/v1/stats aggregate endpoint a month ago
                                                                                
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
export class AuthError extends Error {
  override name = 'AuthError'
  constructor(message: string) {
    super(message)
  }
}

export class APIError extends Error {
  override name = 'APIError'
  status: number
  code: string
  constructor(message: string, status: number, code: string) {
    super(message)
    this.status = status
    this.code = code
  }
}

export async function apiFetch<T>(path: string, init?: RequestInit): Promise<T> {
  const resp = await fetch(path, {
    ...init,
    headers: {
      Accept: 'application/json',
      ...init?.headers,
    },
  })

  if (resp.status === 401) {
    throw new AuthError('not authenticated')
  }

  if (!resp.ok) {
    const ct = resp.headers.get('Content-Type') ?? ''
    if (ct.includes('application/problem+json')) {
      const body = await resp.json() as { detail?: string; title?: string; status?: number; code?: string }
      throw new APIError(body.detail ?? body.title ?? 'error', body.status ?? resp.status, body.code ?? '')
    }
    if (resp.status >= 500) {
      throw new APIError('server error', resp.status, '')
    }
    throw new APIError(`request failed: ${resp.status}`, resp.status, '')
  }

  return resp.json() as Promise<T>
}