acacode / swagger-typescript-api

Generate the API Client for Fetch or Axios from an OpenAPI Specification
MIT License
3.08k stars 338 forks source link

Multi file upload #776

Closed VadimDez closed 1 day ago

VadimDez commented 2 weeks ago

Having a use case where we need to upload multiple files at the same time, seems like the underlying logic is not considering arrays.

Consider following scenario when data is something like this:

const data = {
   files: [
     new File([], '1.txt'),
     new File([], '2.txt')
   ]
}

and having uploadPost method like this:

uploadPost: (data: BodyUploadPost, params: RequestParams = {}) =>
  this.request<MessageResponse, HTTPValidationError>({
        path: `/api/upload-files`,
        method: "POST",
        body: data,
        type: ContentType.FormData,
        format: "json",
        ...params,
      }),

The contentFormatters method is in question here as it is called within the request and transforms Object to FormData but not considering property to be an Array:

[ContentType.FormData]: (input: any) =>
      Object.keys(input || {}).reduce((formData, key) => {
        const property = input[key];
        formData.append(key, property instanceof Blob ? property : typeof property === "object" && property !== null ? JSON.stringify(property) : `${property}`);
        return formData;
      }, new FormData()),

Are we doing something wrong or multi-file upload is not supported?