MCPcopy Create free account
hub / github.com/Effect-TS/effect / sendBody

Function sendBody

packages/platform/browser/src/BrowserHttpClient.ts:171–207  ·  view source on GitHub ↗
(
  xhr: globalThis.XMLHttpRequest,
  request: HttpClientRequest.HttpClientRequest
)

Source from the content-addressed store, hash-verified

169)
170
171const 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
209const encoder = new TextEncoder()
210

Callers 1

Calls 4

sendMethod · 0.80
setMethod · 0.65
syncMethod · 0.45
failMethod · 0.45

Tested by

no test coverage detected