@@ -10,6 +10,7 @@ import ApiActionInfo from './elements/ApiActionInfo'
10
10
import ApiActionSQL from './elements/ApiActionSQL'
11
11
import ApiActionLog from './elements/ApiActionLog'
12
12
import ApiActionEvents from './elements/ApiActionEvents'
13
+ import { objectToFormData } from '../libs/object' ;
13
14
14
15
interface Props {
15
16
lrdDocsItem : IAPIInfo ,
@@ -29,7 +30,7 @@ export default function ApiAction(props: Props) {
29
30
const [ sendingRequest , setSendingRequest ] = useState ( false ) ;
30
31
const [ queryParams , setQueryParams ] = useState ( '' ) ;
31
32
const [ bodyParams , setBodyParams ] = useState ( '' ) ;
32
- const [ fileParams , setFileParams ] = useState ( null ) ;
33
+ const [ fileParams , setFileParams ] = useState < FormData > ( ) ;
33
34
const [ responseData , setResponseData ] = useState ( "" ) ;
34
35
const [ sqlQueriesCount , setSqlQueriesCount ] = useState ( 0 ) ;
35
36
const [ sqlData , setSqlData ] = useState ( "" ) ;
@@ -43,15 +44,21 @@ export default function ApiAction(props: Props) {
43
44
const [ responseHeaders , setResponseHeaders ] = useState ( "" ) ;
44
45
const [ activeTab , setActiveTab ] = useState ( 'info' ) ;
45
46
46
- const handleFileChange = ( files : any , file : any ) => {
47
- const formData : any = new FormData ( )
48
- if ( file . includes ( '.*' ) ) {
49
- const fileParam = file . replace ( '.*' , '' )
47
+ const handleFileChange = ( files : any , key : any ) => {
48
+ const formData = fileParams || new FormData ( ) ;
49
+ const parts = key . split ( '.' ) ;
50
+ const fileKey = key . split ( "." ) . reduce ( ( current : string , part : string , index : number ) => {
51
+ if ( index === parts . length - 1 && ( part === '*' || ! isNaN ( Number ( part ) ) ) ) {
52
+ return current
53
+ }
54
+ return ! current ? part : `${ current } [${ part } ]`
55
+ } , '' )
56
+ if ( key . includes ( '.*' ) ) {
50
57
for ( let i = 0 ; i < files . length ; i ++ ) {
51
- formData . append ( `${ fileParam } [${ i } ]` , files [ i ] ) ;
58
+ formData . append ( `${ fileKey } [${ i } ]` , files [ i ] ) ;
52
59
}
53
60
} else {
54
- formData . append ( file , files [ 0 ] )
61
+ formData . append ( fileKey , files [ 0 ] )
55
62
}
56
63
setFileParams ( formData )
57
64
}
@@ -79,7 +86,7 @@ export default function ApiAction(props: Props) {
79
86
}
80
87
const headers = JSON . parse ( requestHeaders )
81
88
headers [ 'X-Request-LRD' ] = true
82
- if ( fileParams != null ) {
89
+ if ( fileParams ) {
83
90
delete headers [ 'Content-Type' ]
84
91
headers [ 'Accept' ] = 'multipart/form-data'
85
92
}
@@ -90,13 +97,11 @@ export default function ApiAction(props: Props) {
90
97
headers : headers ,
91
98
}
92
99
100
+
93
101
if ( method == 'POST' || method == 'PUT' || method == 'PATCH' ) {
94
102
try {
95
- JSON . parse ( bodyParams )
96
103
if ( fileParams != null ) {
97
- for ( const [ key , value ] of Object . entries ( JSON . parse ( bodyParams ) ) ) {
98
- fileParams . append ( key , value )
99
- }
104
+ objectToFormData ( JSON . parse ( bodyParams ) , fileParams as FormData )
100
105
}
101
106
102
107
} catch ( error : any ) {
@@ -201,10 +206,14 @@ export default function ApiAction(props: Props) {
201
206
let index = 0
202
207
for ( const [ key ] of Object . entries ( lrdDocsItem . rules ) ) {
203
208
index ++
209
+ const parts = key . split ( '.' ) ;
210
+ const queryKey = parts . reduce ( ( current : string , part : string ) => {
211
+ return current ? `${ current } [${ part !== '*' ? part : 0 } ]` : part
212
+ } , '' )
204
213
if ( index == 1 ) {
205
- queries += `?${ key } =\n`
214
+ queries += `?${ queryKey } =\n`
206
215
} else {
207
- queries += `&${ key } =\n`
216
+ queries += `&${ queryKey } =\n`
208
217
}
209
218
}
210
219
setQueryParams ( queries )
@@ -217,30 +226,39 @@ export default function ApiAction(props: Props) {
217
226
setCurlCommand ( makeCurlCommand ( host , lrdDocsItem . uri , method , cached , requestHeaders ) )
218
227
return
219
228
}
220
- const body : any = { }
221
- for ( const [ key , rule ] of Object . entries ( lrdDocsItem . rules ) ) {
229
+ const body : any = Object . entries ( lrdDocsItem . rules ) . reduce ( ( acc , [ key , rule ] ) => {
222
230
if ( rule . length == 0 ) {
223
- continue
231
+ return acc
224
232
}
225
- const theRule = rule [ 0 ] . split ( "|" )
226
- if ( theRule . includes ( 'file' ) || theRule . includes ( 'image' ) ) {
227
- continue
228
- }
229
- if ( key . includes ( ".*" ) ) {
230
- body [ key ] = [ ]
231
- continue
233
+
234
+ const ruleObj = rule [ 0 ] . split ( '|' ) ;
235
+
236
+ if ( ruleObj . includes ( 'file' ) || ruleObj . includes ( 'image' ) ) {
237
+ return acc
232
238
}
233
- if ( key . includes ( "." ) ) {
234
- const keys = key . split ( "." )
235
- if ( keys . length == 2 ) {
236
- body [ keys [ 0 ] ] = { }
237
- body [ keys [ 0 ] ] [ keys [ 1 ] ] = ""
239
+
240
+ const keys = key . split ( '.' ) ;
241
+ keys . reduce ( ( current : any , key , index ) => {
242
+ key = key === "*" ? "0" : key ;
243
+ if ( index === keys . length - 1 ) {
244
+ if ( ! isNaN ( Number ( key ) ) ) {
245
+ current = ! Array . isArray ( current ) ? [ ] : current ;
246
+ return current
247
+ }
248
+ current [ key ] = ruleObj . includes ( 'array' ) ? [ ] : "" ;
249
+ } else {
250
+ if ( ruleObj . includes ( 'array' ) || keys [ index + 1 ] === "*" || ! isNaN ( Number ( keys [ index + 1 ] ) ) ) {
251
+ current [ key ] = current [ key ] || [ ] ;
252
+ } else {
253
+ current [ key ] = current [ key ] || { } ;
254
+ }
238
255
}
239
- continue
240
- }
256
+ return current [ key ] ;
257
+ } , acc ) ;
258
+
259
+ return acc ;
260
+ } , { } )
241
261
242
- body [ key ] = ""
243
- }
244
262
const jsonBody = JSON . stringify ( body , null , 2 )
245
263
setBodyParams ( jsonBody )
246
264
setCurlCommand ( makeCurlCommand ( host , lrdDocsItem . uri , method , jsonBody , requestHeaders ) )
0 commit comments