1+ /**
2+ * Utility to convert parsed cURL data from @bany/curl-to-json library
3+ * to the format expected by REST API query components
4+ */
5+
6+ // Body type mapping to match the dropdown values in httpQuery.tsx
7+ const CONTENT_TYPE_TO_BODY_TYPE : Record < string , string > = {
8+ "application/json" : "application/json" ,
9+ "text/plain" : "text/plain" ,
10+ "text/html" : "text/plain" ,
11+ "text/xml" : "text/plain" ,
12+ "application/xml" : "text/plain" ,
13+ "application/x-www-form-urlencoded" : "application/x-www-form-urlencoded" ,
14+ "multipart/form-data" : "multipart/form-data" ,
15+ } ;
16+
17+ /**
18+ * Parse URL-encoded form data - handles both string and object input
19+ */
20+ function parseUrlEncodedData ( data : string | object ) : Array < { key : string ; value : string ; type : string } > {
21+ if ( ! data ) {
22+ return [ { key : "" , value : "" , type : "text" } ] ;
23+ }
24+
25+ try {
26+ let result : Array < { key : string ; value : string ; type : string } > = [ ] ;
27+
28+ if ( typeof data === 'object' ) {
29+ // @bany /curl-to-json already parsed it into an object
30+ Object . entries ( data ) . forEach ( ( [ key , value ] ) => {
31+ result . push ( {
32+ key : key ,
33+ value : decodeURIComponent ( String ( value ) . replace ( / \+ / g, ' ' ) ) , // Handle URL encoding
34+ type : "text"
35+ } ) ;
36+ } ) ;
37+ } else if ( typeof data === 'string' ) {
38+ // Raw URL-encoded string - use URLSearchParams
39+ const params = new URLSearchParams ( data ) ;
40+ params . forEach ( ( value , key ) => {
41+ result . push ( {
42+ key : key ,
43+ value : value ,
44+ type : "text"
45+ } ) ;
46+ } ) ;
47+ }
48+
49+ return result . length > 0 ? result : [ { key : "" , value : "" , type : "text" } ] ;
50+ } catch ( error ) {
51+ console . warn ( 'Failed to parse URL-encoded data:' , error ) ;
52+ return [ { key : "" , value : "" , type : "text" } ] ;
53+ }
54+ }
55+
56+ export function processCurlData ( curlData : any ) {
57+ if ( ! curlData ) return null ;
58+
59+
60+ // Convert headers object to key-value array format expected by UI
61+ const headers = curlData . header
62+ ? Object . entries ( curlData . header ) . map ( ( [ key , value ] ) => ( { key, value } ) )
63+ : [ { key : "" , value : "" } ] ;
64+
65+ // Convert query params object to key-value array format expected by UI
66+ const params = curlData . params
67+ ? Object . entries ( curlData . params ) . map ( ( [ key , value ] ) => ( { key, value } ) )
68+ : [ { key : "" , value : "" } ] ;
69+
70+ // Get request body - @bany/curl-to-json may use 'body' or 'data'
71+ const bodyContent = curlData . body !== undefined ? curlData . body : curlData . data ;
72+
73+ // Determine body type based on Content-Type header or content structure
74+ let bodyType = "none" ;
75+ let bodyFormData = [ { key : "" , value : "" , type : "text" } ] ;
76+ let processedBody = "" ;
77+
78+ if ( bodyContent !== undefined && bodyContent !== "" ) {
79+ const contentTypeHeader = curlData . header ?. [ "Content-Type" ] || curlData . header ?. [ "content-type" ] ;
80+
81+ if ( contentTypeHeader ) {
82+ // Extract base content type (remove charset, boundary, etc.)
83+ const baseContentType = contentTypeHeader . split ( ';' ) [ 0 ] . trim ( ) . toLowerCase ( ) ;
84+ bodyType = CONTENT_TYPE_TO_BODY_TYPE [ baseContentType ] || "text/plain" ;
85+ } else {
86+ // Fallback: infer from content structure
87+ if ( typeof bodyContent === "object" ) {
88+ bodyType = "application/json" ;
89+ } else {
90+ bodyType = "text/plain" ;
91+ }
92+ }
93+
94+ // Handle different body types
95+ if ( bodyType === "application/x-www-form-urlencoded" ) {
96+ bodyFormData = parseUrlEncodedData ( bodyContent ) ;
97+ processedBody = "" ; // Form data goes in bodyFormData, not body
98+ } else if ( typeof bodyContent === "object" ) {
99+ processedBody = JSON . stringify ( bodyContent , null , 2 ) ;
100+ } else {
101+ processedBody = bodyContent ;
102+ }
103+ }
104+
105+ return {
106+ method : curlData . method || "GET" ,
107+ url : curlData . url || "" ,
108+ headers,
109+ params,
110+ bodyType,
111+ body : processedBody ,
112+ bodyFormData,
113+ } ;
114+ }
0 commit comments