Skip to content

Commit e1081cb

Browse files
committed
Documntation improvements.
1 parent 1be71c3 commit e1081cb

File tree

1 file changed

+51
-44
lines changed

1 file changed

+51
-44
lines changed

readme.md

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ import {FormDataEncoder} from "form-data-encoder"
1818

1919
import fetch from "node-fetch"
2020

21-
const fd = new FormData()
21+
const form = new FormData()
2222

23-
fd.set("greeting", "Hello, World!")
24-
fd.set("file", new File(["On Soviet Moon landscape see binoculars through YOU"], "file.txt"))
23+
form.set("greeting", "Hello, World!")
24+
form.set("file", new File(["On Soviet Moon landscape see binoculars through YOU"], "file.txt"))
2525

26-
const encoder = new FormDataEncoder(fd)
26+
const encoder = new FormDataEncoder(form)
2727

2828
const options = {
2929
method: "post",
@@ -35,7 +35,7 @@ const options = {
3535
// Create a Readable stream from the Encoder.
3636
// You can omit usage of `Readable.from` for HTTP clients whose support async iterables.
3737
// The Encoder will yield FormData content portions encoded into the multipart/form-data format as node-fetch consumes the stream.
38-
body: Readable.from(encoder.encode()) // or Readable.from(encoder)
38+
body: Readable.from(encoder.encode()) // or just Readable.from(encoder)
3939
}
4040

4141
const response = await fetch("https://httpbin.org/post", options)
@@ -52,12 +52,12 @@ import {FormDataEncoder} from "form-data-encoder"
5252
import {FormData} from "formdata-polyfill/esm-min.js"
5353
import {File} from "fetch-blob" // v3
5454

55-
const fd = new FormData()
55+
const form = new FormData()
5656

57-
fd.set("field", "Some value")
58-
fd.set("file", new File(["File content goes here"], "file.txt"))
57+
form.set("field", "Some value")
58+
form.set("file", new File(["File content goes here"], "file.txt"))
5959

60-
const encoder = new FormDataEncoder(fd)
60+
const encoder = new FormDataEncoder(form)
6161

6262
const options = {
6363
method: "post",
@@ -79,19 +79,21 @@ import {FormData, File, Blob, fileFromPath} from "formdata-node"
7979

8080
import fetch from "node-fetch"
8181

82-
const fd = new FormData()
82+
const form = new FormData()
8383

84-
fd.set("field", "Just a random string")
85-
fd.set("file", new File(["Using files is class amazing"], "file.txt"))
86-
fd.set("fileFromPath", await fileFromPath("path/to/a/file.txt"))
84+
form.set("field", "Just a random string")
85+
form.set("file", new File(["Using files is class amazing"], "file.txt"))
86+
form.set("fileFromPath", await fileFromPath("path/to/a/file.txt"))
8787

88-
const encoder = new FormDataEncoder(fd)
88+
// Note 1: When using with native Blob or fetch-blob@2 you might also need to generate boundary string for your FormDataEncoder instance
89+
// because Blob will lowercase value of the `type` option and default boundary generator produces a string with both lower and upper cased alphabetical characters.
90+
const encoder = new FormDataEncoder(form)
8991

9092
const options = {
9193
method: "post",
9294

93-
// To use this approach with fetch-blob@2 you probably gonna need to convert the encoder parts output to an array first:
94-
// new Blob([...encoder], {type: encoder.connectType})
95+
// Note 2: To use this approach with fetch-blob@2 you probably gonna need to convert the encoder parts output to an array first:
96+
// new Blob([...encoder], {type: encoder.contentType})
9597
body: new Blob(encoder, {type: encoder.contentType})
9698
}
9799

@@ -110,6 +112,7 @@ import {FormDataEncoder} from "form-data-encoder"
110112
import Blob from "fetch-blob"
111113
import fetch from "node-fetch"
112114

115+
// This approach may require much more RAM compared to the previous one, but it works too.
113116
async function toBlob(form) {
114117
const encoder = new Encoder(form)
115118
const chunks = []
@@ -121,14 +124,14 @@ async function toBlob(form) {
121124
return new Blob(chunks, {type: encoder.contentType})
122125
}
123126

124-
const fd = new FormData()
127+
const form = new FormData()
125128

126-
fd.set("name", "John Doe")
127-
fd.set("avatar", await blobFrom("path/to/an/avatar.png"), "avatar.png")
129+
form.set("name", "John Doe")
130+
form.set("avatar", await blobFrom("path/to/an/avatar.png"), "avatar.png")
128131

129132
const options = {
130133
method: "post",
131-
body: await toBlob(fd)
134+
body: await toBlob(form)
132135
}
133136

134137
await fetch("https://httpbin.org/post", options)
@@ -139,9 +142,9 @@ await fetch("https://httpbin.org/post", options)
139142
```js
140143
import {Readable} from "stream"
141144

145+
import {FormDataEncoder} from "form-data-encoder"
142146
import {FormData} from "formdata-polyfill/esm-min.js"
143147
import {blobFrom} from "fetch-blob/from.js"
144-
import {FormDataEncoder} from "form-data-encoder"
145148

146149
import Blob from "fetch-blob"
147150
import fetch from "node-fetch"
@@ -170,12 +173,12 @@ class BlobDataItem {
170173
}
171174
}
172175

173-
const fd = new FormData()
176+
const form = new FormData()
174177

175-
fd.set("name", "John Doe")
176-
fd.set("avatar", await blobFrom("path/to/an/avatar.png"), "avatar.png")
178+
form.set("name", "John Doe")
179+
form.set("avatar", await blobFrom("path/to/an/avatar.png"), "avatar.png")
177180

178-
const encoder = new FormDataEncoder(fd)
181+
const encoder = new FormDataEncoder(form)
179182

180183
// Note that node-fetch@2 performs more strictness tests for Blob objects, so you may need to do extra steps before you set up request body (like, maybe you'll need to instaniate a Blob with BlobDataItem as one of its blobPart)
181184
const blob = new BlobDataItem(enocoder) // or new Blob([new BlobDataItem(enocoder)], {type: encoder.contentType})
@@ -199,28 +202,32 @@ import {FormData} from "formdata-node"
199202

200203
import fetch from "node-fetch"
201204

202-
const toReadableStream = iterator => new ReadableStream({
203-
async pull(controller) {
204-
const {value, done} = await iterator.next()
205+
function toReadableStream(encoder) {
206+
const iterator = encoder.encode()
205207

206-
if (done) {
207-
return controller.close()
208-
}
208+
return new ReadableStream({
209+
async pull(controller) {
210+
const {value, done} = await iterator.next()
209211

210-
controller.enqueue(value)
211-
}
212-
})
212+
if (done) {
213+
return controller.close()
214+
}
215+
216+
controller.enqueue(value)
217+
}
218+
})
219+
}
213220

214-
const fd = new FormData()
221+
const form = new FormData()
215222

216-
fd.set("field", "My hovercraft is full of eels")
223+
form.set("field", "My hovercraft is full of eels")
217224

218-
const encoder = new FormDataEncoder(fd)
225+
const encoder = new FormDataEncoder(form)
219226

220227
const options = {
221228
method: "post",
222229
headers: encoder.headers,
223-
body: toReadableStream(encoder.encode())
230+
body: toReadableStream(encoder)
224231
}
225232

226233
// Note that this example requires `fetch` to support Symbol.asyncIterator, which node-fetch lacks of (but will support eventually)
@@ -235,11 +242,11 @@ import {FormData} from "formdata-node"
235242

236243
import fetch from "node-fetch"
237244

238-
const fd = new FormData()
245+
const form = new FormData()
239246

240-
fd.set("field", "My hovercraft is full of eels")
247+
form.set("field", "My hovercraft is full of eels")
241248

242-
const encoder = new FormDataEncoder(fd)
249+
const encoder = new FormDataEncoder(form)
243250

244251
const options = {
245252
method: "post",
@@ -257,13 +264,13 @@ import {FormData} from "formdata-node" // Or any other spec-compatible implement
257264

258265
import fetch from "node-fetch"
259266

260-
const fd = new FormData()
267+
const form = new FormData()
261268

262-
fd.set("field", "My hovercraft is full of eels")
269+
form.set("field", "My hovercraft is full of eels")
263270

264271
const options = {
265272
method: "post",
266-
body: fd
273+
body: form
267274
}
268275

269276
// Note that node-fetch does NOT support form-data-encoder

0 commit comments

Comments
 (0)