Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request] Support (nested) objects in formData #68

Closed
itsgoingd opened this issue Jan 24, 2020 · 4 comments
Closed

[Feature request] Support (nested) objects in formData #68

itsgoingd opened this issue Jan 24, 2020 · 4 comments

Comments

@itsgoingd
Copy link

The formData method can already handle arrays as seen here:

https://github.com/elbywan/wretch/blob/master/src/wretcher.ts#L312-L324

But many server-side languages like PHP or Rails also support objects and even nested data, eg. { 'foo[bar]': 123, 'foo[baz][bar]': 456 }.

It would be very helpful if wretch supported this, should be an easy change to the convertFormData method above.

I'm down to work on a PR if you agree on this feature.

@elbywan
Copy link
Owner

elbywan commented Jan 25, 2020

Hi @itsgoingd,

It would be very helpful if wretch supported this

Indeed, thanks for the suggestion!

should be an easy change to the convertFormData method above.

Actually there are some hidden subtlties, so I took the liberty of implementing the feature (which is now done and published in v1.7.0).

Here is the updated documentation.

I kept the current behaviour as the default because a common use of FormData is to upload files (most likely Blob instances in the browser, and ReadableStream or Buffer instances when using a node.js polyfill) which are also of type "object" and should not be treated the same way.

I hope this change will suit your needs!

@itsgoingd
Copy link
Author

Hey, thanks for including this!

Found a small issue - combination of null values and nesting, eg. formData({ foo: null}, true), leads to a crash where null is passed to Object.entries().

Looks like this check will need special handling of nulls, since typeof null is object 🤦‍♂

https://github.com/elbywan/wretch/blob/master/src/wretcher.ts#L329-L335

@elbywan
Copy link
Owner

elbywan commented Jan 25, 2020

Woops indeed! I just published a new release with the fix (v1.7.1).

Thanks, good catch 👍!

@itsgoingd
Copy link
Author

Works great, thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants