diff --git a/packages/bruno-app/src/utils/codemirror/autocomplete.js b/packages/bruno-app/src/utils/codemirror/autocomplete.js index a1b6d1e0c..def351389 100644 --- a/packages/bruno-app/src/utils/codemirror/autocomplete.js +++ b/packages/bruno-app/src/utils/codemirror/autocomplete.js @@ -93,6 +93,9 @@ const STATIC_API_HINTS = { 'bru.cookies.jar().clear(callback)', 'bru.cookies.jar().deleteCookies(url, callback)', 'bru.cookies.jar().deleteCookie(url, name, callback)', + 'bru.utils', + 'bru.utils.minifyJson(json)', + 'bru.utils.minifyXml(xml)' ] }; diff --git a/packages/bruno-js/src/bru.js b/packages/bruno-js/src/bru.js index 2bcb0a7d9..680fe7c27 100644 --- a/packages/bruno-js/src/bru.js +++ b/packages/bruno-js/src/bru.js @@ -1,4 +1,5 @@ const { cloneDeep } = require('lodash'); +const xmlFormat = require('xml-formatter'); const { interpolate: _interpolate } = require('@usebruno/common'); const { sendRequest } = require('@usebruno/requests').scripting; const { jar: createCookieJar } = require('@usebruno/requests').cookies; @@ -74,6 +75,50 @@ class Bru { this.nextRequest = nextRequest; } }; + + this.utils = { + minifyJson: (json) => { + if (json === null || json === undefined) { + throw new Error('Failed to minify'); + } + + if (typeof json === 'object') { + try { + return JSON.stringify(json); + } catch (err) { + throw new Error(`Failed to minify: ${err?.message || err}`); + } + } + + if (typeof json === 'string') { + const trimmed = json.trim(); + if (trimmed === '') return trimmed; + try { + return JSON.stringify(JSON.parse(trimmed)); + } catch (err) { + throw new Error(`Failed to minify: ${err?.message || err}`); + } + } + + throw new TypeError('minifyJson expects a string or object'); + }, + + minifyXml: (xml) => { + if (xml === null || xml === undefined) { + throw new Error('Failed to minify'); + } + + if (typeof xml === 'string') { + try { + return xmlFormat(xml, { collapseContent: false, indentation: '', lineSeparator: '' }); + } catch (err) { + throw new Error(`Failed to minify: ${err?.message || err}`); + } + } + + throw new TypeError('minifyXml expects a string'); + } + }; } interpolate = (strOrObj) => {