@visulima/api-platform
Version:
Visulima API platform is a set of tools to build and consume web APIs
6 lines • 11.6 kB
JavaScript
;var child_process=require('child_process'),fs=require('fs'),ee=require('process'),path=require('@visulima/path'),d=require('chalk'),jsdocOpenApi=require('@visulima/jsdoc-open-api'),promises=require('fs/promises'),utils=require('@visulima/path/utils');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var ee__default=/*#__PURE__*/_interopDefault(ee);var d__default=/*#__PURE__*/_interopDefault(d);var oe=(e,t)=>{if(t.length===0)throw new Error("must include at least one key to map");let r=e.toString();return t.forEach(o=>{r=o.optional?r.replace("(?:\\/([^\\/]+?))?\\",`/:${o.name}?`):r.replace("(?:([^\\/]+?))",`:${o.name}`);}),r.replace("/?(?=\\/|$)/i","").replace("/^","").replaceAll("\\","").replaceAll(/\/{2,}/gu,"/")},_=oe;var se=(e,t)=>{if(typeof e=="string")return e;if(e.fast_slash)return "";if(e.fast_star)return "*";let r="";t.length>0&&(r=_(e,t));let o=/^\/\^((?:\\[$()*+./?[\\\]^{|}]|[^$()*+./?[\\\]^{|}])*)\$\//u.exec(e.toString().replace("\\/?","").replace("(?=\\/|$)","$"));return Array.isArray(o)&&o.length>1?o[1].replaceAll(/\\(.)/gu,"$1").slice(1):r?r.slice(1):e.toString()},O=se;var ne=(e,t,r)=>{let o=e.route.stack.at(-1),s=t.map(i=>({in:"path",name:i.name,required:!i.optional})),n=e.route.stack.filter(i=>i.handle.metadata);if(n.length>1)throw new Error("Only one metadata middleware is allowed per route");let l=(r+e.route.path).replaceAll(/\/{2,}/gu,"/");return n.length===0?{method:o.method,path:l,pathParams:s}:{metadata:n[0].handle.metadata,method:o.method,path:l,pathParams:s}},j=(e,t,r,o)=>{if(o=[...o,...r.keys],r.name==="router"&&r.handle?.stack!==void 0)for(let s of r.handle.stack)t=t||"",j(e,`${t}/${O(r.regexp,r.keys)}`,s,o);!r.route||r.route.stack.length===0||e.push(ne(r,o,t));},ae=e=>{let t=e._router||e.router,r=[];for(let o of t.stack)j(r,"",o,[]);return r},$=ae;var ie=e=>{let t=[];return $(e).forEach(r=>{t.push({file:"unknown",method:r.method.toUpperCase(),path:r.path,tags:[]});}),t},L=ie;var I=e=>e.replaceAll(/ \(.*\)/gu,"").trim(),C=e=>e.trim().split(" ")[1].slice(1,-1),pe=e=>{let r=e.printRoutes().replaceAll(/[─│└├]/gu," ").trimEnd().split(`
`),o=r.reduce((n,l,i)=>{let p=I(l);if(I(r[i-1]??"")===p){let R=n.filter(F=>F.index<i&&F.segment===p),{methods:v}=R.at(-1);return v!==null&&v.push(C(l)),n}let c=l.replaceAll(/ \(.*\)/gu,"").match(/ /gu);if(c===null)throw new Error("Invalid spaces");let a=c.length/4,m=l.includes("("),P=m?[C(l)]:null;return n.push({depth:a,index:i,isRoute:m,methods:P,segment:p}),n},[]),s=[];return o.filter(n=>n.isRoute).forEach(n=>{let i=[...o.filter(p=>p.index<n.index&&p.depth<n.depth).filter((p,f,c)=>!c.find(a=>a.depth===p.depth&&a.index>p.index)).map(p=>p.segment),n.segment].join("");if(n.methods===null)throw new Error("Invalid methods");n.methods.forEach(p=>{s.push({file:"unknown",method:p.toUpperCase(),path:i,tags:[]});});}),s},N=pe;var le=e=>{let r=e._core.router.routes,o=[];return [...r.keys()].forEach(s=>{r.get(s).routes.forEach(n=>{o.push({file:"unknown",method:n.route.method.toUpperCase(),path:n.path,tags:[]});});}),o},M=le;var ce=e=>{let t=[];return e.middleware.filter(r=>r.router).flatMap(r=>r.router.stack).forEach(r=>{t.push({file:"unknown",method:r.methods.join("|").toUpperCase(),path:r.path,tags:[]});}),t},K=ce;var H=/\.(js|ts|mjs|cjs)$/u,he=(e,t,r=false)=>{e=path.toNamespacedPath(e);let o=path.toNamespacedPath(ee.cwd()),s=[],n=jsdocOpenApi.parseFile(e,jsdocOpenApi.jsDocumentCommentsToOpenApi,r);s=[...s,...n.map(p=>p.spec)];let l=jsdocOpenApi.parseFile(e,jsdocOpenApi.swaggerJsDocumentCommentsToOpenApi,r);s=[...s,...l.map(p=>p.spec)];let i=[];return s.length===0?(fs.readFileSync(e,"utf8").split(/\r?\n/u).forEach(f=>{let c=/[=aces|]+\s["'|](GET|POST|PUT|PATCH|HEAD|DELETE|OPTIONS)["'|]/u.exec(f);if(c){let[,a]=c;a==="GET"&&(a="GET|HEAD"),i.push({file:e.replace(`${o}/`,""),method:a,path:path.toNamespacedPath(e.replace(t,"").replace(H,"")),tags:[]});}}),i.length===0&&i.push({file:e.replace(`${o}/`,""),method:"GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS",path:path.toNamespacedPath(e.replace(t,"").replace(H,"")),tags:[]}),i):(s.forEach(p=>{Object.entries(p?.paths??{}).forEach(([c,a])=>{Object.entries(a).forEach(([P,R])=>{i.push({file:e.replace(`${o}/`,""),method:P.toUpperCase(),path:path.toNamespacedPath(c),tags:R.tags});});});}),i)},J=he;var ge=Object.defineProperty,ye=(e,t)=>ge(e,"name",{value:t,configurable:true}),U=ye(e=>{if(!e||!(e instanceof URL)&&typeof e!="string")throw new TypeError("Path must be a non-empty string or URL.")},"assertValidFileOrDirectoryPath");var xe=Object.defineProperty,Ee=(e,t)=>xe(e,"name",{value:t,configurable:true}),y=class extends Error{static{Ee(this,"WalkError");}root;constructor(t,r){super(`${t instanceof Error?t.message:t} for path "${r}"`),this.cause=t,this.root=r;}get name(){return "WalkError"}set name(t){throw new Error("Cannot overwrite name of WalkError")}};var we=Object.defineProperty,Se=(e,t)=>we(e,"name",{value:t,configurable:true}),k=Se(e=>{let t=e.replace(/\.\*/g,".([^/]*)").replace(/\*\*/g,"(.*)").replace(/(?<!\.)\*(?!\*)/g,"([^/]*)").replace(/\?/g,"[^/]").replace(/\.(?!\*)/g,"\\.").replace(/\{/g,"(").replace(/\}/g,")").replace(/,/g,"|").replace(/\[!(.*?)\]/g,"[^$1]");return new RegExp(`^${t}$`)},"globToRegExp"),Pe=Object.defineProperty,Re=(e,t)=>Pe(e,"name",{value:t,configurable:true}),x=Re((e,t,r,o)=>Array.isArray(t)&&t.length>0&&!t.some(s=>e.endsWith(s))||r&&!r.some(s=>s.test(e))?false:!o?.some(s=>s.test(e)),"walkInclude");var Oe=Object.defineProperty,h=(e,t)=>Oe(e,"name",{value:t,configurable:true}),je=h(async e=>{let t=path.normalize(e),r=path.basename(t),o=await promises.stat(t);return {isDirectory:h(()=>o.isDirectory(),"isDirectory"),isFile:h(()=>o.isFile(),"isFile"),isSymbolicLink:h(()=>o.isSymbolicLink(),"isSymbolicLink"),name:r,path:t}},"_createWalkEntry");async function*w(e,{extensions:t,followSymlinks:r=false,includeDirs:o=true,includeFiles:s=true,includeSymlinks:n=true,match:l,maxDepth:i=Number.POSITIVE_INFINITY,skip:p}={}){if(U(e),i<0)return;let f=l?l.map(a=>typeof a=="string"?k(a):a):void 0,c=p?p.map(a=>typeof a=="string"?k(a):a):void 0;if(e=path.resolve(utils.toPath(e)),o&&x(e,t,f,c)&&(yield await je(e)),!(i<1||!x(e,void 0,void 0,c)))try{for await(let a of await promises.readdir(e,{withFileTypes:!0})){let m=path.join(e,a.name);if(a.isSymbolicLink())if(r)m=await promises.realpath(m);else if(n&&x(m,t,f,c))yield {isDirectory:a.isDirectory,isFile:a.isFile,isSymbolicLink:a.isSymbolicLink,name:a.name,path:m};else continue;a.isSymbolicLink()||a.isDirectory()?yield*w(m,{extensions:t,followSymlinks:r,includeDirs:o,includeFiles:s,includeSymlinks:n,match:f,maxDepth:i-1,skip:c}):a.isFile()&&s&&x(m,t,f,c)&&(yield {isDirectory:h(()=>a.isDirectory(),"isDirectory"),isFile:h(()=>a.isFile(),"isFile"),isSymbolicLink:h(()=>a.isSymbolicLink(),"isSymbolicLink"),name:a.name,path:m});}}catch(a){throw a instanceof y?a:new y(a,e)}}h(w,"walk");var $e=Object.defineProperty,Le=(e,t)=>$e(e,"name",{value:t,configurable:true}),A=Le(async(e,t={})=>{Array.isArray(t.extensions)||(t.extensions=["js","mjs","cjs","ts"]);let r=[];for await(let o of w(e,t))r.push(o.path);return r},"collect");var S=[".js",".ts",".mjs",".cjs"],B=e=>{let t=path.parse(e);for(;t.base&&t.root!==t.dir;){if(fs.readdirSync(t.dir).find(s=>s==="package.json"))return t.dir;t=path.parse(t.dir);}return null},q=e=>{let t=`${e}/package.json`,{dependencies:r}=JSON.parse(fs.readFileSync(t).toString());return r?.express?"express":r?.koa&&(r["@koa/router"]||r["koa-router"])?"koa":r?.next?"next":r?.["@hapi/hapi"]?"hapi":r?.fastify?"fastify":null},V=(e,t)=>Object.keys(e).length===0?null:t==="hapi"?typeof e.app.app=="string"?e.app:e:e.app??e;var z=e=>{try{return fs.statSync(e).isDirectory()}catch{return false}},Me=async(e="")=>{let t=path.join(e,"pages/api");return !z(t)&&(t=path.join(e,"src/pages/api"),!z(t))?[]:A(t,{extensions:S,includeDirs:false})},Y=Me;var b=async(e,t,r)=>{if(t==="express")return L(e);if(t==="koa")return K(e);if(t==="hapi")return M(e);if(t==="fastify")return N(e);if(t==="next"){let o=await Y(e);if(o.length===0)throw new Error(`No API routes found, in "${e}".`);return o.flatMap(s=>J(s,e,r))}return null};var Ke=(e,t)=>{let r=new Map;return e.forEach(o=>{let s=t(o),n=r.get(s);n?n.push(o):r.set(s,[o]);}),r},Q=Ke;var We=(e,t)=>{let r={ANY:d__default.default.redBright,DELETE:d__default.default.redBright,GET:d__default.default.blue,HEAD:d__default.default.hex("#6C7280"),OPTIONS:d__default.default.hex("#6C7280"),PATCH:d__default.default.yellow,POST:d__default.default.yellow,PUT:d__default.default.yellow},o;if(e==="GET|HEAD")o=`${d__default.default.blue("GET")}${d__default.default.grey("|HEAD")}`;else {let f=r[e](e);o=e==="GET"?`${f}${d__default.default.grey("|HEAD")}`:f;}let s=e==="GET"?6:14-e.length,n=Array.from({length:s}).fill(" ").join(""),l=process.stdout.columns-16-t.length-4,i=l>0?Array.from({length:l}).fill(".").join(""):"",p=t.split("/").map(f=>[":","["].includes(f[0]??"")?d__default.default.yellowBright(f):f).join("/");return ` ${o}${n}${p}${d__default.default.grey(i)}`},He=(e,t={})=>e.map(r=>{if(!(Array.isArray(t.methods)&&t.methods.includes(r.method)))return r.method==="GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS"&&(r.method="ANY"),We(r.method,r.path.replace("/pages",""))}).filter(Boolean),D=He;var Ge=async(e,t,r={})=>{let o=path.join(ee__default.default.cwd(),t);if(!fs.existsSync(o))throw new Error("No such file, invalid path provided.");let s=B(o);if(!s)throw new Error("Please initialize local package.json.");if(e===void 0){let l=q(s);if(!l)throw new Error("Couldn't detect supported back-end framework.");e=l;}let n=null;if(e==="next")n=await b(o,"next",r.verbose??false);else {if(!fs.statSync(o).isFile())throw new Error(`${o} is directory, but file expected.`);if(!S.includes(path.extname(o)))throw new Error("Please specify application .ts/.js/.mjs/.cjs file.");let l=`${s}/.env`;fs.existsSync(l)&&(await import(`${s}/node_modules/dotenv/lib/main.js`)).config({path:l});let i=path.extname(o)===".ts",p=path.join(s,"node_modules/.bin/tsc");if(i&&!fs.existsSync(p))throw new Error(`Please install typescript in ${s}`);try{if(i)try{child_process.execSync(`${p} --outDir framework-list >&2`,{cwd:s});}catch(a){console.log(`TSC compilation failed. Please resolve issues in your project.
`),console.log(a),fs.rmSync(path.join(s,"framework-list"),{recursive:!0});}let f=i?path.join(s,"framework-list",o.replace(s,"").replace(".ts",".js")):o,{default:c}=await import(f);n=await b(["AsyncFunction","Function"].includes(c.constructor.name)?await c():V(c,e),e,r.verbose??!1);}finally{i&&fs.rmSync(path.join(s,"framework-list"),{recursive:true});}}if(n===null)throw new Error(`Framework "${e}" is not supported.`);if(Array.isArray(r.includePaths)&&r.includePaths.length>0&&(n=r.includePaths.flatMap(l=>n.filter(i=>i.path.startsWith(l)))),Array.isArray(r.excludePaths)&&r.excludePaths.length>0&&(n=r.excludePaths.flatMap(l=>n.filter(i=>!i.path.startsWith(l)))),typeof r.group=="string"&&r.group!==""){console.log();let l=Q(n,p=>r.group==="path"?p.path.replace("/pages","").split("/")[1]:p.tags[0]??"unsorted"),i=0;l.forEach((p,f)=>{i>0&&console.log();let c=(ee__default.default.stdout.columns-16-f.length)/2,a=c>0?Array.from({length:c}).fill(" ").join(""):"";console.log(a+d__default.default.bold.underline(f)),D(p,r).forEach(m=>{console.log(m);}),i+=1;});}else console.log(),D(n,r).forEach(l=>{console.log(l);});console.log(`
Listed ${d__default.default.greenBright(String(n.length))} HTTP ${n.length===1?"route":"routes"}.
`);},mr=Ge;exports.a=mr;//# sourceMappingURL=chunk-OSYBNW3V.js.map
//# sourceMappingURL=chunk-OSYBNW3V.js.map