diff --git a/package.json b/package.json index 9aaaf23f2..2d5841607 100644 --- a/package.json +++ b/package.json @@ -38,5 +38,8 @@ }, "overrides": { "rollup": "3.2.5" + }, + "dependencies": { + "xml-formatter": "^3.3.2" } } diff --git a/packages/bruno-app/src/components/ResponsePane/index.js b/packages/bruno-app/src/components/ResponsePane/index.js index 9d123dcfe..14f4e9930 100644 --- a/packages/bruno-app/src/components/ResponsePane/index.js +++ b/packages/bruno-app/src/components/ResponsePane/index.js @@ -1,8 +1,8 @@ import React from 'react'; import find from 'lodash/find'; import classnames from 'classnames'; -import { safeStringifyJSON } from 'utils/common'; import { useDispatch, useSelector } from 'react-redux'; +import { getContentType, formatResponse } from 'utils/common'; import { updateResponsePaneTab } from 'providers/ReduxStore/slices/tabs'; import QueryResult from './QueryResult'; import Overlay from './Overlay'; @@ -41,9 +41,7 @@ const ResponsePane = ({ rightPaneWidth, item, collection }) => { item={item} collection={collection} width={rightPaneWidth} - value={ - response.data ? (isJson(response.headers) ? safeStringifyJSON(response.data, true) : response.data) : '' - } + value={response.data ? formatResponse(response) : ''} mode={getContentType(response.headers)} /> ); @@ -95,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 7db86d386..443dca664 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 = () => { @@ -61,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; +};