@visulima/api-platform
Version:
Visulima API platform is a set of tools to build and consume web APIs
6 lines • 8.27 kB
JavaScript
import {execSync}from'child_process';import {existsSync,statSync,rmSync,readdirSync,readFileSync}from'fs';import X,{cwd}from'process';import {join,extname,parse,toNamespacedPath}from'@visulima/path';import h from'chalk';import {parseFile,jsDocumentCommentsToOpenApi,swaggerJsDocumentCommentsToOpenApi}from'@visulima/jsdoc-open-api';import {collect}from'@visulima/fs';var Q=(e,r)=>{if(r.length===0)throw new Error("must include at least one key to map");let t=e.toString();return r.forEach(o=>{t=o.optional?t.replace("(?:\\/([^\\/]+?))?\\",`/:${o.name}?`):t.replace(String.raw`(?:([^\/]+?))`,`:${o.name}`);}),t.replace(String.raw`/?(?=\/|$)/i`,"").replace("/^","").replaceAll("\\","").replaceAll(/\/{2,}/gu,"/")},T=Q;var V=(e,r)=>{if(typeof e=="string")return e;if(e.fast_slash)return "";if(e.fast_star)return "*";let t="";r.length>0&&(t=T(e,r));let o=/^\/\^((?:\\[$()*+./?[\\\]^{|}]|[^$()*+./?[\\\]^{|}])*)\$\//u.exec(e.toString().replace(String.raw`\/?`,"").replace(String.raw`(?=\/|$)`,"$"));return Array.isArray(o)&&o.length>1?o[1].replaceAll(/\\(.)/gu,"$1").slice(1):t?t.slice(1):e.toString()},D=V;var Z=(e,r,t)=>{let o=e.route.stack.at(-1),n=r.map(a=>({in:"path",name:a.name,required:!a.optional})),s=e.route.stack.filter(a=>a.handle.metadata);if(s.length>1)throw new Error("Only one metadata middleware is allowed per route");let p=(t+e.route.path).replaceAll(/\/{2,}/gu,"/");return s.length===0?{method:o.method,path:p,pathParams:n}:{metadata:s[0].handle.metadata,method:o.method,path:p,pathParams:n}},$=(e,r,t,o)=>{if(o=[...o,...t.keys],t.name==="router"&&t.handle?.stack!==void 0)for(let n of t.handle.stack)r=r||"",$(e,`${r}/${D(t.regexp,t.keys)}`,n,o);!t.route||t.route.stack.length===0||e.push(Z(t,o,r));},ee=e=>{let r=e._router||e.router,t=[];for(let o of r.stack)$(t,"",o,[]);return t},j=ee;var te=e=>{let r=[];return j(e).forEach(t=>{r.push({file:"unknown",method:t.method.toUpperCase(),path:t.path,tags:[]});}),r},v=te;var O=e=>e.replaceAll(/ \(.*\)/gu,"").trim(),b=e=>e.trim().split(" ")[1].slice(1,-1),re=e=>{let t=e.printRoutes().replaceAll(/[─│└├]/gu," ").trimEnd().split(`
`),o=t.reduce((s,p,a)=>{let i=O(p);if(O(t[a-1]??"")===i){let R=s.filter(k=>k.index<a&&k.segment===i),{methods:A}=R.at(-1);return A!==null&&A.push(b(p)),s}let c=p.replaceAll(/ \(.*\)/gu,"").match(/ /gu);if(c===null)throw new Error("Invalid spaces");let l=c.length/4,d=p.includes("("),x=d?[b(p)]:null;return s.push({depth:l,index:a,isRoute:d,methods:x,segment:i}),s},[]),n=[];return o.filter(s=>s.isRoute).forEach(s=>{let a=[...o.filter(i=>i.index<s.index&&i.depth<s.depth).filter((i,u,c)=>!c.find(l=>l.depth===i.depth&&l.index>i.index)).map(i=>i.segment),s.segment].join("");if(s.methods===null)throw new Error("Invalid methods");s.methods.forEach(i=>{n.push({file:"unknown",method:i.toUpperCase(),path:a,tags:[]});});}),n},C=re;var oe=e=>{let t=e._core.router.routes,o=[];return [...t.keys()].forEach(n=>{t.get(n).routes.forEach(s=>{o.push({file:"unknown",method:s.route.method.toUpperCase(),path:s.path,tags:[]});});}),o},F=oe;var se=e=>{let r=[];return e.middleware.filter(t=>t.router).flatMap(t=>t.router.stack).forEach(t=>{r.push({file:"unknown",method:t.methods.join("|").toUpperCase(),path:t.path,tags:[]});}),r},L=se;var M=/\.(js|ts|mjs|cjs)$/u,ce=(e,r,t=false)=>{e=toNamespacedPath(e);let o=toNamespacedPath(cwd()),n=[],s=parseFile(e,jsDocumentCommentsToOpenApi,t);n=[...n,...s.map(i=>i.spec)];let p=parseFile(e,swaggerJsDocumentCommentsToOpenApi,t);n=[...n,...p.map(i=>i.spec)];let a=[];return n.length===0?(readFileSync(e,"utf8").split(/\r?\n/u).forEach(u=>{let c=/[=aces|]+\s["'|](GET|POST|PUT|PATCH|HEAD|DELETE|OPTIONS)["'|]/u.exec(u);if(c){let[,l]=c;l==="GET"&&(l="GET|HEAD"),a.push({file:e.replace(`${o}/`,""),method:l,path:toNamespacedPath(e.replace(r,"").replace(M,"")),tags:[]});}}),a.length===0&&a.push({file:e.replace(`${o}/`,""),method:"GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS",path:toNamespacedPath(e.replace(r,"").replace(M,"")),tags:[]}),a):(n.forEach(i=>{Object.entries(i?.paths??{}).forEach(([c,l])=>{Object.entries(l).forEach(([x,R])=>{a.push({file:e.replace(`${o}/`,""),method:x.toUpperCase(),path:toNamespacedPath(c),tags:R.tags});});});}),a)},I=ce;var E=[".js",".ts",".mjs",".cjs"],K=e=>{let r=parse(e);for(;r.base&&r.root!==r.dir;){if(readdirSync(r.dir).find(n=>n==="package.json"))return r.dir;r=parse(r.dir);}return null},G=e=>{let r=`${e}/package.json`,{dependencies:t}=JSON.parse(readFileSync(r).toString());return t?.express?"express":t?.koa&&(t["@koa/router"]||t["koa-router"])?"koa":t?.next?"next":t?.["@hapi/hapi"]?"hapi":t?.fastify?"fastify":null},J=(e,r)=>Object.keys(e).length===0?null:r==="hapi"?typeof e.app.app=="string"?e.app:e:e.app??e;var U=e=>{try{return statSync(e).isDirectory()}catch{return false}},he=async(e="")=>{let r=join(e,"pages/api");return !U(r)&&(r=join(e,"src/pages/api"),!U(r))?[]:collect(r,{extensions:E,includeDirs:false})},W=he;var w=async(e,r,t)=>{if(r==="express")return v(e);if(r==="koa")return L(e);if(r==="hapi")return F(e);if(r==="fastify")return C(e);if(r==="next"){let o=await W(e);if(o.length===0)throw new Error(`No API routes found, in "${e}".`);return o.flatMap(n=>I(n,e,t))}return null};var de=(e,r)=>{let t=new Map;return e.forEach(o=>{let n=r(o),s=t.get(n);s?s.push(o):t.set(n,[o]);}),t},B=de;var ge=(e,r)=>{let t={ANY:h.redBright,DELETE:h.redBright,GET:h.blue,HEAD:h.hex("#6C7280"),OPTIONS:h.hex("#6C7280"),PATCH:h.yellow,POST:h.yellow,PUT:h.yellow},o;if(e==="GET|HEAD")o=`${h.blue("GET")}${h.grey("|HEAD")}`;else {let u=t[e](e);o=e==="GET"?`${u}${h.grey("|HEAD")}`:u;}let n=e==="GET"?6:14-e.length,s=Array.from({length:n}).fill(" ").join(""),p=process.stdout.columns-16-r.length-4,a=p>0?Array.from({length:p}).fill(".").join(""):"",i=r.split("/").map(u=>[":","["].includes(u[0]??"")?h.yellowBright(u):u).join("/");return ` ${o}${s}${i}${h.grey(a)}`},ye=(e,r={})=>e.map(t=>{if(!(Array.isArray(r.methods)&&r.methods.includes(t.method)))return t.method==="GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS"&&(t.method="ANY"),ge(t.method,t.path.replace("/pages",""))}).filter(Boolean),P=ye;var Re=async(e,r,t={})=>{let o=join(X.cwd(),r);if(!existsSync(o))throw new Error("No such file, invalid path provided.");let n=K(o);if(!n)throw new Error("Please initialize local package.json.");if(e===void 0){let p=G(n);if(!p)throw new Error("Couldn't detect supported back-end framework.");e=p;}let s=null;if(e==="next")s=await w(o,"next",t.verbose??false);else {if(!statSync(o).isFile())throw new Error(`${o} is directory, but file expected.`);if(!E.includes(extname(o)))throw new Error("Please specify application .ts/.js/.mjs/.cjs file.");let p=`${n}/.env`;existsSync(p)&&(await import(`${n}/node_modules/dotenv/lib/main.js`)).config({path:p});let a=extname(o)===".ts",i=join(n,"node_modules/.bin/tsc");if(a&&!existsSync(i))throw new Error(`Please install typescript in ${n}`);try{if(a)try{execSync(`${i} --outDir framework-list >&2`,{cwd:n});}catch(l){console.log(`TSC compilation failed. Please resolve issues in your project.
`),console.log(l),rmSync(join(n,"framework-list"),{recursive:!0});}let u=a?join(n,"framework-list",o.replace(n,"").replace(".ts",".js")):o,{default:c}=await import(u);s=await w(["AsyncFunction","Function"].includes(c.constructor.name)?await c():J(c,e),e,t.verbose??!1);}finally{a&&rmSync(join(n,"framework-list"),{recursive:true});}}if(s===null)throw new Error(`Framework "${e}" is not supported.`);if(Array.isArray(t.includePaths)&&t.includePaths.length>0&&(s=t.includePaths.flatMap(p=>s.filter(a=>a.path.startsWith(p)))),Array.isArray(t.excludePaths)&&t.excludePaths.length>0&&(s=t.excludePaths.flatMap(p=>s.filter(a=>!a.path.startsWith(p)))),typeof t.group=="string"&&t.group!==""){console.log();let p=B(s,i=>t.group==="path"?i.path.replace("/pages","").split("/")[1]:i.tags[0]??"unsorted"),a=0;p.forEach((i,u)=>{a>0&&console.log();let c=(X.stdout.columns-16-u.length)/2,l=c>0?Array.from({length:c}).fill(" ").join(""):"";console.log(l+h.bold.underline(u)),P(i,t).forEach(d=>{console.log(d);}),a+=1;});}else console.log(),P(s,t).forEach(p=>{console.log(p);});console.log(`
Listed ${h.greenBright(String(s.length))} HTTP ${s.length===1?"route":"routes"}.
`);},Pt=Re;export{Pt as a};//# sourceMappingURL=chunk-JPUMGEAE.mjs.map
//# sourceMappingURL=chunk-JPUMGEAE.mjs.map