File tree Expand file tree Collapse file tree 5 files changed +67
-23
lines changed Expand file tree Collapse file tree 5 files changed +67
-23
lines changed Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ export interface FileLike {
7
7
8
8
lastModified : number
9
9
10
- stream ( ) : { [ Symbol . asyncIterator ] ( ) : AsyncIterableIterator < any > }
10
+ stream ( ) : { [ Symbol . asyncIterator ] ( ) : AsyncIterableIterator < Uint8Array > }
11
11
12
12
[ Symbol . toStringTag ] : string
13
13
}
Original file line number Diff line number Diff line change 1
1
import { FileLike } from "./FileLike"
2
2
3
+ /**
4
+ * This interface reflects possible values of each FormData entry
5
+ */
3
6
export type FormDataEntryValue = string | FileLike
4
7
8
+ /**
9
+ * This interface reflects minimal shape of the FormData
10
+ */
5
11
export interface FormDataLike {
6
- set ( name : string , value : unknown ) : void
12
+ append ( name : string , value : unknown , filename ?: string ) : void
7
13
8
- append ( name : string , value : unknown ) : void
14
+ getAll ( name : string ) : FormDataEntryValue [ ]
9
15
10
- has ( name : string ) : boolean
16
+ entries ( ) : Generator < [ string , FormDataEntryValue ] >
11
17
12
- get ( name : string ) : null | FormDataEntryValue
13
-
14
- getAll ( name : string ) : Array < null | FormDataEntryValue >
15
-
16
- delete ( name : string ) : void
17
-
18
- keys ( ) : IterableIterator < string >
19
-
20
- values ( ) : IterableIterator < FormDataEntryValue >
21
-
22
- entries ( ) : IterableIterator < [ string , FormDataEntryValue ] >
23
-
24
- [ Symbol . iterator ] ( ) : IterableIterator < [ string , FormDataEntryValue ] >
18
+ [ Symbol . iterator ] ( ) : Generator < [ string , FormDataEntryValue ] >
25
19
26
20
[ Symbol . toStringTag ] : string
27
21
}
Original file line number Diff line number Diff line change @@ -5,7 +5,9 @@ import {FileLike} from "../FileLike"
5
5
const VALID_NAMES = [ "File" , "Blob" ]
6
6
7
7
/**
8
- * Check if given object is File
8
+ * Check if given object is File.
9
+ *
10
+ * Note that this function will return "false" for Blob, because the Encoder expects FormData to return File when a value is binary data.
9
11
*
10
12
* @param value an object to test
11
13
*/
@@ -15,6 +17,9 @@ const isFile = (value?: unknown): value is FileLike => Boolean(
15
17
&& isFunction ( ( value as FileLike ) . constructor )
16
18
&& VALID_NAMES . includes ( ( value as FileLike ) [ Symbol . toStringTag ] )
17
19
&& isFunction ( ( value as FileLike ) . stream )
20
+ && ( value as FileLike ) . name != null
21
+ && ( value as FileLike ) . size != null
22
+ && ( value as FileLike ) . lastModified != null
18
23
)
19
24
20
25
export default isFile
Original file line number Diff line number Diff line change @@ -4,10 +4,60 @@ import {FormData} from "formdata-node"
4
4
5
5
import isFormData from "./isFormData"
6
6
7
+ import { FormDataLike , FormDataEntryValue } from "../FormDataLike"
8
+
7
9
test ( "Returns true for spec-compatible FormData instance" , t => {
8
10
t . true ( isFormData ( new FormData ( ) ) )
9
11
} )
10
12
13
+ test ( "Returns true for a class that implements minimal FormData" , t => {
14
+ class MyFormData implements FormDataLike {
15
+ append ( ) : void { }
16
+
17
+ getAll ( ) : FormDataEntryValue [ ] {
18
+ return [ ]
19
+ }
20
+
21
+ * entries ( ) : Generator < [ string , FormDataEntryValue ] > {
22
+ yield [ "name" , "value" ]
23
+ }
24
+
25
+ [ Symbol . iterator ] ( ) {
26
+ return this . entries ( )
27
+ }
28
+
29
+ get [ Symbol . toStringTag ] ( ) : string {
30
+ return "FormData"
31
+ }
32
+ }
33
+
34
+ t . true ( isFormData ( new MyFormData ( ) ) )
35
+ } )
36
+
37
+ test ( "Returns true for FormData-shaped object" , t => {
38
+ const object = {
39
+ append ( ) : void { } ,
40
+
41
+ getAll ( ) : FormDataEntryValue [ ] {
42
+ return [ ]
43
+ } ,
44
+
45
+ * entries ( ) : Generator < [ string , FormDataEntryValue ] > {
46
+ yield [ "name" , "value" ]
47
+ } ,
48
+
49
+ [ Symbol . iterator ] ( ) {
50
+ return this . entries ( )
51
+ } ,
52
+
53
+ get [ Symbol . toStringTag ] ( ) : string {
54
+ return "FormData"
55
+ }
56
+ }
57
+
58
+ t . true ( isFormData ( object ) )
59
+ } )
60
+
11
61
test ( "Returns false for null" , t => {
12
62
t . false ( isFormData ( null ) )
13
63
} )
Original file line number Diff line number Diff line change @@ -11,13 +11,8 @@ const isFormData = (value?: unknown): value is FormDataLike => Boolean(
11
11
( value as FormDataLike )
12
12
&& isFunction ( ( value as FormDataLike ) . constructor )
13
13
&& ( value as FormDataLike ) [ Symbol . toStringTag ] === "FormData"
14
- && isFunction ( ( value as FormDataLike ) . set )
15
14
&& isFunction ( ( value as FormDataLike ) . append )
16
- && isFunction ( ( value as FormDataLike ) . has )
17
- && isFunction ( ( value as FormDataLike ) . get )
18
15
&& isFunction ( ( value as FormDataLike ) . getAll )
19
- && isFunction ( ( value as FormDataLike ) . keys )
20
- && isFunction ( ( value as FormDataLike ) . values )
21
16
&& isFunction ( ( value as FormDataLike ) . entries )
22
17
&& isFunction ( ( value as FormDataLike ) [ Symbol . iterator ] )
23
18
)
You can’t perform that action at this time.
0 commit comments