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.
In browsers
FormData
instances don't have.toJSON
: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:
Logs
works
in a browser, but[object FormData]
when importingFormData
fromformdata-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:
But then I am changing code to match this library instead of the native
FormData
API.