(url: string, init?: RequestInit)
| 31 | } |
| 32 | |
| 33 | async function request<T>(url: string, init?: RequestInit): Promise<T> { |
| 34 | const headers = new Headers(init?.headers); |
| 35 | if (init?.body != null && !headers.has("Content-Type")) { |
| 36 | headers.set("Content-Type", "application/json"); |
| 37 | } |
| 38 | const response = await fetch(url, { |
| 39 | ...init, |
| 40 | headers |
| 41 | }); |
| 42 | const text = await response.text(); |
| 43 | const data = safeParseJson(text); |
| 44 | if (!response.ok) { |
| 45 | const message = data?.error?.message ?? `请求失败:${response.status}`; |
| 46 | throw new Error(message); |
| 47 | } |
| 48 | return data as T; |
| 49 | } |
| 50 | |
| 51 | export const api = { |
| 52 | async listProjects(includeArchived = false) { |
no test coverage detected