@@ -7,71 +7,70 @@ import { Readable } from 'stream';
7
7
8
8
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9
9
export const appendBodyToFormData = ( formData : UnifiedFormData , body : Record < string , any > ) : void => {
10
- for ( const [ key , value ] of Object . entries ( body ) ) {
11
- if ( Array . isArray ( value ) ) {
12
- value . forEach ( ( item ) => formData . append ( key , item ) ) ;
13
- } else {
14
- formData . append ( key , value ) ;
15
- }
10
+ for ( const [ key , value ] of Object . entries ( body ) ) {
11
+ if ( Array . isArray ( value ) ) {
12
+ value . forEach ( ( item ) => formData . append ( key , item ) ) ;
13
+ } else {
14
+ formData . append ( key , value ) ;
16
15
}
17
- } ;
16
+ }
17
+ } ;
18
18
19
19
// Convert WHATWG ReadableStream to Node.js Readable Stream
20
20
function convertReadableStream ( whatwgStream : ReadableStream ) : Readable {
21
- const reader = whatwgStream . getReader ( ) ;
21
+ const reader = whatwgStream . getReader ( ) ;
22
22
23
- return new Readable ( {
24
- async read ( ) {
25
- const { done, value } = await reader . read ( ) ;
26
- if ( done ) {
27
- this . push ( null ) ;
28
- } else {
29
- this . push ( value ) ;
30
- }
31
- } ,
32
- } ) ;
23
+ return new Readable ( {
24
+ async read ( ) {
25
+ const { done, value } = await reader . read ( ) ;
26
+ if ( done ) {
27
+ this . push ( null ) ;
28
+ } else {
29
+ this . push ( value ) ;
30
+ }
31
+ } ,
32
+ } ) ;
33
33
}
34
34
35
-
36
35
export async function createFormData ( file : FilePathOrFileObject ) : Promise < UnifiedFormData > {
36
+ if ( Runtime . isBrowser ) {
37
+ const formData = new FormData ( ) ;
37
38
38
- if ( Runtime . isBrowser ) {
39
- const formData = new FormData ( ) ;
40
-
41
- if ( file instanceof window . File ) {
42
- formData . append ( 'file' , file ) ;
43
- } else {
44
- throw new Error ( 'Unsupported file type in browser' ) ;
45
- }
46
-
47
- return formData ;
48
- } else { // Node environment:
39
+ if ( file instanceof window . File ) {
40
+ formData . append ( 'file' , file ) ;
41
+ } else {
42
+ throw new Error ( 'Unsupported file type in browser' ) ;
43
+ }
49
44
50
- const { default : FormDataNode } = await import ( 'form-data' ) ;
51
- const formData = new FormDataNode ( ) ;
45
+ return formData ;
46
+ } else {
47
+ // Node environment:
52
48
53
- if ( typeof file === 'string' ) {
54
- const { createReadStream } = await import ( 'fs' ) ;
55
- formData . append ( 'file' , createReadStream ( file ) , { filename : file . split ( '/' ) . pop ( ) } ) ;
56
- } else if ( Buffer . isBuffer ( file ) ) {
57
- formData . append ( 'file' , file , { filename : 'TODO - add filename to buffer flow' } ) ;
58
- } else if ( file instanceof File ) {
59
- const nodeStream = convertReadableStream ( file . stream ( ) ) ;
60
- formData . append ( 'file' , nodeStream , file . name ) ;
61
- } else {
62
- throw new Error ( 'Unsupported file type in Node.js' ) ;
63
- }
49
+ const { default : FormDataNode } = await import ( 'form-data' ) ;
50
+ const formData = new FormDataNode ( ) ;
64
51
65
- return formData ;
52
+ if ( typeof file === 'string' ) {
53
+ const { createReadStream } = await import ( 'fs' ) ;
54
+ formData . append ( 'file' , createReadStream ( file ) , { filename : file . split ( '/' ) . pop ( ) } ) ;
55
+ } else if ( Buffer . isBuffer ( file ) ) {
56
+ formData . append ( 'file' , file , { filename : 'TODO - add filename to buffer flow' } ) ;
57
+ } else if ( file instanceof File ) {
58
+ const nodeStream = convertReadableStream ( file . stream ( ) ) ;
59
+ formData . append ( 'file' , nodeStream , file . name ) ;
60
+ } else {
61
+ throw new Error ( 'Unsupported file type in Node.js' ) ;
66
62
}
63
+
64
+ return formData ;
65
+ }
67
66
}
68
67
69
68
export async function getBoundary ( formData : UnifiedFormData ) : Promise < string | undefined > {
70
- if ( ! Runtime . isBrowser ) {
71
- const { default : FormDataNode } = await import ( 'form-data' ) ;
72
- if ( formData instanceof FormDataNode ) {
73
- return formData . getBoundary ( ) ;
74
- }
69
+ if ( ! Runtime . isBrowser ) {
70
+ const { default : FormDataNode } = await import ( 'form-data' ) ;
71
+ if ( formData instanceof FormDataNode ) {
72
+ return formData . getBoundary ( ) ;
75
73
}
76
- return undefined ;
74
+ }
75
+ return undefined ;
77
76
}
0 commit comments