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-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);
|
|
|
|
|
|
|
|
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);
|
2023-12-28 18:22:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = jsonToToml;
|