bruno/packages/bruno-toml/src/jsonToToml.js

54 lines
1.6 KiB
JavaScript
Raw Normal View History

const stringify = require('../lib/stringify');
const { get, each, filter } = require('lodash');
const keyValPairHasDuplicateKeys = (keyValPair) => {
if (!keyValPair || !Array.isArray(keyValPair) || !keyValPair.length) {
return false;
}
const names = keyValPair.map((pair) => pair.name);
const uniqueNames = new Set(names);
return names.length !== uniqueNames.size;
};
const jsonToToml = (json) => {
2023-12-30 14:39:17 +01:00
const formattedJson = {
meta: {
name: get(json, 'meta.name'),
type: get(json, 'meta.type'),
seq: get(json, 'meta.seq')
},
http: {
method: get(json, 'http.method'),
url: get(json, 'http.url', '')
}
};
if (json.headers && json.headers.length) {
const hasDuplicateHeaders = keyValPairHasDuplicateKeys(json.headers);
if (!hasDuplicateHeaders) {
const enabledHeaders = filter(json.headers, (header) => header.enabled);
const disabledHeaders = filter(json.headers, (header) => !header.enabled);
each(enabledHeaders, (header) => {
formattedJson.headers = formattedJson.headers || {};
formattedJson.headers[header.name] = header.value;
});
each(disabledHeaders, (header) => {
formattedJson.headers = formattedJson.headers || {};
formattedJson.headers.disabled = formattedJson.headers.disabled || {};
formattedJson.headers.disabled[header.name] = header.value;
});
} else {
formattedJson.headers = {
raw: JSON.stringify(json.headers, null, 2)
};
}
2023-12-30 14:39:17 +01:00
}
return stringify(formattedJson);
};
module.exports = jsonToToml;