-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
122 lines (99 loc) · 3.3 KB
/
index.ts
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env node
import requestController from '@/controller/requestController';
import chalk from 'chalk';
import Table from 'cli-table3';
import { stdout as terminalWidth } from 'process';
// flows import
import { checkHealth } from '@/utils';
import { getCliProgram } from '@/utils/cli';
import { generateFullUrls, parseHeaders } from './utils/requests';
const cli = getCliProgram(process.argv);
const cliParams = cli.opts()
const checkHealthFlow: boolean = cliParams.checkHealth ?? false;
// Comprueba si no se proporcionaron parámetros 2 because 0 => node, 1 => scriptname (insomnie.js)
const cliRequestFlow: boolean = process.argv.length > 2;
const tuiRequestFlow: boolean = !cliRequestFlow;
// 0 - exceptions and missings
if (cliParams.type === 'gql') {
console.debug("Graphql is not implemented :c (not yet 7u7)");
process.exit(0);
}
// 1: check health flow
if (checkHealthFlow) {
const fullPathUrls: string[] = generateFullUrls(cliParams.url, cliParams.urlpath);
await checkHealth(fullPathUrls, cliParams.tries);
process.exit(0);
}
// TODO: using json by default but i need support another types
let requestHeaders = {
'content-type': 'application/json'
}
try {
const headersFromCli = JSON.parse(cliParams.headers)
requestHeaders = { ...requestHeaders, ...headersFromCli }
} catch (error) {
console.debug('Invalid Request Headers On Cli Params.')
}
let requestBody = null;
if (cliParams.body !== undefined) {
requestBody = JSON.parse(cliParams.body);
}
// 2: cli request
if (cliRequestFlow && !checkHealthFlow) {
// Procesa los parámetros de URL y cabeceras
const requestData = {
url: cliParams.url,
type: cliParams.type,
headers: requestHeaders,
body: requestBody,
}
let response = null;
let requestError = null;
try {
response = await requestController.call(requestData);
} catch (error) {
requestError = error;
}
if (requestError !== null) {
console.log('Error Calling API', requestError)
process.exit(1);
}
console.log('Parsing Response Data ...')
// extracct states from api response if this is not null
let responseData = null;
const headers: any = parseHeaders(response.headers);
if (headers.isJson) {
// responseData = 'json';
responseData = await response.json();
}
if (headers.isHtml || headers.isXml || headers.isText) {
responseData = await response.text();
}
console.log('Printing Response Data ...')
const data = {
status: response.status,
stausText: response.stausText,
data: responseData,
};
const consoleTable = new Table({
style: { border: [], header: [] },
head: [chalk.white('State')],
wordWrap: true,
// colWidths: [50, Math.floor(terminalWidth.columns - 53)],
colWidths: [terminalWidth.columns < 50 ? 50 : Math.floor(terminalWidth.columns / 2)],
});
const col1 = {
url: requestData.url,
status: `${data.status} ${data.stausText}`,
}
consoleTable.push([JSON.stringify(col1, null, 1)]);
console.log(consoleTable.toString());
console.log(chalk.yellow('Response Body:'));
console.log(JSON.stringify(data.data, null, 1));
} else {
// weird bug if this is imported from the begin you need to procces.exit or you get a kind of blessed empty screen
const ui = require("./ui");
console.log("Rendering ================>");
ui.renderui();
//process.exit(0);
}