-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcli.js
107 lines (100 loc) · 2.58 KB
/
cli.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env node
// cli bridge which ensures calls just go through without security
process.env.haxcms_middleware = "node-cli";
// HAXcms core settings
const { HAXCMS } = require('./lib/HAXCMS.js');
const RoutesMap = require('./lib/RoutesMap.js');
// process arguments from commandline appropriately
let body = {};
let cliOp = null;
const cli = {
post: (path, callback) =>
callback(
{
route: {
path: path
},
body: body,
method: "post"
},
{
query: {},
send: (data) => console.log(data),
}
),
get: (path, callback) => callback({
route: {
path: path
},
body: body,
method: "get"
},
{
query: {},
send: (data) => console.log(data),
}),
};
// loop through methods and apply the route to the file to deliver it
// @todo ensure that we apply the same JWT checking that we do in the PHP side
// instead of a simple array of what to let go through we could put it into our
// routes object above and apply JWT requirement on paths in a better way
for (var method in RoutesMap) {
for (var route in RoutesMap[method]) {
if (cliOp === 'listCalls') {
console.log(route);
}
else if (route === cliOp) {
cli[method](`${HAXCMS.basePath}${HAXCMS.systemRequestBase}${route}`, (req, res) => {
const op = req.route.path.replace(`${HAXCMS.basePath}${HAXCMS.systemRequestBase}`, '');
const rMethod = req.method.toLowerCase();
if (HAXCMS.validateJWT(req, res)) {
// call the method
RoutesMap[rMethod][op](req, res);
}
else {
console.error("route connection issue");
}
});
}
}
}
// fake response clas so we can capture the response from the headless route as opposed to print to console
class Res {
constructor() {
this.query = {};
this.data = null;
this.statusCode = null;
}
send(data) {
this.data = data;
return this;
}
status(status) {
this.statusCode = status;
return this;
}
setHeader() {
return this;
}
}
// method to bridge api calls in similar manner given a site already loaded into scope
export async function cliBridge(op, body = {}) {
let req = {
route: {
path: `${HAXCMS.basePath}${HAXCMS.systemRequestBase}${route}`
},
body: body,
method: "post"
};
let res = new Res();
const rMethod = req.method.toLowerCase();
if (HAXCMS.validateJWT(req, res)) {
// call the method
await RoutesMap.RoutesMap[rMethod][op](req, res);
return {req: req, res: res};
}
else {
console.error("route connection issue");
}
}
export { cli };