elysiajs / eden

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

Regression in treaty2: cannot send files #86

Closed etcd closed 5 months ago

etcd commented 7 months ago

The treaty2 client does not send files properly. The original treaty client works fine. Reproduction code:

import { Elysia, t } from "elysia";
import { edenTreaty, treaty } from "@elysiajs/eden";

export const app = new Elysia()
  .post("/upload", async ({ body: { file } }) => `Got file: ${file.name}`, {
    body: t.Object({ file: t.File() }),
  })
  .listen(0);

const treaty1Client = edenTreaty<typeof app>(String(app.server?.url));
const treaty2Client = treaty<typeof app>(String(app.server?.url));

const file = new File(["testfile"], "test body");

console.log((await treaty1Client.upload.post({ file })).data); // successful
console.log((await treaty2Client.upload.post({ file })).data); // unsuccessful

app.stop();

The regression is due to these lines: https://github.com/elysiajs/eden/blob/dab2e53ac44539611b4545d3ee5ff937fa9f6b63/src/treaty2/index.ts#L235-L240

This condition runs because body is an object, and the line fetchInit.body = JSON.stringify(fetchInit.body) wipes out the file being uploaded.

etcd commented 7 months ago

I just noticed that there's a PR for this fix already: https://github.com/elysiajs/eden/pull/73. This issue can be closed once that's merged.

For anyone running into this issue, one simple workaround is to just use the original treaty client (edenTreaty) as demonstrated above until this issue is fixed.

fucksophie commented 6 months ago

Can reproduce, even after #73 merged, running Eden 1.0.12

fucksophie commented 6 months ago

Fixed by #90 , can be closed.