fix: validate curl command before submit

This commit is contained in:
Dipin Jagadish 2023-11-01 10:58:07 +00:00
parent bbcab8d338
commit dbd3ab6064
2 changed files with 74 additions and 69 deletions

View File

@ -37,6 +37,14 @@ const NewRequest = ({ collection, item, isEphemeral, onClose }) => {
const trimmedValue = value ? value.trim().toLowerCase() : ''; const trimmedValue = value ? value.trim().toLowerCase() : '';
return !['collection', 'folder'].includes(trimmedValue); 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) => { 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')); .catch((err) => toast.error(err ? err.message : 'An error occurred while adding the request'));
} else if (values.requestType === 'from-curl') { } else if (values.requestType === 'from-curl') {
getRequestFromCurlCommand(values.curlCommand).then((request) => const request = getRequestFromCurlCommand(values.curlCommand);
dispatch( dispatch(
newHttpRequest({ newHttpRequest({
requestName: values.requestName, requestName: values.requestName,
requestType: 'http-request', requestType: 'http-request',
requestUrl: request.url, requestUrl: request.url,
requestMethod: request.method, requestMethod: request.method,
collectionUid: collection.uid, collectionUid: collection.uid,
itemUid: item ? item.uid : null, itemUid: item ? item.uid : null,
headers: request.headers, headers: request.headers,
body: request.body body: request.body
}) })
) )
.then(() => onClose()) .then(() => onClose())
.catch((err) => toast.error(err ? err.message : 'An error occurred while adding the request')) .catch((err) => toast.error(err ? err.message : 'An error occurred while adding the request'));
);
} else { } else {
dispatch( dispatch(
newHttpRequest({ newHttpRequest({
@ -227,6 +234,9 @@ const NewRequest = ({ collection, item, isEphemeral, onClose }) => {
value={formik.values.curlCommand} value={formik.values.curlCommand}
onChange={formik.handleChange} onChange={formik.handleChange}
></textarea> ></textarea>
{formik.touched.curlCommand && formik.errors.curlCommand ? (
<div className="text-red-500">{formik.errors.curlCommand}</div>
) : null}
</div> </div>
)} )}
</form> </form>

View File

@ -6,60 +6,55 @@ export const getRequestFromCurlCommand = (command) => {
const parseFormData = (parsedBody) => { const parseFormData = (parsedBody) => {
parseQueryParams(parsedBody); parseQueryParams(parsedBody);
}; };
return new Promise((resolve, reject) => { try {
try { const request = parse(command);
const request = parse(command); const parsedHeader = request?.header;
const parsedHeader = request?.header; const headers =
const headers = parsedHeader && Object.keys(parsedHeader).map((key) => ({ name: key, value: parsedHeader[key], enabled: true }));
parsedHeader &&
Object.keys(parsedHeader).map((key) => ({ name: key, value: parsedHeader[key], enabled: true }));
const contentType = headers?.find((h) => h.name.toLowerCase() === 'content-type'); const contentType = headers?.find((h) => h.name.toLowerCase() === 'content-type');
const body = { const body = {
mode: 'none', mode: 'none',
json: null, json: null,
text: null, text: null,
xml: null, xml: null,
sparql: null, sparql: null,
multipartForm: null, multipartForm: null,
formUrlEncoded: null formUrlEncoded: null
}; };
const parsedBody = request?.body; const parsedBody = request?.body;
if (parsedBody && contentType) { if (parsedBody && contentType) {
switch (contentType.value.toLowerCase()) { switch (contentType.value.toLowerCase()) {
case 'application/json': case 'application/json':
body.mode = 'json'; body.mode = 'json';
body.json = parsedBody; body.json = parsedBody;
break; break;
case 'text/xml': case 'text/xml':
body.mode = 'xml'; body.mode = 'xml';
body.xml = parsedBody; body.xml = parsedBody;
break; break;
case 'application/x-www-form-urlencoded': case 'application/x-www-form-urlencoded':
body.mode = 'formUrlEncoded'; body.mode = 'formUrlEncoded';
body.formUrlEncoded = parseFormData(parsedBody); body.formUrlEncoded = parseFormData(parsedBody);
break; break;
case 'multipart/form-data': case 'multipart/form-data':
body.mode = 'multipartForm'; body.mode = 'multipartForm';
body.multipartForm = parsedBody; body.multipartForm = parsedBody;
break; break;
case 'text/plain': case 'text/plain':
default: default:
body.mode = 'text'; body.mode = 'text';
body.text = parsedBody; body.text = parsedBody;
break; 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;
}
}; };