mirror of
https://github.com/usebruno/bruno.git
synced 2025-06-25 14:31:44 +02:00
feat: add json body prettify
This commit is contained in:
parent
a9459bc236
commit
6e7fc2a9aa
@ -6,12 +6,15 @@ import { useDispatch } from 'react-redux';
|
|||||||
import { updateRequestBodyMode } from 'providers/ReduxStore/slices/collections';
|
import { updateRequestBodyMode } from 'providers/ReduxStore/slices/collections';
|
||||||
import { humanizeRequestBodyMode } from 'utils/collections';
|
import { humanizeRequestBodyMode } from 'utils/collections';
|
||||||
import StyledWrapper from './StyledWrapper';
|
import StyledWrapper from './StyledWrapper';
|
||||||
|
import { updateRequestBody } from 'providers/ReduxStore/slices/collections/index';
|
||||||
|
import { toastError } from 'utils/common/error';
|
||||||
|
|
||||||
const RequestBodyMode = ({ item, collection }) => {
|
const RequestBodyMode = ({ item, collection }) => {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const dropdownTippyRef = useRef();
|
const dropdownTippyRef = useRef();
|
||||||
const onDropdownCreate = (ref) => (dropdownTippyRef.current = ref);
|
const onDropdownCreate = (ref) => (dropdownTippyRef.current = ref);
|
||||||
const bodyMode = item.draft ? get(item, 'draft.request.body.mode') : get(item, 'request.body.mode');
|
const body = item.draft ? get(item, 'draft.request.body') : get(item, 'request.body');
|
||||||
|
const bodyMode = body?.mode;
|
||||||
|
|
||||||
const Icon = forwardRef((props, ref) => {
|
const Icon = forwardRef((props, ref) => {
|
||||||
return (
|
return (
|
||||||
@ -31,6 +34,24 @@ const RequestBodyMode = ({ item, collection }) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const onPrettify = () => {
|
||||||
|
if (body?.json && bodyMode === 'json') {
|
||||||
|
try {
|
||||||
|
const bodyJson = JSON.parse(body.json);
|
||||||
|
const prettyBodyJson = JSON.stringify(bodyJson, null, 2);
|
||||||
|
dispatch(
|
||||||
|
updateRequestBody({
|
||||||
|
content: prettyBodyJson,
|
||||||
|
itemUid: item.uid,
|
||||||
|
collectionUid: collection.uid
|
||||||
|
})
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
toastError(new Error('Unable to prettify. Invalid JSON format.'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<StyledWrapper>
|
<StyledWrapper>
|
||||||
<div className="inline-flex items-center cursor-pointer body-mode-selector">
|
<div className="inline-flex items-center cursor-pointer body-mode-selector">
|
||||||
@ -103,6 +124,11 @@ const RequestBodyMode = ({ item, collection }) => {
|
|||||||
</div>
|
</div>
|
||||||
</Dropdown>
|
</Dropdown>
|
||||||
</div>
|
</div>
|
||||||
|
{bodyMode === 'json' && (
|
||||||
|
<button className="ml-1" onClick={onPrettify}>
|
||||||
|
Prettify
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
</StyledWrapper>
|
</StyledWrapper>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user