elysiajs / eden

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

Upload multiple Files doesn't work from Bun client #65

Open bogeychan opened 8 months ago

bogeychan commented 8 months ago

reported on discord


Code

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

export const app = new Elysia()
    .post(
        '/uploadMultipleFiles',
        async ({ body }) => {
            for (const file of body.files) {
                console.log(`${file.name} uploaded`)
            }
            return 'File uploaded'
        },
        {
            type: 'formdata',
            body: t.Object({
                files: t.Files({
                    type: 'text/plain'
                })
            })
        }
    )
    .listen(0)

const client = edenTreaty<typeof app>(`${app.server?.url}`)

let fileContent = 'hello'
let file1 = new File([fileContent], 'input1.txt')
let file2 = new File([fileContent], 'input2.txt')

const { data: multipleFileUpload } = await client.uploadMultipleFiles.post({
    files: [file1, file2] // Type Error: Type 'File[]' is not assignable to type 'Files'.
})

app.stop()
console.log('Test for POST /uploadMultipleFiles path: ' + multipleFileUpload)

Expected Console output:

input1.txt uploaded
input2.txt uploaded
File uploaded

What i see instead:

{
  "type": "validation",
  "on": "body",
  "property": "/files",
  "message": "Expected kind 'Files'",
  "expected": {
    "files": "Files"
  },
  "found": {
    "files": "[object Blob],[object Blob]"
  },
  "errors": [
    {
      "type": 31,
      "schema": {
        "type": "array",
        "elysiaMeta": "Files",
        "default": "Files",
        "extension": "text/plain",
        "items": {
          "type": "string",
          "default": "Files",
          "format": "binary"
        }
      },
      "path": "/files",
      "value": "[object Blob],[object Blob]",
      "message": "Expected kind 'Files'"
    }
  ]
}

Solution (idk about treaty2):

https://github.com/elysiajs/eden/blob/59480e567ab76054e80c8bf6d191f071956b9c51/src/treaty/index.ts#L268-L270

doesn't check for Array.isArray(field)

https://github.com/elysiajs/eden/blob/59480e567ab76054e80c8bf6d191f071956b9c51/src/treaty/types.ts#L7

should include File[]