elysiajs / eden

Fully type-safe Elysia client
MIT License
174 stars 41 forks source link

Create a way to get direct access to the Response object without the stream being read #35

Open MatthewAry opened 11 months ago

MatthewAry commented 11 months ago

So taking a look at this https://github.com/elysiajs/eden/blob/688011d103ec14f4fdc8f85be2e716b267ab9c5e/src/treaty/index.ts#L314-L327 block of code, I'm seeing that eden treaty can return either json data, or data as text. This is great except when you want to use Eden Treaty to download binary data or something else that needs to be treated in a particular way. Take this https://elysiajs.com/plugins/stream.html#fetch-stream feature for example. Elysia lets you stream a fetch request, basically making an endpoint into a proxy. But I don't think this will work with Eden Treaty. And what if I wanted to stream the download of a file? If the response returned is application/octet-stream Eden Treaty resolves the request and converts it to text, making it so I can't get at the binary data. The request object that's returned by calling endpoints with Eden Treaty has response.body locked, making it so the stream can't be read. SO! I have a proposed solution,

Eden Treaty can have this block https://github.com/elysiajs/eden/blob/688011d103ec14f4fdc8f85be2e716b267ab9c5e/src/treaty/index.ts#L314-L327 extended to be helpful for more usecases like mine, but there should also be an option that can be passed into the HTTP method to get around a whole class of issues like mine:

EdenTreaty.<1>.<2>.<n>.<method>({
    ...body,
    $query?: {},
    $fetch?: RequestInit,
    getRaw?: boolean // 👈 This!
})

that will make it so no data property is returned and instead you'll have to handle the Response object yourself to get the data.

I have created a PR which provides a solution but without proper TypeSafety (if you set getRaw: true the signature will not change to the Response type). I welcome improvements to the PR. https://github.com/elysiajs/eden/pull/34

MatthewAry commented 8 months ago

This issue was fixed with the introduction of getRaw in eden treaty 1 but this has regressed in eden treaty 2.