( xhr: globalThis.XMLHttpRequest, request: HttpClientRequest.HttpClientRequest )
| 169 | ) |
| 170 | |
| 171 | const sendBody = ( |
| 172 | xhr: globalThis.XMLHttpRequest, |
| 173 | request: HttpClientRequest.HttpClientRequest |
| 174 | ): Effect.Effect<void, HttpClientError.HttpClientError> => { |
| 175 | const body = request.body |
| 176 | switch (body._tag) { |
| 177 | case "Empty": |
| 178 | return Effect.sync(() => xhr.send()) |
| 179 | case "Raw": |
| 180 | return Effect.sync(() => xhr.send(body.body as any)) |
| 181 | case "Uint8Array": |
| 182 | return Effect.sync(() => xhr.send(body.body as any)) |
| 183 | case "FormData": |
| 184 | return Effect.sync(() => xhr.send(body.formData)) |
| 185 | case "Stream": |
| 186 | return Effect.matchEffect( |
| 187 | Stream.runFold(body.stream, () => new Uint8Array(0), (acc, chunk) => { |
| 188 | const next = new Uint8Array(acc.length + chunk.length) |
| 189 | next.set(acc, 0) |
| 190 | next.set(chunk, acc.length) |
| 191 | return next |
| 192 | }), |
| 193 | { |
| 194 | onFailure: (cause) => |
| 195 | Effect.fail( |
| 196 | new HttpClientError.HttpClientError({ |
| 197 | reason: new HttpClientError.EncodeError({ |
| 198 | request, |
| 199 | cause |
| 200 | }) |
| 201 | }) |
| 202 | ), |
| 203 | onSuccess: (body) => Effect.sync(() => xhr.send(body)) |
| 204 | } |
| 205 | ) |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | const encoder = new TextEncoder() |
| 210 |
no test coverage detected