mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-07 16:44:27 +01:00
fix: validate curl command before submit
This commit is contained in:
parent
bbcab8d338
commit
dbd3ab6064
@ -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}
|
||||
></textarea>
|
||||
{formik.touched.curlCommand && formik.errors.curlCommand ? (
|
||||
<div className="text-red-500">{formik.errors.curlCommand}</div>
|
||||
) : null}
|
||||
</div>
|
||||
)}
|
||||
</form>
|
||||
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user