|
1 | 1 | import * as v from "@badrap/valita"; |
| 2 | +import { fetch, Headers } from "undici"; |
2 | 3 |
|
3 | 4 | const json = JSON.stringify; |
4 | 5 |
|
@@ -69,14 +70,6 @@ function joinPath(path: string[]): string { |
69 | 70 | .join("/"); |
70 | 71 | } |
71 | 72 |
|
72 | | -async function dump(stream: ReadableStream | null) { |
73 | | - if (stream) { |
74 | | - for await (const _ of stream) { |
75 | | - // pass |
76 | | - } |
77 | | - } |
78 | | -} |
79 | | - |
80 | 73 | export interface ClientConfig { |
81 | 74 | url: string; |
82 | 75 | token: string; |
@@ -123,40 +116,47 @@ export class Client { |
123 | 116 | headers, |
124 | 117 | body, |
125 | 118 | }); |
126 | | - const contentType = response.headers.get("content-type"); |
127 | | - const isJSON = /^application\/json\s*(;|$)/.test(contentType ?? ""); |
128 | | - |
129 | | - if (response.status >= 400 && response.status < 600) { |
130 | | - if (isJSON) { |
131 | | - const rawBody: unknown = await response.json(); |
132 | | - |
133 | | - const body = APIErrorBody.try(rawBody, { mode: "strip" }); |
134 | | - if (body.ok) { |
135 | | - throw new APIError( |
136 | | - response.status, |
137 | | - response.statusText, |
138 | | - body.value.error.code, |
139 | | - body.value.error.reason, |
140 | | - ); |
| 119 | + try { |
| 120 | + const contentType = response.headers.get("content-type"); |
| 121 | + const isJSON = /^application\/json\s*(;|$)/.test(contentType ?? ""); |
| 122 | + |
| 123 | + if (response.status >= 400 && response.status < 600) { |
| 124 | + if (isJSON) { |
| 125 | + const rawBody: unknown = await response.json(); |
| 126 | + |
| 127 | + const body = APIErrorBody.try(rawBody, { mode: "strip" }); |
| 128 | + if (body.ok) { |
| 129 | + throw new APIError( |
| 130 | + response.status, |
| 131 | + response.statusText, |
| 132 | + body.value.error.code, |
| 133 | + body.value.error.reason, |
| 134 | + ); |
| 135 | + } |
141 | 136 | } |
142 | | - } else { |
143 | | - await dump(response.body); |
| 137 | + throw new HTTPError(response.status, response.statusText); |
144 | 138 | } |
145 | | - throw new HTTPError(response.status, response.statusText); |
146 | | - } |
147 | 139 |
|
148 | | - let json: unknown = undefined; |
149 | | - if (isJSON) { |
150 | | - json = await response.json(); |
151 | | - } else { |
152 | | - await dump(response.body); |
153 | | - } |
| 140 | + let json: unknown = undefined; |
| 141 | + if (isJSON) { |
| 142 | + json = await response.json(); |
| 143 | + } |
154 | 144 |
|
155 | | - const etag = response.headers.get("etag") ?? undefined; |
156 | | - if (!options.responseType) { |
157 | | - return { body: json as v.Infer<T>, etag }; |
| 145 | + const etag = response.headers.get("etag") ?? undefined; |
| 146 | + if (!options.responseType) { |
| 147 | + return { body: json as v.Infer<T>, etag }; |
| 148 | + } |
| 149 | + return { |
| 150 | + body: options.responseType.parse(json, { mode: "strip" }), |
| 151 | + etag, |
| 152 | + }; |
| 153 | + } finally { |
| 154 | + if (!response.bodyUsed && response.body) { |
| 155 | + for await (const _ of response.body) { |
| 156 | + // Dump the response data. |
| 157 | + } |
| 158 | + } |
158 | 159 | } |
159 | | - return { body: options.responseType.parse(json, { mode: "strip" }), etag }; |
160 | 160 | } |
161 | 161 |
|
162 | 162 | async request<T extends v.Type>( |
|
0 commit comments