3.5.0
This release is mostly about deprecation of an old spec-incompatible features of this package on its way to the next major release.
Update
- Documentation improvements
- Fixes for typings
- Update
polyfill.mjsandpolyfill.jsso they will not affectglobalThiswhenFile,BloborFormDatapresented.
Deprecate
- Every serialisation method and property:
FormData#stream,FormData#headers,FormData#getComputedLength()andFormData#[Symbol.asyncIterator]()will be removed in4.xversion, use formdata-node with HTTP clients that support spec-compatible form-data or useform-data-encoderto perform the serialisation manually
This is how serialisation would look like, when using form-data-encoder:
import {Readable} from "stream"
import fetch from "node-fetch"
import {FormData, fileFromPath} from "formdata-node"
+ import {Encoder} from "form-data-encoder"
const fd = new FormData()
fd.set("name", "John Doe")
fd.set("avatar", await fileFromPath("/path/to/an/avatar.png"))
- const options = {
- method: "post",
- headers: fd.headers,
- body: Readable.from(fd)
- }
+ const encoder = new Encoder(fd)
+
+ const options = {
+ method: "post",
+ headers: encoder.headers // Note that this property has both `Content-Type` and `Content-Length` headers in it, unlike FormData#headers which only has `Content-Type` header
+ body: Readable.from(encoder)
+ }
await fetch("https://httpbin.org/post", options)You don't have to worry about the form-data serialisation if your HTTP client support spec-compatible form-data, but this may help you otherwise
- Due to compatibility reasons, the
BufferandReadStreamin entry's value will be removed in4.xversion. So they're deprecated too. To append files to the formdata-node instances, useFile,Blob,fileFromPath()orfileFromPathSync:
This is how you'll append files from Buffer:
- import {FormData, Blob} from "formdata-node"
+ import {FormData, Blob, fileFromPathSync} from "formdata-node"
const fd = new FormData()
- fd.set("blob", Buffer.from("Some data in buffer"))
+ // Note that you don't have to convert a string into buffer first to use it in Blobs. The source of Buffer may vary depending on the context. This one is here just for an example.
+ const buffer = Buffer.from("Some data in buffer")
+ fd.set("blob", new Blob([buffer], {type: "text/plain"}))Appending files using fileFromPath:
- import {createReadStream} from "fs"
- import {FormData} from "formdata-node"
+ import {FormData, fileFromPath} from "formdata-node"
const fd = new FormData()
- fd.set("file", createReadStream("/path/to/a/file"))
+ fd.set("file", await fileFromPath("/path/to/a/file")) // Or use `fileFromPathSync` if you need synchronous version- The third upcoming change is deprecation of CommonJS support in this package. You may still use it (as all deprecated features mentioned above), but CJS version of this package will not be included starting from
4.x.
All changes: v3.4.0...v3.5.0