@@ -274,15 +274,51 @@ export const createHook = ({
274
274
const generateBodyProps = ( ) => {
275
275
if ( definitionKey && ! hasRequestBodyArrray ) {
276
276
const scheme = schemasComponents ?. schemas ?. [ definitionKey ] as SchemaObject ;
277
- const generatedBodyProps = Object . keys ( scheme . properties as SchemaObject )
277
+ const schemProperties = Object . keys ( scheme . properties as SchemaObject ) ;
278
+ if (
279
+ operation . requestBody &&
280
+ 'content' in operation . requestBody &&
281
+ Object . keys ( operation . requestBody . content ) [ 0 ] === 'multipart/form-data'
282
+ ) {
283
+ let formData = `const body = new FormData()` ;
284
+ schemProperties . forEach ( ( item ) => {
285
+ if ( scheme . required ?. includes ( item ) ) {
286
+ formData += `\nbody.append("${ item } ", props["${ item } "]);` ;
287
+ } else {
288
+ formData += `\n if (props["${ item } "] != null) {\n body.append("${ item } ", props["${ item } "]);\n}` ;
289
+ }
290
+ } ) ;
291
+ return formData ;
292
+ }
293
+
294
+ const generatedBodyProps = schemProperties
278
295
. map ( ( item : string ) => `["${ item } "]: props["${ item } "]` )
279
296
. join ( ',' ) ;
280
297
return `const body = {${ generatedBodyProps } }` ;
281
298
}
282
299
283
300
if ( operation . requestBody && 'content' in operation . requestBody ) {
284
301
let generatedBodyProps = [ ] ;
285
- for ( let contentType of Object . keys ( operation . requestBody . content ) ) {
302
+ let bodyData = '' ;
303
+ const contentTypes = Object . keys ( operation . requestBody . content ) ;
304
+ if ( contentTypes . includes ( 'multipart/form-data' ) ) {
305
+ bodyData += `const body = new FormData()` ;
306
+ for ( let contentType of contentTypes ) {
307
+ const schema = operation . requestBody . content [ contentType ] . schema ! ;
308
+ if ( 'properties' in schema ) {
309
+ const schemaProperties = Object . keys ( schema . properties || [ ] ) ;
310
+ for ( let item of schemaProperties ) {
311
+ if ( schema . required ?. includes ( item ) ) {
312
+ bodyData += `\nbody.append("${ item } ", props["${ item } "].toString());` ;
313
+ } else {
314
+ bodyData += `\n if (props["${ item } "] != null) {\n body.append("${ item } ", props["${ item } "].toString());\n}` ;
315
+ }
316
+ }
317
+ }
318
+ }
319
+ return bodyData ;
320
+ }
321
+ for ( let contentType of contentTypes ) {
286
322
if (
287
323
contentType . startsWith ( 'application/json' ) ||
288
324
contentType . startsWith ( 'application/ld+json' ) ||
@@ -296,10 +332,8 @@ export const createHook = ({
296
332
}
297
333
}
298
334
}
299
- return `const body = {${ generatedBodyProps . map ( ( item ) => `${ item } : props. ${ item } ` ) . join ( ',' ) } }` ;
335
+ return `const body = {${ generatedBodyProps . map ( ( item ) => `${ item } : props[" ${ item } "] ` ) . join ( ',' ) } }` ;
300
336
}
301
-
302
- return `ERROR` ;
303
337
} ;
304
338
305
339
if ( ! requestBodyComponent && ! paramsInPath . length && ! queryParam && ! headerParam ) {
0 commit comments