This commit is contained in:
pedward99 2023-09-26 09:22:41 -04:00
parent 9c8ef09d01
commit b814c84411
3 changed files with 31 additions and 45 deletions

View File

@ -3,7 +3,6 @@ import CodeEditor from 'components/CodeEditor';
import { useTheme } from 'providers/Theme';
import { useDispatch } from 'react-redux';
import { sendRequest } from 'providers/ReduxStore/slices/collections/actions';
import { getContentTypeHeader } from 'utils/common';
import StyledWrapper from './StyledWrapper';
@ -17,12 +16,7 @@ const QueryResult = ({ item, collection, value, width, disableRunEventListener,
}
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 (
<StyledWrapper className="px-3 w-full" style={{ maxWidth: width }}>
<div className="h-full">

View File

@ -2,7 +2,7 @@ import React from 'react';
import find from 'lodash/find';
import classnames from 'classnames';
import { useDispatch, useSelector } from 'react-redux';
import { formatResponse } from 'utils/common';
import { getContentType, formatResponse } from 'utils/common';
import { updateResponsePaneTab } from 'providers/ReduxStore/slices/tabs';
import QueryResult from './QueryResult';
import Overlay from './Overlay';
@ -93,24 +93,6 @@ const ResponsePane = ({ rightPaneWidth, item, collection }) => {
});
};
const getContentType = (headers) => {
if (headers && headers.length) {
let contentType = headers
.filter((header) => header[0].toLowerCase() === 'content-type')
.map((header) => {
return header[1];
});
if (contentType && contentType.length) {
if (typeof contentType[0] == 'string' && /^[\w\-]+\/([\w\-]+\+)?json/.test(contentType[0])) {
return 'application/ld+json';
} else if (typeof contentType[0] == 'string' && /^[\w\-]+\/([\w\-]+\+)?xml/.test(contentType[0])) {
return 'application/xml';
}
}
}
return '';
};
const isJson = (headers) => {
return getContentType(headers) === 'application/ld+json';
};

View File

@ -51,25 +51,6 @@ 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
export const normalizeFileName = (name) => {
if (!name) {
@ -81,3 +62,32 @@ export const normalizeFileName = (name) => {
return formattedName;
};
export const getContentType = (headers) => {
if (headers && headers.length) {
let contentType = headers
.filter((header) => header[0].toLowerCase() === 'content-type')
.map((header) => {
return header[1];
});
if (contentType && contentType.length) {
if (typeof contentType[0] == 'string' && /^[\w\-]+\/([\w\-]+\+)?json/.test(contentType[0])) {
return 'application/ld+json';
} else if (typeof contentType[0] == 'string' && /^[\w\-]+\/([\w\-]+\+)?xml/.test(contentType[0])) {
return 'application/xml';
}
}
}
return '';
};
export const formatResponse = (response) => {
let type = getContentType(response.headers);
if (type.includes('json')) {
return safeStringifyJSON(response.data);
}
if (type.includes('xml')) {
return xmlFormat(response.data, { collapseContent: true });
}
return response.data;
};