2023-02-04 19:57:18 +01:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
const {
|
|
|
|
indentString,
|
|
|
|
} = require('../../v1/src/utils');
|
|
|
|
|
|
|
|
const enabled = (items = []) => items.filter(item => item.enabled);
|
|
|
|
const disabled = (items = []) => items.filter(item => !item.enabled);
|
|
|
|
|
|
|
|
// remove the last line if two new lines are found
|
|
|
|
const stripLastLine = (text) => {
|
|
|
|
if(!text || !text.length) return text;
|
|
|
|
|
|
|
|
return text.replace(/(\r?\n)$/, '');
|
|
|
|
};
|
|
|
|
|
|
|
|
const jsonToBru = (json) => {
|
|
|
|
const {
|
|
|
|
meta,
|
|
|
|
http,
|
|
|
|
query,
|
|
|
|
headers,
|
|
|
|
body,
|
|
|
|
script,
|
|
|
|
tests,
|
|
|
|
vars,
|
|
|
|
assert,
|
|
|
|
docs
|
|
|
|
} = json;
|
|
|
|
|
|
|
|
let bru = '';
|
|
|
|
|
|
|
|
if(meta) {
|
|
|
|
bru += 'meta {\n';
|
|
|
|
for (const key in meta) {
|
|
|
|
bru += ` ${key}: ${meta[key]}\n`;
|
|
|
|
}
|
|
|
|
bru += '}\n\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
if(http && http.method) {
|
|
|
|
bru += `${http.method} {
|
|
|
|
url: ${http.url}`;
|
|
|
|
|
|
|
|
if(http.body && http.body.length) {
|
|
|
|
bru += `
|
|
|
|
body: ${http.body}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
bru += `
|
|
|
|
}
|
|
|
|
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
if(query && query.length) {
|
|
|
|
bru += 'query {';
|
|
|
|
if(enabled(query).length) {
|
|
|
|
bru += `\n${indentString(enabled(query).map(item => `${item.name}: ${item.value}`).join('\n'))}`;
|
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
if(disabled(query).length) {
|
|
|
|
bru += `\n${indentString(disabled(query).map(item => `~${item.name}: ${item.value}`).join('\n'))}`;
|
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += '\n}\n\n';
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
if(headers && headers.length) {
|
|
|
|
bru += 'headers {';
|
|
|
|
if(enabled(headers).length) {
|
|
|
|
bru += `\n${indentString(enabled(headers).map(item => `${item.name}: ${item.value}`).join('\n'))}`;
|
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
if(disabled(headers).length) {
|
|
|
|
bru += `\n${indentString(disabled(headers).map(item => `~${item.name}: ${item.value}`).join('\n'))}`;
|
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += '\n}\n\n';
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(body && body.json && body.json.length) {
|
|
|
|
bru += `body:json {
|
|
|
|
${indentString(body.json)}
|
|
|
|
}
|
|
|
|
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(body && body.text && body.text.length) {
|
|
|
|
bru += `body:text {
|
|
|
|
${indentString(body.text)}
|
|
|
|
}
|
|
|
|
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(body && body.xml && body.xml.length) {
|
|
|
|
bru += `body:xml {
|
|
|
|
${indentString(body.xml)}
|
|
|
|
}
|
|
|
|
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
if(body && body.formUrlEncoded) {
|
|
|
|
bru += `body:form-urlencoded {`;
|
|
|
|
if(enabled(body.formUrlEncoded).length) {
|
|
|
|
bru += `\n${indentString(enabled(body.formUrlEncoded).map(item => `${item.name}: ${item.value}`).join('\n'))}`;
|
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
if(disabled(body.formUrlEncoded).length) {
|
|
|
|
bru += `\n${indentString(disabled(body.formUrlEncoded).map(item => `~${item.name}: ${item.value}`).join('\n'))}`;
|
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += '\n}\n\n';
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
if(body && body.multipartForm) {
|
|
|
|
bru += `body:multipart-form {`;
|
|
|
|
if(enabled(body.multipartForm).length) {
|
|
|
|
bru += `\n${indentString(enabled(body.multipartForm).map(item => `${item.name}: ${item.value}`).join('\n'))}`;
|
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
if(disabled(body.multipartForm).length) {
|
|
|
|
bru += `\n${indentString(disabled(body.multipartForm).map(item => `~${item.name}: ${item.value}`).join('\n'))}`;
|
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += '\n}\n\n';
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(body && body.graphql && body.graphql.query) {
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += `body:graphql {\n`;
|
|
|
|
bru += `${indentString(body.graphql.query)}`;
|
|
|
|
bru += '\n}\n\n';
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(body && body.graphql && body.graphql.variables) {
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += `body:graphql:vars {\n`;
|
|
|
|
bru += `${indentString(body.graphql.variables)}`
|
|
|
|
bru += '\n}\n\n';
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(vars && vars.length) {
|
|
|
|
const varsEnabled = _.filter(vars, (v) => v.enabled && !v.local);
|
|
|
|
const varsDisabled = _.filter(vars, (v) => !v.enabled && !v.local);
|
|
|
|
const varsLocalEnabled = _.filter(vars, (v) => v.enabled && v.local);
|
|
|
|
const varsLocalDisabled = _.filter(vars, (v) => !v.enabled && v.local);
|
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += `vars {`;
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
if(varsEnabled.length) {
|
|
|
|
bru += `\n${indentString(varsEnabled.map(item => `${item.name}: ${item.value}`).join('\n'))}`;
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(varsLocalEnabled.length) {
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += `\n${indentString(varsLocalEnabled.map(item => `@${item.name}: ${item.value}`).join('\n'))}`;
|
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
if(varsDisabled.length) {
|
|
|
|
bru += `\n${indentString(varsDisabled.map(item => `~${item.name}: ${item.value}`).join('\n'))}`;
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(varsLocalDisabled.length) {
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += `\n${indentString(varsLocalDisabled.map(item => `~@${item.name}: ${item.value}`).join('\n'))}`;
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
2023-02-05 18:43:18 +01:00
|
|
|
|
|
|
|
bru += '\n}\n\n';
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
if(assert && assert.length) {
|
|
|
|
bru += `assert {`;
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
if(enabled(assert).length) {
|
|
|
|
bru += `\n${indentString(enabled(assert).map(item => `${item.name}: ${item.value}`).join('\n'))}`;
|
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
if(disabled(assert).length) {
|
|
|
|
bru += `\n${indentString(disabled(assert).map(item => `~${item.name}: ${item.value}`).join('\n'))}`;
|
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += '\n}\n\n';
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(script && script.length) {
|
|
|
|
bru += `script {
|
|
|
|
${indentString(script)}
|
|
|
|
}
|
|
|
|
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(tests && tests.length) {
|
|
|
|
bru += `tests {
|
|
|
|
${indentString(tests)}
|
|
|
|
}
|
|
|
|
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(docs && docs.length) {
|
|
|
|
bru += `docs {
|
|
|
|
${indentString(docs)}
|
|
|
|
}
|
|
|
|
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stripLastLine(bru);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = jsonToBru;
|