Skip to content

Commit 3f86fd7

Browse files
committed
Add an example of Symbol.iterator method usage.
1 parent a86ba61 commit 3f86fd7

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

lib/Encoder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export class Encoder {
150150
*
151151
* fd.set("field", "Just a random string")
152152
* fd.set("file", new File(["Using files is class amazing"]))
153-
* fd.set("fileFromPath", await fileFromPath("path/to/a/file.txt"))
153+
* fd.set("fileFromPath", await fileFrom("path/to/a/file.txt"))
154154
*
155155
* const encoder = new Encoder(fd)
156156
*

readme.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,36 @@ const options = {
6767
await fetch("https://httpbin.org/post", options)
6868
```
6969

70-
3. Because the Encoder is async iterable, you can use it with different targets. Let's say you want to put FormData content into `Blob`, for that you can write a function like this:
70+
3. Because the Encoder is iterable (it has both Symbol.asyncIterator and Symbol.iterator methods), you can use it with different targets. Let's say you want to convert FormData content into `Blob`, for that you can write a function like this:
71+
72+
```js
73+
import {Readable} from "stream"
74+
75+
import {Encoder} from "form-data-encoder"
76+
77+
import {FormData, File, Blob, fileFromPath} from "formdata-node"
78+
79+
import fetch from "node-fetch"
80+
81+
const fd = new FormData()
82+
83+
fd.set("field", "Just a random string")
84+
fd.set("file", new File(["Using files is class amazing"]))
85+
fd.set("fileFromPath", await fileFromPath("path/to/a/file.txt"))
86+
87+
const encoder = new Encoder(fd)
88+
89+
const options = {
90+
method: "post",
91+
body: new Blob(encoder, {type: encoder.contentType})
92+
}
93+
94+
const response = await fetch("https://httpbin.org/post", options)
95+
96+
console.log(await response.json())
97+
```
98+
99+
4. Here's FormData to Blob conversion with async-iterator approach:
71100

72101
```js
73102
import {FormData} from "formdata-polyfill/esm-min.js"
@@ -101,7 +130,7 @@ const options = {
101130
await fetch("https://httpbin.org/post", options)
102131
```
103132

104-
4. Another way to convert FormData parts to blob using `form-data-encoder` is making a Blob-ish class:
133+
5. Another way to convert FormData parts to blob using `form-data-encoder` is making a Blob-ish class:
105134

106135
```js
107136
import {Readable} from "stream"
@@ -155,7 +184,7 @@ const options = {
155184
await fetch("https://httpbin.org/post", options)
156185
```
157186

158-
5. In this example we will pull FormData content into the ReadableStream:
187+
6. In this example we will pull FormData content into the ReadableStream:
159188

160189
```js
161190
// This module is only necessary when you targeting Node.js or need web streams that implement Symbol.asyncIterator
@@ -194,7 +223,7 @@ const options = {
194223
await fetch("https://httpbin.org/post", options)
195224
```
196225

197-
6. Speaking of async iterables - if HTTP client supports them, you can use encoder like this:
226+
7. Speaking of async iterables - if HTTP client supports them, you can use encoder like this:
198227

199228
```js
200229
import {Encoder} from "form-data-encoder"
@@ -217,7 +246,7 @@ const options = {
217246
await fetch("https://httpbin.org/post", options)
218247
```
219248

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

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

0 commit comments

Comments
 (0)