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() : '';
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,7 +72,7 @@ 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) =>
const request = getRequestFromCurlCommand(values.curlCommand);
dispatch(
newHttpRequest({
requestName: values.requestName,
@ -78,8 +86,7 @@ const NewRequest = ({ collection, item, isEphemeral, 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 {
dispatch(
newHttpRequest({
@ -227,6 +234,9 @@ const NewRequest = ({ collection, item, isEphemeral, onClose }) => {
value={formik.values.curlCommand}
onChange={formik.handleChange}
></textarea>
{formik.touched.curlCommand && formik.errors.curlCommand ? (
<div className="text-red-500">{formik.errors.curlCommand}</div>
) : null}
</div>
)}
</form>

View File

@ -6,13 +6,11 @@ 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 }));
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 = {
@ -50,16 +48,13 @@ export const getRequestFromCurlCommand = (command) => {
break;
}
}
debugger;
console.log(request);
return resolve({
return {
url: request.url,
method: request.method,
body,
headers: headers
});
} catch (error) {
return reject(new BrunoError('Unable to parse the cURL command'));
}
});
};
} catch (error) {
return null;
}
};