express-zod-api
Version:
A Typescript framework to help you get an API server up and running with I/O schema validation and custom middlewares in minutes.
17 lines • 63.6 kB
JavaScript
import{createRequire as e}from"node:module";import{getBrand as t}from"@express-zod-api/zod-plugin";import*as n from"ramda";import{globalRegistry as r,z as i}from"zod";import a,{isHttpError as o}from"http-errors";import s,{blue as c,cyanBright as l,gray as u,green as d,hex as f,italic as p,red as m,whiteBright as h}from"ansis";import{inspect as g}from"node:util";import{performance as _}from"node:perf_hooks";import v from"express";import y from"node:http";import ee from"node:https";import{setInterval as te}from"node:timers/promises";import{OpenApiBuilder as ne,isReferenceObject as re,isSchemaObject as b}from"openapi3-ts/oas31";import{createRequest as ie,createResponse as ae}from"node-mocks-http";function oe(e){return e}const x={json:`application/json`,upload:`multipart/form-data`,raw:`application/octet-stream`,sse:`text/event-stream`,form:`application/x-www-form-urlencoded`},se=[`get`,`post`,`put`,`delete`,`patch`],ce=[...se,`head`],S=e=>se.includes(e),le=i.object({}),ue=/:([A-Za-z0-9_]+)/g,de=e=>e.match(ue)?.map(e=>e.slice(1))||[],fe=e=>{let t=(e.header(`content-type`)||``).toLowerCase().startsWith(x.upload);return`files`in e&&t},pe={get:[`query`,`params`],post:[`body`,`params`,`files`],put:[`body`,`params`],patch:[`body`,`params`],delete:[`query`,`params`]},me=[`body`,`query`,`params`],he=e=>e.method.toLowerCase(),ge=(e,t={})=>{if(e===`options`)return[];let n=e===`head`?`get`:S(e)?e:void 0;return(n?t[n]||pe[n]:void 0)||me},_e=(e,t={})=>ge(he(e),t).filter(t=>t===`files`?fe(e):!0).reduce((t,n)=>Object.assign(t,e[n]),{}),C=e=>e instanceof Error?e:e instanceof i.ZodError?new i.ZodRealError(e.issues):Error(String(e)),w=e=>e instanceof i.ZodError?e.issues.map(({path:e,message:t})=>`${e.length?`${i.core.toDotPath(e)}: `:``}${t}`).join(`; `):e.message,T=(e,t)=>O(e)&&`_zod`in e&&(t?n.path([`_zod`,`def`,`type`],e)===t:!0),E=(e,t,r)=>e.length&&t.length?n.xprod(e,t).map(r):e.concat(t),ve=e=>e.charAt(0).toUpperCase()+e.slice(1).toLowerCase(),D=(...e)=>{let t=n.chain(e=>e.split(/[^A-Z0-9]/gi),e);return n.chain(e=>e.replaceAll(/[A-Z]+/g,e=>`/${e}`).split(`/`),t).map(ve).join(``)},ye=n.tryCatch((e,t)=>typeof i.parse(e,t),n.always(void 0)),O=e=>typeof e==`object`&&!!e,be=n.memoizeWith(()=>`static`,()=>process.env.NODE_ENV===`production`),xe=(e,t)=>!!t&&e!==`head`,k=Symbol(`Buffer`),Se=()=>i.custom(e=>Buffer.isBuffer(e),{error:`Expected Buffer`}).brand(k),A=Symbol(`DateIn`),Ce=({examples:e,...t}={})=>i.union([i.iso.date(),i.iso.datetime(),i.iso.datetime({local:!0})]).meta({examples:e}).transform(e=>new Date(e)).pipe(i.date()).brand(A).meta(t),j=Symbol(`DateOut`),we=(e={})=>i.date().transform(e=>e.toISOString()).brand(j).meta(e);var M=class extends Error{name=`RoutingError`;cause;constructor(e,t,n){super(e),this.cause={method:t,path:n}}},N=class extends Error{name=`DocumentationError`;cause;constructor(e,{method:t,path:n,isResponse:r}){super(e),this.cause=`${r?`Response`:`Input`} schema of an Endpoint assigned to ${t.toUpperCase()} method of ${n} path.`}},Te=class extends Error{name=`IOSchemaError`},Ee=class extends Te{name=`DeepCheckError`;constructor(e){super(`Found`,{cause:e}),this.cause=e}},De=class extends Te{name=`OutputValidationError`;constructor(e){let t=new i.ZodError(e.issues.map(({path:e,...t})=>({...t,path:[`output`,...e]})));super(w(t),{cause:e}),this.cause=e}},P=class extends Te{name=`InputValidationError`;constructor(e){super(w(e),{cause:e}),this.cause=e}},Oe=class extends Error{name=`ResultHandlerError`;constructor(e,t){super(w(e),{cause:e}),this.cause=e,this.handled=t}},ke=class extends Error{name=`MissingPeerError`;constructor(e){super(`Missing peer dependency: ${e}. Please install it to use the feature.`)}};const Ae=Symbol(`Form`),je=e=>(e instanceof i.ZodObject?e:i.object(e)).brand(Ae),F=Symbol(`Upload`),Me=()=>i.custom(e=>typeof e==`object`&&!!e&&`name`in e&&`encoding`in e&&`mimetype`in e&&`data`in e&&`tempFilePath`in e&&`truncated`in e&&`size`in e&&`md5`in e&&`mv`in e&&typeof e.name==`string`&&typeof e.encoding==`string`&&typeof e.mimetype==`string`&&Buffer.isBuffer(e.data)&&typeof e.tempFilePath==`string`&&typeof e.truncated==`boolean`&&typeof e.size==`number`&&typeof e.md5==`string`&&typeof e.mv==`function`,{error:({input:e})=>({message:`Expected file upload, received ${typeof e}`})}).brand(F),I=Symbol(`Raw`),Ne=i.object({raw:Se()}),Pe=e=>Ne.extend(e).brand(I);function Fe(e){return e?Pe(e):Ne.brand(I)}const Ie=(e,{io:t,condition:r})=>n.tryCatch(()=>void i.toJSONSchema(e,{io:t,unrepresentable:`any`,override:({zodSchema:e})=>{if(r(e))throw new Ee(e)}}),e=>e.cause)(),Le=(e,{io:t})=>{let r=[i.toJSONSchema(e,{io:t,unrepresentable:`any`})];for(;r.length;){let e=r.shift();if(n.is(Object,e)){if(e.$ref===`#`)return!0;r.push(...n.values(e))}n.is(Array,e)&&r.push(...n.values(e))}return!1},Re=e=>Ie(e,{condition:e=>{let n=t(e);return typeof n==`symbol`&&[F,I,Ae].includes(n)},io:`input`}),ze=[`nan`,`symbol`,`map`,`set`,`bigint`,`void`,`promise`,`never`,`function`],Be=(e,n)=>Ie(e,{io:n,condition:e=>{let r=t(e),{type:i}=e._zod.def;return!!(ze.includes(i)||r===k||n===`input`&&(i===`date`||r===j)||n===`output`&&(r===A||r===I||r===F))}}),Ve=(e,{variant:t,args:n,...r})=>{if(typeof e==`function`&&(e=e(...n)),e instanceof i.ZodType)return[{schema:e,...r}];if(Array.isArray(e)&&!e.length)throw new Oe(Error(`At least one ${t} response schema required.`));return(Array.isArray(e)?e:[e]).map(({schema:e,statusCode:t,mimeType:n})=>({schema:e,statusCodes:typeof t==`number`?[t]:t||r.statusCodes,mimeTypes:typeof n==`string`?[n]:n===void 0?r.mimeTypes:n}))},He=(e,t,{url:n},r)=>!e.expose&&t.error(`Server side error`,{error:e,url:n,payload:r}),L=e=>o(e)?e:a(e instanceof P?400:500,w(e),{cause:e.cause||e}),R=e=>be()&&!e.expose?a(e.statusCode).message:e.message,Ue=e=>Object.entries(e._zod.def.shape).reduce((e,[t,i])=>{let{examples:a=[]}=r.get(i)||{};return E(e,a.map(n.objOf(t)),([e,t])=>({...e,...t}))},[]),We=({error:e,logger:t,response:n})=>{t.error(`Result handler failure`,e);let r=R(a(500,`An error occurred while serving the result: ${e.message}.`+(e.handled?`\nOriginal error: ${e.handled.message}.`:``),{expose:o(e.cause)?e.cause.expose:!1}));n.status(500).type(`text/plain`).end(r)};var Ge=class{},z=class extends Ge{#e;#t;#n;constructor({input:e,security:t,handler:n}){super(),this.#e=e,this.#t=t,this.#n=n}get security(){return this.#t}get schema(){return this.#e}async execute({input:e,...t}){try{let n=await(this.#e||le).parseAsync(e);return this.#n({...t,input:n})}catch(e){throw e instanceof i.ZodError?new P(e):e}}},Ke=class extends z{constructor(e,{provider:t=()=>({}),transformer:n=e=>e}={}){super({handler:async({request:r,response:i})=>new Promise((a,o)=>{let s=e=>{if(e&&e instanceof Error)return o(n(e));a(t(r,i))};e(r,i,s)?.catch(s)})})}},B=class{nest(e){return{...e,"":this}}},qe=class e extends B{#e;#t=n.once(()=>{if(r.get(this.#e.outputSchema)?.examples?.length||!T(this.#e.outputSchema,`object`))return;let e=Ue(this.#e.outputSchema);if(!e.length)return;let t=this.#e.outputSchema.meta();r.remove(this.#e.outputSchema).add(this.#e.outputSchema,{...t,examples:e})});constructor(e){super(),this.#e=e}#n(t){return new e({...this.#e,...t})}deprecated(){return this.#n({deprecated:!0})}get isDeprecated(){return this.#e.deprecated||!1}get description(){return this.#e.description}get shortDescription(){return this.#e.shortDescription}get methods(){return Object.freeze(this.#e.methods)}get inputSchema(){return this.#e.inputSchema}get outputSchema(){return this.#t(),this.#e.outputSchema}get requestType(){let e=Re(this.#e.inputSchema);if(e){let n=t(e);if(n===F)return`upload`;if(n===I)return`raw`;if(n===Ae)return`form`}return`json`}getResponses(e){return e===`positive`&&this.#t(),Object.freeze(e===`negative`?this.#e.resultHandler.getNegativeResponse():this.#e.resultHandler.getPositiveResponse(this.#e.outputSchema))}get security(){let e=n.pluck(`security`,this.#e.middlewares||[]);return n.reject(n.isNil,e)}get scopes(){return Object.freeze(this.#e.scopes||[])}get tags(){return Object.freeze(this.#e.tags||[])}getOperationId(e){return this.#e.getOperationId?.(e)}async#r(e){try{return await this.#e.outputSchema.parseAsync(e)}catch(e){throw e instanceof i.ZodError?new De(e):e}}async#i({method:e,logger:t,ctx:n,response:r,...i}){for(let a of this.#e.middlewares||[])if(!(e===`options`&&!(a instanceof Ke))&&(Object.assign(n,await a.execute({...i,ctx:n,response:r,logger:t})),r.writableEnded)){t.warn(`A middleware has closed the stream. Accumulated context:`,n);break}}async#a({input:e,...t}){let n;try{n=await this.#e.inputSchema.parseAsync(e)}catch(e){throw e instanceof i.ZodError?new P(e):e}return this.#e.handler({...t,input:n})}async#o(e){try{await this.#e.resultHandler.execute(e)}catch(t){We({...e,error:new Oe(C(t),e.error||void 0)})}}async execute({request:e,response:t,logger:n,config:r}){let i=he(e),a={},o={output:{},error:null},s=_e(e,r.inputSources);try{if(await this.#i({method:i,input:s,request:e,response:t,logger:n,ctx:a}),t.writableEnded)return;if(i===`options`)return void t.status(200).end();o={output:await this.#r(await this.#a({input:s,logger:n,ctx:a})),error:null}}catch(e){o={output:null,error:C(e)}}await this.#o({...o,input:s,request:e,response:t,logger:n,ctx:a})}};const Je=(e,t)=>e&&t?e.and(t):e||t,Ye=(e,t)=>e?e.and(t):t,V={positive:200,negative:400},H=Object.keys(V);var Xe=class{#e;constructor(e){this.#e=e}execute(...e){return this.#e(...e)}},U=class extends Xe{#e;#t;constructor(e){super(e.handler),this.#e=e.positive,this.#t=e.negative}getPositiveResponse(e){return Ve(this.#e,{variant:`positive`,args:[e],statusCodes:[V.positive],mimeTypes:[x.json]})}getNegativeResponse(){return Ve(this.#t,{variant:`negative`,args:[],statusCodes:[V.negative],mimeTypes:[x.json]})}};const Ze=i.object({status:i.literal(`error`),error:i.object({message:i.string()})});r.add(Ze,{examples:[{status:`error`,error:{message:`Sample error message`}}]});const W=new U({positive:e=>{let t=i.object({status:i.literal(`success`),data:e}),{examples:n}=r.get(e)||{};return n?.length&&r.add(t,{examples:n.map(e=>({status:`success`,data:e}))}),t},negative:Ze,handler:({error:e,input:t,output:n,request:r,response:i,logger:a})=>{if(e){let n=L(e);He(n,a,r,t),i.status(n.statusCode).set(n.headers).json({status:`error`,error:{message:R(n)}});return}i.status(V.positive).json({status:`success`,data:n})}}),Qe=i.string();r.add(Qe,{examples:[`Sample error message`]});const $e=new U({positive:e=>{let t=e instanceof i.ZodObject&&`items`in e.shape&&e.shape.items instanceof i.ZodArray?e.shape.items:i.array(i.any());if(r.get(t)?.examples?.length)return t;let n=r.get(e)?.examples?.filter(e=>O(e)&&`items`in e&&Array.isArray(e.items)).map(e=>e.items);if(n?.length){let e=t.meta();r.remove(t).add(t,{...e,examples:n})}return t},negative:{schema:Qe,mimeType:`text/plain`},handler:({response:e,output:t,error:n,logger:r,request:i,input:a})=>{if(n){let t=L(n);He(t,r,i,a),e.status(t.statusCode).type(`text/plain`).send(R(t));return}if(`items`in t&&Array.isArray(t.items)){e.status(V.positive).json(t.items);return}throw Error(`Property 'items' is missing in the endpoint output`)}});var G=class e{schema=void 0;middlewares=[];constructor(e){this.resultHandler=e}#e(t){let n=new e(this.resultHandler);return n.middlewares=this.middlewares.concat(t),n.schema=Je(this.schema,t.schema),n}addMiddleware(e){return this.#e(e instanceof z?e:new z(e))}use=this.addExpressMiddleware;addExpressMiddleware(...e){return this.#e(new Ke(...e))}addContext(e){return this.#e(new z({handler:e}))}build({input:e=le,output:t,operationId:n,scope:r,tag:i,method:a,...o}){let{middlewares:s,resultHandler:c}=this,l=typeof a==`string`?[a]:a,u=typeof n==`function`?n:e=>n&&`${n}${e===`head`?`__HEAD`:``}`,d=typeof r==`string`?[r]:r||[],f=typeof i==`string`?[i]:i||[];return new qe({...o,middlewares:s,outputSchema:t,resultHandler:c,scopes:d,tags:f,methods:l,getOperationId:u,inputSchema:Ye(this.schema,e)})}buildVoid({handler:e,...t}){return this.build({...t,output:le,handler:async t=>(await e(t),{})})}};const et=new G(W),tt=new G($e),nt={debug:c,info:d,warn:f(`#FFA500`),error:m,ctx:l},K={debug:10,info:20,warn:30,error:40},rt=e=>O(e)&&Object.keys(K).some(t=>t in e),it=e=>e in K,at=(e,t)=>K[e]<K[t],q=n.memoizeWith((e,t)=>`${e}${t}`,(e,t=0)=>Intl.NumberFormat(void 0,{useGrouping:!1,minimumFractionDigits:0,maximumFractionDigits:t,style:`unit`,unitDisplay:`long`,unit:e})),ot=e=>e<1e-6?q(`nanosecond`,3).format(e/1e-6):e<.001?q(`nanosecond`).format(e/1e-6):e<1?q(`microsecond`).format(e/.001):e<1e3?q(`millisecond`).format(e):e<6e4?q(`second`,2).format(e/1e3):q(`minute`,2).format(e/6e4);var st=class e{config;constructor({color:e=s.isSupported(),level:t=be()?`warn`:`debug`,depth:n=2,ctx:r={}}={}){this.config={color:e,level:t,depth:n,ctx:r}}format(e){let{depth:t,color:n,level:r}=this.config;return g(e,{depth:t,colors:n,breakLength:r===`debug`?80:1/0,compact:r===`debug`?3:!0})}print(e,t,n){let{level:r,ctx:{requestId:i,...a},color:o}=this.config;if(r===`silent`||at(e,r))return;let s=[new Date().toISOString()];i&&s.push(o?nt.ctx(i):i),s.push(o?`${nt[e](e)}:`:`${e}:`,t),n!==void 0&&s.push(this.format(n)),Object.keys(a).length>0&&s.push(this.format(a)),console.log(s.join(` `))}debug(e,t){this.print(`debug`,e,t)}info(e,t){this.print(`info`,e,t)}warn(e,t){this.print(`warn`,e,t)}error(e,t){this.print(`error`,e,t)}child(t){return new e({...this.config,ctx:t})}get ctx(){return this.config.ctx}profile(e){let t=_.now();return()=>{let n=_.now()-t,{message:r,severity:i=`debug`,formatter:a=ot}=typeof e==`object`?e:{message:e};this.print(typeof i==`function`?i(n):i,r,a(n))}}},ct=class{#e;constructor(...e){this.#e=e}apply(e,t){return t(e,v.static(...this.#e))}};const lt=async(e,t=`default`)=>{try{return(await import(e))[t]}catch{}throw new ke(e)},ut=e=>e.type===`object`,dt=n.mergeDeepWith((e,t)=>{if(Array.isArray(e)&&Array.isArray(t))return n.concat(e,t);if(e===t)return t;throw Error(`Can not flatten properties`,{cause:{a:e,b:t}})}),ft=n.pipe(Object.keys,n.without([`type`,`properties`,`required`,`examples`,`description`,`additionalProperties`]),n.isEmpty),pt=n.pair(!0),J=(e,t=`coerce`)=>{let r=[n.pair(!1,e)],i={type:`object`,properties:{}},a=[];for(;r.length;){let[e,o]=r.shift();if(o.description&&(i.description??=o.description),o.allOf&&r.push(...o.allOf.map(r=>{if(t===`throw`&&!(r.type===`object`&&ft(r)))throw Error(`Can not merge`);return n.pair(e,r)})),o.anyOf&&r.push(...n.map(pt,o.anyOf)),o.oneOf&&r.push(...n.map(pt,o.oneOf)),o.examples?.length&&(e?i.examples=n.concat(i.examples||[],o.examples):i.examples=E(i.examples?.filter(O)||[],o.examples.filter(O),([e,t])=>n.mergeDeepRight(e,t))),ut(o)&&(r.push([e,{examples:mt(o)}]),o.properties&&(i.properties=(t===`throw`?dt:n.mergeDeepRight)(i.properties,o.properties),!e&&o.required&&a.push(...o.required)),O(o.propertyNames))){let t=[];typeof o.propertyNames.const==`string`&&t.push(o.propertyNames.const),o.propertyNames.enum&&t.push(...o.propertyNames.enum.filter(e=>typeof e==`string`));let n={...Object(o.additionalProperties)};for(let e of t)i.properties[e]??=n;e||a.push(...t)}}return a.length&&(i.required=[...new Set(a)]),i},mt=e=>Object.entries(e.properties||{}).reduce((e,[t,r])=>{let{examples:i=[]}=O(r)?r:{};return E(e,i.map(n.objOf(t)),([e,t])=>({...e,...t}))},[]);var ht=class{#e=new WeakMap;constructor(e){this.logger=e}#t(e,t,n){if(!e.isSchemaChecked){for(let e of[`input`,`output`]){let r=[i.toJSONSchema(t[`${e}Schema`],{unrepresentable:`any`})];for(;r.length>0;){let t=r.shift();t.type&&t.type!==`object`&&this.logger.warn(`Endpoint ${e} schema is not object-based`,n);for(let e of[`allOf`,`oneOf`,`anyOf`])t[e]&&r.push(...t[e])}}if(t.requestType===`json`){let e=Be(t.inputSchema,`input`);e&&this.logger.warn(`The final input schema of the endpoint contains an unsupported JSON payload type.`,{...n,reason:e})}for(let e of H)for(let{mimeTypes:r,schema:i}of t.getResponses(e)){if(!r?.includes(x.json))continue;let t=Be(i,`output`);t&&this.logger.warn(`The final ${e} response schema of the endpoint contains an unsupported JSON payload type.`,{...n,reason:t})}e.isSchemaChecked=!0}}#n(e,t,n,r){if(e.paths.has(n))return;let a=de(n);if(a.length!==0){e.flat??=J(i.toJSONSchema(t.inputSchema,{unrepresentable:`any`,io:`input`}));for(let t of a)t in e.flat.properties||this.logger.warn(`The input schema of the endpoint is most likely missing the parameter of the path it's assigned to.`,{...r,path:n,param:t});e.paths.add(n)}}check=(e,t,n)=>{let r=this.#e.get(n);r||(r={isSchemaChecked:!1,paths:new Set},this.#e.set(n,r)),this.#t(r,n,{method:e,path:t}),this.#n(r,n,t,{method:e})}};const gt=e=>(t,...n)=>{e(t,...n),t===`get`&&e(`head`,...n)},_t=e=>{let[t,n]=e.trim().split(/ (.+)/,2);return n&&S(t)?[n,t]:[e]},vt=e=>e.trim().split(`/`).filter(Boolean).join(`/`),yt=({methodLikeRouteBehavior:e=`method`},t,n)=>{let r=e===`method`;return Object.entries(t).map(([e,t])=>{let[i,a]=r&&S(e)&&t instanceof B?[`/`,e]:_t(e);return[[n||``].concat(vt(i)||[]).join(`/`),t,a]})},bt=(e,t)=>{throw new M(`Route with explicit method can only be assigned with Endpoint`,e,t)},xt=(e,t,n)=>{if(!(!n||n.includes(e)))throw new M(`Method ${e} is not supported by the assigned Endpoint.`,e,t)},St=(e,t,n)=>{let r=`${e} ${t}`;if(n.has(r))throw new M(`Route has a duplicate`,e,t);n.add(r)},Ct=({routing:e,config:t,onEndpoint:n,onStatic:r})=>{let i=yt(t,e),a=new Set;for(;i.length;){let[e,o,s]=i.shift();if(o instanceof B)if(s)St(s,e,a),xt(s,e,o.methods),n(s,e,o);else{let{methods:t=[`get`]}=o;for(let r of t)St(r,e,a),n(r,e,o)}else s&&bt(s,e),o instanceof ct?r&&o.apply(e,r):i.unshift(...yt(t,o,e))}},wt=e=>e.sort((e,t)=>S(t)-+S(e)||e.localeCompare(t)).join(`, `).toUpperCase(),Tt=e=>({method:t},n,r)=>{let i=wt(e);n.set({Allow:i}),r(a(405,`${t} is not allowed`,{headers:{Allow:i}}))},Et=e=>({"Access-Control-Allow-Origin":`*`,"Access-Control-Allow-Methods":wt(e),"Access-Control-Allow-Headers":`content-type`}),Dt=({app:e,getLogger:t,config:r,routing:i,parsers:a})=>{let o=be()?void 0:new ht(t()),s=new Map;return Ct({routing:i,config:r,onEndpoint:(e,t,i)=>{o?.check(e,t,i);let c=a?.[i.requestType]||[],l=n.pair(c,i);s.has(t)||s.set(t,new Map(r.cors?[[`options`,l]]:[])),s.get(t)?.set(e,l)},onStatic:e.use.bind(e)}),s},Ot=({app:e,config:t,getLogger:n,...r})=>{let i=Dt({app:e,getLogger:n,config:t,...r}),a=new Map;for(let[r,o]of i){let i=Array.from(o.keys());i.includes(`get`)&&i.push(`head`);for(let[a,[s,c]]of o){let o=s.slice().concat(async(e,r)=>{let i=n(e);return c.execute({request:e,response:r,logger:i,config:t})});t.cors&&o.unshift(async(e,r,a)=>{let o=n(e),s=Et(i),l=typeof t.cors==`function`?await t.cors({request:e,endpoint:c,logger:o,defaultHeaders:s}):s;r.set(l),a()}),e[a](r,...o)}t.wrongMethodBehavior!==404&&a.set(r,Tt(i))}for(let[t,n]of a)e.all(t,n)},kt=e=>`_httpMessage`in e&&typeof e._httpMessage==`object`&&e._httpMessage!==null&&`headersSent`in e._httpMessage&&typeof e._httpMessage.headersSent==`boolean`&&`setHeader`in e._httpMessage&&typeof e._httpMessage.setHeader==`function`,At=e=>`server`in e&&typeof e.server==`object`&&e.server!==null&&`close`in e.server&&typeof e.server.close==`function`,jt=e=>`encrypted`in e&&typeof e.encrypted==`boolean`&&e.encrypted,Mt=({},e)=>void(!e.headersSent&&e.setHeader(`connection`,`close`)),Nt=e=>new Promise((t,n)=>void e.close(e=>e?n(e):t())),Pt=(e,{timeout:t=1e3,logger:n}={})=>{let r,i=new Set,a=e=>void i.delete(e.destroy()),o=e=>void(kt(e)?!e._httpMessage.headersSent&&e._httpMessage.setHeader(`connection`,`close`):a(e)),s=e=>void(r?e.destroy():i.add(e.once(`close`,()=>void i.delete(e))));for(let t of e)for(let e of[`connection`,`secureConnection`])t.on(e,s);let c=async()=>{for(let t of e)t.on(`request`,Mt);n?.info(`Graceful shutdown`,{sockets:i.size,timeout:t});for(let e of i)(jt(e)||At(e))&&o(e);for await(let e of te(10,Date.now()))if(i.size===0||Date.now()-e>=t)break;for(let e of i)a(e);return Promise.allSettled(e.map(Nt))};return{sockets:i,shutdown:()=>r??=c()}},Ft=Symbol.for(`express-zod-api`),It=({errorHandler:e,getLogger:t})=>async(n,r,i,a)=>n?e.execute({error:C(n),request:r,response:i,input:null,output:null,ctx:{},logger:t(r)}):a(),Lt=({errorHandler:e,getLogger:t})=>async(n,r)=>{let i=a(404,`Can not ${n.method} ${n.path}`),o=t(n);try{await e.execute({request:n,response:r,logger:o,error:i,input:null,output:null,ctx:{}})}catch(e){We({response:r,logger:o,error:new Oe(C(e),i)})}},Rt=e=>(t,{},n)=>{if(Object.values(t?.files||[]).flat().find(({truncated:e})=>e))return n(e);n()},zt=e=>({log:e.debug.bind(e)}),Bt=async({getLogger:e,config:t})=>{let n=await lt(`express-fileupload`),{limitError:r,beforeUpload:i,...a}={...typeof t.upload==`object`&&t.upload},o=[];return o.push(async(t,r,o)=>{let s=e(t);return await i?.({request:t,logger:s}),n({debug:!0,...a,abortOnLimit:!1,parseNested:!0,logger:zt(s)})(t,r,o)}),r&&o.push(Rt(r)),o},Vt=(e,{},t)=>{Buffer.isBuffer(e.body)&&(e.body={raw:e.body}),t()},Ht=({logger:e,config:{childLoggerProvider:t,accessLogger:n=({method:e,path:t},n)=>n.debug(`${e}: ${t}`)}})=>async(r,i,a)=>{let o=await t?.({request:r,parent:e})||e;n?.(r,o),r.res&&(r.res.locals[Ft]={logger:o}),a()},Ut=e=>t=>t?.res?.locals[Ft]?.logger||e,Wt=e=>process.on(`deprecation`,({message:t,namespace:n,name:r,stack:i})=>e.warn(`${r} (${n}): ${t}`,i.split(`
`).slice(1))),Gt=({servers:e,logger:t,options:{timeout:n,beforeExit:r,events:i=[`SIGINT`,`SIGTERM`]}})=>{let a=Pt(e,{logger:t,timeout:n}),o=async()=>{await a.shutdown(),await r?.(),process.exit()};for(let e of i)process.on(e,o)},Kt=e=>{if(e.columns<132)return;let t=p(`Proudly supports transgender community.`.padStart(109)),n=p(`Start your API server with I/O schema validation and custom middlewares in minutes.`.padStart(109)),r=p(`Thank you for choosing Express Zod API for your project.`.padStart(132)),i=p(`for Lia`.padEnd(20)),a=f(`#F5A9B8`),o=f(`#5BCEFA`),s=Array(14).fill(o,1,3).fill(a,3,5).fill(h,5,7).fill(a,7,9).fill(o,9,12).fill(u,12,13),c=`
8888888888 8888888888P 888 d8888 8888888b. 8888888
888 d88P 888 d88888 888 Y88b 888
888 d88P 888 d88P888 888 888 888
8888888 888 888 88888b. 888d888 .d88b. .d8888b .d8888b d88P .d88b. .d88888 d88P 888 888 d88P 888
888 `Y8bd8P' 888 "88b 888P" d8P Y8b 88K 88K d88P d88""88b d88" 888 d88P 888 8888888P" 888
888 X88K 888 888 888 88888888 "Y8888b. "Y8888b. d88P 888 888 888 888 d88P 888 888 888
888 .d8""8b. 888 d88P 888 Y8b. X88 X88 d88P Y88..88P Y88b 888 d8888888888 888 888
8888888888 888 888 88888P" 888 "Y8888 88888P' 88888P' d8888888888 "Y88P" "Y88888 d88P 888 888 8888888
888
888${t}
${i}888${n}
${r}
`;e.write(c.split(`
`).map((e,t)=>s[t]?s[t](e):e).join(`
`))},qt=e=>{e.startupLogo!==!1&&Kt(process.stdout);let t=e.errorHandler||W,n=rt(e.logger)?e.logger:new st(e.logger);n.debug(`Running`,{build:`v26.3.1`,env:process.env.NODE_ENV||`development`}),Wt(n);let r=Ht({logger:n,config:e}),i={getLogger:Ut(n),errorHandler:t},a=Lt(i),o=It(i);return{...i,logger:n,notFoundHandler:a,catcher:o,loggingMiddleware:r}},Jt=(e,t)=>{let{logger:n,getLogger:r,notFoundHandler:i,loggingMiddleware:a}=qt(e);return Ot({app:e.app.use(a),routing:t,getLogger:r,config:e}),{notFoundHandler:i,logger:n}},Yt=async(e,t)=>{let{logger:n,getLogger:r,notFoundHandler:i,catcher:a,loggingMiddleware:o}=qt(e),s=v().disable(`x-powered-by`).set(`query parser`,e.queryParser??`simple`).use(o);if(e.compression){let t=await lt(`compression`);s.use(t(typeof e.compression==`object`?e.compression:void 0))}await e.beforeRouting?.({app:s,getLogger:r}),Ot({app:s,routing:t,getLogger:r,config:e,parsers:{json:[e.jsonParser||v.json()],raw:[e.rawParser||v.raw(),Vt],form:[e.formParser||v.urlencoded()],upload:e.upload?await Bt({config:e,getLogger:r}):[]}}),await e.afterRouting?.({app:s,getLogger:r}),s.use(a,i);let c=[],l=(e,t)=>()=>e.listen(t,()=>n.info(`Listening`,t)),u=[];if(e.http){let t=y.createServer(s);c.push(t),u.push(l(t,e.http.listen))}if(e.https){let t=ee.createServer(e.https.options,s);c.push(t),u.push(l(t,e.https.listen))}return c.length||n.warn(`No servers configured.`),e.gracefulShutdown&&Gt({logger:n,servers:c,options:e.gracefulShutdown===!0?{}:e.gracefulShutdown}),{app:s,logger:n,servers:u.map(e=>e())}},Xt=e=>O(e)&&`or`in e,Zt=e=>O(e)&&`and`in e,Qt=e=>!Zt(e)&&!Xt(e),$t=e=>{let t=n.filter(Qt,e),r=n.chain(n.prop(`and`),n.filter(Zt,e)),[i,a]=n.partition(Qt,r),o=n.concat(t,i),s=n.filter(Xt,e);return n.map(n.prop(`or`),n.concat(s,a)).reduce((e,t)=>E(e,n.map(e=>Qt(e)?[e]:e.and,t),([e,t])=>n.concat(e,t)),n.reject(n.isEmpty,[o]))};var en=`a-im.accept.accept-additions.accept-ch.accept-charset.accept-datetime.accept-encoding.accept-features.accept-language.accept-signature.access-control.access-control-request-headers.access-control-request-method.alpn.alt-used.alternates.amp-cache-transform.apply-to-redirect-ref.authentication-control.authentication-info.authorization.available-dictionary.c-ext.c-man.c-opt.c-pep.c-pep-info.cache-control.cal-managed-id.caldav-timezones.capsule-protocol.cert-not-after.cert-not-before.client-cert.client-cert-chain.close.cmcd-object.cmcd-request.cmcd-session.cmcd-status.cmsd-dynamic.cmsd-static.concealed-auth-export.configuration-context.connection.content-digest.content-disposition.content-encoding.content-id.content-language.content-length.content-location.content-md5.content-range.content-script-type.content-type.cookie.cookie2.cross-origin-embedder-policy.cross-origin-embedder-policy-report-only.cross-origin-opener-policy.cross-origin-opener-policy-report-only.cross-origin-resource-policy.cta-common-access-token.dasl.date.dav.default-style.delta-base.deprecation.depth.derived-from.destination.detached-jws.differential-id.dictionary-id.digest.dpop.dpop-nonce.early-data.ediint-features.expect.expect-ct.ext.forwarded.from.getprofile.hobareg.host.http2-settings.if.if-match.if-modified-since.if-none-match.if-range.if-schedule-tag-match.if-unmodified-since.im.include-referred-token-binding-id.isolation.keep-alive.label.last-event-id.link.link-template.lock-token.man.max-forwards.memento-datetime.meter.method-check.method-check-expires.mime-version.negotiate.nel.odata-entityid.odata-isolation.odata-maxversion.odata-version.opt.ordering-type.origin.origin-agent-cluster.oscore.oslc-core-version.overwrite.p3p.pep.pep-info.permissions-policy.pics-label.ping-from.ping-to.position.pragma.prefer.preference-applied.priority.profileobject.protocol.protocol-info.protocol-query.protocol-request.proxy-authorization.proxy-features.proxy-instruction.public.public-key-pins.public-key-pins-report-only.range.redirect-ref.referer.referer-root.referrer-policy.repeatability-client-id.repeatability-first-sent.repeatability-request-id.repeatability-result.replay-nonce.reporting-endpoints.repr-digest.safe.schedule-reply.schedule-tag.sec-fetch-storage-access.sec-gpc.sec-purpose.sec-token-binding.sec-websocket-extensions.sec-websocket-key.sec-websocket-protocol.sec-websocket-version.security-scheme.setprofile.signature.signature-input.slug.soapaction.status-uri.sunset.surrogate-capability.tcn.te.timeout.topic.traceparent.tracestate.trailer.transfer-encoding.ttl.upgrade.urgency.uri.use-as-dictionary.user-agent.variant-vary.via.want-content-digest.want-digest.want-repr-digest.warning.x-content-type-options.x-frame-options`.split(`.`);const tn=`https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString`,nn={integer:0,number:0,string:``,boolean:!1,object:{},null:null,array:[]},rn=e=>e.replace(ue,e=>`{${e.slice(1)}}`),an=({},e)=>{if(e.isResponse)throw new N(`Please use ez.upload() only for input.`,e);return{type:`string`,format:`binary`}},on=({jsonSchema:e})=>({...e,externalDocs:{description:`raw binary data`,url:`https://swagger.io/specification/#working-with-binary-data`}}),sn=({zodSchema:e,jsonSchema:t})=>{if(!T(e,`union`)||!(`discriminator`in e._zod.def))return t;let n=e._zod.def.discriminator;return{...t,discriminator:t.discriminator??{propertyName:n}}},cn=n.tryCatch(({jsonSchema:e})=>{if(!e.allOf)throw`no allOf`;return J(e,`throw`)},(e,{jsonSchema:t})=>t),ln=({jsonSchema:e})=>{if(!e.anyOf)return e;let t=e.anyOf[0];return Object.assign(t,{type:hn(t.type)})},Y=e=>e,un=({jsonSchema:{examples:e,description:t}},n)=>{if(n.isResponse)throw new N(`Please use ez.dateOut() for output.`,n);let r={description:t||`YYYY-MM-DDTHH:mm:ss.sssZ`,type:`string`,format:`date-time`,pattern:`^\\d{4}-\\d{2}-\\d{2}(T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?)?Z?$`,externalDocs:{url:tn}};return e?.length&&(r.examples=e),r},dn=({jsonSchema:{examples:e,description:t}},n)=>{if(!n.isResponse)throw new N(`Please use ez.dateIn() for input.`,n);let r={description:t||`YYYY-MM-DDTHH:mm:ss.sssZ`,type:`string`,format:`date-time`,externalDocs:{url:tn}};return e?.length&&(r.examples=e),r},fn=()=>({type:`string`,format:`bigint`,pattern:`^-?\\d+$`}),pn=({zodSchema:e,jsonSchema:t})=>e._zod.def.rest===null?{...t,items:{not:{}}}:t,mn=e=>{let t=Array.isArray(e.type)?e.type[0]:e.type;return nn?.[t]},hn=e=>e===`null`?e:typeof e==`string`?[e,`null`]:e&&[...new Set(e).add(`null`)],gn=({zodSchema:e,jsonSchema:t},n)=>{let r=e._zod.def[n.isResponse?`out`:`in`],i=e._zod.def[n.isResponse?`in`:`out`];if(!T(r,`transform`))return t;let a=Y(Cn(i,{ctx:n}));if(b(a))if(n.isResponse){let e=ye(r,mn(a));if(e&&[`number`,`string`,`boolean`].includes(e))return{...t,type:e}}else{let{type:e,...t}=a;return{...t,format:`${t.format||e} (preprocessed)`}}return t},_n=({jsonSchema:e})=>{if(e.type!==`object`)return e;let t=e;return!t.properties||!(`raw`in t.properties)||!O(t.properties.raw)?e:t.properties.raw},vn=e=>e.length?n.fromPairs(n.zip(n.times(e=>`example${e+1}`,e.length),n.map(n.objOf(`value`),e))):void 0,yn=(e,t)=>t?.includes(e)||e.startsWith(`x-`)||en.includes(e),bn=({path:e,method:t,request:r,inputSources:i,makeRef:a,composition:o,isHeader:s,security:c,description:l=`${t.toUpperCase()} ${e} Parameter`})=>{let u=J(r),d=de(e),f=i.includes(`query`),p=i.includes(`params`),m=i.includes(`headers`),h=e=>p&&d.includes(e),g=n.chain(n.filter(e=>e.type===`header`),c??[]).map(({name:e})=>e),_=n=>m&&(s?.(n,t,e)??yn(n,g));return Object.entries(u.properties).reduce((e,[t,r])=>{if(!O(r))return e;let i=h(t)?`path`:_(t)?`header`:f?`query`:void 0;if(!i)return e;let s=Y(r),c=o===`components`?a(r.id||JSON.stringify(r),s,r.id||D(l,t)):s;return e.concat({name:t,in:i,deprecated:r.deprecated,required:u.required?.includes(t)||!1,description:s.description||l,schema:c,examples:vn(b(s)&&s.examples?.length?s.examples:n.pluck(t,u.examples?.filter(n.both(O,n.has(t)))||[]))})},[])},xn={nullable:ln,union:sn,bigint:fn,intersection:cn,tuple:pn,pipe:gn,[A]:un,[j]:dn,[F]:an,[I]:_n,[k]:on},Sn=(e,t,r)=>{let i=[e,t];for(;i.length;){let e=i.shift();if(n.is(Object,e)){if(re(e)&&!e.$ref.startsWith(`#/components`)){let n=t[e.$ref.split(`/`).pop()];n&&(e.$ref=r.makeRef(n.id||n,Y(n),n.id).$ref);continue}i.push(...n.values(e))}n.is(Array,e)&&i.push(...n.values(e))}return e},Cn=(e,{ctx:n,rules:r=xn})=>{let{$defs:a={},properties:o={}}=i.toJSONSchema(i.object({subject:e}),{unrepresentable:`any`,io:n.isResponse?`output`:`input`,override:e=>{let i=t(e.zodSchema),a=r[i&&i in r?i:e.zodSchema._zod.def.type];if(a){let t={...a(e,n)};for(let t in e.jsonSchema)delete e.jsonSchema[t];Object.assign(e.jsonSchema,t)}}});return Sn(O(o.subject)?o.subject:{},a,n)},wn=(e,t)=>{if(re(e))return[e,!1];let r=!1,i=n.map(e=>{let[n,i]=wn(e,t);return r||=i,n}),a=n.omit(t),o={properties:a,examples:n.map(a),required:n.without(t),allOf:i,oneOf:i,anyOf:i},s=n.evolve(o,e);return[s,r||!!s.required?.length]},Tn=({method:e,path:t,schema:r,mimeTypes:i,variant:a,makeRef:o,composition:s,hasMultipleStatusCodes:c,statusCode:l,brandHandling:u,description:d=`${e.toUpperCase()} ${t} ${ve(a)} response ${c?l:``}`.trim()})=>{if(!xe(e,i))return{description:d};let f=Y(Cn(r,{rules:{...u,...xn},ctx:{isResponse:!0,makeRef:o,path:t,method:e}})),p=[];b(f)&&f.examples&&(p.push(...f.examples),delete f.examples);let m={schema:s===`components`?o(r,f,D(d)):f,examples:vn(p)};return{description:d,content:n.fromPairs(n.xprod(i,[m]))}},En=({format:e})=>{let t={type:`http`,scheme:`bearer`};return e&&(t.bearerFormat=e),t},Dn=({name:e},t)=>{let n={type:`apiKey`,in:`query`,name:e};return t?.includes(`body`)&&(t?.includes(`query`)?(n[`x-in-alternative`]=`body`,n.description=`${e} CAN also be supplied within the request body`):(n[`x-in-actual`]=`body`,n.description=`${e} MUST be supplied within the request body instead of query`)),n},On=({name:e})=>({type:`apiKey`,in:`header`,name:e}),kn=({name:e})=>({type:`apiKey`,in:`cookie`,name:e}),An=({url:e})=>({type:`openIdConnect`,openIdConnectUrl:e}),jn=({flows:e={}})=>({type:`oauth2`,flows:n.map(e=>({...e,scopes:e.scopes||{}}),n.reject(n.isNil,e))}),Mn=(e,t=[])=>{let n=e=>e.type===`basic`?{type:`http`,scheme:`basic`}:e.type===`bearer`?En(e):e.type===`input`?Dn(e,t):e.type===`header`?On(e):e.type===`cookie`?kn(e):e.type===`openid`?An(e):jn(e);return e.map(e=>e.map(n))},Nn=(e,t,n)=>e.map(e=>e.reduce((e,r)=>{let i=n(r),a=[`oauth2`,`openIdConnect`].includes(r.type);return Object.assign(e,{[i]:a?t:[]})},{})),Pn=({schema:e,brandHandling:t,makeRef:n,path:r,method:i})=>Cn(e,{rules:{...t,...xn},ctx:{isResponse:!1,makeRef:n,path:r,method:i}}),Fn=({method:e,path:t,schema:r,request:i,mimeType:a,makeRef:o,composition:s,paramNames:c,description:l=`${e.toUpperCase()} ${t} Request body`})=>{let[u,d]=wn(Y(i),c),f=[];b(u)&&u.examples&&(f.push(...u.examples),delete u.examples);let p={schema:s===`components`?o(r,u,D(l)):u,examples:vn(f.length?f:J(i).examples?.filter(e=>O(e)&&!Array.isArray(e)).map(n.omit(c))||[])},m={description:l,content:{[a]:p}};return(d||a===x.raw)&&(m.required=!0),m},In=e=>Object.entries(e).reduce((e,[t,n])=>{if(!n)return e;let r={name:t,description:typeof n==`string`?n:n.description};return typeof n==`object`&&n.url&&(r.externalDocs={url:n.url}),e.concat(r)},[]),Ln=e=>e.length<=50?e:e.slice(0,49)+`…`,X=e=>e.length?e.slice():void 0;var Rn=class extends ne{#e=new Map;#t=new Map;#n=new Map;#r(e,t,n){let r=this.#n.get(e);if(!r){let t=n?0:1;do r=`${n??`Schema`}${t?this.#n.size+t:``}`,t++;while(this.rootDoc.components?.schemas?.[r]);this.#n.set(e,r)}return this.addSchema(r,t),{$ref:`#/components/schemas/${r}`}}#i(e,t,n){let r=n||D(t,e),i=this.#t.get(r);if(i===void 0)return this.#t.set(r,1),r;if(n)throw new N(`Duplicated operationId: "${n}"`,{method:t,isResponse:!1,path:e});return i++,this.#t.set(r,i),`${r}${i}`}#a(e){let t=JSON.stringify(e);for(let e in this.rootDoc.components?.securitySchemes||{})if(t===JSON.stringify(this.rootDoc.components?.securitySchemes?.[e]))return e;let n=(this.#e.get(e.type)||0)+1;return this.#e.set(e.type,n),`${e.type.toUpperCase()}_${n}`}constructor({routing:e,config:t,title:r,version:i,serverUrl:a,descriptions:o,brandHandling:s,tags:c,isHeader:l,hasSummaryFromDescription:u=!0,hasHeadMethod:d=!0,composition:f=`inline`}){super(),this.addInfo({title:r,version:i});for(let e of typeof a==`string`?[a]:a)this.addServer({url:e});let p=(e,r,i)=>{let a={path:r,method:e,endpoint:i,composition:f,brandHandling:s,makeRef:this.#r.bind(this)},{description:c,shortDescription:d,scopes:p,inputSchema:m}=i,h=d?Ln(d):u&&c?Ln(c):void 0,g=ge(e,t.inputSources),_=this.#i(r,e,i.getOperationId(e)),v=Pn({...a,schema:m}),y=$t(i.security),ee=bn({...a,inputSources:g,isHeader:l,security:y,request:v,description:o?.requestParameter?.call(null,{method:e,path:r,operationId:_})}),te={};for(let t of H){let n=i.getResponses(t);for(let{mimeTypes:i,schema:s,statusCodes:c}of n)for(let l of c)te[l]=Tn({...a,variant:t,schema:s,mimeTypes:i,statusCode:l,hasMultipleStatusCodes:n.length>1||c.length>1,description:o?.[`${t}Response`]?.call(null,{method:e,path:r,operationId:_,statusCode:l})})}let ne=g.includes(`body`)?Fn({...a,request:v,paramNames:n.pluck(`name`,ee),schema:m,mimeType:x[i.requestType],description:o?.requestBody?.call(null,{method:e,path:r,operationId:_})}):void 0,re=Nn(Mn(y,g),p,e=>{let t=this.#a(e);return this.addSecurityScheme(t,e),t}),b={operationId:_,summary:h,description:c,deprecated:i.isDeprecated||void 0,tags:X(i.tags),parameters:X(ee),requestBody:ne,security:X(re),responses:te};this.addPath(rn(r),{[e]:b})};Ct({routing:e,config:t,onEndpoint:d?gt(p):p}),c&&(this.rootDoc.tags=In(c))}};const zn=e=>ie({...e,headers:{"content-type":x.json,...e?.headers}}),Bn=e=>ae(e),Vn=e=>{let t={warn:[],error:[],info:[],debug:[]};return new Proxy(e||{},{get(e,n,r){return n===`_getLogs`?()=>t:it(n)?(...e)=>t[n].push(e):Reflect.get(e,n,r)}})},Hn=({requestProps:e,responseOptions:t,configProps:n,loggerProps:r})=>{let i=zn(e),a=Bn({req:i,...t});a.req=t?.req||i,i.res=a;let o=Vn(r);return{requestMock:i,responseMock:a,loggerMock:o,configMock:{cors:!1,logger:o,...n}}},Un=async({endpoint:e,...t})=>{let{requestMock:n,responseMock:r,loggerMock:i,configMock:a}=Hn(t);return await e.execute({request:n,response:r,config:a,logger:i}),{requestMock:n,responseMock:r,loggerMock:i}},Wn=async({middleware:e,ctx:t={},...n})=>{let{requestMock:r,responseMock:i,loggerMock:a,configMock:{inputSources:o,errorHandler:s=W}}=Hn(n),c={request:r,response:i,logger:a,input:_e(r,o),ctx:t};try{return{requestMock:r,responseMock:i,loggerMock:a,output:await e.execute(c)}}catch(e){return await s.execute({...c,error:C(e),output:null}),{requestMock:r,responseMock:i,loggerMock:a,output:{}}}};var Gn=class t{ts;f;exportModifier;asyncModifier;accessModifiers;#e;static#t=/^[A-Za-z_$][A-Za-z0-9_$]*$/;constructor(){this.ts=e(import.meta.url)(`typescript`),this.f=this.ts.factory,this.exportModifier=[this.f.createModifier(this.ts.SyntaxKind.ExportKeyword)],this.asyncModifier=[this.f.createModifier(this.ts.SyntaxKind.AsyncKeyword)],this.accessModifiers={public:[this.f.createModifier(this.ts.SyntaxKind.PublicKeyword)],protectedReadonly:[this.f.createModifier(this.ts.SyntaxKind.ProtectedKeyword),this.f.createModifier(this.ts.SyntaxKind.ReadonlyKeyword)]},this.#e=[this.ts.SyntaxKind.AnyKeyword,this.ts.SyntaxKind.BigIntKeyword,this.ts.SyntaxKind.BooleanKeyword,this.ts.SyntaxKind.NeverKeyword,this.ts.SyntaxKind.NumberKeyword,this.ts.SyntaxKind.ObjectKeyword,this.ts.SyntaxKind.StringKeyword,this.ts.SyntaxKind.SymbolKeyword,this.ts.SyntaxKind.UndefinedKeyword,this.ts.SyntaxKind.UnknownKeyword,this.ts.SyntaxKind.VoidKeyword]}addJsDoc=(e,t)=>this.ts.addSyntheticLeadingComment(e,this.ts.SyntaxKind.MultiLineCommentTrivia,`* ${t} `,!0);printNode=(e,t)=>{let n=this.ts.createSourceFile(`print.ts`,``,this.ts.ScriptTarget.Latest,!1,this.ts.ScriptKind.TS);return this.ts.createPrinter(t).printNode(this.ts.EmitHint.Unspecified,e,n)};makePropertyIdentifier=e=>typeof e==`string`&&t.#t.test(e)?this.f.createIdentifier(e):this.literally(e);makeTemplate=(e,...t)=>this.f.createTemplateExpression(this.f.createTemplateHead(e),t.map(([e,n=``],r)=>this.f.createTemplateSpan(e,r===t.length-1?this.f.createTemplateTail(n):this.f.createTemplateMiddle(n))));makeParam=(e,{type:t,mod:n,init:r,optional:i}={})=>this.f.createParameterDeclaration(n,void 0,e,i?this.f.createToken(this.ts.SyntaxKind.QuestionToken):void 0,t?this.ensureTypeNode(t):void 0,r);makeParams=e=>Object.entries(e).map(([e,t])=>this.makeParam(e,typeof t==`string`||typeof t==`number`||typeof t==`object`&&`kind`in t?{type:t}:t));makePublicConstructor=(e,t=[])=>this.f.createConstructorDeclaration(this.accessModifiers.public,e,this.f.createBlock(t));ensureTypeNode=(e,t)=>typeof e==`number`?this.f.createKeywordTypeNode(e):typeof e==`string`||this.ts.isIdentifier(e)?this.f.createTypeReferenceNode(e,t&&n.map(this.ensureTypeNode.bind(this),t)):e;makeRecordStringAny=()=>this.ensureTypeNode(`Record`,[this.ts.SyntaxKind.StringKeyword,this.ts.SyntaxKind.AnyKeyword]);makeUnion=e=>{let t=new Map;for(let n of e)t.set(this.isPrimitive(n)?n.kind:n,n);return this.f.createUnionTypeNode(Array.from(t.values()))};makeInterfaceProp=(e,t,{isOptional:r,hasUndefined:i=r,isDeprecated:a,comment:o}={})=>{let s=this.ensureTypeNode(t),c=this.f.createPropertySignature(void 0,this.makePropertyIdentifier(e),r?this.f.createToken(this.ts.SyntaxKind.QuestionToken):void 0,i?this.makeUnion([s,this.ensureTypeNode(this.ts.SyntaxKind.UndefinedKeyword)]):s),l=n.reject(n.isNil,[a?`@deprecated`:void 0,o]);return l.length?this.addJsDoc(c,l.join(` `)):c};makeOneLine=e=>this.ts.setEmitFlags(e,this.ts.EmitFlags.SingleLine);makeDeconstruction=(...e)=>this.f.createArrayBindingPattern(e.map(e=>this.f.createBindingElement(void 0,void 0,e)));makeConst=(e,t,{type:n,expose:r}={})=>this.f.createVariableStatement(r&&this.exportModifier,this.f.createVariableDeclarationList([this.f.createVariableDeclaration(e,void 0,n?this.ensureTypeNode(n):void 0,t)],this.ts.NodeFlags.Const));makePublicLiteralType=(e,t)=>this.makeType(e,this.makeUnion(n.map(this.makeLiteralType.bind(this),t)),{expose:!0});makeType=(e,t,{expose:n,comment:r,params:i}={})=>{let a=this.f.createTypeAliasDeclaration(n?this.exportModifier:void 0,e,i&&this.makeTypeParams(i),t);return r?this.addJsDoc(a,r):a};makePublicProperty=(e,t)=>this.f.createPropertyDeclaration(this.accessModifiers.public,e,void 0,this.ensureTypeNode(t),void 0);makePublicMethod=(e,t,n,{typeParams:r,returns:i}={})=>this.f.createMethodDeclaration(this.accessModifiers.public,void 0,e,void 0,r&&this.makeTypeParams(r),t,i,this.f.createBlock(n));makePublicClass=(e,t,{typeParams:n}={})=>this.f.createClassDeclaration(this.exportModifier,e,n&&this.makeTypeParams(n),void 0,t);makeKeyOf=e=>this.f.createTypeOperatorNode(this.ts.SyntaxKind.KeyOfKeyword,this.ensureTypeNode(e));makePromise=e=>this.ensureTypeNode(Promise.name,[e]);makeInterface=(e,t,{expose:n,comment:r}={})=>{let i=this.f.createInterfaceDeclaration(n?this.exportModifier:void 0,e,void 0,void 0,t);return r?this.addJsDoc(i,r):i};makeTypeParams=e=>(Array.isArray(e)?e.map(e=>n.pair(e,void 0)):Object.entries(e)).map(([e,t])=>{let{type:n,init:r}=typeof t==`object`&&`init`in t?t:{type:t};return this.f.createTypeParameterDeclaration([],e,n?this.ensureTypeNode(n):void 0,r?this.ensureTypeNode(r):void 0)});makeArrowFn=(e,t,{isAsync:r}={})=>this.f.createArrowFunction(r?this.asyncModifier:void 0,void 0,Array.isArray(e)?n.map(this.makeParam.bind(this),e):this.makeParams(e),void 0,void 0,t);makeTernary=(e,t,n)=>this.f.createConditionalExpression(e,this.f.createToken(this.ts.SyntaxKind.QuestionToken),t,this.f.createToken(this.ts.SyntaxKind.ColonToken),n);makeCall=(e,...t)=>(...n)=>this.f.createCallExpression(t.reduce((e,t)=>typeof t==`string`||this.ts.isIdentifier(t)?this.f.createPropertyAccessExpression(e,t):this.f.createElementAccessExpression(e,t),typeof e==`string`?this.f.createIdentifier(e):e),void 0,n);makeNew=(e,...t)=>this.f.createNewExpression(this.f.createIdentifier(e),void 0,t);makeExtract=(e,t)=>this.ensureTypeNode(`Extract`,[e,t]);makeAssignment=(e,t)=>this.f.createExpressionStatement(this.f.createBinaryExpression(e,this.f.createToken(this.ts.SyntaxKind.EqualsToken),t));makeIndexed=(e,t)=>this.f.createIndexedAccessTypeNode(this.ensureTypeNode(e),this.ensureTypeNode(t));makeMaybeAsync=e=>this.makeUnion([this.ensureTypeNode(e),this.makePromise(e)]);makeFnType=(e,t)=>this.f.createFunctionTypeNode(void 0,this.makeParams(e),this.ensureTypeNode(t));literally=e=>typeof e==`number`?this.f.createNumericLiteral(e):typeof e==`bigint`?this.f.createBigIntLiteral(e.toString()):typeof e==`boolean`?e?this.f.createTrue():this.f.createFalse():e===null?this.f.createNull():this.f.createStringLiteral(e);makeLiteralType=e=>this.f.createLiteralTypeNode(this.literally(e));isPrimitive=e=>this.#e.includes(e.kind)};const Z=e=>e;var Kn=class{api=new Gn;paths=new Set;tags=new Map;registry=new Map;#e={pathType:this.api.f.createIdentifier(`Path`),implementationType:this.api.f.createIdentifier(`Implementation`),keyParameter:this.api.f.createIdentifier(`key`),pathParameter:this.api.f.createIdentifier(`path`),paramsArgument:this.api.f.createIdentifier(`params`),ctxArgument:this.api.f.createIdentifier(`ctx`),methodParameter:this.api.f.createIdentifier(`method`),requestParameter:this.api.f.createIdentifier(`request`),eventParameter:this.api.f.createIdentifier(`event`),dataParameter:this.api.f.createIdentifier(`data`),handlerParameter:this.api.f.createIdentifier(`handler`),msgParameter:this.api.f.createIdentifier(`msg`),parseRequestFn:this.api.f.createIdentifier(`parseRequest`),substituteFn:this.api.f.createIdentifier(`substitute`),provideMethod:this.api.f.createIdentifier(`provide`),onMethod:this.api.f.createIdentifier(`on`),implementationArgument:this.api.f.createIdentifier(`implementation`),hasBodyConst:this.api.f.createIdentifier(`hasBody`),undefinedValue:this.api.f.createIdentifier(`undefined`),responseConst:this.api.f.createIdentifier(`response`),restConst:this.api.f.createIdentifier(`rest`),searchParamsConst:this.api.f.createIdentifier(`searchParams`),defaultImplementationConst:this.api.f.createIdentifier(`defaultImplementation`),clientConst:this.api.f.createIdentifier(`client`),contentTypeConst:this.api.f.createIdentifier(`contentType`),isJsonConst:this.api.f.createIdentifier(`isJSON`),sourceProp:this.api.f.createIdentifier(`source`)};interfaces={input:this.api.f.createIdentifier(`Input`),positive:this.api.f.createIdentifier(`PositiveResponse`),negative:this.api.f.createIdentifier(`NegativeResponse`),encoded:this.api.f.createIdentifier(`EncodedResponse`),response:this.api.f.createIdentifier(`Response`)};methodType=this.api.makePublicLiteralType(`Method`,ce);someOfType=this.api.makeType(`SomeOf`,this.api.makeIndexed(`T`,this.api.makeKeyOf(`T`)),{params:[`T`]});requestType=this.api.makeType(`Request`,this.api.makeKeyOf(this.interfaces.input),{expose:!0});constructor(e){this.serverUrl=e}someOf=({name:e})=>this.api.ensureTypeNode(this.someOfType.name,[e]);makePathType=()=>this.api.makePublicLiteralType(this.#e.pathType,Array.from(this.paths));makePublicInterfaces=()=>Object.keys(this.interfaces).map(e=>this.api.makeInterface(this.interfaces[e],Array.from(this.registry).map(([t,{store:n,isDeprecated:r}])=>this.api.makeInterfaceProp(t,n[e],{isDeprecated:r})),{expose:!0}));makeEndpointTags=()=>this.api.makeConst(`endpointTags`,this.api.f.createObjectLiteralExpression(Array.from(this.tags).map(([e,t])=>this.api.f.createPropertyAssignment(this.api.makePropertyIdentifier(e),this.api.f.createArrayLiteralExpression(n.map(this.api.literally.bind(this.api),t))))),{expose:!0});makeImplementationType=()=>this.api.makeType(this.#e.implementationType,this.api.makeFnType({[this.#e.methodParameter.text]:this.methodType.name,[this.#e.pathParameter.text]:this.api.ts.SyntaxKind.StringKeyword,[this.#e.paramsArgument.text]:this.api.makeRecordStringAny(),[this.#e.ctxArgument.text]:{optional:!0,type:`T`}},this.api.makePromise(this.api.ts.SyntaxKind.AnyKeyword)),{expose:!0,params:{T:{init:this.api.ts.SyntaxKind.UnknownKeyword}}});makeParseRequestFn=()=>this.api.makeConst(this.#e.parseRequestFn,this.api.makeArrowFn({[this.#e.requestParameter.text]:this.api.ts.SyntaxKind.StringKeyword},this.api.f.createAsExpression(this.api.makeCall(this.#e.requestParameter,Z(`split`))(this.api.f.createRegularExpressionLiteral(`/ (.+)/`),this.api.literally(2)),this.api.f.createTupleTypeNode([this.api.ensureTypeNode(this.methodType.name),this.api.ensureTypeNode(this.#e.pathType)]))));makeSubstituteFn=()=>this.api.makeConst(this.#e.substituteFn,this.api.makeArrowFn({[this.#e.pathParameter.text]:this.api.ts.SyntaxKind.StringKeyword,[this.#e.paramsArgument.text]:this.api.makeRecordStringAny()},this.api.f.createBlock([this.api.makeConst(this.#e.restConst,this.api.f.createObjectLiteralExpression([this.api.f.createSpreadAssignment(this.#e.paramsArgument)])),this.api.f.createForInStatement(this.api.f.createVariableDeclarationList([this.api.f.createVariableDeclaration(this.#e.keyParameter)],this.api.ts.NodeFlags.Const),this.#e.paramsArgument,this.api.f.createBlock([this.api.makeAssignment(this.#e.pathParameter,this.api.makeCall(this.#e.pathParameter,Z(`replace`))(this.api.makeTemplate(`:`,[this.#e.keyParameter]),this.api.makeArrowFn([],this.api.f.createBlock([this.api.f.createExpressionStatement(this.api.f.createDeleteExpression(this.api.f.createElementAccessExpression(this.#e.restConst,this.#e.keyParameter))),this.api.f.createReturnStatement(this.api.f.createElementAccessExpression(this.#e.paramsArgument,this.#e.keyParameter))]))))])),this.api.f.createReturnStatement(this.api.f.createAsExpression(this.api.f.createArrayLiteralExpression([this.#e.pathParameter,this.#e.restConst]),this.api.ensureTypeNode(`const`)))])));#t=()=>this.api.makePublicMethod(this.#e.provideMethod,this.api.makeParams({[this.#e.requestParameter.text]:`K`,[this.#e.paramsArgument.text]:this.api.makeIndexed(this.interfaces.input,`K`),[this.#e.ctxArgument.text]:{optional:!0,type:`T`}}),[this.api.makeConst(this.api.makeDeconstruction(this.#e.methodParameter,this.#e.pathParameter),this.api.makeCall(this.#e.parseRequestFn)(this.#e.requestParameter)),this.api.f.createReturnStatement(this.api.makeCall(this.api.f.createThis(),this.#e.implementationArgument)(this.#e.methodParameter,this.api.f.createSpreadElement(this.api.makeCall(this.#e.substituteFn)(this.#e.pathParameter,this.#e.paramsArgument)),this.#e.ctxArgument))],{typeParams:{K:this.requestType.name},returns:this.api.makePromise(this.api.makeIndexed(this.interfaces.response,`K`))});makeClientClass=e=>this.api.makePublicClass(e,[this.api.makePublicConstructor([this.api.makeParam(this.#e.implementationArgument,{type:this.api.ensureTypeNode(this.#e.implementationType,[`T`]),mod:this.api.accessModifiers.protectedReadonly,init:this.#e.defaultImplementationConst})]),this.#t()],{typeParams:[`T`]});#n=e=>this.api.makeTemplate(`?`,[this.api.makeNew(URLSearchParams.name,e)]);#r=()=>this.api.makeNew(URL.name,this.api.makeTemplate(``,[this.#e.pathParameter],[this.#e.searchPar