From ad905d1a0a893a63c7d23dd7da9462ff6a915571 Mon Sep 17 00:00:00 2001 From: Paul Edwards Date: Wed, 26 Apr 2023 22:06:52 -0400 Subject: [PATCH 1/3] XML Indenting with header check --- package.json | 5 ++++- .../bruno-app/src/components/ResponsePane/index.js | 5 +++-- packages/bruno-app/src/utils/common/index.js | 11 +++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 8e2c2715f..bc1b16f90 100644 --- a/package.json +++ b/package.json @@ -34,5 +34,8 @@ }, "overrides": { "rollup": "3.2.5" + }, + "dependencies": { + "xml-formatter": "^3.3.2" } -} \ No newline at end of file +} diff --git a/packages/bruno-app/src/components/ResponsePane/index.js b/packages/bruno-app/src/components/ResponsePane/index.js index 1648c7080..8847ef6be 100644 --- a/packages/bruno-app/src/components/ResponsePane/index.js +++ b/packages/bruno-app/src/components/ResponsePane/index.js @@ -1,7 +1,7 @@ import React from 'react'; import find from 'lodash/find'; import classnames from 'classnames'; -import { safeStringifyJSON } from 'utils/common'; +import { formatResponse } from 'utils/common'; import { useSelector, useDispatch } from 'react-redux'; import { updateResponsePaneTab } from 'providers/ReduxStore/slices/tabs'; import QueryResult from './QueryResult'; @@ -16,6 +16,7 @@ import TestResults from './TestResults'; import TestResultsLabel from './TestResultsLabel'; import StyledWrapper from './StyledWrapper'; + const ResponsePane = ({ rightPaneWidth, item, collection }) => { const dispatch = useDispatch(); const tabs = useSelector((state) => state.tabs.tabs); @@ -40,7 +41,7 @@ const ResponsePane = ({ rightPaneWidth, item, collection }) => { item={item} collection={collection} width={rightPaneWidth} - value={response.data ? safeStringifyJSON(response.data, true) : ''} + value={response.data ? formatResponse(response): ''} />; } case 'headers': { diff --git a/packages/bruno-app/src/utils/common/index.js b/packages/bruno-app/src/utils/common/index.js index f5b3b2dfb..f11c972d3 100644 --- a/packages/bruno-app/src/utils/common/index.js +++ b/packages/bruno-app/src/utils/common/index.js @@ -1,4 +1,5 @@ import { customAlphabet } from 'nanoid'; +import xmlFormat from 'xml-formatter'; // a customized version of nanoid without using _ and - export const uuid = () => { @@ -50,6 +51,16 @@ export const safeStringifyJSON = (obj, indent=false) => { } } +export const formatResponse = (response) => { + var type = response.headers.find((element) => element[0]=='content-type')[1]; + 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) { From aeb29393c55f4edd14bb9669435a622894816dee Mon Sep 17 00:00:00 2001 From: Paul Edwards Date: Wed, 19 Jul 2023 20:06:37 -0400 Subject: [PATCH 2/3] Code Editor Mode and formatting --- .../src/components/ResponsePane/QueryResult/index.js | 10 ++++++++-- packages/bruno-app/src/utils/common/index.js | 10 +++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/bruno-app/src/components/ResponsePane/QueryResult/index.js b/packages/bruno-app/src/components/ResponsePane/QueryResult/index.js index 1dd05c8c5..a9974c7c6 100644 --- a/packages/bruno-app/src/components/ResponsePane/QueryResult/index.js +++ b/packages/bruno-app/src/components/ResponsePane/QueryResult/index.js @@ -3,6 +3,7 @@ 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'; @@ -18,11 +19,16 @@ 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(responseType); return (
- +
); diff --git a/packages/bruno-app/src/utils/common/index.js b/packages/bruno-app/src/utils/common/index.js index f11c972d3..ef8f151b6 100644 --- a/packages/bruno-app/src/utils/common/index.js +++ b/packages/bruno-app/src/utils/common/index.js @@ -51,8 +51,16 @@ 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 = response.headers.find((element) => element[0]=='content-type')[1]; + var type = getContentTypeHeader(response.headers); if(type.includes("json")){ return safeStringifyJSON(response.data); }if(type.includes("xml")){ From b814c84411b8e8d7b2dea1cb0da902dc01889e6d Mon Sep 17 00:00:00 2001 From: pedward99 Date: Tue, 26 Sep 2023 09:22:41 -0400 Subject: [PATCH 3/3] 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 6c289e0be..e6d6f94a8 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 5fd53ce5d..14f4e9930 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 20e876364..443dca664 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; +};