Skip to content

Commit 6643b07

Browse files
committed
Add an example with alternative approach when targeting Blob.
1 parent aaad80b commit 6643b07

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

readme.md

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,61 @@ const options = {
101101
await fetch("https://httpbin.org/post", options)
102102
```
103103

104-
4. In this example we will pull FormData content into the ReadableStream:
104+
4. Another way to convert FormData parts to blob using `form-data-encoder` is making a Blob-ish class:
105+
106+
```js
107+
import {Readable} from "stream"
108+
109+
import {FormData} from "formdata-polyfill/esm-min.js"
110+
import {blobFrom} from "fetch-blob/from.js"
111+
import {Encoder} from "form-data-encoder"
112+
113+
import Blob from "fetch-blob"
114+
import fetch from "node-fetch"
115+
116+
class BlobDataItem {
117+
constructor(encoder) {
118+
this.#encoder = encoder
119+
this.#size = encoder.headers["Content-Length"]
120+
this.#type = encoder.headers["Content-Type"]
121+
}
122+
123+
get type() {
124+
return this.#type
125+
}
126+
127+
get size() {
128+
return this.#size
129+
}
130+
131+
stream() {
132+
return Readable.from(this.#encoder)
133+
}
134+
135+
get [Symbol.toStringTag]() {
136+
return "Blob"
137+
}
138+
}
139+
140+
const fd = new FormData()
141+
142+
fd.set("name", "John Doe")
143+
fd.set("avatar", await blobFrom("path/to/an/avatar.png"), "avatar.png")
144+
145+
const encoder = new Encoder(fd)
146+
147+
// 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)
148+
const blob = new BlobDataItem(enocoder) // or new Blob([new BlobDataItem(enocoder)], {type: encoder.contentType})
149+
150+
const options = {
151+
method: "post",
152+
body: blob
153+
}
154+
155+
await fetch("https://httpbin.org/post", options)
156+
```
157+
158+
5. In this example we will pull FormData content into the ReadableStream:
105159

106160
```js
107161
// This module is only necessary when you targeting Node.js or need web streams that implement Symbol.asyncIterator
@@ -140,7 +194,7 @@ const options = {
140194
await fetch("https://httpbin.org/post", options)
141195
```
142196

143-
5. Speaking of async iterables - if HTTP client supports them, you can use encoder like this:
197+
6. Speaking of async iterables - if HTTP client supports them, you can use encoder like this:
144198

145199
```js
146200
import {Encoder} from "form-data-encoder"
@@ -163,7 +217,7 @@ const options = {
163217
await fetch("https://httpbin.org/post", options)
164218
```
165219

166-
6. ...And for those client whose supporting form-data-encoder out of the box, the usage will be much, much more simpler:
220+
7. ...And for those client whose supporting form-data-encoder out of the box, the usage will be much, much more simpler:
167221

168222
```js
169223
import {FormData} from "formdata-node" // Or any other spec-compatible implementation

0 commit comments

Comments
 (0)