@@ -199,17 +199,70 @@ export const QueryLibraryEditor = () => {
199
199
const newName = nameGenerator . genItemName ( trans ( "queryLibrary.unnamed" ) ) ;
200
200
201
201
const handleAdd = ( type : BottomResTypeEnum , extraInfo ?: any ) => {
202
+ // Build basic query DSL
203
+ let queryDSL : any = {
204
+ triggerType : "manual" ,
205
+ datasourceId : extraInfo ?. dataSourceId ,
206
+ compType : extraInfo ?. compType ,
207
+ } ;
208
+
209
+ // If it is a REST API created from cURL, pre-populate the HTTP query fields
210
+ if ( extraInfo ?. compType === "restApi" && extraInfo ?. curlData ) {
211
+ const curlData = extraInfo . curlData ;
212
+
213
+ const rawHeaders : Record < string , any > | undefined =
214
+ curlData . header || curlData . headers ;
215
+ const rawParams : Record < string , any > | undefined =
216
+ curlData . params || curlData . parameters || curlData . query ;
217
+
218
+ const headersArr = rawHeaders
219
+ ? Object . entries ( rawHeaders ) . map ( ( [ key , value ] ) => ( { key, value } ) )
220
+ : [ { key : "" , value : "" } ] ;
221
+
222
+ const paramsArr = rawParams
223
+ ? Object . entries ( rawParams ) . map ( ( [ key , value ] ) => ( { key, value } ) )
224
+ : [ { key : "" , value : "" } ] ;
225
+
226
+ const bodyContent : any =
227
+ curlData . body ?? curlData . data ?? curlData . postData ?? undefined ;
228
+
229
+ let bodyType : string = "none" ;
230
+ if ( bodyContent !== undefined && bodyContent !== "" ) {
231
+ const contentTypeHeader =
232
+ ( rawHeaders && ( rawHeaders [ "Content-Type" ] || rawHeaders [ "content-type" ] ) ) || "" ;
233
+ if ( contentTypeHeader ) {
234
+ bodyType = contentTypeHeader ;
235
+ } else if ( typeof bodyContent === "object" ) {
236
+ bodyType = "application/json" ;
237
+ } else {
238
+ bodyType = "text/plain" ;
239
+ }
240
+ }
241
+
242
+ queryDSL = {
243
+ ...queryDSL ,
244
+ comp : {
245
+ httpMethod : curlData . method || "GET" ,
246
+ path : curlData . url || curlData . path || "" ,
247
+ headers : headersArr ,
248
+ params : paramsArr ,
249
+ bodyType : bodyType ,
250
+ body :
251
+ typeof bodyContent === "object"
252
+ ? JSON . stringify ( bodyContent , null , 2 )
253
+ : bodyContent || "" ,
254
+ bodyFormData : [ { key : "" , value : "" , type : "text" } ] ,
255
+ } ,
256
+ } ;
257
+ }
258
+
202
259
dispatch (
203
260
createQueryLibrary (
204
261
{
205
262
name : newName ,
206
263
organizationId : orgId ,
207
264
libraryQueryDSL : {
208
- query : {
209
- triggerType : "manual" ,
210
- datasourceId : extraInfo ?. dataSourceId ,
211
- compType : extraInfo ?. compType ,
212
- } ,
265
+ query : queryDSL ,
213
266
} ,
214
267
} ,
215
268
( resp ) => {
@@ -218,7 +271,6 @@ export const QueryLibraryEditor = () => {
218
271
setModify ( ! modify ) ;
219
272
} , 200 ) ;
220
273
setCurrentPage ( Math . ceil ( elements . total / pageSize ) ) ;
221
-
222
274
} ,
223
275
( ) => { }
224
276
)
0 commit comments