feat: bruno lang parse .bru file

This commit is contained in:
Anoop M D 2023-01-14 20:16:09 +05:30
parent 137df3c5c0
commit b75baf57ba
5 changed files with 154 additions and 45 deletions

View File

@ -1,48 +1,51 @@
const bruToJson = (input) => {
const {
sepBy,
regex,
many,
choice,
anyChar
} = require("arcsecond");
const inlineTag = require('./inline-tag');
const paramsTag = require('./params-tag');
const bruToJson = (fileContents) => {
const newline = regex(/^\r?\n/);
const line = inlineTag;
const lines = many(line);
// const parser = sepBy(newline)(lines);
let parser = choice([
sepBy(newline)(lines),
paramsTag
]);
parser = many(choice([
inlineTag,
paramsTag,
anyChar
]));
const parsed = parser
.run(fileContents)
.result
.reduce((acc, item) => {
return {
...acc,
...item
};
}, {});
return {
ver: parsed.ver,
type: parsed.type,
name: parsed.name,
method: parsed.method,
url: parsed.url,
params: parsed.params
}
};
const a = {
"ver": "1.0",
"type": "http-request",
"name": "Send Bulk SMS",
"method": "GET",
"url": "https://api.textlocal.in/bulk_json?apiKey=secret=&numbers=919988776655&message=hello&sender=600010",
"params": [{
"enabled": true,
"key": "apiKey",
"value": "secret"
}, {
"enabled": true,
"key": "numbers",
"value": "998877665"
}, {
"enabled": true,
"key": "message",
"value": "hello"
}],
"headers": [{
"enabled": true,
"key": "Content-Type",
"value": "application/json"
}, {
"enabled": true,
"key": "Accept-Language",
"value": "en-US,en;q=0.9,hi;q=0.8"
}, {
"enabled": false,
"key": "transaction-id",
"value": "{{transactionId}}"
}],
"body": {
"mode": "json",
"json": "{\n \"apikey: \"secret\",\n \"numbers: \"+91998877665\",\n \"data: {\n \"sender: \"TXTLCL\",\n \"messages: [{\n \"numbers: \"+91998877665\",\n \"message: \"Hello World\"\n }]\n }\n}",
"graphql": "{\n launchesPast {\n launch_site {\n site_name\n }\n launch_success\n }\n}",
}
}
export {
module.exports = {
bruToJson
};

View File

@ -0,0 +1,54 @@
const {
sequenceOf,
whitespace,
optionalWhitespace,
choice,
endOfInput,
everyCharUntil,
between,
digit,
many,
regex,
sepBy
} = require("arcsecond");
const newline = regex(/^\r?\n/);
const newLineOrEndOfInput = choice([newline, endOfInput]);
const begin = sequenceOf([
regex(/^params[^\S\r\n]*/),
newline
]);
const end = sequenceOf([
regex(/^\/params[^\S\r\n]*/),
newLineOrEndOfInput
]);
const key = everyCharUntil(whitespace);
const value = everyCharUntil(whitespace);
const line = sequenceOf([
optionalWhitespace,
digit,
whitespace,
key,
whitespace,
value,
newLineOrEndOfInput
]).map(([_, enabled, __, key, ___, value]) => {
return {
"enabled": enabled,
"key": key,
"value": value
};
});
const lines = many(line);
const paramsLines = sepBy(newline)(lines);
const paramsTag = between(begin)(end)(paramsLines).map(([params]) => {
return {
params
};
});
module.exports = paramsTag;

View File

@ -0,0 +1,12 @@
ver 1.0
type http-request
name Send Bulk SMS
method GET
url https://api.textlocal.in/bulk_json?apiKey=secret=&numbers=919988776655&message=hello&sender=600010
body-mode json
params
1 apiKey secret
1 numbers 998877665
1 message hello
/params

View File

@ -1,4 +1,4 @@
const inlineTag = require('./inline-tag');
const inlineTag = require('../src/inline-tag');
const {
sepBy,
char,

View File

@ -0,0 +1,40 @@
const fs = require('fs');
const path = require('path');
const {
bruToJson
} = require('../src');
describe('bruToJson', () => {
it('should parse .bru file contents', () => {
const requestFile = fs.readFileSync(path.join(__dirname, 'fixtures', 'request.bru'), 'utf8');
const result = bruToJson(requestFile);
expect(result).toEqual({
"ver": "1.0",
"type": "http-request",
"name": "Send Bulk SMS",
"method": "GET",
"url": "https://api.textlocal.in/bulk_json?apiKey=secret=&numbers=919988776655&message=hello&sender=600010",
"params": [
{
"enabled": "1",
"key": "apiKey",
"value": "secret"
},
{
"enabled": "1",
"key": "numbers",
"value": "998877665"
},
{
"enabled": "1",
"key": "message",
"value": "hello"
}
]
});
});
});