From b814c84411b8e8d7b2dea1cb0da902dc01889e6d Mon Sep 17 00:00:00 2001 From: pedward99 Date: Tue, 26 Sep 2023 09:22:41 -0400 Subject: [PATCH] Clean Up --- .../ResponsePane/QueryResult/index.js | 8 +--- .../src/components/ResponsePane/index.js | 20 +------- packages/bruno-app/src/utils/common/index.js | 48 +++++++++++-------- 3 files changed, 31 insertions(+), 45 deletions(-) diff --git a/packages/bruno-app/src/components/ResponsePane/QueryResult/index.js b/packages/bruno-app/src/components/ResponsePane/QueryResult/index.js index 6c289e0b..e6d6f94a 100644 --- a/packages/bruno-app/src/components/ResponsePane/QueryResult/index.js +++ b/packages/bruno-app/src/components/ResponsePane/QueryResult/index.js @@ -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 (
diff --git a/packages/bruno-app/src/components/ResponsePane/index.js b/packages/bruno-app/src/components/ResponsePane/index.js index 5fd53ce5..14f4e993 100644 --- a/packages/bruno-app/src/components/ResponsePane/index.js +++ b/packages/bruno-app/src/components/ResponsePane/index.js @@ -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'; }; diff --git a/packages/bruno-app/src/utils/common/index.js b/packages/bruno-app/src/utils/common/index.js index 20e87636..443dca66 100644 --- a/packages/bruno-app/src/utils/common/index.js +++ b/packages/bruno-app/src/utils/common/index.js @@ -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; +};