mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-21 23:43:15 +01:00
feat: bruno lang parse .bru file
This commit is contained in:
parent
137df3c5c0
commit
b75baf57ba
@ -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
|
||||
};
|
54
packages/bruno-lang/src/params-tag.js
Normal file
54
packages/bruno-lang/src/params-tag.js
Normal 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;
|
12
packages/bruno-lang/tests/fixtures/request.bru
vendored
Normal file
12
packages/bruno-lang/tests/fixtures/request.bru
vendored
Normal 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
|
@ -1,4 +1,4 @@
|
||||
const inlineTag = require('./inline-tag');
|
||||
const inlineTag = require('../src/inline-tag');
|
||||
const {
|
||||
sepBy,
|
||||
char,
|
40
packages/bruno-lang/tests/parser.spec.js
Normal file
40
packages/bruno-lang/tests/parser.spec.js
Normal 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"
|
||||
}
|
||||
]
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user