- Notifications
You must be signed in to change notification settings - Fork254
[Feat]: Add Import from cURL in Query Library#1803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
0ae6c23
console log curl to json
iamfaran850ccda
edits in querycomp
iamfaran1e0295f
fix body issue
iamfarand8560aa
add global query library curl
iamfaran16da9fd
curl refactor
iamfaran12b4b41
[Feat] #1799 add simple examples on curl modal
iamfaranFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionsclient/packages/lowcoder/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletionsclient/packages/lowcoder/src/components/CurlImport.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import React, { useState } from "react"; | ||
import { Modal, Input, Button, message } from "antd"; | ||
import { trans } from "i18n"; | ||
import parseCurl from "@bany/curl-to-json"; | ||
const { TextArea } = Input; | ||
interface CurlImportModalProps { | ||
open: boolean; | ||
onCancel: () => void; | ||
onSuccess: (parsedData: any) => void; | ||
} | ||
export function CurlImportModal(props: CurlImportModalProps) { | ||
const { open, onCancel, onSuccess } = props; | ||
const [curlCommand, setCurlCommand] = useState(""); | ||
const [loading, setLoading] = useState(false); | ||
const handleImport = async () => { | ||
if (!curlCommand.trim()) { | ||
message.error("Please enter a cURL command"); | ||
return; | ||
} | ||
setLoading(true); | ||
try { | ||
// Parse the cURL command using the correct import | ||
const parsedData = parseCurl(curlCommand); | ||
// Log the result for now as requested | ||
// console.log("Parsed cURL data:", parsedData); | ||
// Call success callback with parsed data | ||
onSuccess(parsedData); | ||
// Reset form and close modal | ||
setCurlCommand(""); | ||
onCancel(); | ||
message.success("cURL command imported successfully!"); | ||
} catch (error: any) { | ||
console.error("Error parsing cURL command:", error); | ||
message.error(`Failed to parse cURL command: ${error.message}`); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
const handleCancel = () => { | ||
setCurlCommand(""); | ||
onCancel(); | ||
}; | ||
return ( | ||
<Modal | ||
title="Import from cURL" | ||
open={open} | ||
onCancel={handleCancel} | ||
footer={[ | ||
<Button key="cancel" onClick={handleCancel}> | ||
Cancel | ||
</Button>, | ||
<Button key="import" type="primary" loading={loading} onClick={handleImport}> | ||
Import | ||
</Button>, | ||
]} | ||
width={600} | ||
> | ||
<div style={{ marginBottom: 16 }}> | ||
<div style={{ marginBottom: 8, fontWeight: 500 }}> | ||
Paste cURL Command Here | ||
</div> | ||
<div style={{ marginBottom: 12, color: "#666", fontSize: "12px" }}> | ||
<div style={{ marginBottom: 4 }}> | ||
<strong>Examples:</strong> | ||
</div> | ||
<div style={{ marginBottom: 2 }}> | ||
GET: <code>curl -X GET https://jsonplaceholder.typicode.com/posts/1</code> | ||
</div> | ||
<div style={{ marginBottom: 2 }}> | ||
POST: <code>curl -X POST https://jsonplaceholder.typicode.com/posts -H "Content-Type: application/json" -d '{"title":"foo","body":"bar","userId":1}'</code> | ||
</div> | ||
<div> | ||
Users: <code>curl -X GET https://jsonplaceholder.typicode.com/users</code> | ||
</div> | ||
</div> | ||
<TextArea | ||
value={curlCommand} | ||
onChange={(e) => setCurlCommand(e.target.value)} | ||
placeholder="curl -X GET https://jsonplaceholder.typicode.com/posts/1" | ||
rows={8} | ||
style={{ fontFamily: "monospace" }} | ||
/> | ||
</div> | ||
</Modal> | ||
); | ||
} |
19 changes: 19 additions & 0 deletionsclient/packages/lowcoder/src/components/ResCreatePanel.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
43 changes: 34 additions & 9 deletionsclient/packages/lowcoder/src/comps/queries/queryComp.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
34 changes: 28 additions & 6 deletionsclient/packages/lowcoder/src/pages/queryLibrary/QueryLibraryEditor.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletionsclient/packages/lowcoder/src/util/curlUtils.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/** | ||
* Utility to convert parsed cURL data from @bany/curl-to-json library | ||
* to the format expected by REST API query components | ||
*/ | ||
// Body type mapping to match the dropdown values in httpQuery.tsx | ||
const CONTENT_TYPE_TO_BODY_TYPE: Record<string, string> = { | ||
"application/json": "application/json", | ||
"text/plain": "text/plain", | ||
"text/html": "text/plain", | ||
"text/xml": "text/plain", | ||
"application/xml": "text/plain", | ||
"application/x-www-form-urlencoded": "application/x-www-form-urlencoded", | ||
"multipart/form-data": "multipart/form-data", | ||
}; | ||
/** | ||
* Parse URL-encoded form data - handles both string and object input | ||
*/ | ||
function parseUrlEncodedData(data: string | object): Array<{ key: string; value: string; type: string }> { | ||
if (!data) { | ||
return [{ key: "", value: "", type: "text" }]; | ||
} | ||
try { | ||
let result: Array<{ key: string; value: string; type: string }> = []; | ||
if (typeof data === 'object') { | ||
// @bany/curl-to-json already parsed it into an object | ||
Object.entries(data).forEach(([key, value]) => { | ||
result.push({ | ||
key: key, | ||
value: decodeURIComponent(String(value).replace(/\+/g, ' ')), // Handle URL encoding | ||
type: "text" | ||
}); | ||
}); | ||
} else if (typeof data === 'string') { | ||
// Raw URL-encoded string - use URLSearchParams | ||
const params = new URLSearchParams(data); | ||
params.forEach((value, key) => { | ||
result.push({ | ||
key: key, | ||
value: value, | ||
type: "text" | ||
}); | ||
}); | ||
} | ||
return result.length > 0 ? result : [{ key: "", value: "", type: "text" }]; | ||
} catch (error) { | ||
console.warn('Failed to parse URL-encoded data:', error); | ||
return [{ key: "", value: "", type: "text" }]; | ||
} | ||
} | ||
export function processCurlData(curlData: any) { | ||
if (!curlData) return null; | ||
// Convert headers object to key-value array format expected by UI | ||
const headers = curlData.header | ||
? Object.entries(curlData.header).map(([key, value]) => ({ key, value })) | ||
: [{ key: "", value: "" }]; | ||
// Convert query params object to key-value array format expected by UI | ||
const params = curlData.params | ||
? Object.entries(curlData.params).map(([key, value]) => ({ key, value })) | ||
: [{ key: "", value: "" }]; | ||
// Get request body - @bany/curl-to-json may use 'body' or 'data' | ||
const bodyContent = curlData.body !== undefined ? curlData.body : curlData.data; | ||
// Determine body type based on Content-Type header or content structure | ||
let bodyType = "none"; | ||
let bodyFormData = [{ key: "", value: "", type: "text" }]; | ||
let processedBody = ""; | ||
if (bodyContent !== undefined && bodyContent !== "") { | ||
const contentTypeHeader = curlData.header?.["Content-Type"] || curlData.header?.["content-type"]; | ||
if (contentTypeHeader) { | ||
// Extract base content type (remove charset, boundary, etc.) | ||
const baseContentType = contentTypeHeader.split(';')[0].trim().toLowerCase(); | ||
bodyType = CONTENT_TYPE_TO_BODY_TYPE[baseContentType] || "text/plain"; | ||
} else { | ||
// Fallback: infer from content structure | ||
if (typeof bodyContent === "object") { | ||
bodyType = "application/json"; | ||
} else { | ||
bodyType = "text/plain"; | ||
} | ||
} | ||
// Handle different body types | ||
if (bodyType === "application/x-www-form-urlencoded") { | ||
bodyFormData = parseUrlEncodedData(bodyContent); | ||
processedBody = ""; // Form data goes in bodyFormData, not body | ||
} else if (typeof bodyContent === "object") { | ||
processedBody = JSON.stringify(bodyContent, null, 2); | ||
} else { | ||
processedBody = bodyContent; | ||
} | ||
} | ||
return { | ||
method: curlData.method || "GET", | ||
url: curlData.url || "", | ||
headers, | ||
params, | ||
bodyType, | ||
body: processedBody, | ||
bodyFormData, | ||
}; | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.