@@ -236,64 +236,48 @@ const getSchemaFormDataAndUrlEncoded = ({
236
236
) ;
237
237
238
238
const variableName = isUrlEncoded ? 'formUrlEncoded' : 'formData' ;
239
- const form = isUrlEncoded
239
+ let form = isUrlEncoded
240
240
? `const ${ variableName } = new URLSearchParams();\n`
241
241
: `const ${ variableName } = new FormData();\n` ;
242
242
243
- if ( schema . type === 'object' && schema . properties ) {
244
- const formDataValues = Object . entries ( schema . properties ) . reduce (
245
- ( acc , [ key , value ] ) => {
246
- const { schema : property } = resolveRef < SchemaObject > ( value , context ) ;
247
-
248
- let formDataValue = '' ;
249
-
250
- const formatedKey = ! keyword . isIdentifierNameES5 ( key )
251
- ? `['${ key } ']`
252
- : `.${ key } ` ;
253
-
254
- if ( property . type === 'object' ) {
255
- formDataValue = `${ variableName } .append('${ key } ', JSON.stringify(${ propName } ${ formatedKey } ));\n` ;
256
- } else if ( property . type === 'array' ) {
257
- formDataValue = `${ propName } ${ formatedKey } .forEach(value => ${ variableName } .append('${ key } ', value));\n` ;
258
- } else if (
259
- property . type === 'number' ||
260
- property . type === 'integer' ||
261
- property . type === 'boolean'
262
- ) {
263
- formDataValue = `${ variableName } .append('${ key } ', ${ propName } ${ formatedKey } .toString())\n` ;
264
- } else {
265
- formDataValue = `${ variableName } .append('${ key } ', ${ propName } ${ formatedKey } )\n` ;
266
- }
267
-
268
- const isRequired = schema . required ?. includes ( key ) ;
243
+ if ( schema . type === 'object' ) {
244
+ if ( schema . oneOf || schema . anyOf || schema . allOf ) {
245
+ const combinedSchemas = schema . oneOf || schema . anyOf || schema . allOf ;
269
246
270
- if ( property . nullable ) {
271
- if ( isRequired ) {
272
- return (
273
- acc +
274
- `if(${ propName } ${ formatedKey } !== null) {\n ${ formDataValue } }\n`
275
- ) ;
276
- }
247
+ const shouldCast = ! ! schema . oneOf || ! ! schema . anyOf ;
277
248
278
- return (
279
- acc +
280
- `if(${ propName } ${ formatedKey } !== undefined && ${ propName } ${ formatedKey } !== null) {\n ${ formDataValue } }\n`
249
+ const combinedSchemasFormData = combinedSchemas !
250
+ . map ( ( schema ) => {
251
+ const { schema : combinedSchema , imports } = resolveRef < SchemaObject > (
252
+ schema ,
253
+ context ,
281
254
) ;
282
- }
283
-
284
- if ( isRequired ) {
285
- return acc + formDataValue ;
286
- }
287
-
288
- return (
289
- acc +
290
- `if(${ propName } ${ formatedKey } !== undefined) {\n ${ formDataValue } }\n`
291
- ) ;
292
- } ,
293
- '' ,
294
- ) ;
295
255
296
- return `${ form } ${ formDataValues } ` ;
256
+ return resolveSchemaPropertiesToFormData ( {
257
+ schema : combinedSchema ,
258
+ variableName,
259
+ propName : shouldCast ? `(${ propName } as any)` : propName ,
260
+ context,
261
+ } ) ;
262
+ } )
263
+ . filter ( ( x ) => x )
264
+ . join ( '\n' ) ;
265
+
266
+ form += combinedSchemasFormData ;
267
+ }
268
+
269
+ if ( schema . properties ) {
270
+ const formDataValues = resolveSchemaPropertiesToFormData ( {
271
+ schema,
272
+ variableName,
273
+ propName,
274
+ context,
275
+ } ) ;
276
+
277
+ form += formDataValues ;
278
+ }
279
+
280
+ return form ;
297
281
}
298
282
299
283
if ( schema . type === 'array' ) {
@@ -306,3 +290,65 @@ const getSchemaFormDataAndUrlEncoded = ({
306
290
307
291
return `${ form } ${ variableName } .append('data', ${ propName } )\n` ;
308
292
} ;
293
+
294
+ const resolveSchemaPropertiesToFormData = ( {
295
+ schema,
296
+ variableName,
297
+ propName,
298
+ context,
299
+ } : {
300
+ schema : SchemaObject ;
301
+ variableName : string ;
302
+ propName : string ;
303
+ context : ContextSpecs ;
304
+ } ) => {
305
+ const formDataValues = Object . entries ( schema . properties ?? { } ) . reduce (
306
+ ( acc , [ key , value ] ) => {
307
+ const { schema : property } = resolveRef < SchemaObject > ( value , context ) ;
308
+
309
+ let formDataValue = '' ;
310
+
311
+ const formatedKey = ! keyword . isIdentifierNameES5 ( key )
312
+ ? `['${ key } ']`
313
+ : `.${ key } ` ;
314
+
315
+ const valueKey = `${ propName } ${ formatedKey } ` ;
316
+
317
+ if ( property . type === 'object' ) {
318
+ formDataValue = `${ variableName } .append('${ key } ', JSON.stringify(${ valueKey } ));\n` ;
319
+ } else if ( property . type === 'array' ) {
320
+ formDataValue = `${ valueKey } .forEach(value => ${ variableName } .append('${ key } ', value));\n` ;
321
+ } else if (
322
+ property . type === 'number' ||
323
+ property . type === 'integer' ||
324
+ property . type === 'boolean'
325
+ ) {
326
+ formDataValue = `${ variableName } .append('${ key } ', ${ valueKey } .toString())\n` ;
327
+ } else {
328
+ formDataValue = `${ variableName } .append('${ key } ', ${ valueKey } )\n` ;
329
+ }
330
+
331
+ const isRequired = schema . required ?. includes ( key ) ;
332
+
333
+ if ( property . nullable ) {
334
+ if ( isRequired ) {
335
+ return acc + `if(${ valueKey } !== null) {\n ${ formDataValue } }\n` ;
336
+ }
337
+
338
+ return (
339
+ acc +
340
+ `if(${ valueKey } !== undefined && ${ valueKey } !== null) {\n ${ formDataValue } }\n`
341
+ ) ;
342
+ }
343
+
344
+ if ( isRequired ) {
345
+ return acc + formDataValue ;
346
+ }
347
+
348
+ return acc + `if(${ valueKey } !== undefined) {\n ${ formDataValue } }\n` ;
349
+ } ,
350
+ '' ,
351
+ ) ;
352
+
353
+ return formDataValues ;
354
+ } ;
0 commit comments