Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression in treaty2: cannot send files #86

Closed
etcd opened this issue Apr 22, 2024 · 3 comments
Closed

Regression in treaty2: cannot send files #86

etcd opened this issue Apr 22, 2024 · 3 comments

Comments

@etcd
Copy link

etcd commented Apr 22, 2024

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:

eden/src/treaty2/index.ts

Lines 235 to 240 in dab2e53

if (typeof fetchInit.body === 'object') {
;(fetchInit.headers as Record<string, string>)[
'content-type'
] = 'application/json'
fetchInit.body = JSON.stringify(fetchInit.body)

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

@etcd
Copy link
Author

etcd commented Apr 22, 2024

I just noticed that there's a PR for this fix already: #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
Copy link

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

@fucksophie
Copy link

Fixed by #90 , can be closed.

@etcd etcd closed this as completed Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants