(
url: string,
options?: RequestInit,
)
| 150 | * **Streaming endpoints should use {@link streamRequest} instead.** |
| 151 | */ |
| 152 | export async function apiRequest<T = any>( |
| 153 | url: string, |
| 154 | options?: RequestInit, |
| 155 | ): Promise<{ data: T }> { |
| 156 | const response = await fetchWithIdentity(url, options); |
| 157 | |
| 158 | let body: any; |
| 159 | try { |
| 160 | body = await response.json(); |
| 161 | } catch { |
| 162 | if (!response.ok) { |
| 163 | throw new ApiRequestError( |
| 164 | { code: 'HTTP_ERROR', message: `HTTP ${response.status}`, retry: false }, |
| 165 | response.status, |
| 166 | ); |
| 167 | } |
| 168 | throw new ApiRequestError( |
| 169 | { code: 'PARSE_ERROR', message: 'Invalid JSON response', retry: false }, |
| 170 | response.status, |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | // parseApiResponse handles body-level success/error envelopes. |
| 175 | return parseApiResponse<T>(body, response.status); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Validate a file/blob download response without assuming JSON success bodies. |
no test coverage detected