octet-stream / form-data

Spec-compliant FormData implementation for Node.js
https://www.npmjs.com/package/formdata-node
MIT License
142 stars 17 forks source link

Remove FormData.toJSON() #2

Closed jaydenseric closed 5 years ago

jaydenseric commented 5 years ago

In browsers FormData instances don't have .toJSON:

screen shot 2019-02-12 at 10 57 18 pm

For consistency with browsers it would be nice to remove that from the API here:

https://github.com/octet-stream/form-data/blob/10bf4e653bfe990f1537d4e3bbd66d5b8bb45ea8/src/lib/FormData.mjs#L405

Specifically, this logic:

const form = new FormData()
form.append('a', 'b')

const replacer = (key, value) => typeof FormData !== 'undefined' && value instanceof FormData
    ? 'works'
    : value

console.log(JSON.stringify(form, replacer))

Logs works in a browser, but [object FormData] when importing FormData from formdata-node in Node.js. This is because when a value has .toJSON(), JSON.stringify provides the output of that instead of the original value to the replacer function.

I would like to be able to use this package to test some FormData related code that is mostly intended to run in browsers.

A workaround is to do this instead:

function replacer(key) {
  const value = this[key]
  // …

But then I am changing code to match this library instead of the native FormData API.

octet-stream commented 5 years ago

Sounds reasonable. Sure, I can remove that. Thanks for your feedback!

octet-stream commented 5 years ago

Done! Try v1.3.1

jaydenseric commented 5 years ago

Looks good 🙌