diff --git a/packages/bruno-app/src/components/Sidebar/NewRequest/index.js b/packages/bruno-app/src/components/Sidebar/NewRequest/index.js index 1476a7de8..f6bcdfeb0 100644 --- a/packages/bruno-app/src/components/Sidebar/NewRequest/index.js +++ b/packages/bruno-app/src/components/Sidebar/NewRequest/index.js @@ -37,6 +37,14 @@ const NewRequest = ({ collection, item, isEphemeral, onClose }) => { const trimmedValue = value ? value.trim().toLowerCase() : ''; return !['collection', 'folder'].includes(trimmedValue); } + }), + curlCommand: Yup.string() + .min(1, 'must be at least 1 character') + .required('curlCommand is required') + .test({ + name: 'curlCommand', + message: `Invalid cURL Command`, + test: (value) => getRequestFromCurlCommand(value) !== null }) }), onSubmit: (values) => { @@ -64,22 +72,21 @@ const NewRequest = ({ collection, item, isEphemeral, onClose }) => { }) .catch((err) => toast.error(err ? err.message : 'An error occurred while adding the request')); } else if (values.requestType === 'from-curl') { - getRequestFromCurlCommand(values.curlCommand).then((request) => - dispatch( - newHttpRequest({ - requestName: values.requestName, - requestType: 'http-request', - requestUrl: request.url, - requestMethod: request.method, - collectionUid: collection.uid, - itemUid: item ? item.uid : null, - headers: request.headers, - body: request.body - }) - ) - .then(() => onClose()) - .catch((err) => toast.error(err ? err.message : 'An error occurred while adding the request')) - ); + const request = getRequestFromCurlCommand(values.curlCommand); + dispatch( + newHttpRequest({ + requestName: values.requestName, + requestType: 'http-request', + requestUrl: request.url, + requestMethod: request.method, + collectionUid: collection.uid, + itemUid: item ? item.uid : null, + headers: request.headers, + body: request.body + }) + ) + .then(() => onClose()) + .catch((err) => toast.error(err ? err.message : 'An error occurred while adding the request')); } else { dispatch( newHttpRequest({ @@ -227,6 +234,9 @@ const NewRequest = ({ collection, item, isEphemeral, onClose }) => { value={formik.values.curlCommand} onChange={formik.handleChange} > + {formik.touched.curlCommand && formik.errors.curlCommand ? ( +
{formik.errors.curlCommand}
+ ) : null} )} diff --git a/packages/bruno-app/src/utils/curl/index.js b/packages/bruno-app/src/utils/curl/index.js index 37282f09c..30080e467 100644 --- a/packages/bruno-app/src/utils/curl/index.js +++ b/packages/bruno-app/src/utils/curl/index.js @@ -6,60 +6,55 @@ export const getRequestFromCurlCommand = (command) => { const parseFormData = (parsedBody) => { parseQueryParams(parsedBody); }; - return new Promise((resolve, reject) => { - try { - const request = parse(command); - const parsedHeader = request?.header; - const headers = - parsedHeader && - Object.keys(parsedHeader).map((key) => ({ name: key, value: parsedHeader[key], enabled: true })); + try { + const request = parse(command); + const parsedHeader = request?.header; + const headers = + parsedHeader && Object.keys(parsedHeader).map((key) => ({ name: key, value: parsedHeader[key], enabled: true })); - const contentType = headers?.find((h) => h.name.toLowerCase() === 'content-type'); - const body = { - mode: 'none', - json: null, - text: null, - xml: null, - sparql: null, - multipartForm: null, - formUrlEncoded: null - }; - const parsedBody = request?.body; - if (parsedBody && contentType) { - switch (contentType.value.toLowerCase()) { - case 'application/json': - body.mode = 'json'; - body.json = parsedBody; - break; - case 'text/xml': - body.mode = 'xml'; - body.xml = parsedBody; - break; - case 'application/x-www-form-urlencoded': - body.mode = 'formUrlEncoded'; - body.formUrlEncoded = parseFormData(parsedBody); - break; - case 'multipart/form-data': - body.mode = 'multipartForm'; - body.multipartForm = parsedBody; - break; - case 'text/plain': - default: - body.mode = 'text'; - body.text = parsedBody; - break; - } + const contentType = headers?.find((h) => h.name.toLowerCase() === 'content-type'); + const body = { + mode: 'none', + json: null, + text: null, + xml: null, + sparql: null, + multipartForm: null, + formUrlEncoded: null + }; + const parsedBody = request?.body; + if (parsedBody && contentType) { + switch (contentType.value.toLowerCase()) { + case 'application/json': + body.mode = 'json'; + body.json = parsedBody; + break; + case 'text/xml': + body.mode = 'xml'; + body.xml = parsedBody; + break; + case 'application/x-www-form-urlencoded': + body.mode = 'formUrlEncoded'; + body.formUrlEncoded = parseFormData(parsedBody); + break; + case 'multipart/form-data': + body.mode = 'multipartForm'; + body.multipartForm = parsedBody; + break; + case 'text/plain': + default: + body.mode = 'text'; + body.text = parsedBody; + break; } - debugger; - console.log(request); - return resolve({ - url: request.url, - method: request.method, - body, - headers: headers - }); - } catch (error) { - return reject(new BrunoError('Unable to parse the cURL command')); } - }); + return { + url: request.url, + method: request.method, + body, + headers: headers + }; + } catch (error) { + return null; + } };