ts-rest / ts-rest

RPC-like client, contract, and server implementation for a pure REST API
https://ts-rest.com
MIT License
2.11k stars 91 forks source link

Add support for multipart/form-data file array #571

Closed victormachado-ada-tech closed 2 months ago

victormachado-ada-tech commented 2 months ago

I want to be able to submit a POST request with a File array in the body. Currently the client only supports a single File.

I can do what I want by passing a FormData to the body, but it's not much convenient because I lose type safety.

Here is an example of code of what I would like to happen:

const c = initContract();
const contract = c.router({
  uploadWithArray: {
    method: "POST",
    path: "/upload",
    body: c.type<{ files: File[] }>(),
    responses: {
      200: c.type<{ totalFileSize: number }>(),
    },
  },
});

const client = initClient(contract, {
  baseUrl: "http://localhost:3333",
  baseHeaders: {},
});

async function makeUpload() {
  const response = await client.uploadWithArray({
    body: {
      files: [new File([""], "file1.txt"), new File([""], "file2.txt")],
    },
  });
  return response;
}