XML Format working

This commit is contained in:
pedward99 2023-09-25 13:13:14 -04:00
commit 9c8ef09d01
4 changed files with 32 additions and 5 deletions

View File

@ -38,5 +38,8 @@
}, },
"overrides": { "overrides": {
"rollup": "3.2.5" "rollup": "3.2.5"
},
"dependencies": {
"xml-formatter": "^3.3.2"
} }
} }

View File

@ -3,6 +3,7 @@ import CodeEditor from 'components/CodeEditor';
import { useTheme } from 'providers/Theme'; import { useTheme } from 'providers/Theme';
import { useDispatch } from 'react-redux'; import { useDispatch } from 'react-redux';
import { sendRequest } from 'providers/ReduxStore/slices/collections/actions'; import { sendRequest } from 'providers/ReduxStore/slices/collections/actions';
import { getContentTypeHeader } from 'utils/common';
import StyledWrapper from './StyledWrapper'; import StyledWrapper from './StyledWrapper';
@ -16,7 +17,12 @@ const QueryResult = ({ item, collection, value, width, disableRunEventListener,
} }
dispatch(sendRequest(item, collection.uid)); dispatch(sendRequest(item, collection.uid));
}; };
/*var responseType = getContentTypeHeader(item.response.headers);
let mode = 'application/json';//TODO: What to default??? json probbaly
if(responseType.includes("xml")){
mode = "application/xml";
}*/
console.log('Mode:' + mode);
return ( return (
<StyledWrapper className="px-3 w-full" style={{ maxWidth: width }}> <StyledWrapper className="px-3 w-full" style={{ maxWidth: width }}>
<div className="h-full"> <div className="h-full">

View File

@ -1,8 +1,8 @@
import React from 'react'; import React from 'react';
import find from 'lodash/find'; import find from 'lodash/find';
import classnames from 'classnames'; import classnames from 'classnames';
import { safeStringifyJSON } from 'utils/common';
import { useDispatch, useSelector } from 'react-redux'; import { useDispatch, useSelector } from 'react-redux';
import { formatResponse } from 'utils/common';
import { updateResponsePaneTab } from 'providers/ReduxStore/slices/tabs'; import { updateResponsePaneTab } from 'providers/ReduxStore/slices/tabs';
import QueryResult from './QueryResult'; import QueryResult from './QueryResult';
import Overlay from './Overlay'; import Overlay from './Overlay';
@ -41,9 +41,7 @@ const ResponsePane = ({ rightPaneWidth, item, collection }) => {
item={item} item={item}
collection={collection} collection={collection}
width={rightPaneWidth} width={rightPaneWidth}
value={ value={response.data ? formatResponse(response) : ''}
response.data ? (isJson(response.headers) ? safeStringifyJSON(response.data, true) : response.data) : ''
}
mode={getContentType(response.headers)} mode={getContentType(response.headers)}
/> />
); );

View File

@ -1,4 +1,5 @@
import { customAlphabet } from 'nanoid'; import { customAlphabet } from 'nanoid';
import xmlFormat from 'xml-formatter';
// a customized version of nanoid without using _ and - // a customized version of nanoid without using _ and -
export const uuid = () => { export const uuid = () => {
@ -50,6 +51,25 @@ export const safeStringifyJSON = (obj, indent = false) => {
} }
}; };
export const getContentTypeHeader = (headers) => {
let header = headers.find((element) => element[0] == 'content-type');
if (header && header[1]) {
return header[1];
}
return '';
};
export const formatResponse = (response) => {
var type = getContentTypeHeader(response.headers);
if (type.includes('json')) {
return safeStringifyJSON(response.data);
}
if (type.includes('xml')) {
return xmlFormat(response.data, { collapseContent: true });
}
return response.data;
};
// Remove any characters that are not alphanumeric, spaces, hyphens, or underscores // Remove any characters that are not alphanumeric, spaces, hyphens, or underscores
export const normalizeFileName = (name) => { export const normalizeFileName = (name) => {
if (!name) { if (!name) {