@@ -17,14 +17,20 @@ interface FileParamValue {
17
17
type ?: string ;
18
18
}
19
19
20
- export function getFileData ( value : FileParamValue | string ) : File | Buffer {
20
+ export function getFileData ( value : FileParamValue | string ) : File | Buffer | null {
21
+ if ( ! value ) {
22
+ return null ;
23
+ }
21
24
if ( typeof value === "string" ) {
22
25
return Buffer . from ( value , "base64" ) ;
23
26
}
24
27
if ( value instanceof Buffer || value instanceof File ) {
25
28
return value ;
26
29
}
27
30
const { data = "" , name = "file" , type } = value || { } ;
31
+ if ( ! data ) {
32
+ return null ;
33
+ }
28
34
return new File ( [ Buffer . from ( data , "base64" ) ] , name , { type } ) ;
29
35
}
30
36
@@ -107,10 +113,12 @@ export function normalizeParams(
107
113
}
108
114
const isFile = isFileData ( name , operation , isOas3 ) ;
109
115
const value = isFile ? getFileData ( params [ key ] ) : params [ key ] ;
110
- if ( isOas3 && position === "body" ) {
111
- bodyEntries . push ( [ name , value ] ) ;
112
- } else {
113
- paramEntries . push ( [ name , value ] ) ;
116
+ if ( value ) {
117
+ if ( isOas3 && position === "body" ) {
118
+ bodyEntries . push ( [ name , value ] ) ;
119
+ } else {
120
+ paramEntries . push ( [ name , value ] ) ;
121
+ }
114
122
}
115
123
} ) ;
116
124
@@ -130,7 +138,10 @@ export function normalizeParams(
130
138
// process file fields
131
139
const fileFields = findOas3FilePropertiesFromSchema ( schema ) ;
132
140
fileFields . forEach ( ( [ name ] ) => {
133
- requestBody [ name ] = getFileData ( requestBody [ name ] ) ;
141
+ const file = getFileData ( requestBody [ name ] ) ;
142
+ if ( file ) {
143
+ requestBody [ name ] = file ;
144
+ }
134
145
} ) ;
135
146
}
136
147
}
@@ -143,7 +154,10 @@ export function normalizeParams(
143
154
const [ name , value ] = bodyEntries [ 0 ] ;
144
155
if ( name === "body" ) {
145
156
if ( mediaType === MediaTypeOctetStream ) {
146
- requestBody = getFileData ( value ) ;
157
+ const file = getFileData ( value ) ;
158
+ if ( file ) {
159
+ requestBody = file ;
160
+ }
147
161
} else {
148
162
requestBody = value ;
149
163
}
@@ -191,6 +205,10 @@ export function findOperation(id: string, spec: OpenAPI.Document) {
191
205
return null ;
192
206
}
193
207
208
+ export function isFile ( file : any ) : file is File {
209
+ return file instanceof File ;
210
+ }
211
+
194
212
export function isFileData ( name : string , operation : OpenAPI . Operation , isOas3 : boolean ) : boolean {
195
213
if ( isOas3 ) {
196
214
const oas3Operation = operation as OpenAPIV3 . OperationObject ;
0 commit comments