-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (47 loc) · 1.23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
const tq = require('thng-query');
module.exports = api => {
const command = {
about: 'Allows to use custom DSL for API queries',
firstArg: 'query',
operations: {
run: {
execute: run,
pattern: 'run'
},
help: {
execute: help,
pattern: 'help'
}
}
};
setup(api.getConfig());
api.registerCommand(command);
};
function run([,query]) {
return tq.run(query)
.then(results => console.log(JSON.stringify(results)));
}
function setup(config) {
const operators = config.get('operators');
const regions = config.get('regions');
const using = config.get('using');
const operator = operators[using];
const apiUrl = regions[operator.region];
const authorization = operator.apiKey;
tq.setup({
authorization,
apiUrl
});
}
function help() {
console.log(`
thng-query is a custom DSL allowing to execute complex requests agains EVRYTHNG API
full documentation could be found at package's README.
Examples:
evrythng query run "thngs named Consumer*"
evrythng query run "products tagged Apparel where properties.scan_count>5000"
evrytnhg query run "products where category=Consumables" | jq '.[] | { id }'
evrythng query help
`);
}