~bigbes/lethe

ref: 34d3e982765568f6d25f0e486603d3c2d73f8321 lethe/web/src/api/client.ts -rw-r--r-- 2.6 KiB
34d3e982 — Eugene Blikh docs(lethe-web-ui-login): plan + execute hands-off decisions 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { tokenStore } from '../lib/auth'

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
  }
}

/**
 * Build the Authorization header value from the in-memory token store.
 * Returns undefined when no token is present so the header is omitted (IV5).
 */
function authHeader(): { Authorization: string } | Record<string, never> {
  const token = tokenStore.get()
  return token !== null ? { Authorization: `Bearer ${token}` } : {}
}

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

/**
 * apiFetchVoid is a variant of apiFetch for endpoints that return no body
 * (e.g. DELETE → 204 No Content). It shares the same auth and error handling
 * as apiFetch but does not attempt to parse the response body.
 */
export async function apiFetchVoid(path: string, init?: RequestInit): Promise<void> {
  const resp = await fetch(path, {
    ...init,
    headers: {
      Accept: 'application/json',
      ...authHeader(),
      ...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, '')
  }
}