(response: Response)
| 19 | } |
| 20 | |
| 21 | async function readJson<TResponse>(response: Response): Promise<TResponse> { |
| 22 | let payload: TResponse | { error?: string } | null = null |
| 23 | |
| 24 | try { |
| 25 | payload = (await response.json()) as TResponse | { error?: string } |
| 26 | } catch { |
| 27 | if (response.ok) { |
| 28 | throw new Error( |
| 29 | `Failed to parse JSON response with status ${response.status}.`, |
| 30 | ) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | if (!response.ok) { |
| 35 | const errorDetail = |
| 36 | payload && |
| 37 | typeof payload === 'object' && |
| 38 | 'error' in payload && |
| 39 | typeof payload.error === 'string' |
| 40 | ? `: ${payload.error}` |
| 41 | : '' |
| 42 | |
| 43 | throw new Error( |
| 44 | `Request failed with status ${response.status}${errorDetail}`, |
| 45 | ) |
| 46 | } |
| 47 | |
| 48 | return payload as TResponse |
| 49 | } |
| 50 | |
| 51 | export function createDataQueryOptions(apiBaseUrl = '') { |
| 52 | return { |
no outgoing calls
no test coverage detected