(response: Response)
| 1004 | } |
| 1005 | |
| 1006 | async function json(response: Response): Promise<unknown> { |
| 1007 | if (!isContentType(response, "application/json") && !response.headers.get("content-type")?.includes("+json")) { |
| 1008 | try { |
| 1009 | await response.body?.cancel() |
| 1010 | } catch {} |
| 1011 | throw new ClientError("UnsupportedContentType") |
| 1012 | } |
| 1013 | let text: string |
| 1014 | try { |
| 1015 | text = await response.text() |
| 1016 | } catch (cause) { |
| 1017 | throw new ClientError("Transport", { cause }) |
| 1018 | } |
| 1019 | if (text === "") throw new ClientError("MalformedResponse") |
| 1020 | try { |
| 1021 | return JSON.parse(text) |
| 1022 | } catch (cause) { |
| 1023 | throw new ClientError("MalformedResponse", { cause }) |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | function isContentType(response: Response, expected: string) { |
| 1028 | return response.headers.get("content-type")?.split(";", 1)[0]?.trim().toLowerCase() === expected |
no test coverage detected