forked from extern/bruno
feat: support for sending xml and text request body (resolves #10)
This commit is contained in:
parent
6e926f0ba6
commit
7f0f496bb4
@ -31,7 +31,7 @@ export default class QueryEditor extends React.Component {
|
|||||||
lineNumbers: true,
|
lineNumbers: true,
|
||||||
lineWrapping: true,
|
lineWrapping: true,
|
||||||
tabSize: 2,
|
tabSize: 2,
|
||||||
mode: 'application/ld+json',
|
mode: this.props.mode || 'application/ld+json',
|
||||||
keyMap: 'sublime',
|
keyMap: 'sublime',
|
||||||
autoCloseBrackets: true,
|
autoCloseBrackets: true,
|
||||||
matchBrackets: true,
|
matchBrackets: true,
|
||||||
@ -89,6 +89,7 @@ export default class QueryEditor extends React.Component {
|
|||||||
) {
|
) {
|
||||||
this.cachedValue = this.props.value;
|
this.cachedValue = this.props.value;
|
||||||
this.editor.setValue(this.props.value);
|
this.editor.setValue(this.props.value);
|
||||||
|
this.editor.setOption("mode", this.props.mode);
|
||||||
}
|
}
|
||||||
this.ignoreChangeEvent = false;
|
this.ignoreChangeEvent = false;
|
||||||
}
|
}
|
||||||
|
@ -67,7 +67,7 @@ const HttpRequestPane = ({item, collection, leftPaneWidth}) => {
|
|||||||
{/* <div className={getTabClassname('auth')} role="tab" onClick={() => selectTab('auth')}>Auth</div> */}
|
{/* <div className={getTabClassname('auth')} role="tab" onClick={() => selectTab('auth')}>Auth</div> */}
|
||||||
{focusedTab.requestPaneTab === 'body' ? (
|
{focusedTab.requestPaneTab === 'body' ? (
|
||||||
<div className="flex flex-grow justify-end items-center">
|
<div className="flex flex-grow justify-end items-center">
|
||||||
<RequestBodyMode />
|
<RequestBodyMode item={item} collection={collection}/>
|
||||||
</div>
|
</div>
|
||||||
) : null }
|
) : null }
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,20 +1,35 @@
|
|||||||
import React, { useRef, forwardRef } from 'react';
|
import React, { useRef, forwardRef } from 'react';
|
||||||
|
import get from 'lodash/get';
|
||||||
import { IconCaretDown } from '@tabler/icons';
|
import { IconCaretDown } from '@tabler/icons';
|
||||||
import Dropdown from 'components/Dropdown';
|
import Dropdown from 'components/Dropdown';
|
||||||
|
import { useDispatch } from 'react-redux';
|
||||||
|
import { updateRequestBodyMode } from 'providers/ReduxStore/slices/collections';
|
||||||
|
import { humanizeRequestBodyMode } from 'utils/collections';
|
||||||
import StyledWrapper from './StyledWrapper';
|
import StyledWrapper from './StyledWrapper';
|
||||||
|
|
||||||
const RequestBodyMode = () => {
|
const RequestBodyMode = ({item, collection}) => {
|
||||||
|
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 Icon = forwardRef((props, ref) => {
|
const Icon = forwardRef((props, ref) => {
|
||||||
return (
|
return (
|
||||||
<div ref={ref} className="flex items-center justify-center pl-3 py-1 select-none">
|
<div ref={ref} className="flex items-center justify-center pl-3 py-1 select-none">
|
||||||
JSON <IconCaretDown className="caret ml-2 mr-2" size={14} strokeWidth={2}/>
|
{humanizeRequestBodyMode(bodyMode)} <IconCaretDown className="caret ml-2 mr-2" size={14} strokeWidth={2}/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const onModeChange = (value) => {
|
||||||
|
dispatch(updateRequestBodyMode({
|
||||||
|
itemUid: item.uid,
|
||||||
|
collectionUid: collection.uid,
|
||||||
|
mode: value
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
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">
|
||||||
@ -24,11 +39,13 @@ const RequestBodyMode = () => {
|
|||||||
</div>
|
</div>
|
||||||
<div className="dropdown-item" onClick={() => {
|
<div className="dropdown-item" onClick={() => {
|
||||||
dropdownTippyRef.current.hide();
|
dropdownTippyRef.current.hide();
|
||||||
|
onModeChange('multipartForm');
|
||||||
}}>
|
}}>
|
||||||
Multipart Form
|
Multipart Form
|
||||||
</div>
|
</div>
|
||||||
<div className="dropdown-item" onClick={() => {
|
<div className="dropdown-item" onClick={() => {
|
||||||
dropdownTippyRef.current.hide();
|
dropdownTippyRef.current.hide();
|
||||||
|
onModeChange('formUrlEncoded');
|
||||||
}}>
|
}}>
|
||||||
Form Url Encoded
|
Form Url Encoded
|
||||||
</div>
|
</div>
|
||||||
@ -37,16 +54,19 @@ const RequestBodyMode = () => {
|
|||||||
</div>
|
</div>
|
||||||
<div className="dropdown-item" onClick={() => {
|
<div className="dropdown-item" onClick={() => {
|
||||||
dropdownTippyRef.current.hide();
|
dropdownTippyRef.current.hide();
|
||||||
|
onModeChange('json');
|
||||||
}}>
|
}}>
|
||||||
JSON
|
JSON
|
||||||
</div>
|
</div>
|
||||||
<div className="dropdown-item" onClick={() => {
|
<div className="dropdown-item" onClick={() => {
|
||||||
dropdownTippyRef.current.hide();
|
dropdownTippyRef.current.hide();
|
||||||
|
onModeChange('xml');
|
||||||
}}>
|
}}>
|
||||||
XML
|
XML
|
||||||
</div>
|
</div>
|
||||||
<div className="dropdown-item" onClick={() => {
|
<div className="dropdown-item" onClick={() => {
|
||||||
dropdownTippyRef.current.hide();
|
dropdownTippyRef.current.hide();
|
||||||
|
onModeChange('text');
|
||||||
}}>
|
}}>
|
||||||
TEXT
|
TEXT
|
||||||
</div>
|
</div>
|
||||||
@ -55,6 +75,7 @@ const RequestBodyMode = () => {
|
|||||||
</div>
|
</div>
|
||||||
<div className="dropdown-item" onClick={() => {
|
<div className="dropdown-item" onClick={() => {
|
||||||
dropdownTippyRef.current.hide();
|
dropdownTippyRef.current.hide();
|
||||||
|
onModeChange('none');
|
||||||
}}>
|
}}>
|
||||||
No Body
|
No Body
|
||||||
</div>
|
</div>
|
||||||
|
@ -7,11 +7,11 @@ import StyledWrapper from './StyledWrapper';
|
|||||||
|
|
||||||
const RequestBody = ({item, collection}) => {
|
const RequestBody = ({item, collection}) => {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const bodyContent = item.draft ? get(item, 'draft.request.body.content') : get(item, 'request.body.content');
|
const body = item.draft ? get(item, 'draft.request.body') : get(item, 'request.body');
|
||||||
|
const bodyMode = item.draft ? get(item, 'draft.request.body.mode') : get(item, 'request.body.mode');
|
||||||
|
|
||||||
const onEdit = (value) => {
|
const onEdit = (value) => {
|
||||||
dispatch(updateRequestBody({
|
dispatch(updateRequestBody({
|
||||||
mode: 'json',
|
|
||||||
content: value,
|
content: value,
|
||||||
itemUid: item.uid,
|
itemUid: item.uid,
|
||||||
collectionUid: collection.uid,
|
collectionUid: collection.uid,
|
||||||
@ -19,11 +19,37 @@ const RequestBody = ({item, collection}) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const onRun = () => dispatch(sendRequest(item, collection.uid));;
|
const onRun = () => dispatch(sendRequest(item, collection.uid));;
|
||||||
const onSave = () => dispatch(saveRequest(item.uid, collection.uid));;
|
const onSave = () => dispatch(saveRequest(item.uid, collection.uid));
|
||||||
|
|
||||||
|
if(['json', 'xml', 'text'].includes(bodyMode)) {
|
||||||
|
let codeMirrorMode = {
|
||||||
|
json: 'application/ld+json',
|
||||||
|
text: 'application/text',
|
||||||
|
xml: 'application/xml'
|
||||||
|
};
|
||||||
|
|
||||||
|
let bodyContent = {
|
||||||
|
json: body.json,
|
||||||
|
text: body.text,
|
||||||
|
xml: body.xml
|
||||||
|
};
|
||||||
|
|
||||||
|
return(
|
||||||
|
<StyledWrapper className="w-full">
|
||||||
|
<CodeEditor
|
||||||
|
value={bodyContent[bodyMode] || ''}
|
||||||
|
onEdit={onEdit}
|
||||||
|
onRun={onRun}
|
||||||
|
onSave={onSave}
|
||||||
|
mode={codeMirrorMode[bodyMode]}
|
||||||
|
/>
|
||||||
|
</StyledWrapper>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return(
|
return(
|
||||||
<StyledWrapper className="w-full">
|
<StyledWrapper className="w-full">
|
||||||
<CodeEditor value={bodyContent || ''} onEdit={onEdit} onRun={onRun} onSave={onSave}/>
|
No Body
|
||||||
</StyledWrapper>
|
</StyledWrapper>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -9,7 +9,7 @@ import StyledWrapper from './StyledWrapper';
|
|||||||
const SERVER_RENDERED = typeof navigator === 'undefined' || global['PREVENT_CODEMIRROR_RENDER'] === true;
|
const SERVER_RENDERED = typeof navigator === 'undefined' || global['PREVENT_CODEMIRROR_RENDER'] === true;
|
||||||
if(!SERVER_RENDERED) {
|
if(!SERVER_RENDERED) {
|
||||||
require('codemirror/mode/javascript/javascript');
|
require('codemirror/mode/javascript/javascript');
|
||||||
require('codemirror/mode/javascript/javascript');
|
require('codemirror/mode/xml/xml');
|
||||||
require('codemirror/addon/edit/matchbrackets');
|
require('codemirror/addon/edit/matchbrackets');
|
||||||
require('codemirror/addon/fold/brace-fold');
|
require('codemirror/addon/fold/brace-fold');
|
||||||
require('codemirror/addon/fold/foldgutter');
|
require('codemirror/addon/fold/foldgutter');
|
||||||
|
@ -343,6 +343,20 @@ export const collectionsSlice = createSlice({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
updateRequestBodyMode: (state, action) => {
|
||||||
|
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
|
||||||
|
|
||||||
|
if(collection) {
|
||||||
|
const item = findItemInCollection(collection, action.payload.itemUid);
|
||||||
|
|
||||||
|
if(item && isItemARequest(item)) {
|
||||||
|
if(!item.draft) {
|
||||||
|
item.draft = cloneDeep(item);
|
||||||
|
}
|
||||||
|
item.draft.request.body.mode = action.payload.mode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
updateRequestBody: (state, action) => {
|
updateRequestBody: (state, action) => {
|
||||||
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
|
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
|
||||||
|
|
||||||
@ -353,9 +367,27 @@ export const collectionsSlice = createSlice({
|
|||||||
if(!item.draft) {
|
if(!item.draft) {
|
||||||
item.draft = cloneDeep(item);
|
item.draft = cloneDeep(item);
|
||||||
}
|
}
|
||||||
item.draft.request.body = {
|
switch(item.draft.request.body.mode) {
|
||||||
mode: action.payload.mode,
|
case 'json': {
|
||||||
content: action.payload.content
|
item.draft.request.body.json = action.payload.content;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'text': {
|
||||||
|
item.draft.request.body.text = action.payload.content;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'xml': {
|
||||||
|
item.draft.request.body.xml = action.payload.content;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'formUrlEncoded': {
|
||||||
|
item.draft.request.body.formUrlEncoded = action.payload.content;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'multipartForm': {
|
||||||
|
item.draft.request.body.multipartForm = action.payload.content;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -397,6 +429,7 @@ export const {
|
|||||||
addRequestHeader,
|
addRequestHeader,
|
||||||
updateRequestHeader,
|
updateRequestHeader,
|
||||||
deleteRequestHeader,
|
deleteRequestHeader,
|
||||||
|
updateRequestBodyMode,
|
||||||
updateRequestBody,
|
updateRequestBody,
|
||||||
updateRequestMethod
|
updateRequestMethod
|
||||||
} = collectionsSlice.actions;
|
} = collectionsSlice.actions;
|
||||||
@ -520,7 +553,11 @@ export const newHttpRequest = (params) => (dispatch, getState) => {
|
|||||||
headers: [],
|
headers: [],
|
||||||
body: {
|
body: {
|
||||||
mode: 'none',
|
mode: 'none',
|
||||||
content: ''
|
json: null,
|
||||||
|
text: null,
|
||||||
|
xml: null,
|
||||||
|
multipartForm: null,
|
||||||
|
formUrlEncoded: null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -147,7 +147,11 @@ export const transformCollectionToSaveToIdb = (collection, options = {}) => {
|
|||||||
params: copyQueryParams(si.draft.request.params),
|
params: copyQueryParams(si.draft.request.params),
|
||||||
body: {
|
body: {
|
||||||
mode: si.draft.request.body.mode,
|
mode: si.draft.request.body.mode,
|
||||||
content: replaceTabsWithSpaces(si.draft.request.body.content)
|
json: si.draft.request.body.json,
|
||||||
|
text: si.draft.request.body.text,
|
||||||
|
xml: si.draft.request.body.xml,
|
||||||
|
multipartForm: si.draft.request.body.multipartForm,
|
||||||
|
xmformUrlEncodedl: si.draft.request.body.formUrlEncoded
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -160,14 +164,18 @@ export const transformCollectionToSaveToIdb = (collection, options = {}) => {
|
|||||||
params: copyQueryParams(si.request.params),
|
params: copyQueryParams(si.request.params),
|
||||||
body: {
|
body: {
|
||||||
mode: si.request.body.mode,
|
mode: si.request.body.mode,
|
||||||
content: replaceTabsWithSpaces(si.request.body.content)
|
json: si.request.body.json,
|
||||||
|
text: si.request.body.text,
|
||||||
|
xml: si.request.body.xml,
|
||||||
|
multipartForm: si.request.body.multipartForm,
|
||||||
|
xmformUrlEncodedl: si.request.body.formUrlEncoded
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if(di.request && di.request.body.mode === 'json') {
|
if(di.request && di.request.body.mode === 'json') {
|
||||||
di.request.body.content = replaceTabsWithSpaces(di.request.body.content);
|
di.request.body.json = replaceTabsWithSpaces(di.request.body.json);
|
||||||
}
|
}
|
||||||
|
|
||||||
destItems.push(di);
|
destItems.push(di);
|
||||||
@ -214,3 +222,31 @@ export const isItemARequest = (item) => {
|
|||||||
export const isItemAFolder = (item) => {
|
export const isItemAFolder = (item) => {
|
||||||
return !item.hasOwnProperty('request') && item.type === 'folder';
|
return !item.hasOwnProperty('request') && item.type === 'folder';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const humanizeRequestBodyMode = (mode) => {
|
||||||
|
let label = 'No Body';
|
||||||
|
switch(mode) {
|
||||||
|
case 'json': {
|
||||||
|
label = 'JSON';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'text': {
|
||||||
|
label = 'TEXT';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'xml': {
|
||||||
|
label = 'XML';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'formUrlEncoded': {
|
||||||
|
label = 'Form Url Encoded';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'multipartForm': {
|
||||||
|
label = 'Multipart Form';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return label;
|
||||||
|
};
|
||||||
|
@ -39,9 +39,26 @@ const sendHttpRequest = async (request) => {
|
|||||||
headers: headers
|
headers: headers
|
||||||
};
|
};
|
||||||
|
|
||||||
if(request.body && request.body.mode === 'json' && request.body.content) {
|
if(request.body.mode === 'json') {
|
||||||
options.data = JSON.parse(request.body.content);
|
options.headers['Content-Type'] = 'application/json';
|
||||||
|
try {
|
||||||
|
options.data = JSON.parse(request.body.json);
|
||||||
|
} catch (ex) {
|
||||||
|
options.data = request.body.json;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(request.body.mode === 'text') {
|
||||||
|
options.headers['Content-Type'] = 'text/plain';
|
||||||
|
options.data = request.body.text;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(request.body.mode === 'xml') {
|
||||||
|
options.headers['Content-Type'] = 'text/xml';
|
||||||
|
options.data = request.body.xml;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('>>> Sending Request');
|
||||||
console.log(request);
|
console.log(request);
|
||||||
|
|
||||||
ipcRenderer
|
ipcRenderer
|
||||||
|
Loading…
Reference in New Issue
Block a user