-
Notifications
You must be signed in to change notification settings - Fork 0
/
insomnie.dev.js
executable file
·520 lines (507 loc) · 16.2 KB
/
insomnie.dev.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#!/usr/bin/env node
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __esm = (fn, res) => function __init() {
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// ui/index.ts
var ui_exports = {};
__export(ui_exports, {
preview: () => preview,
renderui: () => renderui,
screen: () => screen,
sendButton: () => sendButton
});
import blessed from "blessed";
var loremText, screen, form, methodLabel, methodList, urlLabel, urlInput, headersLabel, headersInput, bodyLabel, bodyInput, sendButton, preview, makeRequest, renderui;
var init_ui = __esm({
"ui/index.ts"() {
loremText = `Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
Nulla sed interdum nisi. Sed pharetra libero id nunc tristique, nec scelerisque dui sagittis.
Nunc eu hendrerit lacus. Pellentesque ut tristique risus. Proin euismod lacus ligula, eu ullamcorper justo luctus a.
Mauris et sapien ligula. In pellentesque sem a finibus interdum.
Integer condimentum nec eros at placerat. Curabitur consequat convallis sem ac auctor. Etiam non dapibus sem.
Vestibulum rutrum fringilla turpis, et scelerisque elit convallis in.
Cras tincidunt rhoncus metus nec eleifend. Aliquam erat volutpat. Sed auctor fringilla pharetra. Aenean non mi ante.
Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;
Sed ut lacus id magna commodo vestibulum. Donec feugiat, felis sit amet laoreet facilisis, arcu velit tristique enim, nec volutpat lectus metus nec velit.
Phasellus aliquet, nisl et sollicitudin dapibus, ex lectus tincidunt tellus, sed congue dolor risus at risus. Vestibulum placerat mauris non leo volutpat efficitur.
Fusce bibendum iaculis tincidunt. Nam eget consequat lectus. Sed tristique odio et sem elementum, eget interdum velit pharetra. Nulla facilisi. Curabitur ac mauris consectetur, feugiat mauris quis, consequat metus. Integer vitae magna non dolor aliquet lobortis eu eget mauris.
Suspendisse efficitur arcu at nibh finibus, id vulputate turpis fermentum. Aenean nec nibh eu lectus vehicula placerat. Donec id placerat leo. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris pellentesque, mi eget auctor varius, erat odio ultrices quam, in convallis odio mi in urna. Cras sodales tempus eros id viverra. Sed vulputate congue turpis, vitae scelerisque tellus efficitur ut. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
Etiam eu rutrum nunc, a eleifend ante. Fusce pharetra purus nec ex placerat pharetra. Aenean auctor, enim vitae sollicitudin rutrum, risus nulla auctor lacus, vel cursus ligula tellus eget nunc. Vestibulum feugiat dictum sem, sit amet auctor orci pulvinar a. Pellentesque at sem dui. Nullam sit amet turpis lacinia, ullamcorper orci sit amet, fringilla elit. Quisque facilisis pellentesque enim, nec vulputate leo malesuada quis. Proin congue ipsum sed aliquet pretium. Etiam placerat vehicula purus vitae malesuada. Suspendisse ullamcorper, quam vel vestibulum consectetur, lectus nibh fringilla velit, at dignissim mauris ex vel justo. Cras volutpat euismod erat, sit amet faucibus lorem gravida id. Sed dapibus nibh ac lectus sagittis, vel efficitur lorem fermentum. Nullam cursus dolor et luctus faucibus. Nullam id gravida nunc.`;
screen = blessed.screen({
smartCSR: true
});
form = blessed.form({
parent: screen,
//top: 'center',
//left: 'center',
width: "50%",
height: "100%",
//border: {
//type: 'line',
//},
scrollable: true,
// Habilitar scroll
keys: true,
vi: true,
mouse: true,
scrollbar: {
style: {
fg: "white",
bg: "blue"
}
}
});
methodLabel = blessed.text({
parent: form,
top: 1,
left: 2,
content: "M\xE9todo:"
});
methodList = blessed.list({
parent: form,
top: 2,
left: 2,
width: "96%",
height: 3,
keys: true,
mouse: true,
autoCommandKeys: true,
border: {
type: "line"
},
style: {
item: {
fg: "white",
bg: "black",
hover: {
bg: "green"
}
},
selected: {
bg: "green"
}
},
items: [
"GET",
"POST",
"PUT",
"DELETE"
]
});
urlLabel = blessed.text({
parent: form,
top: 6,
left: 2,
content: "URL:"
});
urlInput = blessed.textbox({
parent: form,
top: 7,
left: 2,
width: "96%",
height: 3,
vi: true,
keys: true,
mouse: true,
border: {
type: "line"
},
style: {
fg: "white",
bg: "black",
focus: {
border: {
fg: "green"
}
}
}
});
headersLabel = blessed.text({
parent: form,
top: 10,
left: 2,
content: "Headers:"
});
headersInput = blessed.textarea({
parent: form,
top: 11,
left: 2,
width: "96%",
height: 8,
vi: true,
keys: true,
mouse: true,
border: {
type: "line"
},
style: {
fg: "white",
bg: "black",
focus: {
border: {
fg: "green"
}
}
}
});
bodyLabel = blessed.text({
parent: form,
top: 21,
left: 2,
content: "Body:"
});
bodyInput = blessed.textarea({
parent: form,
top: 22,
left: 2,
width: "96%",
height: 10,
vi: true,
keys: true,
mouse: true,
border: {
type: "line"
},
style: {
fg: "white",
bg: "black",
focus: {
border: {
fg: "green"
}
}
}
});
sendButton = blessed.button({
parent: form,
top: 34,
left: "center",
width: 10,
height: 3,
content: "Enviar",
border: {
type: "line"
},
vi: true,
keys: true,
mouse: true,
style: {
fg: "white",
bg: "black",
focus: {
border: {
fg: "green"
}
}
}
});
preview = blessed.box({
parent: screen,
left: "50%-1",
width: "50%",
height: "100%",
border: {
type: "line"
},
style: {
fg: "white",
bg: "black"
},
scrollable: true,
// Habilitar scroll
keys: true,
vi: true,
mouse: true,
scrollbar: {
style: {
bg: "green",
fg: "green"
}
}
});
makeRequest = async () => {
const method = !!methodList.options?.items ? methodList.options.items[methodList.selected] : "GET";
const url = urlInput.getValue();
const headers = headersInput.getValue();
const body = bodyInput.getValue();
const data = {
method,
url,
headers,
body
};
preview.setContent(`hola marco ${Date.now()}
${JSON.stringify(data, null, 2)}`);
};
renderui = () => {
sendButton.on("press", makeRequest);
screen.key(["escape", "q", "C-c"], () => process.exit(0));
preview.setContent(loremText);
screen.render();
};
}
});
// controller/requestController.ts
var requestController = {
call: async (requestData) => {
console.debug("Calling API");
let response = {
data: null
};
let requestConfig = {
method: requestData.type
// headers: requestData.headers,
// body: requestData.body
};
if (requestData.headers) {
requestConfig.headers = requestData.headers;
}
if (requestData.body) {
requestConfig.body = JSON.stringify(requestData.body);
}
response = fetch(requestData.url, requestConfig);
return response;
},
save: () => {
return "save";
},
delete: () => {
return "delete";
}
};
var requestController_default = requestController;
// index.ts
import chalk2 from "chalk";
import Table2 from "cli-table3";
import { stdout as terminalWidth } from "process";
// utils/index.ts
import chalk from "chalk";
import Table from "cli-table3";
var printTable = async (tableData) => {
const table = new Table({
head: [chalk.white("URL"), chalk.white("Status")],
colWidths: [80, 18]
});
tableData.forEach(async (row, index) => {
if (row.status === "rejected") {
table.push([chalk.blue(index), chalk.red(row.status)]);
} else {
let statusColor;
if (row.value.status === "Error") {
statusColor = chalk.red;
} else if (row.value.status >= 200 && row.value.status < 300) {
statusColor = chalk.green;
} else {
statusColor = chalk.yellow;
}
table.push([chalk.blue(row.value.url), statusColor(`${row.value.status} ${row.value.statusText}`)]);
}
});
console.log("\n" + chalk.bold(`Requesting ${tableData.length} URLs:`) + "\n");
console.log(table.toString());
};
var checkHealth = async (fullPathUrls, tries) => {
for (let index = 0; index < tries; index++) {
let promises = [];
for (const path of fullPathUrls) {
promises.push(fetch(path));
}
;
let responses = await Promise.allSettled(promises);
printTable(responses);
}
};
// utils/cli.ts
import { Option, program } from "commander";
// package.json
var package_default = {
name: "insomnie",
version: "1.2.1",
description: "insomnie is a rest client for the terminal (TUI) ",
type: "module",
main: "./insomnie.js",
module: "./insomnie.js",
private: false,
engines: {
node: ">=20",
npm: ">=10.7"
},
scripts: {
test: 'echo "Error: no test specified" && exit 1',
buildInline: "esbuild index.ts --bundle --platform=node --minify --packages=external --outfile=insomnie.js",
build: "node build.mjs prod",
"build:dev": "node build.mjs dev",
watch: "nodemon --exec 'npm run build:dev' -e ts,mjs,json --ignore insomnie.mjs",
changelog: "git cliff > changelog.md",
"changelog:patch": "git cliff --bump patch -o changelog.md",
"changelog:minor": "git cliff --bump minor -o changelog.md",
"docs:dev": "vitepress dev docs --host",
"docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs",
"release:docs": "node bin/generate_version.js && npm run docs:build && cp version.json docs/.vitepress/dist/"
},
bin: {
insomnie: "./insomnie.js"
},
keywords: [
"rest",
"rest client",
"tui",
"cli",
"http",
"client"
],
author: "Marco A. Gallegos Loaeza",
license: "GPL-3.0-only",
repository: {
type: "git",
url: "git+https://github.com/marco-gallegos/insomnie"
},
homepage: "https://github.com/marco-gallegos/insomnie",
dependencies: {
blessed: "^0.1.81",
chalk: "^5.3.0",
"cli-table3": "^0.6.5",
commander: "^12.1.0",
figlet: "^1.8.0",
zustand: "^5.0.0"
},
devDependencies: {
"@types/blessed": "^0.1.25",
esbuild: "^0.24.0",
nodemon: "^3.1.7",
vitepress: "^1.4.1"
}
};
// utils/version.ts
var getVersion = (checkUpdates = false) => {
if (checkUpdates === true) {
}
return package_default.version;
};
// utils/cli.ts
var getCliProgram = (args) => {
const cli2 = program.version(getVersion(true)).description("A Simple terminal CLI and TUI local first http client for developers.").addOption(new Option("-chk, --check-health", "this enables check health mode to make a helth check on given urls.")).addOption(new Option("-tr, --tries <number>", "Max try number (on chk is how many times is executed).").default(10)).addOption(new Option("-u, --url <url>", "URL to hit, full parth or base url to work with -up - url path")).addOption(new Option("-p, --urlpath <url>", "a single ppath or a csv list of url paths to hit (path is a url complement <request_url> = <url> + <path>)")).addOption(new Option("-H, --headers <headers>", "Headers in JSON format")).addOption(new Option("-B, --body <body>", "Request body")).addOption(new Option("-t, --type <type>", "request type GET, POST, ...").default("get").choices(["get", "post", "put", "delete", "patch", "gql"])).addOption(new Option("-rq, --request <id>", "request to execute")).addOption(new Option("-s, --save", "Save request.")).addOption(new Option("-d, --delete <id>", "Delete the request with id:<id>")).addOption(new Option("-v, --view <id>", "Show all datails freom the request with id:<id>")).addOption(new Option("-l, --list", "Show all requests according current space."));
return cli2.parse(args);
};
// utils/requests.ts
var generateFullUrls = (baseUrl, urlPaths) => {
if (!urlPaths) {
return [baseUrl];
}
const paths = urlPaths.split(",").map((path) => path.trim());
return paths.map((path) => `${baseUrl}${path}`);
};
var parseHeaders = (headers) => {
let parsedHeaders = {
isJson: false,
isXml: false,
isHtml: false,
isText: false,
isBinary: false
};
const contentType = headers.get("content-type");
parsedHeaders.isJson = contentType?.includes("json");
parsedHeaders.isXml = contentType?.includes("xml");
parsedHeaders.isHtml = contentType?.includes("html");
parsedHeaders.isText = contentType?.includes("text");
return parsedHeaders;
};
// index.ts
var cli = getCliProgram(process.argv);
var cliParams = cli.opts();
var checkHealthFlow = cliParams.checkHealth ?? false;
var cliRequestFlow = process.argv.length > 2;
if (cliParams.type === "gql") {
console.debug("Graphql is not implemented :c (not yet 7u7)");
process.exit(0);
}
if (checkHealthFlow) {
const fullPathUrls = generateFullUrls(cliParams.url, cliParams.urlpath);
await checkHealth(fullPathUrls, cliParams.tries);
process.exit(0);
}
var 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.");
}
var requestBody = null;
if (cliParams.body !== void 0) {
requestBody = JSON.parse(cliParams.body);
}
if (cliRequestFlow && !checkHealthFlow) {
const requestData = {
url: cliParams.url,
type: cliParams.type,
headers: requestHeaders,
body: requestBody
};
let response = null;
let requestError = null;
try {
response = await requestController_default.call(requestData);
} catch (error) {
requestError = error;
}
if (requestError !== null) {
console.log("Error Calling API", requestError);
process.exit(1);
}
console.log("Parsing Response Data ...");
let responseData = null;
const headers = parseHeaders(response.headers);
if (headers.isJson) {
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 Table2({
style: { border: [], header: [] },
head: [chalk2.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(chalk2.yellow("Response Body:"));
console.log(JSON.stringify(data.data, null, 1));
} else {
const ui = (init_ui(), __toCommonJS(ui_exports));
console.log("Rendering ================>");
ui.renderui();
}