hono-openapi
Version:
OpenAPI schema generator for Hono
2 lines (1 loc) • 4.41 kB
JavaScript
;var e=require("./utils.cjs"),t=require("hono/http-exception");const n=["GET","PUT","POST","DELETE","OPTIONS","HEAD","PATCH","TRACE"],s=e=>e.charAt(0).toUpperCase()+e.slice(1),o=new Map,a=(e,t)=>{const n=`${e}:${t}`;if(o.has(n))return o.get(n);let a=e;if("/"===t)return`${a}Index`;for(const e of t.split("/"))123===e.charCodeAt(0)?a+=`By${s(e.slice(1,-1))}`:a+=s(e);return o.set(n,a),a};function c({path:e,method:t,data:n,schema:s}){e=(e=>e.split("/").map((e=>{let t=e;return t.startsWith(":")&&(t=t.slice(1,t.length),t.endsWith("?")&&(t=t.slice(0,-1)),t=`{${t}}`),t})).join("/"))(e);const o=t.toLowerCase();s[e]={...s[e]?s[e]:{},[o]:{responses:{},operationId:a(o,e),...s[e]?.[o]??{},...n,parameters:r(s[e]?.[o]?.parameters??[],n.parameters??[])}}}function r(e,t){const n=e=>"$ref"in e?e.$ref:`${e.in} ${e.name}`;if(0===e.length||0===t.length)return 0===e.length?t:e;const s=t.reduce(((e,t)=>(e.set(n(t),t),e)),new Map),o=e.reduce(((e,t)=>{const o=n(t),a=s.get(o);return s.delete(o),e.push(a??t),e}),[]);return o.push(...s.values()),o}function i(e,{excludeStaticFile:t=!0,exclude:n=[]}){const s={},o=Array.isArray(n)?n:[n];for(const[n,a]of Object.entries(e))if(!(o.some((e=>"string"==typeof e?n===e:e.test(n)))||n.includes("*")||t&&n.includes("."))){for(const e of Object.keys(a)){const t=a[e];if(n.includes("{")){t.parameters||(t.parameters=[]);const e=n.split("/").filter((e=>e.startsWith("{")&&!t.parameters.find((t=>"path"===t.in&&t.name===e.slice(1,e.length-1)))));for(const n of e){const e=n.slice(1,n.length-1),s=t.parameters.findIndex((t=>"param"===t.in&&t.name===e));-1!==s?t.parameters[s].in="path":t.parameters.push({schema:{type:"string"},in:"path",name:e,required:!0})}}t.responses||(t.responses={200:{}})}s[n]=a}return s}async function p(t,{documentation:s={},excludeStaticFile:o=!0,exclude:a=[],excludeMethods:r=["OPTIONS"],excludeTags:p=[],defaultOptions:l}={documentation:{},excludeStaticFile:!0,exclude:[],excludeMethods:["OPTIONS"],excludeTags:[]},{version:u="3.1.0",components:d={}}={version:"3.1.0",components:{}},m){const h={version:u,components:d},f={};for(const s of t.routes){if(!(e.uniqueSymbol in s.handler))continue;if(r.includes(s.method))continue;if(!1===n.includes(s.method)&&"ALL"!==s.method)continue;const{resolver:t,metadata:o={}}=s.handler[e.uniqueSymbol],a=l?.[s.method],{docs:i,components:p}=await t({...h,...o},a);if(h.components={...h.components,...p??{}},"ALL"===s.method)for(const e of n)c({path:s.path,data:i,method:e,schema:f});else c({method:s.method,path:s.path,data:i,schema:f})}for(const e in f)for(const t in f[e]){const n=f[e][t]?.hide;n&&("boolean"==typeof n?n:m&&n(m))&&delete f[e][t]}return{openapi:h.version,...{...s,tags:s.tags?.filter((e=>!p?.includes(e?.name))),info:{title:"Hono Documentation",description:"Development documentation",version:"0.0.0",...s.info},paths:{...i(f,{excludeStaticFile:o,exclude:a}),...s.paths},components:{...s.components,schemas:{...h.components,...s.components?.schemas}}}}}async function l(e,t,n={}){let s={};const o={...n,...t,responses:{...n?.responses,...t.responses}};if(o.responses)for(const t of Object.keys(o.responses)){const n=o.responses[t];if(n&&"content"in n)for(const t of Object.keys(n.content??{})){const o=n.content?.[t];if(o&&(o.schema&&"builder"in o.schema)){const t=await o.schema.builder(e);o.schema=t.schema,t.components&&(s={...s,...t.components})}}}return{docs:o,components:s}}exports.generateValidatorDocs=e.generateValidatorDocs,exports.uniqueSymbol=e.uniqueSymbol,exports.describeRoute=function(n){const{validateResponse:s,...o}=n;return Object.assign((async(e,o)=>{if(await o(),s&&n.responses){const o=e.res.status,a=e.res.headers.get("content-type");if(o&&a){const c=n.responses[o];if(c&&"content"in c&&c.content){const n=a.split(";")[0],o=c.content[n];if(o?.schema&&"validator"in o.schema)try{let t;const s=e.res.clone();if("application/json"===n?t=await s.json():"text/plain"===n&&(t=await s.text()),!t)throw new Error("No data to validate!");await o.schema.validator(t)}catch(e){let n={status:500,message:"Response validation failed!"};throw"object"==typeof s&&(n={...n,...s}),new t.HTTPException(n.status,{message:n.message,cause:e})}}}}}),{[e.uniqueSymbol]:{resolver:(e,t)=>l(e,o,t)}})},exports.generateRouteSpecs=l,exports.generateSpecs=p,exports.openAPISpecs=function(e,t){const n={version:"3.1.0",components:{}};let s=null;return async o=>(s||(s=await p(e,t,n,o)),o.json(s))};