mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-23 12:45:38 +00:00
Merge pull request #5809 from 0x416c6578/feature/minify-json-xml
Add `bru.utils.minifyXml` and `bru.utils.minifyJson`
This commit is contained in:
@@ -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)'
|
||||
]
|
||||
};
|
||||
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user