acacode / swagger-typescript-api

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

When I lost many time on non working multi file upload, I was surprised why it's not supported. Moreover, fix is trivial, something like #965

Open ikzsl opened 4 weeks ago

ikzsl commented 4 weeks ago

When I lost many time on non working multi file upload, I was surprised why it's not supported. Moreover, fix is trivial, something like

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

Originally posted by @darky in https://github.com/acacode/swagger-typescript-api/discussions/815#discussioncomment-10907960

guatedude2 commented 3 weeks ago

I have a similar issue in React Native but I use uri with local system files so I added one more check:

[ContentType.FormData]: (input: any) =>
  Object.keys(input || {}).reduce((formData, key) => {
    const property = input[key]
    if (Array.isArray(property) && property.every((p) => 'uri' in p)) {
      property.forEach((p) => formData.append(key, p))
    } else if (Array.isArray(property) && property.every((p) => p instanceof Blob)) {
      property.forEach((p) => formData.append(key, p))
    } else {
      formData.append(
        key,
        property instanceof Blob ? property : typeof property === 'object' && property !== null ? JSON.stringify(property) : `${property}`,
      )
    }
    return formData
  }, new FormData()),