2023-12-28 18:22:30 +01:00
|
|
|
const stringify = require('../lib/stringify');
|
2023-12-30 15:59:31 +01:00
|
|
|
const { get, each, filter } = require('lodash');
|
2023-12-28 18:22:30 +01:00
|
|
|
|
2023-12-30 16:57:15 +01:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2023-12-30 17:23:37 +01:00
|
|
|
// these keys are reserved: disabled, description, enum
|
|
|
|
const keyValPairHasReservedKeys = (keyValPair) => {
|
|
|
|
if (!keyValPair || !Array.isArray(keyValPair) || !keyValPair.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-12-30 22:22:41 +01:00
|
|
|
const reservedKeys = ['disabled', 'description', 'enum', 'bru'];
|
2023-12-30 17:23:37 +01:00
|
|
|
const names = keyValPair.map((pair) => pair.name);
|
|
|
|
|
|
|
|
return names.some((name) => reservedKeys.includes(name));
|
|
|
|
};
|
|
|
|
|
2023-12-30 22:22:41 +01:00
|
|
|
/**
|
|
|
|
* Json to Toml
|
|
|
|
*
|
|
|
|
* Note: Bruno always append a new line at the end of text blocks
|
|
|
|
* This is to aid readability when viewing the toml representation of the request
|
|
|
|
* The newline is removed when converting back to json
|
|
|
|
*
|
|
|
|
* @param {object} json
|
|
|
|
* @returns string
|
|
|
|
*/
|
2023-12-28 18:22:30 +01:00
|
|
|
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) {
|
2023-12-30 16:57:15 +01:00
|
|
|
const hasDuplicateHeaders = keyValPairHasDuplicateKeys(json.headers);
|
2023-12-30 17:23:37 +01:00
|
|
|
const hasReservedHeaders = keyValPairHasReservedKeys(json.headers);
|
2023-12-30 16:57:15 +01:00
|
|
|
|
2023-12-30 17:23:37 +01:00
|
|
|
if (!hasDuplicateHeaders && !hasReservedHeaders) {
|
2023-12-30 16:57:15 +01:00
|
|
|
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 = {
|
2023-12-30 22:22:41 +01:00
|
|
|
bru: JSON.stringify(json.headers, null, 2) + '\n'
|
2023-12-30 16:57:15 +01:00
|
|
|
};
|
|
|
|
}
|
2023-12-30 14:39:17 +01:00
|
|
|
}
|
|
|
|
|
2023-12-30 22:22:41 +01:00
|
|
|
if (json.script) {
|
|
|
|
let preRequestScript = get(json, 'script.req', '');
|
|
|
|
if (preRequestScript.trim().length > 0) {
|
|
|
|
formattedJson.script = formattedJson.script || {};
|
|
|
|
formattedJson.script['pre-request'] = preRequestScript + '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
let postResponseScript = get(json, 'script.res', '');
|
|
|
|
if (postResponseScript.trim().length > 0) {
|
|
|
|
formattedJson.script = formattedJson.script || {};
|
|
|
|
formattedJson.script['post-response'] = postResponseScript + '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json.tests) {
|
|
|
|
let testsScript = get(json, 'tests', '');
|
|
|
|
if (testsScript.trim().length > 0) {
|
|
|
|
formattedJson.script = formattedJson.script || {};
|
|
|
|
formattedJson.script['tests'] = testsScript + '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-30 14:39:17 +01:00
|
|
|
return stringify(formattedJson);
|
2023-12-28 18:22:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = jsonToToml;
|