-
Notifications
You must be signed in to change notification settings - Fork 0
/
insomnie.js
executable file
·21 lines (18 loc) · 9.94 KB
/
insomnie.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env node
var p=Object.defineProperty;var E=Object.getOwnPropertyDescriptor;var L=Object.getOwnPropertyNames;var N=Object.prototype.hasOwnProperty;var J=(e,t)=>()=>(e&&(t=e(e=0)),t);var F=(e,t)=>{for(var s in t)p(e,s,{get:t[s],enumerable:!0})},U=(e,t,s,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of L(t))!N.call(e,i)&&i!==s&&p(e,i,{get:()=>t[i],enumerable:!(r=E(t,i))||r.enumerable});return e};var V=e=>U(p({},"__esModule",{value:!0}),e);var C={};F(C,{preview:()=>g,renderui:()=>K,screen:()=>d,sendButton:()=>S});import o from"blessed";var M,d,u,me,m,ge,W,he,X,fe,z,S,g,_,K,O=J(()=>{M=`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.`,d=o.screen({smartCSR:!0}),u=o.form({parent:d,width:"50%",height:"100%",scrollable:!0,keys:!0,vi:!0,mouse:!0,scrollbar:{style:{fg:"white",bg:"blue"}}}),me=o.text({parent:u,top:1,left:2,content:"M\xE9todo:"}),m=o.list({parent:u,top:2,left:2,width:"96%",height:3,keys:!0,mouse:!0,autoCommandKeys:!0,border:{type:"line"},style:{item:{fg:"white",bg:"black",hover:{bg:"green"}},selected:{bg:"green"}},items:["GET","POST","PUT","DELETE"]}),ge=o.text({parent:u,top:6,left:2,content:"URL:"}),W=o.textbox({parent:u,top:7,left:2,width:"96%",height:3,vi:!0,keys:!0,mouse:!0,border:{type:"line"},style:{fg:"white",bg:"black",focus:{border:{fg:"green"}}}}),he=o.text({parent:u,top:10,left:2,content:"Headers:"}),X=o.textarea({parent:u,top:11,left:2,width:"96%",height:8,vi:!0,keys:!0,mouse:!0,border:{type:"line"},style:{fg:"white",bg:"black",focus:{border:{fg:"green"}}}}),fe=o.text({parent:u,top:21,left:2,content:"Body:"}),z=o.textarea({parent:u,top:22,left:2,width:"96%",height:10,vi:!0,keys:!0,mouse:!0,border:{type:"line"},style:{fg:"white",bg:"black",focus:{border:{fg:"green"}}}}),S=o.button({parent:u,top:34,left:"center",width:10,height:3,content:"Enviar",border:{type:"line"},vi:!0,keys:!0,mouse:!0,style:{fg:"white",bg:"black",focus:{border:{fg:"green"}}}}),g=o.box({parent:d,left:"50%-1",width:"50%",height:"100%",border:{type:"line"},style:{fg:"white",bg:"black"},scrollable:!0,keys:!0,vi:!0,mouse:!0,scrollbar:{style:{bg:"green",fg:"green"}}}),_=async()=>{let e=m.options?.items?m.options.items[m.selected]:"GET",t=W.getValue(),s=X.getValue(),r=z.getValue(),i={method:e,url:t,headers:s,body:r};g.setContent(`hola marco ${Date.now()}
${JSON.stringify(i,null,2)}`)},K=()=>{S.on("press",_),d.key(["escape","q","C-c"],()=>process.exit(0)),g.setContent(M),d.render()}});var $={call:async e=>{console.debug("Calling API");let t={data:null},s={method:e.type};return e.headers&&(s.headers=e.headers),e.body&&(s.body=JSON.stringify(e.body)),t=fetch(e.url,s),t},save:()=>"save",delete:()=>"delete"},b=$;import T from"chalk";import Q from"cli-table3";import{stdout as P}from"process";import a from"chalk";import A from"cli-table3";var B=async e=>{let t=new A({head:[a.white("URL"),a.white("Status")],colWidths:[80,18]});e.forEach(async(s,r)=>{if(s.status==="rejected")t.push([a.blue(r),a.red(s.status)]);else{let i;s.value.status==="Error"?i=a.red:s.value.status>=200&&s.value.status<300?i=a.green:i=a.yellow,t.push([a.blue(s.value.url),i(`${s.value.status} ${s.value.statusText}`)])}}),console.log(`
`+a.bold(`Requesting ${e.length} URLs:`)+`
`),console.log(t.toString())},y=async(e,t)=>{for(let s=0;s<t;s++){let r=[];for(let c of e)r.push(fetch(c));let i=await Promise.allSettled(r);B(i)}};import{Option as n,program as G}from"commander";var v={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:!1,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"}};var q=(e=!1)=>v.version;var w=e=>G.version(q(!0)).description("A Simple terminal CLI and TUI local first http client for developers.").addOption(new n("-chk, --check-health","this enables check health mode to make a helth check on given urls.")).addOption(new n("-tr, --tries <number>","Max try number (on chk is how many times is executed).").default(10)).addOption(new n("-u, --url <url>","URL to hit, full parth or base url to work with -up - url path")).addOption(new n("-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 n("-H, --headers <headers>","Headers in JSON format")).addOption(new n("-B, --body <body>","Request body")).addOption(new n("-t, --type <type>","request type GET, POST, ...").default("get").choices(["get","post","put","delete","patch","gql"])).addOption(new n("-rq, --request <id>","request to execute")).addOption(new n("-s, --save","Save request.")).addOption(new n("-d, --delete <id>","Delete the request with id:<id>")).addOption(new n("-v, --view <id>","Show all datails freom the request with id:<id>")).addOption(new n("-l, --list","Show all requests according current space.")).parse(e);var x=(e,t)=>t?t.split(",").map(r=>r.trim()).map(r=>`${e}${r}`):[e],k=e=>{let t={isJson:!1,isXml:!1,isHtml:!1,isText:!1,isBinary:!1},s=e.get("content-type");return t.isJson=s?.includes("json"),t.isXml=s?.includes("xml"),t.isHtml=s?.includes("html"),t.isText=s?.includes("text"),t};var Y=w(process.argv),l=Y.opts(),j=l.checkHealth??!1,Z=process.argv.length>2;l.type==="gql"&&(console.debug("Graphql is not implemented :c (not yet 7u7)"),process.exit(0));if(j){let e=x(l.url,l.urlpath);await y(e,l.tries),process.exit(0)}var h={"content-type":"application/json"};try{let e=JSON.parse(l.headers);h={...h,...e}}catch{console.debug("Invalid Request Headers On Cli Params.")}var I=null;l.body!==void 0&&(I=JSON.parse(l.body));if(Z&&!j){let e={url:l.url,type:l.type,headers:h,body:I},t=null,s=null;try{t=await b.call(e)}catch(H){s=H}s!==null&&(console.log("Error Calling API",s),process.exit(1)),console.log("Parsing Response Data ...");let r=null,i=k(t.headers);i.isJson&&(r=await t.json()),(i.isHtml||i.isXml||i.isText)&&(r=await t.text()),console.log("Printing Response Data ...");let c={status:t.status,stausText:t.stausText,data:r},f=new Q({style:{border:[],header:[]},head:[T.white("State")],wordWrap:!0,colWidths:[P.columns<50?50:Math.floor(P.columns/2)]}),R={url:e.url,status:`${c.status} ${c.stausText}`};f.push([JSON.stringify(R,null,1)]),console.log(f.toString()),console.log(T.yellow("Response Body:")),console.log(JSON.stringify(c.data,null,1))}else{let e=(O(),V(C));console.log("Rendering ================>"),e.renderui()}