Skip to content

Commit 054cbcf

Browse files
committed
Use less ambiguous name for form data encoder class and its exports. Keep Encoder as and alias to FormDataEncoder for backward compatibility.
1 parent 9362f58 commit 054cbcf

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

lib/Encoder.test.ts

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,32 @@ import readStream from "./__helper__/readStream"
1010
import skip from "./__helper__/skipIterations"
1111
import readLine from "./__helper__/readLine"
1212

13-
import {Encoder} from "./Encoder"
13+
import {FormDataEncoder} from "./Encoder"
1414

1515
test("Has boundary string", t => {
16-
const encoder = new Encoder(new FormData())
16+
const encoder = new FormDataEncoder(new FormData())
1717

1818
t.true(typeof encoder.boundary === "string")
1919
})
2020

2121
test("Accepts custom boundary as the second argument", t => {
2222
const expected = "BoundaryString123"
2323

24-
const encoder = new Encoder(new FormData(), expected)
24+
const encoder = new FormDataEncoder(new FormData(), expected)
2525

2626
t.is(encoder.boundary, `form-data-boundary-${expected}`)
2727
})
2828

2929
test("Has content-type string", t => {
30-
const encoder = new Encoder(new FormData())
30+
const encoder = new FormDataEncoder(new FormData())
3131

3232
t.true(encoder.contentType.startsWith("multipart/form-data; boundary="))
3333
})
3434

3535
test("Has content-type string with custom boundary string", t => {
3636
const expected = "BoundaryString123"
3737

38-
const encoder = new Encoder(new FormData(), expected)
38+
const encoder = new FormDataEncoder(new FormData(), expected)
3939

4040
t.is(
4141
encoder.contentType,
@@ -44,7 +44,7 @@ test("Has content-type string with custom boundary string", t => {
4444
})
4545

4646
test("Has correct headers", async t => {
47-
const encoder = new Encoder(new FormData())
47+
const encoder = new FormDataEncoder(new FormData())
4848

4949
t.deepEqual(encoder.headers, {
5050
"Content-Type": `multipart/form-data; boundary=${encoder.boundary}`,
@@ -53,7 +53,7 @@ test("Has correct headers", async t => {
5353
})
5454

5555
test("Yields correct footer for empty FormData", async t => {
56-
const encoder = new Encoder(new FormData())
56+
const encoder = new FormDataEncoder(new FormData())
5757

5858
const iterable = readLine(Readable.from(encoder))
5959

@@ -63,13 +63,13 @@ test("Yields correct footer for empty FormData", async t => {
6363
})
6464

6565
test("The footer ends with two crlf", async t => {
66-
const actual = await readStream(new Encoder(new FormData()), true)
66+
const actual = await readStream(new FormDataEncoder(new FormData()), true)
6767

6868
t.true(actual.endsWith("\r\n\r\n"))
6969
})
7070

7171
test("Returns correct length of the empty FormData content", async t => {
72-
const encoder = new Encoder(new FormData())
72+
const encoder = new FormDataEncoder(new FormData())
7373
const expected = await readStream(encoder).then(({length}) => length)
7474

7575
t.is<number>(encoder.getContentLength(), expected)
@@ -81,7 +81,7 @@ test("Returns the length of the FormData content", async t => {
8181
fd.set("field", "Some string")
8282
fd.set("file", new File(["Some content"], "file.txt"))
8383

84-
const encoder = new Encoder(fd)
84+
const encoder = new FormDataEncoder(fd)
8585

8686
const expected = await readStream(encoder).then(({length}) => length)
8787

@@ -93,7 +93,7 @@ test(".values() yields headers as Uint8Array", t => {
9393

9494
fd.set("field", "Some value")
9595

96-
const iterable = new Encoder(fd).values()
96+
const iterable = new FormDataEncoder(fd).values()
9797

9898
const {value: actual} = skipSync(iterable)
9999

@@ -105,7 +105,7 @@ test(".valeus() yields field as Uint8Array", t => {
105105

106106
fd.set("field", "Some value")
107107

108-
const {value: actual} = skipSync(new Encoder(fd).values(), 2)
108+
const {value: actual} = skipSync(new FormDataEncoder(fd).values(), 2)
109109

110110
t.true(actual instanceof Uint8Array)
111111
})
@@ -118,7 +118,7 @@ test(".valeus() yields field's content", t => {
118118

119119
fd.set("field", string)
120120

121-
const {value: actual} = skipSync(new Encoder(fd).values(), 2)
121+
const {value: actual} = skipSync(new FormDataEncoder(fd).values(), 2)
122122

123123
t.true(Buffer.from(actual as Uint8Array).equals(expected))
124124
})
@@ -130,7 +130,7 @@ test(".values() yields a file as is", async t => {
130130

131131
fd.set("file", file)
132132

133-
const {value: actual} = skipSync(new Encoder(fd).values(), 2)
133+
const {value: actual} = skipSync(new FormDataEncoder(fd).values(), 2)
134134

135135
t.true(actual instanceof File)
136136
t.is(await (actual as File).text(), await file.text())
@@ -141,7 +141,7 @@ test("Yields correct headers for a field", async t => {
141141

142142
fd.set("field", "Some value")
143143

144-
const iterable = readLine(Readable.from(new Encoder(fd)))
144+
const iterable = readLine(Readable.from(new FormDataEncoder(fd)))
145145

146146
const {value} = await skip(iterable, 2)
147147

@@ -155,7 +155,9 @@ test("Yields field's content", async t => {
155155

156156
fd.set("field", expected)
157157

158-
const {value} = await skip(readLine(Readable.from(new Encoder(fd))), 4)
158+
const {
159+
value
160+
} = await skip(readLine(Readable.from(new FormDataEncoder(fd))), 4)
159161

160162
t.is(value, expected)
161163
})
@@ -165,7 +167,9 @@ test("Yields Content-Disposition header for a File", async t => {
165167

166168
fd.set("file", new File(["My hovercraft is full of eels"], "file.txt"))
167169

168-
const {value} = await skip(readLine(Readable.from(new Encoder(fd))), 2)
170+
const {
171+
value
172+
} = await skip(readLine(Readable.from(new FormDataEncoder(fd))), 2)
169173

170174
t.is(
171175
value,
@@ -180,7 +184,9 @@ test("Yields Content-Type header for a File", async t => {
180184
type: "text/plain"
181185
})
182186

183-
const {value} = await skip(readLine(Readable.from(new Encoder(fd))), 3)
187+
const {
188+
value
189+
} = await skip(readLine(Readable.from(new FormDataEncoder(fd))), 3)
184190

185191
t.is(value, "Content-Type: text/plain")
186192
})
@@ -192,7 +198,7 @@ test(
192198

193199
fd.set("file", new File(["Some content"], "file"))
194200

195-
const iterable = readLine(Readable.from(new Encoder(fd)))
201+
const iterable = readLine(Readable.from(new FormDataEncoder(fd)))
196202

197203
const {value} = await skip(iterable, 3)
198204

@@ -208,7 +214,7 @@ test("Yields File's content", async t => {
208214

209215
fd.set("license", await fileFromPath(filePath))
210216

211-
const encoder = new Encoder(fd)
217+
const encoder = new FormDataEncoder(fd)
212218
const iterable = readLine(Readable.from(encoder))
213219

214220
await skip(iterable, 4)
@@ -235,7 +241,7 @@ test("Yields every appended field", async t => {
235241
fd.append("field", "Some string")
236242
fd.append("field", "Some other string")
237243

238-
const iterable = readLine(Readable.from(new Encoder(fd)))
244+
const iterable = readLine(Readable.from(new FormDataEncoder(fd)))
239245

240246
const {value: firstFieldDisposition} = await skip(iterable, 2)
241247

@@ -267,7 +273,7 @@ test("Yields every appended File", async t => {
267273
fd.append("file", firstFile)
268274
fd.append("file", secondFile)
269275

270-
const iterable = readLine(Readable.from(new Encoder(fd)))
276+
const iterable = readLine(Readable.from(new FormDataEncoder(fd)))
271277

272278
const {value: firstFileDisposition} = await skip(iterable, 2)
273279

@@ -300,7 +306,7 @@ test("Can be read through using Blob", async t => {
300306
fd.set("field", "Some field")
301307
fd.set("file", await fileFromPath("license", {type: "text/plain"}))
302308

303-
const encoder = new Encoder(fd)
309+
const encoder = new FormDataEncoder(fd)
304310
const blob = new Blob([...encoder] as any[])
305311

306312
t.true(
@@ -314,7 +320,7 @@ test(
314320
"Throws TypeError when the first argument is not a FormData instance",
315321
t => {
316322
// @ts-expect-error
317-
const trap = () => new Encoder({})
323+
const trap = () => new FormDataEncoder({})
318324

319325
t.throws(trap, {
320326
instanceOf: TypeError,
@@ -325,7 +331,7 @@ test(
325331

326332
test("Throws TypeError when given boundary is not a string", t => {
327333
// @ts-expect-error
328-
const trap = () => new Encoder(new FormData(), 42)
334+
const trap = () => new FormDataEncoder(new FormData(), 42)
329335

330336
t.throws(trap, {
331337
instanceOf: TypeError,

lib/Encoder.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import isFile from "./util/isFile"
66
import {FormDataLike} from "./FormDataLike"
77
import {FileLike} from "./FileLike"
88

9-
export class Encoder {
9+
export class FormDataEncoder {
1010
/**
1111
* Returns boundary string
1212
*/
@@ -230,3 +230,9 @@ export class Encoder {
230230
return this.encode()
231231
}
232232
}
233+
234+
/**
235+
* @deprecated Use FormDataEncoder to import the encoder class instead
236+
*/
237+
/* c8 ignore next */
238+
export const Encoder = FormDataEncoder

0 commit comments

Comments
 (0)