nuxt-open-fetch icon indicating copy to clipboard operation
nuxt-open-fetch copied to clipboard

Support `multipart/formdata` (and other Content-Types)

Open Jak-Ch-ll opened this issue 8 months ago • 0 comments

The problem

One thing missing right now is support for non-json content types, especially multipart/formdata, which is usually used for file upload, as far as I know.

While it is possible to set a FormData object as the body, which will automatically set the right header, there is no type safety for this. Furthermore you even get a TypeScript error, because the nuxt-open-fetch client expects the bodies as normal object. (See #70)

Potential solution

The openapi-fetch library handles this via a bodySerializer option. This would be a huge improvement for nuxt-open-fetch.

There are two potential improvements that could be made on a TypeScript level for this option:

  1. Make the option mandatory, when a Content-Type other then application/json is required for the request
  2. Make the expected return type FormData or URLSearchParams when the Content-Type is set to multipart/formdata or application/x-www-form-urlencoded respectively.

Alternative

I was also thinking about something like a formData (and urlEncodedData) property, that could be used instead of the body and would automatically do the serializing under the hood when set. These could also be enforced via TypeScript. These options would have a nicer DX, but are less flexible and probably a bit more involved.

Additional information

  • [x] Would you be willing to help implement this feature?

Final checks

Jak-Ch-ll avatar Apr 08 '25 16:04 Jak-Ch-ll