(path: string, init?: Omit<RequestInit, 'body'> & { body?: unknown })
| 8 | } from './types'; |
| 9 | |
| 10 | export async function api<T>(path: string, init?: Omit<RequestInit, 'body'> & { body?: unknown }) { |
| 11 | const response = await fetch(path, { |
| 12 | ...init, |
| 13 | headers: { |
| 14 | 'content-type': 'application/json', |
| 15 | ...(init?.headers || {}), |
| 16 | }, |
| 17 | body: init?.body ? JSON.stringify(init.body) : undefined, |
| 18 | }); |
| 19 | |
| 20 | const payload = (await response.json().catch(() => ({}))) as T & { error?: string }; |
| 21 | if (!response.ok) { |
| 22 | throw new Error(payload.error || `请求失败:${response.status}`); |
| 23 | } |
| 24 | return payload; |
| 25 | } |
| 26 | |
| 27 | export function formatRelativeTime(value?: number | string | null) { |
| 28 | if (!value) return ''; |
no outgoing calls
no test coverage detected