elysiajs / eden

Fully type-safe Elysia client
MIT License
147 stars 37 forks source link

[Feature] Eden Treaty2 support FormData response #95

Closed lnfel closed 1 month ago

lnfel commented 3 months ago

Right now the new Eden Treaty implementation only unwraps application/json and application/octet-stream then falls back to parsing response.body as text. https://github.com/elysiajs/eden/blob/ead02aa927b806d6caf6f92a663f7ac2512845d9/src/treaty2/index.ts#L370-L405

response.clone() is used somewhere in the code and I think it would be helpful to pass the clone as response.

Previous implementation on Eden Treaty provided a getRaw flag to return the actual response without the body getting used. This way I was able to do await response.formData().

Sample elysia endpoint:

.post('/convert', async ({ body: { image }, query: { format } }) => {
  console.log({
    image,
    format
  })
  const formData = new FormData()
  await Promise.all(image.map(async (data) => {
    const image = sharp(await data.arrayBuffer())
    const file = await convert[format]({ sharp: image, filename: data.name, metadata: await image.metadata(), options: {} })
    formData.append('image', new Blob([await file.arrayBuffer()], { type: file.type }), file.name)
    return file
  }))
  return formData
})

Eden infers data to be a FormData: image But since treaty falls back to unwrapping response body as text. I get the form data boundaries as text: image

lnfel commented 1 month ago

Closing this with the addition of new Treaty Config (onResponse), I am now able to get the formdata blobs from the new Eden Treaty version:

https://elysiajs.com/eden/treaty/config.html#onresponse