@blimu/codegen 0.4.1 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,10 +1,3440 @@
1
- var Ie=Object.defineProperty;var p=(s,e)=>Ie(s,"name",{value:e,configurable:!0});import{z as h}from"zod";var qe=h.object({type:h.string().min(1,"Type name is required"),package:h.string().min(1,"Package name is required"),importPath:h.string().optional()}),ie=["client.ts.hbs","index.ts.hbs","package.json.hbs","README.md.hbs","schema.ts.hbs","schema.zod.ts.hbs","service.ts.hbs","tsconfig.json.hbs","utils.ts.hbs"],Ee=h.object({type:h.string().min(1,"Type is required"),outDir:h.string().min(1,"OutDir is required"),name:h.string().min(1,"Name is required"),includeTags:h.array(h.string()).optional(),excludeTags:h.array(h.string()).optional(),operationIdParser:h.custom().optional(),preCommand:h.array(h.string()).optional(),postCommand:h.array(h.string()).optional(),defaultBaseURL:h.string().optional(),exclude:h.array(h.string()).optional()}),Fe=Ee.extend({type:h.literal("typescript"),packageName:h.string().min(1,"PackageName is required"),moduleName:h.string().optional(),srcDir:h.string().optional(),includeQueryKeys:h.boolean().optional(),predefinedTypes:h.array(qe).optional(),dependencies:h.record(h.string(),h.string()).optional(),devDependencies:h.record(h.string(),h.string()).optional(),formatCode:h.boolean().optional(),templates:h.record(h.string(),h.string()).refine(s=>Object.keys(s).every(e=>ie.includes(e)),{message:`Template names must be one of: ${ie.join(", ")}`}).optional()}),Ne=h.discriminatedUnion("type",[Fe]),J=h.object({spec:h.string().min(1,"Spec is required"),name:h.string().optional(),clients:h.array(Ne).min(1,"At least one client is required")});import{Injectable as ze,Logger as _e}from"@nestjs/common";import*as ce from"fs";import*as T from"path";import{pathToFileURL as Be}from"url";import*as K from"path";async function W(s){try{let e=K.isAbsolute(s)?s:K.resolve(s),r=await import(Be(e).href),n=r.default||r;if(!n)throw new Error(`Config file must export a default export or named export: ${s}`);return J.parse(n)}catch(e){throw e instanceof Error?new Error(`Failed to load MJS config from ${s}: ${e.message}`):e}}p(W,"loadMjsConfig");function Me(s,e,t,r){var n=arguments.length,o=n<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(s,e,t,r);else for(var i=s.length-1;i>=0;i--)(a=s[i])&&(o=(n<3?a(o):n>3?a(e,t,o):a(e,t))||o);return n>3&&o&&Object.defineProperty(e,t,o),o}p(Me,"_ts_decorate");var k=class s{static{p(this,"ConfigService")}logger=new _e(s.name);DEFAULT_CONFIG_FILE="chunkflow-codegen.config.mjs";async findDefaultConfig(){let e=process.cwd(),t=T.parse(e).root;for(;e!==t;){let r=T.join(e,this.DEFAULT_CONFIG_FILE);try{return await ce.promises.access(r),r}catch{}e=T.dirname(e)}return null}async load(e){try{let t=await W(e);return this.normalizePaths(t,e)}catch(t){throw t instanceof Error?new Error(`Failed to load config from ${e}: ${t.message}`):t}}normalizePaths(e,t){let r=T.dirname(T.resolve(t)),n=e.spec;this.isUrl(n)||T.isAbsolute(n)||(n=T.resolve(r,n));let o=e.clients.map(a=>{let i=a.outDir;return T.isAbsolute(i)||(i=T.resolve(r,i)),{...a,outDir:i}});return{...e,spec:n,clients:o}}isUrl(e){try{let t=new URL(e);return t.protocol==="http:"||t.protocol==="https:"}catch{return!1}}getPreCommand(e){return e.preCommand||[]}getPostCommand(e){return e.postCommand||[]}shouldExcludeFile(e,t){if(!e.exclude||e.exclude.length===0)return!1;let r;try{r=T.relative(e.outDir,t)}catch{return!1}r=T.posix.normalize(r),r==="."&&(r="");for(let n of e.exclude){let o=T.posix.normalize(n);if(r===o||o!==""&&r.startsWith(o+"/"))return!0}return!1}};k=Me([ze()],k);import{Module as We}from"@nestjs/common";function Ue(s,e,t,r){var n=arguments.length,o=n<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(s,e,t,r);else for(var i=s.length-1;i>=0;i--)(a=s[i])&&(o=(n<3?a(o):n>3?a(e,t,o):a(e,t))||o);return n>3&&o&&Object.defineProperty(e,t,o),o}p(Ue,"_ts_decorate");var B=class{static{p(this,"ConfigModule")}};B=Ue([We({providers:[k],exports:[k]})],B);function At(s){return J.parse(s)}p(At,"defineConfig");var l=(function(s){return s.Unknown="unknown",s.String="string",s.Number="number",s.Integer="integer",s.Boolean="boolean",s.Null="null",s.Array="array",s.Object="object",s.Enum="enum",s.Ref="ref",s.OneOf="oneOf",s.AnyOf="anyOf",s.AllOf="allOf",s.Not="not",s})({});import{Injectable as Xe,Logger as et}from"@nestjs/common";import{Injectable as Ze}from"@nestjs/common";import{Injectable as Ge}from"@nestjs/common";function fe(s){let e=s.openapi;return typeof e!="string"?"unknown":e.startsWith("3.1")?"3.1":e.startsWith("3.0")?"3.0":"unknown"}p(fe,"detectOpenAPIVersion");function pe(s){return s==="3.0"||s==="3.1"}p(pe,"isSupportedVersion");function X(s,e){if(!(!e||typeof e!="object")){if("$ref"in e&&e.$ref){let t=e.$ref;if(t.startsWith("#/components/schemas/")){let r=t.replace("#/components/schemas/","");if(s.components?.schemas?.[r]){let n=s.components.schemas[r];return"$ref"in n?X(s,n):n}}return}return e}}p(X,"getSchemaFromRef");function le(s){return s?"nullable"in s&&s.nullable===!0?!0:"type"in s&&Array.isArray(s.type)?s.type.includes("null"):!1:!1}p(le,"isSchemaNullable");function ee(s){if(!(!s||!("type"in s)))return s.type}p(ee,"getSchemaType");function Ve(s,e,t,r){var n=arguments.length,o=n<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(s,e,t,r);else for(var i=s.length-1;i>=0;i--)(a=s[i])&&(o=(n<3?a(o):n>3?a(e,t,o):a(e,t))||o);return n>3&&o&&Object.defineProperty(e,t,o),o}p(Ve,"_ts_decorate");var v=class{static{p(this,"SchemaConverterService")}schemaRefToIR(e,t){if(!t)return{kind:l.Unknown,nullable:!1};if("$ref"in t&&t.$ref){let c=t.$ref;if(c.startsWith("#/components/schemas/")){let u=c.replace("#/components/schemas/","");return{kind:l.Ref,ref:u,nullable:!1}}let f=c.split("/");if(f.length>0){let u=f[f.length-1];if(u)return{kind:l.Ref,ref:u,nullable:!1}}return{kind:l.Unknown,nullable:!1}}let r=X(e,t);if(!r)return{kind:l.Unknown,nullable:!1};let n=le(r),o;if(r.discriminator&&(o={propertyName:r.discriminator.propertyName,mapping:r.discriminator.mapping}),r.oneOf&&r.oneOf.length>0){let c=r.oneOf.map(f=>this.schemaRefToIR(e,f));return{kind:l.OneOf,oneOf:c,nullable:n,discriminator:o}}if(r.anyOf&&r.anyOf.length>0){let c=r.anyOf.map(f=>this.schemaRefToIR(e,f));return{kind:l.AnyOf,anyOf:c,nullable:n,discriminator:o}}if(r.allOf&&r.allOf.length>0){let c=r.allOf.map(f=>this.schemaRefToIR(e,f));return{kind:l.AllOf,allOf:c,nullable:n,discriminator:o}}if(r.not){let c=this.schemaRefToIR(e,r.not);return{kind:l.Not,not:c,nullable:n,discriminator:o}}if(r.enum&&r.enum.length>0){let c=r.enum.map(u=>String(u)),f=this.inferEnumBaseKind(r);return{kind:l.Enum,enumValues:c,enumRaw:r.enum,enumBase:f,nullable:n,discriminator:o}}let a=ee(r),i=Array.isArray(a)?a.filter(c=>c!=="null")[0]:a;if(i)switch(i){case"string":return{kind:l.String,nullable:n,format:r.format,discriminator:o};case"integer":return{kind:l.Integer,nullable:n,discriminator:o};case"number":return{kind:l.Number,nullable:n,discriminator:o};case"boolean":return{kind:l.Boolean,nullable:n,discriminator:o};case"array":let c=r,f=this.schemaRefToIR(e,c.items);return{kind:l.Array,items:f,nullable:n,discriminator:o};case"object":let u=[];if(r.properties){let d=Object.keys(r.properties).sort();for(let S of d){let D=r.properties[S],F=this.schemaRefToIR(e,D),N=r.required?.includes(S)||!1;u.push({name:S,type:F,required:N,annotations:this.extractAnnotations(D)})}}let b;return r.additionalProperties&&typeof r.additionalProperties=="object"&&(b=this.schemaRefToIR(e,r.additionalProperties)),{kind:l.Object,properties:u,additionalProperties:b,nullable:n,discriminator:o}}return{kind:l.Unknown,nullable:n,discriminator:o}}extractAnnotations(e){if(!e||"$ref"in e)return{};let t=e;return{title:t.title,description:t.description,deprecated:t.deprecated,readOnly:t.readOnly,writeOnly:t.writeOnly,default:t.default,examples:t.example?Array.isArray(t.example)?t.example:[t.example]:void 0}}inferEnumBaseKind(e){let t=ee(e);if(t){let r=Array.isArray(t)?t.filter(n=>n!=="null")[0]:t;if(r)switch(r){case"string":return l.String;case"integer":return l.Integer;case"number":return l.Number;case"boolean":return l.Boolean}}if(e.enum&&e.enum.length>0){let r=e.enum[0];if(typeof r=="string")return l.String;if(typeof r=="number")return Number.isInteger(r)?l.Integer:l.Number;if(typeof r=="boolean")return l.Boolean}return l.Unknown}};v=Ve([Ge()],v);function O(s){if(s=s.trim(),s==="")return"";let e=s.split(/[^A-Za-z0-9]+/).filter(r=>r!==""),t=[];for(let r of e){let n=re(r);t.push(...n)}return t.filter(r=>r!=="").map(r=>r.charAt(0).toUpperCase()+r.slice(1).toLowerCase()).join("")}p(O,"toPascalCase");function H(s){let e=O(s);return e===""?"":e.charAt(0).toLowerCase()+e.slice(1)}p(H,"toCamelCase");function V(s){if(s=s.trim(),s==="")return"";let e=s.split(/[^A-Za-z0-9]+/).filter(r=>r!==""),t=[];for(let r of e){let n=re(r);t.push(...n)}return t.filter(r=>r!=="").map(r=>r.toLowerCase()).join("_")}p(V,"toSnakeCase");function ue(s){if(s=s.trim(),s==="")return"";let e=s.split(/[^A-Za-z0-9]+/).filter(r=>r!==""),t=[];for(let r of e){let n=re(r);t.push(...n)}return t.filter(r=>r!=="").map(r=>r.toLowerCase()).join("-")}p(ue,"toKebabCase");function re(s){if(s==="")return[];let e=[],t="",r=Array.from(s);for(let n=0;n<r.length;n++){let o=r[n],a=!1;n>0&&te(o)&&(te(r[n-1])?n<r.length-1&&!te(r[n+1])&&(a=!0):a=!0),a&&t.length>0&&(e.push(t),t=""),t+=o}return t.length>0&&e.push(t),e}p(re,"splitCamelCase");function te(s){return s>="A"&&s<="Z"}p(te,"isUppercase");function Le(s,e,t,r){var n=arguments.length,o=n<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(s,e,t,r);else for(var i=s.length-1;i>=0;i--)(a=s[i])&&(o=(n<3?a(o):n>3?a(e,t,o):a(e,t))||o);return n>3&&o&&Object.defineProperty(e,t,o),o}p(Le,"_ts_decorate");function me(s,e){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(s,e)}p(me,"_ts_metadata");var x=class{static{p(this,"IrBuilderService")}schemaConverter;constructor(e){this.schemaConverter=e}buildIR(e){let t=this.collectTags(e),r=this.collectSecuritySchemes(e),n=this.buildStructuredModels(e),o={};for(let i of t)o[i]=!0;let a=this.buildIRFromDoc(e,o);return a.securitySchemes=r,a.modelDefs=[...n,...a.modelDefs],a.openApiDocument=e,a}filterIR(e,t){let r=this.compileTagFilters(t.includeTags||[]),n=this.compileTagFilters(t.excludeTags||[]),o=[];for(let i of e.services){let c=[];for(let f of i.operations)this.shouldIncludeOperation(f.originalTags,r,n)&&c.push(f);c.length>0&&o.push({...i,operations:c})}let a={services:o,models:e.models,securitySchemes:e.securitySchemes,modelDefs:e.modelDefs,openApiDocument:e.openApiDocument};return a.modelDefs=this.filterUnusedModelDefs(a,e.modelDefs),a}detectStreamingContentType(e){let t=e.toLowerCase().split(";")[0].trim();return t==="text/event-stream"?{isStreaming:!0,format:"sse"}:t==="application/x-ndjson"||t==="application/x-jsonlines"||t==="application/jsonl"?{isStreaming:!0,format:"ndjson"}:t.includes("stream")||t.includes("chunked")?{isStreaming:!0,format:"chunked"}:{isStreaming:!1}}collectTags(e){let t=new Set;if(t.add("misc"),e.paths)for(let[r,n]of Object.entries(e.paths)){if(!n)continue;let o=[n.get,n.post,n.put,n.patch,n.delete,n.options,n.head,n.trace];for(let a of o)if(!(!a||!a.tags))for(let i of a.tags)t.add(i)}return Array.from(t).sort()}compileTagFilters(e){return e.map(t=>{try{return new RegExp(t)}catch(r){throw new Error(`Invalid tag filter pattern "${t}": ${r instanceof Error?r.message:String(r)}`)}})}shouldIncludeOperation(e,t,r){let n=t.length===0;if(t.length>0)for(let o of e){for(let a of t)if(a.test(o)){n=!0;break}if(n)break}if(!n)return!1;if(r.length>0){for(let o of e)for(let a of r)if(a.test(o))return!1}return!0}buildIRFromDoc(e,t){let r={};r.misc={tag:"misc",operations:[]};let n=[],o=new Set;if(e.components?.schemas)for(let c of Object.keys(e.components.schemas))o.add(c);let a=p((c,f,u,b)=>{r[c]||(r[c]={tag:c,operations:[]});let d=f.operationId||"",{pathParams:S,queryParams:D}=this.collectParams(e,f),{requestBody:F,extractedTypes:N}=this.extractRequestBodyWithTypes(e,f,c,d,u,o);n.push(...N);let{response:A,extractedTypes:_}=this.extractResponseWithTypes(e,f,c,d,u,o);n.push(..._);let L=f.tags&&f.tags.length>0?[...f.tags]:["misc"];r[c].operations.push({operationID:d,method:u,path:b,tag:c,originalTags:L,summary:f.summary||"",description:f.description||"",deprecated:f.deprecated||!1,pathParams:S,queryParams:D,requestBody:F,response:A})},"addOp");if(e.paths)for(let[c,f]of Object.entries(e.paths)){if(!f)continue;let u=[{op:f.get,method:"GET"},{op:f.post,method:"POST"},{op:f.put,method:"PUT"},{op:f.patch,method:"PATCH"},{op:f.delete,method:"DELETE"},{op:f.options,method:"OPTIONS"},{op:f.head,method:"HEAD"},{op:f.trace,method:"TRACE"}];for(let{op:b,method:d}of u){if(!b)continue;let S=this.firstAllowedTag(b.tags||[],t);S&&a(S,b,d,c)}}let i=Object.values(r);for(let c of i)c.operations.sort((f,u)=>f.path===u.path?f.method.localeCompare(u.method):f.path.localeCompare(u.path));return i.sort((c,f)=>c.tag.localeCompare(f.tag)),{services:i,models:[],securitySchemes:[],modelDefs:n}}deriveMethodName(e,t,r){if(e){let o=e.indexOf("Controller_"),a=o>=0?e.substring(o+11):e;return H(a)}let n=r.includes("{")&&r.includes("}");switch(t){case"GET":return n?"get":"list";case"POST":return"create";case"PUT":case"PATCH":return"update";case"DELETE":return"delete";default:return t.toLowerCase()}}firstAllowedTag(e,t){for(let r of e)if(t[r])return r;return e.length===0&&t.misc?"misc":""}collectSecuritySchemes(e){if(!e.components?.securitySchemes)return[];let t=Object.keys(e.components.securitySchemes).sort(),r=[];for(let n of t){let o=e.components.securitySchemes[n];if(!o||"$ref"in o)continue;let a={key:n,type:o.type};switch(o.type){case"http":a.scheme=o.scheme,a.bearerFormat=o.bearerFormat;break;case"apiKey":a.in=o.in,a.name=o.name;break;case"oauth2":case"openIdConnect":break}r.push(a)}return r}collectParams(e,t){let r=[],n=[];if(t.parameters)for(let o of t.parameters){if(!o||"$ref"in o)continue;let a=o,i=this.schemaConverter.schemaRefToIR(e,a.schema),c={name:a.name,required:a.required||!1,schema:i,description:a.description||""};a.in==="path"?r.push(c):a.in==="query"&&n.push(c)}return r.sort((o,a)=>o.name.localeCompare(a.name)),n.sort((o,a)=>o.name.localeCompare(a.name)),{pathParams:r,queryParams:n}}findMatchingComponentSchema(e,t){if(!t||!e.components?.schemas)return null;if("$ref"in t&&t.$ref){let n=t.$ref;if(n.startsWith("#/components/schemas/")){let o=n.replace("#/components/schemas/","");if(e.components.schemas[o])return o}}let r=this.schemaConverter.schemaRefToIR(e,t);for(let[n,o]of Object.entries(e.components.schemas)){let a=this.schemaConverter.schemaRefToIR(e,o);if(this.compareSchemas(r,a))return n}return null}compareSchemas(e,t){if(e.kind!==t.kind)return!1;if(e.kind===l.Ref&&t.kind===l.Ref)return e.ref===t.ref;if(e.kind===l.Object&&t.kind===l.Object){let r=e.properties||[],n=t.properties||[];if(r.length!==n.length)return!1;let o=new Map(r.map(i=>[i.name,{type:i.type,required:i.required}])),a=new Map(n.map(i=>[i.name,{type:i.type,required:i.required}]));if(o.size!==a.size)return!1;for(let[i,c]of o){let f=a.get(i);if(!f||c.required!==f.required||!this.compareSchemas(c.type,f.type))return!1}return!0}return e.kind===l.Array&&t.kind===l.Array?!e.items||!t.items?e.items===t.items:this.compareSchemas(e.items,t.items):!0}extractModelNameFromSchema(e){return e.kind===l.Ref&&e.ref?e.ref:e.kind===l.Array&&e.items&&e.items.kind===l.Ref&&e.items.ref?e.items.ref:null}generateTypeName(e,t,r,n,o,a){let i=this.extractModelNameFromSchema(e);if(i)return i;let c=this.deriveMethodName(r,n,o);return`${O(t)}${O(c)}${a}`}extractRequestBodyWithTypes(e,t,r,n,o,a){let i=[];if(!t.requestBody||"$ref"in t.requestBody)return{requestBody:null,extractedTypes:[]};let c=t.requestBody;if(c.content?.["application/json"]){let u=c.content["application/json"],b=this.schemaConverter.schemaRefToIR(e,u.schema),d=this.findMatchingComponentSchema(e,u.schema);if(d)return{requestBody:{contentType:"application/json",typeTS:"",schema:{kind:l.Ref,ref:d,nullable:!1},required:c.required||!1},extractedTypes:[]};let S=this.generateTypeName(b,r,n,o,t.path,"RequestBody");return b.kind===l.Object&&!a.has(S)?(a.add(S),i.push({name:S,schema:b,annotations:this.schemaConverter.extractAnnotations(u.schema)}),{requestBody:{contentType:"application/json",typeTS:"",schema:{kind:l.Ref,ref:S,nullable:!1},required:c.required||!1},extractedTypes:i}):{requestBody:{contentType:"application/json",typeTS:"",schema:b,required:c.required||!1},extractedTypes:[]}}return{requestBody:this.extractRequestBody(e,t),extractedTypes:[]}}extractRequestBody(e,t){if(!t.requestBody||"$ref"in t.requestBody)return null;let r=t.requestBody;if(r.content?.["application/json"]){let n=r.content["application/json"];return{contentType:"application/json",typeTS:"",schema:this.schemaConverter.schemaRefToIR(e,n.schema),required:r.required||!1}}if(r.content?.["application/x-www-form-urlencoded"]){let n=r.content["application/x-www-form-urlencoded"];return{contentType:"application/x-www-form-urlencoded",typeTS:"",schema:this.schemaConverter.schemaRefToIR(e,n.schema),required:r.required||!1}}if(r.content?.["multipart/form-data"])return{contentType:"multipart/form-data",typeTS:"",schema:{kind:"unknown",nullable:!1},required:r.required||!1};if(r.content){let n=Object.keys(r.content)[0],o=r.content[n];return{contentType:n,typeTS:"",schema:this.schemaConverter.schemaRefToIR(e,o.schema),required:r.required||!1}}return null}extractResponseWithTypes(e,t,r,n,o,a){let i=[];if(!t.responses)return{response:{typeTS:"unknown",schema:{kind:l.Unknown,nullable:!1},description:"",isStreaming:!1,contentType:""},extractedTypes:[]};let c=["200","201"];for(let u of c){let b=t.responses[u];if(b&&!("$ref"in b)){let d=b;if(d.content){for(let[A,_]of Object.entries(d.content)){let L=this.detectStreamingContentType(A),U=this.schemaConverter.schemaRefToIR(e,_.schema);if(L.isStreaming)return{response:{typeTS:"",schema:U,description:d.description||"",isStreaming:!0,contentType:A,streamingFormat:L.format},extractedTypes:[]};if(A==="application/json"){let ae=this.findMatchingComponentSchema(e,_.schema);if(ae)return{response:{typeTS:"",schema:{kind:l.Ref,ref:ae,nullable:!1},description:d.description||"",isStreaming:!1,contentType:A},extractedTypes:[]};let Z=this.generateTypeName(U,r,n,o,t.path,"Response");return U.kind===l.Object&&!a.has(Z)?(a.add(Z),i.push({name:Z,schema:U,annotations:this.schemaConverter.extractAnnotations(_.schema)}),{response:{typeTS:"",schema:{kind:l.Ref,ref:Z,nullable:!1},description:d.description||"",isStreaming:!1,contentType:A},extractedTypes:i}):{response:{typeTS:"",schema:U,description:d.description||"",isStreaming:!1,contentType:A},extractedTypes:[]}}}let S=Object.keys(d.content)[0],D=d.content[S],F=this.schemaConverter.schemaRefToIR(e,D.schema),N=this.detectStreamingContentType(S);return{response:{typeTS:"",schema:F,description:d.description||"",isStreaming:N.isStreaming,contentType:S,streamingFormat:N.format},extractedTypes:[]}}return{response:{typeTS:"void",schema:{kind:l.Unknown,nullable:!1},description:d.description||"",isStreaming:!1,contentType:""},extractedTypes:[]}}}return{response:this.extractResponse(e,t),extractedTypes:[]}}extractResponse(e,t){if(!t.responses)return{typeTS:"unknown",schema:{kind:l.Unknown,nullable:!1},description:"",isStreaming:!1,contentType:""};let r=["200","201"];for(let n of r){let o=t.responses[n];if(o&&!("$ref"in o)){let a=o;if(a.content){for(let[u,b]of Object.entries(a.content)){let d=this.detectStreamingContentType(u);if(d.isStreaming)return{typeTS:"",schema:this.schemaConverter.schemaRefToIR(e,b.schema),description:a.description||"",isStreaming:!0,contentType:u,streamingFormat:d.format}}if(a.content["application/json"]){let u=a.content["application/json"];return{typeTS:"",schema:this.schemaConverter.schemaRefToIR(e,u.schema),description:a.description||"",isStreaming:!1,contentType:"application/json"}}let i=Object.keys(a.content)[0],c=a.content[i],f=this.detectStreamingContentType(i);return{typeTS:"",schema:this.schemaConverter.schemaRefToIR(e,c.schema),description:a.description||"",isStreaming:f.isStreaming,contentType:i,streamingFormat:f.format}}return{typeTS:"void",schema:{kind:l.Unknown,nullable:!1},description:a.description||"",isStreaming:!1,contentType:""}}}for(let[n,o]of Object.entries(t.responses))if(n.length===3&&n[0]==="2"&&o&&!("$ref"in o)){let a=o;if(n==="204")return{typeTS:"void",schema:{kind:l.Unknown,nullable:!1},description:a.description||"",isStreaming:!1,contentType:""};if(a.content){for(let[u,b]of Object.entries(a.content)){let d=this.detectStreamingContentType(u);if(d.isStreaming)return{typeTS:"",schema:this.schemaConverter.schemaRefToIR(e,b.schema),description:a.description||"",isStreaming:!0,contentType:u,streamingFormat:d.format}}if(a.content["application/json"]){let u=a.content["application/json"];return{typeTS:"",schema:this.schemaConverter.schemaRefToIR(e,u.schema),description:a.description||"",isStreaming:!1,contentType:"application/json"}}let i=Object.keys(a.content)[0],c=a.content[i],f=this.detectStreamingContentType(i);return{typeTS:"",schema:this.schemaConverter.schemaRefToIR(e,c.schema),description:a.description||"",isStreaming:f.isStreaming,contentType:i,streamingFormat:f.format}}}return{typeTS:"unknown",schema:{kind:l.Unknown,nullable:!1},description:"",isStreaming:!1,contentType:""}}buildStructuredModels(e){let t=[];if(!e.components?.schemas)return t;let r=Object.keys(e.components.schemas).sort(),n=new Set;for(let o of r)n.add(o);for(let o of r){let a=e.components.schemas[o],i=this.schemaConverter.schemaRefToIR(e,a);t.push({name:o,schema:i,annotations:this.schemaConverter.extractAnnotations(a)})}return t}filterUnusedModelDefs(e,t){let r=new Map;for(let i of t)r.set(i.name,i);let n=new Set,o=new Set,a=p(i=>{if(i.kind==="ref"&&i.ref){let c=i.ref;if(n.add(c),!o.has(c)){o.add(c);let f=r.get(c);f&&a(f.schema)}}if(i.items&&a(i.items),i.additionalProperties&&a(i.additionalProperties),i.oneOf)for(let c of i.oneOf)a(c);if(i.anyOf)for(let c of i.anyOf)a(c);if(i.allOf)for(let c of i.allOf)a(c);if(i.not&&a(i.not),i.properties)for(let c of i.properties)a(c.type)},"collectRefs");for(let i of e.services)for(let c of i.operations){for(let f of c.pathParams)a(f.schema);for(let f of c.queryParams)a(f.schema);c.requestBody&&a(c.requestBody.schema),a(c.response.schema)}return t.filter(i=>n.has(i.name))}};x=Le([Ze(),me("design:type",Function),me("design:paramtypes",[typeof v>"u"?Object:v])],x);import{Injectable as Ke,Logger as Qe}from"@nestjs/common";import I from"@apidevtools/swagger-parser";import*as ne from"fs";import*as oe from"path";function Je(s,e,t,r){var n=arguments.length,o=n<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(s,e,t,r);else for(var i=s.length-1;i>=0;i--)(a=s[i])&&(o=(n<3?a(o):n>3?a(e,t,o):a(e,t))||o);return n>3&&o&&Object.defineProperty(e,t,o),o}p(Je,"_ts_decorate");var P=class s{static{p(this,"OpenApiService")}logger=new Qe(s.name);async loadDocument(e){try{let t=null;try{t=new URL(e)}catch{}let r;if(t&&(t.protocol==="http:"||t.protocol==="https:")){this.logger.debug(`Loading OpenAPI spec from URL: ${e}`);let o=await fetch(e,{signal:AbortSignal.timeout(1e4)});if(!o.ok)throw new Error(`Failed to fetch OpenAPI spec: HTTP ${o.status} ${o.statusText}`);let a=await o.text(),i;try{i=JSON.parse(a)}catch{throw new Error("OpenAPI spec is not valid JSON")}try{let c=await I.parse(i);r=await I.bundle(c)}catch(c){this.logger.debug(`Bundle failed: ${c instanceof Error?c.message:String(c)}`),this.logger.debug("Attempting dereference directly");try{let f=await I.parse(i);r=await I.dereference(f)}catch(f){throw f}}}else{let o=oe.resolve(e);if(!ne.existsSync(o))throw new Error(`OpenAPI spec file not found: ${o}`);this.logger.debug(`Loading OpenAPI spec from file: ${o}`);try{r=await I.bundle(o)}catch(a){this.logger.debug(`Bundle failed: ${a instanceof Error?a.message:String(a)}`),this.logger.debug("Attempting dereference directly"),r=await I.dereference(o)}}let n=fe(r);if(!pe(n))throw new Error(`Unsupported OpenAPI version: ${r.openapi}. Only versions 3.0.x and 3.1.0 are supported.`);return this.logger.log(`Detected OpenAPI version: ${n} (${r.openapi})`),r}catch(t){throw t instanceof Error?new Error(`Failed to load OpenAPI document from ${e}: ${t.message}`):t}}async validateDocument(e){try{let t=null;try{t=new URL(e)}catch{}if(t&&(t.protocol==="http:"||t.protocol==="https:"))await I.validate(e);else{let r=oe.resolve(e);if(!ne.existsSync(r))throw new Error(`OpenAPI spec file not found: ${r}`);await I.validate(r)}}catch(t){throw t instanceof Error?new Error(`Invalid OpenAPI document: ${t.message}`):t}}};P=Je([Ke()],P);function Ye(s,e,t,r){var n=arguments.length,o=n<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(s,e,t,r);else for(var i=s.length-1;i>=0;i--)(a=s[i])&&(o=(n<3?a(o):n>3?a(e,t,o):a(e,t))||o);return n>3&&o&&Object.defineProperty(e,t,o),o}p(Ye,"_ts_decorate");function de(s,e){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(s,e)}p(de,"_ts_metadata");var j=class s{static{p(this,"GeneratorService")}irBuilder;openApiService;logger=new et(s.name);generators=new Map;constructor(e,t){this.irBuilder=e,this.openApiService=t}register(e){this.generators.set(e.getType(),e),this.logger.debug(`Registered generator: ${e.getType()}`)}getGenerator(e){return this.generators.get(e)}getAvailableTypes(){return Array.from(this.generators.keys())}async generate(e,t){let r=this.getGenerator(t.type);if(!r)throw new Error(`Unsupported client type: ${t.type}`);let n=await this.openApiService.loadDocument(e),o=this.irBuilder.buildIR(n),a=this.irBuilder.filterIR(o,t);await r.generate(t,a)}};j=Ye([Xe(),de("design:type",Function),de("design:paramtypes",[typeof x>"u"?Object:x,typeof P>"u"?Object:P])],j);import{Module as lt}from"@nestjs/common";import{Module as rt}from"@nestjs/common";function tt(s,e,t,r){var n=arguments.length,o=n<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(s,e,t,r);else for(var i=s.length-1;i>=0;i--)(a=s[i])&&(o=(n<3?a(o):n>3?a(e,t,o):a(e,t))||o);return n>3&&o&&Object.defineProperty(e,t,o),o}p(tt,"_ts_decorate");var M=class{static{p(this,"OpenApiModule")}};M=tt([rt({providers:[P],exports:[P]})],M);import{Injectable as ct,Logger as ft}from"@nestjs/common";import*as w from"fs";import*as g from"path";import*as m from"handlebars";import*as y from"handlebars";function he(){y.registerHelper("pascal",s=>O(s)),y.registerHelper("camel",s=>H(s)),y.registerHelper("kebab",s=>ue(s)),y.registerHelper("snake",s=>V(s)),y.registerHelper("serviceName",s=>O(s)+"Service"),y.registerHelper("serviceProp",s=>H(s)),y.registerHelper("fileBase",s=>V(s).toLowerCase()),y.registerHelper("eq",(s,e)=>s===e),y.registerHelper("ne",(s,e)=>s!==e),y.registerHelper("gt",(s,e)=>s>e),y.registerHelper("lt",(s,e)=>s<e),y.registerHelper("sub",(s,e)=>s-e),y.registerHelper("len",s=>Array.isArray(s)?s.length:0),y.registerHelper("or",function(...s){let e=s[s.length-1];return e&&e.fn?s.slice(0,-1).some(t=>t)?e.fn(this):e.inverse(this):s.slice(0,-1).some(t=>t)}),y.registerHelper("and",function(...s){let e=s[s.length-1];return e&&e.fn?s.slice(0,-1).every(t=>t)?e.fn(this):e.inverse(this):s.slice(0,-1).every(t=>t)}),y.registerHelper("replace",(s,e,t)=>typeof s!="string"?s:s.replace(new RegExp(e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),"g"),t)),y.registerHelper("index",(s,e)=>s?.[e]),y.registerHelper("getServiceName",s=>{let e=s.split(".");return e.length>1?e[1]:s}),y.registerHelper("groupByNamespace",s=>{let e={};for(let t of s){let r=t.tag.split(".");if(r.length===1)e[""]||(e[""]=[]),e[""].push(t);else{let n=r[0];e[n]||(e[n]=[]),e[n].push(t)}}return e}),y.registerHelper("getRootServices",s=>s.filter(e=>!e.tag.includes("."))),y.registerHelper("dict",()=>({})),y.registerHelper("setVar",(s,e,t)=>(t&&t.data&&t.data.root&&(t.data.root[`_${s}`]=e),"")),y.registerHelper("getVar",(s,e)=>e&&e.data&&e.data.root&&e.data.root[`_${s}`]||!1),y.registerHelper("set",(s,e,t)=>(s&&typeof s=="object"&&(s[e]=t),"")),y.registerHelper("hasKey",(s,e)=>s&&typeof s=="object"&&e in s),y.registerHelper("lookup",(s,e)=>s&&typeof s=="object"?s[e]:void 0),y.registerHelper("reMatch",(s,e)=>{try{return new RegExp(s).test(e)}catch{return!1}})}p(he,"registerCommonHandlebarsHelpers");function nt(s){return s.replace(/&quot;/g,'"').replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&#x60;/g,"`").replace(/&#96;/g,"`").replace(/&amp;/g,"&")}p(nt,"decodeHtmlEntities");function C(s,e,t,r=!1){let n;switch(s.kind){case l.String:s.format==="binary"?n="Blob":n="string";break;case l.Number:case l.Integer:n="number";break;case l.Boolean:n="boolean";break;case l.Null:n="null";break;case l.Ref:s.ref?e?.some(o=>o.type===s.ref)||r&&t&&t.find(a=>a.name===s.ref)?n=s.ref:n="Schema."+s.ref:n="unknown";break;case l.Array:if(s.items){let o=C(s.items,e,t);o.includes(" | ")||o.includes(" & ")?n=`Array<(${o})>`:n=`Array<${o}>`}else n="Array<unknown>";break;case l.OneOf:s.oneOf?n=s.oneOf.map(a=>C(a,e,t)).join(" | "):n="unknown";break;case l.AnyOf:s.anyOf?n=s.anyOf.map(a=>C(a,e,t)).join(" | "):n="unknown";break;case l.AllOf:s.allOf?n=s.allOf.map(a=>C(a,e,t)).join(" & "):n="unknown";break;case l.Enum:if(s.enumValues&&s.enumValues.length>0){let o=[];switch(s.enumBase){case l.Number:case l.Integer:for(let a of s.enumValues)o.push(a);break;case l.Boolean:for(let a of s.enumValues)a==="true"||a==="false"?o.push(a):o.push(`"${a}"`);break;default:for(let a of s.enumValues)o.push(`"${a}"`)}n=o.join(" | ")}else n="unknown";break;case l.Object:if(!s.properties||s.properties.length===0)n="Record<string, unknown>";else{let o=[];for(let a of s.properties){let i=C(a.type,e,t,r);a.required?o.push(`${a.name}: ${i}`):o.push(`${a.name}?: ${i}`)}n="{ "+o.join("; ")+" }"}break;default:n="unknown"}return s.nullable&&n!=="null"&&(n+=" | null"),nt(n)}p(C,"schemaToTSType");function z(s){let e=s.path,t=e.includes("{")&&e.includes("}");if(s.operationID)return H(s.operationID);switch(s.method){case"GET":return t?"get":"list";case"POST":return"create";case"PUT":case"PATCH":return"update";case"DELETE":return"delete";default:return s.method.toLowerCase()}}p(z,"deriveMethodName");async function ye(s,e){if(s.operationIdParser)try{let r=await s.operationIdParser(e.operationID,e.method,e.path);if(r)return H(r)}catch{}let t=ot(e.operationID);return t?H(t):z(e)}p(ye,"resolveMethodName");function ot(s){if(!s)return"";let e=s.indexOf("Controller_");return e>=0?s.substring(e+11):s}p(ot,"defaultParseOperationID");function be(s){let e=s.path,t="`";for(let r=0;r<e.length;r++){if(e[r]==="{"){let n=r+1;for(;n<e.length&&e[n]!=="}";)n++;if(n<e.length){let o=e.substring(r+1,n);t+=`\${encodeURIComponent(${o})}`,r=n;continue}}t+=e[r]}return t+="`",t}p(be,"buildPathTemplate");function we(s){let t=s.path.split("/"),r=[];for(let o of t)o!==""&&(o.startsWith("{")&&o.endsWith("}")||r.push(o));return`'${r.join("/")}'`}p(we,"buildQueryKeyBase");function Q(s){let e=[],t=new Map;for(let n=0;n<s.pathParams.length;n++)t.set(s.pathParams[n].name,n);let r=s.path;for(let n=0;n<r.length;n++)if(r[n]==="{"){let o=n+1;for(;o<r.length&&r[o]!=="}";)o++;if(o<r.length){let a=r.substring(n+1,o),i=t.get(a);i!==void 0&&e.push(s.pathParams[i]),n=o;continue}}return e}p(Q,"orderPathParams");function ge(s,e,t,r=!1){return C(s,t,e,r)}p(ge,"schemaToTSTypeWithSimpleTypes");function se(s,e,t,r,n=!1){let o=[];for(let a of Q(s))o.push(`${a.name}: ${ge(a.schema,t,r,n)}`);if(s.queryParams.length>0){let a=O(s.tag)+O(e)+"Query";o.push(`query?: Schema.${a}`)}if(s.requestBody){let a=s.requestBody.required===!0?"":"?";o.push(`body${a}: ${ge(s.requestBody.schema,t,r,n)}`)}return o.push('init?: Omit<RequestInit, "method" | "body">'),o}p(se,"buildMethodSignature");function Se(s,e){if(!e||e.length===0)return[];let t=new Set,r=p(o=>{let a=s.find(i=>i.name===o);return a?a.schema:null},"resolveRef"),n=p(o=>{if(o.kind==="ref"&&o.ref)if(e.some(i=>i.type===o.ref))t.add(o.ref);else{let i=r(o.ref);i&&n(i)}else if(o.kind==="array"&&o.items)n(o.items);else if(o.kind==="object"&&o.properties)for(let a of o.properties)n(a.type);else if(o.kind==="oneOf"&&o.oneOf)for(let a of o.oneOf)n(a);else if(o.kind==="anyOf"&&o.anyOf)for(let a of o.anyOf)n(a);else if(o.kind==="allOf"&&o.allOf)for(let a of o.allOf)n(a)},"checkSchema");for(let o of s)n(o.schema);return e.filter(o=>t.has(o.type))}p(Se,"collectPredefinedTypesUsedInSchema");function Te(s,e,t){if(!e||e.length===0)return[];let r=new Set,n=p(a=>{if(!t)return null;let i=t.find(c=>c.name===a);return i?i.schema:null},"resolveRef"),o=p(a=>{if(a.kind==="ref"&&a.ref)if(e.some(c=>c.type===a.ref))r.add(a.ref);else{let c=n(a.ref);c&&o(c)}else if(a.kind==="array"&&a.items)o(a.items);else if(a.kind==="object"&&a.properties)for(let i of a.properties)o(i.type);else if(a.kind==="oneOf"&&a.oneOf)for(let i of a.oneOf)o(i);else if(a.kind==="anyOf"&&a.anyOf)for(let i of a.anyOf)o(i);else if(a.kind==="allOf"&&a.allOf)for(let i of a.allOf)o(i)},"checkSchema");for(let a of s.operations)for(let i of a.pathParams)o(i.schema);return e.filter(a=>r.has(a.type))}p(Te,"collectPredefinedTypesUsedInService");function Oe(s){let e=[];for(let t of Q(s))e.push(t.name);return s.queryParams.length>0&&e.push("query"),s.requestBody&&e.push("body"),e}p(Oe,"queryKeyArgs");function G(s){let e=!1;for(let t of s)if(!(t>="a"&&t<="z"||t>="A"&&t<="Z"||t>="0"&&t<="9"||t==="_"||t==="$")){e=!0;break}return s.length>0&&s[0]>="0"&&s[0]<="9"&&(e=!0),e?`"${s}"`:s}p(G,"quoteTSPropertyName");function q(s,e=new Set){let t=new Set;if(s.kind==="ref"&&s.ref){let r=s.ref;e.has(r)||(e.add(r),t.add(r))}else if(s.kind==="array"&&s.items)q(s.items,e).forEach(n=>t.add(n));else if(s.kind==="object"&&s.properties){for(let r of s.properties)q(r.type,e).forEach(o=>t.add(o));s.additionalProperties&&q(s.additionalProperties,e).forEach(n=>t.add(n))}else if(s.kind==="oneOf"&&s.oneOf)for(let r of s.oneOf)q(r,e).forEach(o=>t.add(o));else if(s.kind==="anyOf"&&s.anyOf)for(let r of s.anyOf)q(r,e).forEach(o=>t.add(o));else if(s.kind==="allOf"&&s.allOf)for(let r of s.allOf)q(r,e).forEach(o=>t.add(o));else s.kind==="not"&&s.not&&q(s.not,e).forEach(n=>t.add(n));return t}p(q,"extractRefDependencies");function ke(s){let e=new Map;for(let a of s)e.set(a.name,a);let t=new Map;for(let a of s){let i=q(a.schema),c=new Set;for(let f of i)e.has(f)&&c.add(f);t.set(a.name,c)}let r=[],n=new Map,o=[];for(let a of s)n.set(a.name,t.get(a.name)?.size||0),n.get(a.name)===0&&o.push(a.name);for(;o.length>0;){let a=o.shift(),i=e.get(a);i&&r.push(i);for(let c of s)if(t.get(c.name)?.has(a)){let u=(n.get(c.name)||0)-1;n.set(c.name,u),u===0&&o.push(c.name)}}if(r.length<s.length){let a=new Set(r.map(i=>i.name));for(let i of s)a.has(i.name)||r.push(i)}return r}p(ke,"sortModelDefsByDependencies");function ve(s){return s.response.isStreaming===!0}p(ve,"isStreamingOperation");function xe(s){let e=s.response.schema;return e.kind===l.Array&&e.items?C(e.items):s.response.streamingFormat==="sse"?"string":C(e)}p(xe,"getStreamingItemType");function $(s,e="",t,r=!1){let n=e+" ",o;switch(s.kind){case l.String:o="z.string()",s.format==="date"||s.format==="date-time"?o="z.iso.datetime()":s.format==="email"?o="z.email()":s.format==="uri"||s.format==="url"?o="z.url()":s.format==="uuid"&&(o="z.uuid()");break;case l.Number:o="z.number()";break;case l.Integer:o="z.number().int()";break;case l.Boolean:o="z.boolean()";break;case l.Null:o="z.null()";break;case l.Ref:s.ref?o=r?`${s.ref}Schema`:`Schema.${s.ref}Schema`:o="z.unknown()";break;case l.Array:s.items?o=`${$(s.items,n,t,r)}.array()`:o="z.unknown().array()";break;case l.Object:if(!s.properties||s.properties.length===0)s.additionalProperties?o=`z.record(z.string(), ${$(s.additionalProperties,n,t,r)})`:o="z.record(z.string(), z.unknown())";else{let a=[];for(let c of s.properties){let f=$(c.type,n,t,r),u=G(c.name);c.required?a.push(`${n}${u}: ${f}`):a.push(`${n}${u}: ${f}.optional()`)}let i=`z.object({
2
- ${a.join(`,
3
- `)}
4
- ${e}})`;if(s.additionalProperties){let c=$(s.additionalProperties,n,t,r);o=`${i}.catchall(${c})`}else o=i}break;case l.Enum:s.enumValues&&s.enumValues.length>0?o=`z.enum([${s.enumValues.map(i=>i==="true"||i==="false"||/^-?[0-9]+(\.[0-9]+)?$/.test(i)?i:JSON.stringify(i)).join(", ")}])`:o="z.string()";break;case l.OneOf:s.oneOf&&s.oneOf.length>0?o=`z.union([${s.oneOf.map(i=>$(i,n,t,r)).join(", ")}])`:o="z.unknown()";break;case l.AnyOf:s.anyOf&&s.anyOf.length>0?o=`z.union([${s.anyOf.map(i=>$(i,n,t,r)).join(", ")}])`:o="z.unknown()";break;case l.AllOf:if(s.allOf&&s.allOf.length>0){let a=s.allOf.map(i=>$(i,n,t,r));o=a.join(".and(")+")".repeat(a.length-1)}else o="z.unknown()";break;default:o="z.unknown()"}return s.nullable&&o!=="z.null()"&&(o=`${o}.nullable()`),o}p($,"schemaToZodSchema");import{exec as st}from"child_process";import{promisify as at}from"util";import*as je from"path";import*as Ce from"fs";var Pe=at(st);async function Re(s,e,t){let r=t||console;try{try{await Pe("npx --yes prettier --version",{cwd:s,maxBuffer:10*1024*1024})}catch{r.warn?.("Prettier is not available. Skipping code formatting. Install prettier to enable formatting.");return}let n=[];for(let c of e)if(c.endsWith(".ts")||c.endsWith(".tsx")){let f=je.join(s,c);Ce.existsSync(f)&&n.push(c)}if(n.length===0){r.debug?.("No TypeScript files to format.");return}let o=n.map(c=>`"${c.replace(/\\/g,"/")}"`).join(" "),{stdout:a,stderr:i}=await Pe(`npx --yes prettier --write --log-level=error ${o}`,{cwd:s,maxBuffer:10*1024*1024});i&&!i.includes("warning")&&r.warn?.(i)}catch(n){let o=n instanceof Error?n.message:String(n);r.warn?.(`Failed to format code with Prettier: ${o}. Generated code will not be formatted.`)}}p(Re,"formatWithPrettier");function it(s,e,t,r){var n=arguments.length,o=n<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(s,e,t,r);else for(var i=s.length-1;i>=0;i--)(a=s[i])&&(o=(n<3?a(o):n>3?a(e,t,o):a(e,t))||o);return n>3&&o&&Object.defineProperty(e,t,o),o}p(it,"_ts_decorate");function He(s,e){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(s,e)}p(He,"_ts_metadata");var R=class s{static{p(this,"TypeScriptGeneratorService")}configService;logger=new ft(s.name);constructor(e){this.configService=e}getType(){return"typescript"}async generate(e,t){if(!e.defaultBaseURL&&t.openApiDocument?.servers){let f=t.openApiDocument.servers;Array.isArray(f)&&f.length>0&&(e.defaultBaseURL=f[0].url||"")}e.srcDir||(e.srcDir="src");let r=e.srcDir,n=g.join(e.outDir,r),o=g.join(n,"services");await w.promises.mkdir(o,{recursive:!0});let a=await this.preprocessIR(e,t);this.registerHandlebarsHelpers(e);let i=[];i.push(...await this.generateClient(e,a,n)),i.push(...await this.generateAuthStrategies(e,a,n)),i.push(...await this.generateIndex(e,a,n)),i.push(...await this.generateUtils(e,a,n)),i.push(...await this.generateServices(e,a,o)),i.push(...await this.generateSchema(e,a,n)),i.push(...await this.generateZodSchema(e,a,n)),await this.generatePackageJson(e),await this.generateTsConfig(e),i.push(...await this.generateTsupConfig(e)),await this.generateReadme(e,a),e.formatCode!==!1?(await this.generatePrettierConfig(e),this.logger.debug("Formatting generated TypeScript files with Prettier..."),await Re(e.outDir,i,this.logger)):this.logger.debug("Code formatting is disabled for this client.")}async preprocessIR(e,t){let r=await Promise.all(t.services.map(async n=>({...n,operations:await Promise.all(n.operations.map(async o=>{let a=await ye(e,o);return{...o,_resolvedMethodName:a}}))})));return{...t,services:r}}registerHandlebarsHelpers(e){he(),m.registerHelper("methodName",t=>t._resolvedMethodName||z(t)),m.registerHelper("queryTypeName",t=>{let r=t._resolvedMethodName||z(t);return O(t.tag)+O(r)+"Query"}),m.registerHelper("pathTemplate",t=>{let r=be(t);return new m.SafeString(r)}),m.registerHelper("queryKeyBase",t=>{let r=we(t);return new m.SafeString(r)}),m.registerHelper("pathParamsInOrder",t=>Q(t)),m.registerHelper("methodSignature",(t,r)=>{let n=t._resolvedMethodName||z(t),o=r?.data?.root?.IR?.modelDefs||[],a=r?.data?.root?.PredefinedTypes||[],i=r?.data?.root?.isSameFile||!1;return se(t,n,o,a,i).map(f=>new m.SafeString(f))}),m.registerHelper("methodSignatureNoInit",(t,r)=>{let n=t._resolvedMethodName||z(t),o=r?.data?.root?.IR?.modelDefs||[],a=r?.data?.root?.PredefinedTypes||[],i=r?.data?.root?.isSameFile||!1;return se(t,n,o,a,i).slice(0,-1)}),m.registerHelper("queryKeyArgs",t=>Oe(t).map(n=>new m.SafeString(n))),m.registerHelper("tsType",(t,r)=>{if(t&&typeof t=="object"&&"kind"in t){let n=r?.data?.root?.PredefinedTypes||[],o=r?.data?.root?.IR?.modelDefs||[],a=r?.data?.root?.isSameFile||!1;return C(t,n,o,a)}return"unknown"}),m.registerHelper("stripSchemaNs",t=>t.replace(/^Schema\./,"")),m.registerHelper("tsTypeStripNs",(t,r)=>{let n=r?.data?.root?.PredefinedTypes||[],o=r?.data?.root?.IR?.modelDefs||[],a=r?.data?.root?.isSameFile||!1;if(t&&typeof t=="object"&&"kind"in t){let c=C(t,n,o,a).replace(/Schema\./g,"");return new m.SafeString(c)}if(typeof t=="string"){if(t.startsWith("Schema.")){let i=t.replace(/^Schema\./,"");return n.find(f=>f.type===i)?new m.SafeString(i):new m.SafeString(i)}return new m.SafeString(t)}return"unknown"}),m.registerHelper("isPredefinedType",(t,r)=>(r?.data?.root?.PredefinedTypes||[]).some(o=>o.type===t)),m.registerHelper("getPredefinedType",(t,r)=>(r?.data?.root?.PredefinedTypes||[]).find(o=>o.type===t)),m.registerHelper("groupByPackage",t=>{let r={};for(let n of t||[])r[n.package]||(r[n.package]={package:n.package,types:[]}),r[n.package].types.push(n.type);return Object.values(r)}),m.registerHelper("getServicePredefinedTypes",(t,r)=>{let n=r?.data?.root?.PredefinedTypes||[],o=r?.data?.root?.IR?.modelDefs||[];return Te(t,n,o)}),m.registerHelper("getSchemaPredefinedTypes",t=>{let r=t?.data?.root?.PredefinedTypes||[],n=t?.data?.root?.IR?.modelDefs||[];return Se(n,r)}),m.registerHelper("joinTypes",t=>t.join(", ")),m.registerHelper("uniquePackages",t=>{let r=new Set;for(let n of t||[])n.package&&r.add(n.package);return Array.from(r)}),m.registerHelper("isPredefinedPackage",(t,r)=>r?r.some(n=>n.package===t):!1),m.registerHelper("getAllDependencies",t=>{let r={"@blimu/fetch":"^0.2.0",zod:"^4.3.5"};if(t.predefinedTypes)for(let n of t.predefinedTypes)n.package&&!r[n.package]&&(r[n.package]=t.dependencies?.[n.package]||"*");if(t.dependencies)for(let[n,o]of Object.entries(t.dependencies))n!=="zod"&&!r[n]&&(r[n]=o);return r}),m.registerHelper("decodeHtml",t=>{if(typeof t!="string")return t;let r=t.replace(/&amp;#x60;/g,"&#x60;").replace(/&amp;#96;/g,"&#96;").replace(/&amp;quot;/g,"&quot;").replace(/&amp;lt;/g,"&lt;").replace(/&amp;gt;/g,"&gt;");return r=r.replace(/&quot;/g,'"').replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&#x60;/g,"`").replace(/&#96;/g,"`").replace(/&amp;/g,"&"),new m.SafeString(r)}),m.registerHelper("quotePropName",t=>G(t)),m.registerHelper("zodSchema",(t,r)=>{if(t&&typeof t=="object"&&"kind"in t){let n=r?.data?.root?.IR?.modelDefs||[],o=r?.data?.root?._templateName==="schema.zod.ts.hbs";return new m.SafeString($(t,"",n,o))}return"z.unknown()"}),m.registerHelper("isStreaming",t=>ve(t)),m.registerHelper("hasBearerScheme",t=>Array.isArray(t)?t.some(r=>r.type==="http"&&r.scheme==="bearer"):!1),m.registerHelper("hasApiKeyScheme",t=>Array.isArray(t)?t.some(r=>r.type==="apiKey"):!1),m.registerHelper("serviceUsesSchema",t=>!t||!t.operations||!Array.isArray(t.operations)?!1:t.operations.some(r=>{if(r.response?.schema){let n=r.response.schema;if(n.kind==="ref"||n.kind==="object"||n.kind==="array"||n.kind==="oneOf"||n.kind==="anyOf"||n.kind==="allOf")return!0}if(r.requestBody?.schema){let n=r.requestBody.schema;if(n.kind==="ref"||n.kind==="object"||n.kind==="array"||n.kind==="oneOf"||n.kind==="anyOf"||n.kind==="allOf")return!0}return!!(r.queryParams&&r.queryParams.length>0)})),m.registerHelper("streamingItemType",t=>new m.SafeString(xe(t)))}async renderTemplate(e,t,r,n){let o=n.templates?.[e];if(o){this.logger.debug(`Using template override for ${e}: ${o}`);try{await w.promises.access(o,w.constants.R_OK);let d=await w.promises.readFile(o,"utf-8"),S=m.compile(d),D={...t,_templateName:e},F=S(D);await w.promises.writeFile(r,F,"utf-8");return}catch(d){let S=d instanceof Error?d.message:String(d);throw this.logger.error(`Template override file not found or not readable: ${o}. Error: ${S}`),new Error(`Template override file not found or not readable: ${o}`)}}let a=[g.join(__dirname,"generator/typescript/templates",e),g.join(__dirname,"templates",e),g.join(__dirname,"../typescript/templates",e),g.join(process.cwd(),"src/generator/typescript/templates",e)],i=null;for(let d of a)try{await w.promises.access(d),i=d;break}catch{this.logger.debug(`Template not found at: ${d}`)}if(!i)throw this.logger.error(`Template not found: ${e}`),this.logger.error(`Checked paths: ${a.join(", ")}`),this.logger.error(`__dirname: ${__dirname}`),new Error(`Template not found: ${e}`);let c=await w.promises.readFile(i,"utf-8"),f=m.compile(c),u={...t,_templateName:e},b=f(u);await w.promises.writeFile(r,b,"utf-8")}async generateClient(e,t,r){let n=g.join(r,"client.ts");if(this.configService.shouldExcludeFile(e,n))return[];try{return await this.renderTemplate("client.ts.hbs",{Client:e,IR:t},n,e),[g.relative(e.outDir,n)]}catch(o){let a=o instanceof Error?o.message:String(o);return this.logger.warn(`Template client.ts.hbs not found: ${a}, using placeholder`),await w.promises.writeFile(n,"// Generated client - template rendering to be implemented","utf-8"),[g.relative(e.outDir,n)]}}async generateAuthStrategies(e,t,r){let n=g.join(r,"auth-strategies.ts");if(this.configService.shouldExcludeFile(e,n))return[];try{return await this.renderTemplate("auth-strategies.ts.hbs",{Client:e,IR:t},n,e),[g.relative(e.outDir,n)]}catch{return this.logger.warn("Template auth-strategies.ts.hbs not found, using placeholder"),await w.promises.writeFile(n,"// Generated auth-strategies - template rendering to be implemented","utf-8"),[g.relative(e.outDir,n)]}}async generateIndex(e,t,r){let n=g.join(r,"index.ts");if(this.configService.shouldExcludeFile(e,n))return[];try{return await w.promises.access(n),this.logger.debug(`index.ts already exists at ${n}, skipping generation to preserve customizations`),[]}catch{}try{return await this.renderTemplate("index.ts.hbs",{Client:e,IR:t},n,e),[g.relative(e.outDir,n)]}catch{return this.logger.warn("Template index.ts.hbs not found, using placeholder"),await w.promises.writeFile(n,"// Generated index - template rendering to be implemented","utf-8"),[g.relative(e.outDir,n)]}}async generateUtils(e,t,r){let n=g.join(r,"utils.ts");if(this.configService.shouldExcludeFile(e,n))return[];try{return await this.renderTemplate("utils.ts.hbs",{Client:e,IR:t},n,e),[g.relative(e.outDir,n)]}catch{return this.logger.warn("Template utils.ts.hbs not found, using placeholder"),await w.promises.writeFile(n,"// Generated utils - template rendering to be implemented","utf-8"),[g.relative(e.outDir,n)]}}async generateServices(e,t,r){let n=[];for(let o of t.services){let a=g.join(r,`${V(o.tag).toLowerCase()}.ts`);if(!this.configService.shouldExcludeFile(e,a))try{await this.renderTemplate("service.ts.hbs",{Client:e,Service:o,IR:t,PredefinedTypes:e.predefinedTypes||[],isSameFile:!1},a,e),n.push(g.relative(e.outDir,a))}catch{this.logger.warn("Template service.ts.hbs not found, using placeholder");let c=`// Generated service ${o.tag} - template rendering to be implemented`;await w.promises.writeFile(a,c,"utf-8"),n.push(g.relative(e.outDir,a))}}return n}async generateSchema(e,t,r){let n=g.join(r,"schema.ts");if(this.configService.shouldExcludeFile(e,n))return[];try{return await this.renderTemplate("schema.ts.hbs",{Client:e,IR:t,PredefinedTypes:e.predefinedTypes||[],isSameFile:!0},n,e),[g.relative(e.outDir,n)]}catch(o){let a=o instanceof Error?o.message:String(o);return this.logger.warn(`Template schema.ts.hbs error: ${a}, using placeholder`),await w.promises.writeFile(n,"// Generated schema - template rendering to be implemented","utf-8"),[g.relative(e.outDir,n)]}}async generateZodSchema(e,t,r){let n=g.join(r,"schema.zod.ts");if(this.configService.shouldExcludeFile(e,n))return[];try{let o=ke(t.modelDefs),a={...t,modelDefs:o};return await this.renderTemplate("schema.zod.ts.hbs",{Client:e,IR:a},n,e),[g.relative(e.outDir,n)]}catch(o){let a=o instanceof Error?o.message:String(o);return this.logger.warn(`Template schema.zod.ts.hbs error: ${a}, using placeholder`),await w.promises.writeFile(n,"// Generated Zod schemas - template rendering to be implemented","utf-8"),[g.relative(e.outDir,n)]}}async generatePackageJson(e){let t=g.join(e.outDir,"package.json");if(!this.configService.shouldExcludeFile(e,t))try{await this.renderTemplate("package.json.hbs",{Client:e},t,e)}catch{this.logger.warn("Template package.json.hbs not found, using fallback");let n=JSON.stringify({name:e.packageName,version:"0.0.1",main:"dist/index.js",types:"dist/index.d.ts"},null,2);await w.promises.writeFile(t,n,"utf-8")}}async generateTsConfig(e){let t=g.join(e.outDir,"tsconfig.json");if(!this.configService.shouldExcludeFile(e,t))try{await this.renderTemplate("tsconfig.json.hbs",{Client:e},t,e)}catch{this.logger.warn("Template tsconfig.json.hbs not found, using fallback");let n=e.srcDir||"src",o=JSON.stringify({compilerOptions:{target:"ES2020",module:"commonjs",lib:["ES2020"],declaration:!0,outDir:"./dist",rootDir:`./${n}`,strict:!0,esModuleInterop:!0,skipLibCheck:!0,forceConsistentCasingInFileNames:!0},include:[`${n}/**/*`]},null,2);await w.promises.writeFile(t,o,"utf-8")}}async generateTsupConfig(e){let t=g.join(e.outDir,"tsup.config.ts");if(this.configService.shouldExcludeFile(e,t))return[];try{await this.renderTemplate("tsup.config.ts.hbs",{Client:e},t,e)}catch{this.logger.warn("Template tsup.config.ts.hbs not found, using fallback");let o=`import { defineConfig } from "tsup";
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/config/config.schema.ts
5
+ import { z } from "zod";
6
+ var PredefinedTypeSchema = z.object({
7
+ // Component schema name (e.g., "ResourceType")
8
+ type: z.string().min(1, "Type name is required"),
9
+ // Package name (e.g., "@blimu/types")
10
+ package: z.string().min(1, "Package name is required"),
11
+ // Optional import path (defaults to package root)
12
+ importPath: z.string().optional()
13
+ });
14
+ var TYPESCRIPT_TEMPLATE_NAMES = [
15
+ "client.ts.hbs",
16
+ "index.ts.hbs",
17
+ "package.json.hbs",
18
+ "README.md.hbs",
19
+ "schema.ts.hbs",
20
+ "schema.zod.ts.hbs",
21
+ "service.ts.hbs",
22
+ "tsconfig.json.hbs",
23
+ "utils.ts.hbs"
24
+ ];
25
+ var BaseClientSchema = z.object({
26
+ type: z.string().min(1, "Type is required"),
27
+ outDir: z.string().min(1, "OutDir is required"),
28
+ name: z.string().min(1, "Name is required"),
29
+ includeTags: z.array(z.string()).optional(),
30
+ excludeTags: z.array(z.string()).optional(),
31
+ // OperationIDParser is an optional function to transform operationId to a method name.
32
+ // Function signature: (operationId: string, method: string, path: string) => string | Promise<string>
33
+ // Note: Zod doesn't validate function signatures at runtime, but TypeScript will enforce the type
34
+ operationIdParser: z.custom().optional(),
35
+ // PreCommand is an optional command to run before SDK generation starts.
36
+ // Uses Docker Compose array format: ["goimports", "-w", "."]
37
+ // The command will be executed in the output directory.
38
+ preCommand: z.array(z.string()).optional(),
39
+ // PostCommand is an optional command to run after SDK generation completes.
40
+ // Uses Docker Compose array format: ["goimports", "-w", "."]
41
+ // The command will be executed in the output directory.
42
+ postCommand: z.array(z.string()).optional(),
43
+ // DefaultBaseURL is the default base URL that will be used if no base URL is provided when creating a client
44
+ defaultBaseURL: z.string().optional(),
45
+ // ExcludeFiles is a list of file paths (relative to outDir) that should not be generated
46
+ // Example: ["package.json", "src/client.ts"]
47
+ exclude: z.array(z.string()).optional()
48
+ });
49
+ var TypeScriptClientSchema = BaseClientSchema.extend({
50
+ type: z.literal("typescript"),
51
+ packageName: z.string().min(1, "PackageName is required"),
52
+ moduleName: z.string().optional(),
53
+ // Source directory path (e.g., "src" or "src/sdk"). Defaults to "src" if not specified.
54
+ srcDir: z.string().optional(),
55
+ // IncludeQueryKeys toggles generation of __queryKeys helper methods in services
56
+ includeQueryKeys: z.boolean().optional(),
57
+ // Pre-defined types to import from external packages instead of generating locally
58
+ predefinedTypes: z.array(PredefinedTypeSchema).optional(),
59
+ // Dependencies with explicit versions (e.g., { "@blimu/types": "^0.1.0" })
60
+ // If not specified, predefined type packages will use "*" version
61
+ dependencies: z.record(z.string(), z.string()).optional(),
62
+ // DevDependencies with explicit versions (e.g., { "@types/jsonwebtoken": "^9" })
63
+ devDependencies: z.record(z.string(), z.string()).optional(),
64
+ // FormatCode enables automatic formatting of generated TypeScript files using Prettier
65
+ // Defaults to true if not specified
66
+ formatCode: z.boolean().optional(),
67
+ // Template overrides - maps valid template names to file paths
68
+ templates: z.record(z.string(), z.string()).refine((templates) => {
69
+ return Object.keys(templates).every((key) => TYPESCRIPT_TEMPLATE_NAMES.includes(key));
70
+ }, {
71
+ message: `Template names must be one of: ${TYPESCRIPT_TEMPLATE_NAMES.join(", ")}`
72
+ }).optional()
73
+ });
74
+ var ClientSchema = z.discriminatedUnion("type", [
75
+ TypeScriptClientSchema
76
+ ]);
77
+ var ConfigSchema = z.object({
78
+ spec: z.string().min(1, "Spec is required"),
79
+ name: z.string().optional(),
80
+ clients: z.array(ClientSchema).min(1, "At least one client is required")
81
+ });
82
+
83
+ // src/config/config.service.ts
84
+ import { Injectable, Logger } from "@nestjs/common";
85
+ import * as fs from "fs";
86
+ import * as path2 from "path";
87
+
88
+ // src/config/mjs-config-loader.ts
89
+ import { pathToFileURL } from "url";
90
+ import * as path from "path";
91
+ async function loadMjsConfig(configPath) {
92
+ try {
93
+ const absolutePath = path.isAbsolute(configPath) ? configPath : path.resolve(configPath);
94
+ const fileUrl = pathToFileURL(absolutePath).href;
95
+ const configModule = await import(fileUrl);
96
+ const config = configModule.default || configModule;
97
+ if (!config) {
98
+ throw new Error(`Config file must export a default export or named export: ${configPath}`);
99
+ }
100
+ const validated = ConfigSchema.parse(config);
101
+ return validated;
102
+ } catch (error) {
103
+ if (error instanceof Error) {
104
+ throw new Error(`Failed to load MJS config from ${configPath}: ${error.message}`);
105
+ }
106
+ throw error;
107
+ }
108
+ }
109
+ __name(loadMjsConfig, "loadMjsConfig");
110
+
111
+ // src/config/config.service.ts
112
+ function _ts_decorate(decorators, target, key, desc) {
113
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
114
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
115
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
116
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
117
+ }
118
+ __name(_ts_decorate, "_ts_decorate");
119
+ var ConfigService = class _ConfigService {
120
+ static {
121
+ __name(this, "ConfigService");
122
+ }
123
+ logger = new Logger(_ConfigService.name);
124
+ DEFAULT_CONFIG_FILE = "chunkflow-codegen.config.mjs";
125
+ /**
126
+ * Find default config file in current directory and parent directories
127
+ */
128
+ async findDefaultConfig() {
129
+ let currentDir = process.cwd();
130
+ const root = path2.parse(currentDir).root;
131
+ while (currentDir !== root) {
132
+ const configPath = path2.join(currentDir, this.DEFAULT_CONFIG_FILE);
133
+ try {
134
+ await fs.promises.access(configPath);
135
+ return configPath;
136
+ } catch {
137
+ }
138
+ currentDir = path2.dirname(currentDir);
139
+ }
140
+ return null;
141
+ }
142
+ /**
143
+ * Load configuration from an MJS file
144
+ */
145
+ async load(configPath) {
146
+ try {
147
+ const config = await loadMjsConfig(configPath);
148
+ return this.normalizePaths(config, configPath);
149
+ } catch (error) {
150
+ if (error instanceof Error) {
151
+ throw new Error(`Failed to load config from ${configPath}: ${error.message}`);
152
+ }
153
+ throw error;
154
+ }
155
+ }
156
+ /**
157
+ * Normalize paths in the config to be absolute
158
+ */
159
+ normalizePaths(config, configPath) {
160
+ const configDir = path2.dirname(path2.resolve(configPath));
161
+ let spec = config.spec;
162
+ if (!this.isUrl(spec)) {
163
+ if (!path2.isAbsolute(spec)) {
164
+ spec = path2.resolve(configDir, spec);
165
+ }
166
+ }
167
+ const clients = config.clients.map((client) => {
168
+ let outDir = client.outDir;
169
+ if (!path2.isAbsolute(outDir)) {
170
+ outDir = path2.resolve(configDir, outDir);
171
+ }
172
+ return {
173
+ ...client,
174
+ outDir
175
+ };
176
+ });
177
+ return {
178
+ ...config,
179
+ spec,
180
+ clients
181
+ };
182
+ }
183
+ /**
184
+ * Check if a string is a URL
185
+ */
186
+ isUrl(str) {
187
+ try {
188
+ const url = new URL(str);
189
+ return url.protocol === "http:" || url.protocol === "https:";
190
+ } catch {
191
+ return false;
192
+ }
193
+ }
194
+ /**
195
+ * Get pre-command for a client
196
+ */
197
+ getPreCommand(client) {
198
+ return client.preCommand || [];
199
+ }
200
+ /**
201
+ * Get post-command for a client
202
+ */
203
+ getPostCommand(client) {
204
+ return client.postCommand || [];
205
+ }
206
+ /**
207
+ * Check if a file should be excluded based on the ExcludeFiles list.
208
+ * targetPath should be an absolute path, and the comparison is done relative to OutDir.
209
+ */
210
+ shouldExcludeFile(client, targetPath) {
211
+ if (!client.exclude || client.exclude.length === 0) {
212
+ return false;
213
+ }
214
+ let relPath;
215
+ try {
216
+ relPath = path2.relative(client.outDir, targetPath);
217
+ } catch {
218
+ return false;
219
+ }
220
+ relPath = path2.posix.normalize(relPath);
221
+ if (relPath === ".") {
222
+ relPath = "";
223
+ }
224
+ for (const excludePattern of client.exclude) {
225
+ const normalizedExclude = path2.posix.normalize(excludePattern);
226
+ if (relPath === normalizedExclude) {
227
+ return true;
228
+ }
229
+ if (normalizedExclude !== "" && relPath.startsWith(normalizedExclude + "/")) {
230
+ return true;
231
+ }
232
+ }
233
+ return false;
234
+ }
235
+ };
236
+ ConfigService = _ts_decorate([
237
+ Injectable()
238
+ ], ConfigService);
239
+
240
+ // src/config/config.module.ts
241
+ import { Module } from "@nestjs/common";
242
+ function _ts_decorate2(decorators, target, key, desc) {
243
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
244
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
245
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
246
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
247
+ }
248
+ __name(_ts_decorate2, "_ts_decorate");
249
+ var ConfigModule = class {
250
+ static {
251
+ __name(this, "ConfigModule");
252
+ }
253
+ };
254
+ ConfigModule = _ts_decorate2([
255
+ Module({
256
+ providers: [
257
+ ConfigService
258
+ ],
259
+ exports: [
260
+ ConfigService
261
+ ]
262
+ })
263
+ ], ConfigModule);
264
+
265
+ // src/config/config-helper.ts
266
+ function defineConfig(config) {
267
+ return ConfigSchema.parse(config);
268
+ }
269
+ __name(defineConfig, "defineConfig");
270
+
271
+ // src/ir/ir.types.ts
272
+ var IRSchemaKind = /* @__PURE__ */ (function(IRSchemaKind2) {
273
+ IRSchemaKind2["Unknown"] = "unknown";
274
+ IRSchemaKind2["String"] = "string";
275
+ IRSchemaKind2["Number"] = "number";
276
+ IRSchemaKind2["Integer"] = "integer";
277
+ IRSchemaKind2["Boolean"] = "boolean";
278
+ IRSchemaKind2["Null"] = "null";
279
+ IRSchemaKind2["Array"] = "array";
280
+ IRSchemaKind2["Object"] = "object";
281
+ IRSchemaKind2["Enum"] = "enum";
282
+ IRSchemaKind2["Ref"] = "ref";
283
+ IRSchemaKind2["OneOf"] = "oneOf";
284
+ IRSchemaKind2["AnyOf"] = "anyOf";
285
+ IRSchemaKind2["AllOf"] = "allOf";
286
+ IRSchemaKind2["Not"] = "not";
287
+ return IRSchemaKind2;
288
+ })({});
289
+
290
+ // src/generator/generator.service.ts
291
+ import { Injectable as Injectable5, Logger as Logger3 } from "@nestjs/common";
292
+
293
+ // src/generator/ir-builder.service.ts
294
+ import { Injectable as Injectable3 } from "@nestjs/common";
295
+
296
+ // src/generator/schema-converter.service.ts
297
+ import { Injectable as Injectable2 } from "@nestjs/common";
298
+
299
+ // src/openapi/openapi-version.utils.ts
300
+ function detectOpenAPIVersion(doc) {
301
+ const version = doc.openapi;
302
+ if (typeof version !== "string") {
303
+ return "unknown";
304
+ }
305
+ if (version.startsWith("3.1")) {
306
+ return "3.1";
307
+ }
308
+ if (version.startsWith("3.0")) {
309
+ return "3.0";
310
+ }
311
+ return "unknown";
312
+ }
313
+ __name(detectOpenAPIVersion, "detectOpenAPIVersion");
314
+ function isSupportedVersion(version) {
315
+ return version === "3.0" || version === "3.1";
316
+ }
317
+ __name(isSupportedVersion, "isSupportedVersion");
318
+
319
+ // src/openapi/openapi.types.ts
320
+ function getSchemaFromRef(doc, schemaRef) {
321
+ if (!schemaRef || typeof schemaRef !== "object") {
322
+ return void 0;
323
+ }
324
+ if ("$ref" in schemaRef && schemaRef.$ref) {
325
+ const ref = schemaRef.$ref;
326
+ if (ref.startsWith("#/components/schemas/")) {
327
+ const name = ref.replace("#/components/schemas/", "");
328
+ if (doc.components?.schemas?.[name]) {
329
+ const schema = doc.components.schemas[name];
330
+ if ("$ref" in schema) {
331
+ return getSchemaFromRef(doc, schema);
332
+ }
333
+ return schema;
334
+ }
335
+ }
336
+ return void 0;
337
+ }
338
+ return schemaRef;
339
+ }
340
+ __name(getSchemaFromRef, "getSchemaFromRef");
341
+ function isSchemaNullable(schema) {
342
+ if (!schema) {
343
+ return false;
344
+ }
345
+ if ("nullable" in schema && schema.nullable === true) {
346
+ return true;
347
+ }
348
+ if ("type" in schema && Array.isArray(schema.type)) {
349
+ return schema.type.includes("null");
350
+ }
351
+ return false;
352
+ }
353
+ __name(isSchemaNullable, "isSchemaNullable");
354
+ function getSchemaType(schema) {
355
+ if (!schema || !("type" in schema)) {
356
+ return void 0;
357
+ }
358
+ return schema.type;
359
+ }
360
+ __name(getSchemaType, "getSchemaType");
361
+
362
+ // src/generator/schema-converter.service.ts
363
+ function _ts_decorate3(decorators, target, key, desc) {
364
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
365
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
366
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
367
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
368
+ }
369
+ __name(_ts_decorate3, "_ts_decorate");
370
+ var SchemaConverterService = class {
371
+ static {
372
+ __name(this, "SchemaConverterService");
373
+ }
374
+ /**
375
+ * Convert an OpenAPI schema reference to IR schema
376
+ * Supports both OpenAPI 3.0 and 3.1
377
+ */
378
+ schemaRefToIR(doc, schemaRef) {
379
+ if (!schemaRef) {
380
+ return {
381
+ kind: IRSchemaKind.Unknown,
382
+ nullable: false
383
+ };
384
+ }
385
+ if ("$ref" in schemaRef && schemaRef.$ref) {
386
+ const ref = schemaRef.$ref;
387
+ if (ref.startsWith("#/components/schemas/")) {
388
+ const name = ref.replace("#/components/schemas/", "");
389
+ return {
390
+ kind: IRSchemaKind.Ref,
391
+ ref: name,
392
+ nullable: false
393
+ };
394
+ }
395
+ const parts = ref.split("/");
396
+ if (parts.length > 0) {
397
+ const name = parts[parts.length - 1];
398
+ if (name) {
399
+ return {
400
+ kind: IRSchemaKind.Ref,
401
+ ref: name,
402
+ nullable: false
403
+ };
404
+ }
405
+ }
406
+ return {
407
+ kind: IRSchemaKind.Unknown,
408
+ nullable: false
409
+ };
410
+ }
411
+ const schema = getSchemaFromRef(doc, schemaRef);
412
+ if (!schema) {
413
+ return {
414
+ kind: IRSchemaKind.Unknown,
415
+ nullable: false
416
+ };
417
+ }
418
+ const nullable = isSchemaNullable(schema);
419
+ let discriminator;
420
+ if (schema.discriminator) {
421
+ discriminator = {
422
+ propertyName: schema.discriminator.propertyName,
423
+ mapping: schema.discriminator.mapping
424
+ };
425
+ }
426
+ if (schema.oneOf && schema.oneOf.length > 0) {
427
+ const subs = schema.oneOf.map((sub) => this.schemaRefToIR(doc, sub));
428
+ return {
429
+ kind: IRSchemaKind.OneOf,
430
+ oneOf: subs,
431
+ nullable,
432
+ discriminator
433
+ };
434
+ }
435
+ if (schema.anyOf && schema.anyOf.length > 0) {
436
+ const subs = schema.anyOf.map((sub) => this.schemaRefToIR(doc, sub));
437
+ return {
438
+ kind: IRSchemaKind.AnyOf,
439
+ anyOf: subs,
440
+ nullable,
441
+ discriminator
442
+ };
443
+ }
444
+ if (schema.allOf && schema.allOf.length > 0) {
445
+ const subs = schema.allOf.map((sub) => this.schemaRefToIR(doc, sub));
446
+ return {
447
+ kind: IRSchemaKind.AllOf,
448
+ allOf: subs,
449
+ nullable,
450
+ discriminator
451
+ };
452
+ }
453
+ if (schema.not) {
454
+ const not = this.schemaRefToIR(doc, schema.not);
455
+ return {
456
+ kind: IRSchemaKind.Not,
457
+ not,
458
+ nullable,
459
+ discriminator
460
+ };
461
+ }
462
+ if (schema.enum && schema.enum.length > 0) {
463
+ const enumValues = schema.enum.map((v) => String(v));
464
+ const enumBase = this.inferEnumBaseKind(schema);
465
+ return {
466
+ kind: IRSchemaKind.Enum,
467
+ enumValues,
468
+ enumRaw: schema.enum,
469
+ enumBase,
470
+ nullable,
471
+ discriminator
472
+ };
473
+ }
474
+ const type = getSchemaType(schema);
475
+ const normalizedType = Array.isArray(type) ? type.filter((t) => t !== "null")[0] : type;
476
+ if (normalizedType) {
477
+ switch (normalizedType) {
478
+ case "string":
479
+ return {
480
+ kind: IRSchemaKind.String,
481
+ nullable,
482
+ format: schema.format,
483
+ discriminator
484
+ };
485
+ case "integer":
486
+ return {
487
+ kind: IRSchemaKind.Integer,
488
+ nullable,
489
+ discriminator
490
+ };
491
+ case "number":
492
+ return {
493
+ kind: IRSchemaKind.Number,
494
+ nullable,
495
+ discriminator
496
+ };
497
+ case "boolean":
498
+ return {
499
+ kind: IRSchemaKind.Boolean,
500
+ nullable,
501
+ discriminator
502
+ };
503
+ case "array": {
504
+ const arraySchema = schema;
505
+ const items = this.schemaRefToIR(doc, arraySchema.items);
506
+ return {
507
+ kind: IRSchemaKind.Array,
508
+ items,
509
+ nullable,
510
+ discriminator
511
+ };
512
+ }
513
+ case "object": {
514
+ const properties = [];
515
+ if (schema.properties) {
516
+ const propNames = Object.keys(schema.properties).sort();
517
+ for (const name of propNames) {
518
+ const prop = schema.properties[name];
519
+ const fieldType = this.schemaRefToIR(doc, prop);
520
+ const required = schema.required?.includes(name) || false;
521
+ properties.push({
522
+ name,
523
+ type: fieldType,
524
+ required,
525
+ annotations: this.extractAnnotations(prop)
526
+ });
527
+ }
528
+ }
529
+ let additionalProperties;
530
+ if (schema.additionalProperties) {
531
+ if (typeof schema.additionalProperties === "object") {
532
+ additionalProperties = this.schemaRefToIR(doc, schema.additionalProperties);
533
+ }
534
+ }
535
+ return {
536
+ kind: IRSchemaKind.Object,
537
+ properties,
538
+ additionalProperties,
539
+ nullable,
540
+ discriminator
541
+ };
542
+ }
543
+ }
544
+ }
545
+ return {
546
+ kind: IRSchemaKind.Unknown,
547
+ nullable,
548
+ discriminator
549
+ };
550
+ }
551
+ /**
552
+ * Extract annotations from a schema reference
553
+ * Supports both OpenAPI 3.0 and 3.1
554
+ */
555
+ extractAnnotations(schemaRef) {
556
+ if (!schemaRef || "$ref" in schemaRef) {
557
+ return {};
558
+ }
559
+ const schema = schemaRef;
560
+ return {
561
+ title: schema.title,
562
+ description: schema.description,
563
+ deprecated: schema.deprecated,
564
+ readOnly: schema.readOnly,
565
+ writeOnly: schema.writeOnly,
566
+ default: schema.default,
567
+ examples: schema.example ? Array.isArray(schema.example) ? schema.example : [
568
+ schema.example
569
+ ] : void 0
570
+ };
571
+ }
572
+ /**
573
+ * Infer the base kind for an enum
574
+ * Supports both OpenAPI 3.0 and 3.1
575
+ */
576
+ inferEnumBaseKind(schema) {
577
+ const type = getSchemaType(schema);
578
+ if (type) {
579
+ const normalizedType = Array.isArray(type) ? type.filter((t) => t !== "null")[0] : type;
580
+ if (normalizedType) {
581
+ switch (normalizedType) {
582
+ case "string":
583
+ return IRSchemaKind.String;
584
+ case "integer":
585
+ return IRSchemaKind.Integer;
586
+ case "number":
587
+ return IRSchemaKind.Number;
588
+ case "boolean":
589
+ return IRSchemaKind.Boolean;
590
+ }
591
+ }
592
+ }
593
+ if (schema.enum && schema.enum.length > 0) {
594
+ const first = schema.enum[0];
595
+ if (typeof first === "string") {
596
+ return IRSchemaKind.String;
597
+ }
598
+ if (typeof first === "number") {
599
+ return Number.isInteger(first) ? IRSchemaKind.Integer : IRSchemaKind.Number;
600
+ }
601
+ if (typeof first === "boolean") {
602
+ return IRSchemaKind.Boolean;
603
+ }
604
+ }
605
+ return IRSchemaKind.Unknown;
606
+ }
607
+ };
608
+ SchemaConverterService = _ts_decorate3([
609
+ Injectable2()
610
+ ], SchemaConverterService);
611
+
612
+ // src/utils/string.utils.ts
613
+ function toPascalCase(s) {
614
+ s = s.trim();
615
+ if (s === "") {
616
+ return "";
617
+ }
618
+ const parts = s.split(/[^A-Za-z0-9]+/).filter((p) => p !== "");
619
+ const allParts = [];
620
+ for (const part of parts) {
621
+ const subParts = splitCamelCase(part);
622
+ allParts.push(...subParts);
623
+ }
624
+ return allParts.filter((p) => p !== "").map((p) => p.charAt(0).toUpperCase() + p.slice(1).toLowerCase()).join("");
625
+ }
626
+ __name(toPascalCase, "toPascalCase");
627
+ function toCamelCase(s) {
628
+ const pascal = toPascalCase(s);
629
+ if (pascal === "") {
630
+ return "";
631
+ }
632
+ return pascal.charAt(0).toLowerCase() + pascal.slice(1);
633
+ }
634
+ __name(toCamelCase, "toCamelCase");
635
+ function toSnakeCase(s) {
636
+ s = s.trim();
637
+ if (s === "") {
638
+ return "";
639
+ }
640
+ const parts = s.split(/[^A-Za-z0-9]+/).filter((p) => p !== "");
641
+ const allParts = [];
642
+ for (const part of parts) {
643
+ const subParts = splitCamelCase(part);
644
+ allParts.push(...subParts);
645
+ }
646
+ return allParts.filter((p) => p !== "").map((p) => p.toLowerCase()).join("_");
647
+ }
648
+ __name(toSnakeCase, "toSnakeCase");
649
+ function toKebabCase(s) {
650
+ s = s.trim();
651
+ if (s === "") {
652
+ return "";
653
+ }
654
+ const parts = s.split(/[^A-Za-z0-9]+/).filter((p) => p !== "");
655
+ const allParts = [];
656
+ for (const part of parts) {
657
+ const subParts = splitCamelCase(part);
658
+ allParts.push(...subParts);
659
+ }
660
+ return allParts.filter((p) => p !== "").map((p) => p.toLowerCase()).join("-");
661
+ }
662
+ __name(toKebabCase, "toKebabCase");
663
+ function splitCamelCase(s) {
664
+ if (s === "") {
665
+ return [];
666
+ }
667
+ const parts = [];
668
+ let current = "";
669
+ const chars = Array.from(s);
670
+ for (let i = 0; i < chars.length; i++) {
671
+ const char = chars[i];
672
+ if (!char) {
673
+ continue;
674
+ }
675
+ let isNewWord = false;
676
+ if (i > 0 && isUppercase(char)) {
677
+ const prevChar = chars[i - 1];
678
+ if (prevChar && !isUppercase(prevChar)) {
679
+ isNewWord = true;
680
+ } else if (i < chars.length - 1) {
681
+ const nextChar = chars[i + 1];
682
+ if (nextChar && !isUppercase(nextChar)) {
683
+ isNewWord = true;
684
+ }
685
+ }
686
+ }
687
+ if (isNewWord && current.length > 0) {
688
+ parts.push(current);
689
+ current = "";
690
+ }
691
+ current += char;
692
+ }
693
+ if (current.length > 0) {
694
+ parts.push(current);
695
+ }
696
+ return parts;
697
+ }
698
+ __name(splitCamelCase, "splitCamelCase");
699
+ function isUppercase(char) {
700
+ return char >= "A" && char <= "Z";
701
+ }
702
+ __name(isUppercase, "isUppercase");
703
+
704
+ // src/generator/ir-builder.service.ts
705
+ function _ts_decorate4(decorators, target, key, desc) {
706
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
707
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
708
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
709
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
710
+ }
711
+ __name(_ts_decorate4, "_ts_decorate");
712
+ function _ts_metadata(k, v) {
713
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
714
+ }
715
+ __name(_ts_metadata, "_ts_metadata");
716
+ var IrBuilderService = class {
717
+ static {
718
+ __name(this, "IrBuilderService");
719
+ }
720
+ schemaConverter;
721
+ constructor(schemaConverter) {
722
+ this.schemaConverter = schemaConverter;
723
+ }
724
+ /**
725
+ * Build IR from an OpenAPI document
726
+ * Supports both OpenAPI 3.0 and 3.1
727
+ */
728
+ buildIR(doc) {
729
+ const tags = this.collectTags(doc);
730
+ const securitySchemes = this.collectSecuritySchemes(doc);
731
+ const modelDefs = this.buildStructuredModels(doc);
732
+ const allowed = {};
733
+ for (const tag of tags) {
734
+ allowed[tag] = true;
735
+ }
736
+ const result = this.buildIRFromDoc(doc, allowed);
737
+ result.securitySchemes = securitySchemes;
738
+ result.modelDefs = [
739
+ ...modelDefs,
740
+ ...result.modelDefs
741
+ ];
742
+ result.openApiDocument = doc;
743
+ return result;
744
+ }
745
+ /**
746
+ * Filter IR based on client configuration
747
+ */
748
+ filterIR(fullIR, client) {
749
+ const include = this.compileTagFilters(client.includeTags || []);
750
+ const exclude = this.compileTagFilters(client.excludeTags || []);
751
+ const filteredServices = [];
752
+ for (const service of fullIR.services) {
753
+ const filteredOps = [];
754
+ for (const op of service.operations) {
755
+ if (this.shouldIncludeOperation(op.originalTags, include, exclude)) {
756
+ filteredOps.push(op);
757
+ }
758
+ }
759
+ if (filteredOps.length > 0) {
760
+ filteredServices.push({
761
+ ...service,
762
+ operations: filteredOps
763
+ });
764
+ }
765
+ }
766
+ const filteredIR = {
767
+ services: filteredServices,
768
+ models: fullIR.models,
769
+ securitySchemes: fullIR.securitySchemes,
770
+ modelDefs: fullIR.modelDefs,
771
+ openApiDocument: fullIR.openApiDocument
772
+ };
773
+ filteredIR.modelDefs = this.filterUnusedModelDefs(filteredIR, fullIR.modelDefs);
774
+ return filteredIR;
775
+ }
776
+ /**
777
+ * Detect if a content type indicates streaming
778
+ */
779
+ detectStreamingContentType(contentType) {
780
+ const normalized = contentType.toLowerCase().split(";")[0]?.trim();
781
+ if (!normalized) {
782
+ return {
783
+ isStreaming: false
784
+ };
785
+ }
786
+ if (normalized === "text/event-stream") {
787
+ return {
788
+ isStreaming: true,
789
+ format: "sse"
790
+ };
791
+ }
792
+ if (normalized === "application/x-ndjson" || normalized === "application/x-jsonlines" || normalized === "application/jsonl") {
793
+ return {
794
+ isStreaming: true,
795
+ format: "ndjson"
796
+ };
797
+ }
798
+ if (normalized.includes("stream") || normalized.includes("chunked")) {
799
+ return {
800
+ isStreaming: true,
801
+ format: "chunked"
802
+ };
803
+ }
804
+ return {
805
+ isStreaming: false
806
+ };
807
+ }
808
+ /**
809
+ * Collect all tags from the OpenAPI document
810
+ */
811
+ collectTags(doc) {
812
+ const uniq = /* @__PURE__ */ new Set();
813
+ uniq.add("misc");
814
+ if (doc.paths) {
815
+ for (const [_path, pathItem] of Object.entries(doc.paths)) {
816
+ if (!pathItem) continue;
817
+ const operations = [
818
+ pathItem.get,
819
+ pathItem.post,
820
+ pathItem.put,
821
+ pathItem.patch,
822
+ pathItem.delete,
823
+ pathItem.options,
824
+ pathItem.head,
825
+ pathItem.trace
826
+ ];
827
+ for (const op of operations) {
828
+ if (!op || !op.tags) continue;
829
+ for (const tag of op.tags) {
830
+ uniq.add(tag);
831
+ }
832
+ }
833
+ }
834
+ }
835
+ return Array.from(uniq).sort();
836
+ }
837
+ /**
838
+ * Compile regex patterns for tag filtering
839
+ */
840
+ compileTagFilters(patterns) {
841
+ return patterns.map((p) => {
842
+ try {
843
+ return new RegExp(p);
844
+ } catch (error) {
845
+ throw new Error(`Invalid tag filter pattern "${p}": ${error instanceof Error ? error.message : String(error)}`);
846
+ }
847
+ });
848
+ }
849
+ /**
850
+ * Determine if an operation should be included based on its original tags
851
+ */
852
+ shouldIncludeOperation(originalTags, include, exclude) {
853
+ let included = include.length === 0;
854
+ if (include.length > 0) {
855
+ for (const tag of originalTags) {
856
+ for (const r of include) {
857
+ if (r.test(tag)) {
858
+ included = true;
859
+ break;
860
+ }
861
+ }
862
+ if (included) {
863
+ break;
864
+ }
865
+ }
866
+ }
867
+ if (!included) {
868
+ return false;
869
+ }
870
+ if (exclude.length > 0) {
871
+ for (const tag of originalTags) {
872
+ for (const r of exclude) {
873
+ if (r.test(tag)) {
874
+ return false;
875
+ }
876
+ }
877
+ }
878
+ }
879
+ return true;
880
+ }
881
+ /**
882
+ * Build IR structures from OpenAPI document
883
+ */
884
+ buildIRFromDoc(doc, allowed) {
885
+ const servicesMap = {};
886
+ servicesMap["misc"] = {
887
+ tag: "misc",
888
+ operations: []
889
+ };
890
+ const extractedTypes = [];
891
+ const seenTypeNames = /* @__PURE__ */ new Set();
892
+ if (doc.components?.schemas) {
893
+ for (const name of Object.keys(doc.components.schemas)) {
894
+ seenTypeNames.add(name);
895
+ }
896
+ }
897
+ const addOp = /* @__PURE__ */ __name((tag, op, method, path7) => {
898
+ if (!servicesMap[tag]) {
899
+ servicesMap[tag] = {
900
+ tag,
901
+ operations: []
902
+ };
903
+ }
904
+ const id = op.operationId || "";
905
+ const { pathParams, queryParams } = this.collectParams(doc, op);
906
+ const { requestBody: reqBody, extractedTypes: reqBodyTypes } = this.extractRequestBodyWithTypes(doc, op, tag, id, method, seenTypeNames);
907
+ extractedTypes.push(...reqBodyTypes);
908
+ const { response: resp, extractedTypes: respTypes } = this.extractResponseWithTypes(doc, op, tag, id, method, seenTypeNames);
909
+ extractedTypes.push(...respTypes);
910
+ const originalTags = op.tags && op.tags.length > 0 ? [
911
+ ...op.tags
912
+ ] : [
913
+ "misc"
914
+ ];
915
+ servicesMap[tag].operations.push({
916
+ operationID: id,
917
+ method,
918
+ path: path7,
919
+ tag,
920
+ originalTags,
921
+ summary: op.summary || "",
922
+ description: op.description || "",
923
+ deprecated: op.deprecated || false,
924
+ pathParams,
925
+ queryParams,
926
+ requestBody: reqBody,
927
+ response: resp
928
+ });
929
+ }, "addOp");
930
+ if (doc.paths) {
931
+ for (const [path7, pathItem] of Object.entries(doc.paths)) {
932
+ if (!pathItem) continue;
933
+ const operations = [
934
+ {
935
+ op: pathItem.get,
936
+ method: "GET"
937
+ },
938
+ {
939
+ op: pathItem.post,
940
+ method: "POST"
941
+ },
942
+ {
943
+ op: pathItem.put,
944
+ method: "PUT"
945
+ },
946
+ {
947
+ op: pathItem.patch,
948
+ method: "PATCH"
949
+ },
950
+ {
951
+ op: pathItem.delete,
952
+ method: "DELETE"
953
+ },
954
+ {
955
+ op: pathItem.options,
956
+ method: "OPTIONS"
957
+ },
958
+ {
959
+ op: pathItem.head,
960
+ method: "HEAD"
961
+ },
962
+ {
963
+ op: pathItem.trace,
964
+ method: "TRACE"
965
+ }
966
+ ];
967
+ for (const { op, method } of operations) {
968
+ if (!op) continue;
969
+ const t = this.firstAllowedTag(op.tags || [], allowed);
970
+ if (t) {
971
+ addOp(t, op, method, path7);
972
+ }
973
+ }
974
+ }
975
+ }
976
+ const services = Object.values(servicesMap);
977
+ for (const service of services) {
978
+ service.operations.sort((a, b) => {
979
+ if (a.path === b.path) {
980
+ return a.method.localeCompare(b.method);
981
+ }
982
+ return a.path.localeCompare(b.path);
983
+ });
984
+ }
985
+ services.sort((a, b) => a.tag.localeCompare(b.tag));
986
+ return {
987
+ services,
988
+ models: [],
989
+ securitySchemes: [],
990
+ modelDefs: extractedTypes
991
+ };
992
+ }
993
+ /**
994
+ * Get the first allowed tag from a list
995
+ */
996
+ /**
997
+ * Derive method name from operation ID, method, and path
998
+ */
999
+ deriveMethodName(operationId, method, path7) {
1000
+ if (operationId) {
1001
+ const idx = operationId.indexOf("Controller_");
1002
+ const cleanedId = idx >= 0 ? operationId.substring(idx + "Controller_".length) : operationId;
1003
+ return toCamelCase(cleanedId);
1004
+ }
1005
+ const hasID = path7.includes("{") && path7.includes("}");
1006
+ switch (method) {
1007
+ case "GET":
1008
+ return hasID ? "get" : "list";
1009
+ case "POST":
1010
+ return "create";
1011
+ case "PUT":
1012
+ case "PATCH":
1013
+ return "update";
1014
+ case "DELETE":
1015
+ return "delete";
1016
+ default:
1017
+ return method.toLowerCase();
1018
+ }
1019
+ }
1020
+ firstAllowedTag(tags, allowed) {
1021
+ for (const t of tags) {
1022
+ if (allowed[t]) {
1023
+ return t;
1024
+ }
1025
+ }
1026
+ if (tags.length === 0 && allowed["misc"]) {
1027
+ return "misc";
1028
+ }
1029
+ return "";
1030
+ }
1031
+ /**
1032
+ * Collect security schemes
1033
+ */
1034
+ collectSecuritySchemes(doc) {
1035
+ if (!doc.components?.securitySchemes) {
1036
+ return [];
1037
+ }
1038
+ const names = Object.keys(doc.components.securitySchemes).sort();
1039
+ const out = [];
1040
+ for (const name of names) {
1041
+ const scheme = doc.components.securitySchemes[name];
1042
+ if (!scheme || "$ref" in scheme) continue;
1043
+ const sc = {
1044
+ key: name,
1045
+ type: scheme.type
1046
+ };
1047
+ switch (scheme.type) {
1048
+ case "http":
1049
+ sc.scheme = scheme.scheme;
1050
+ if (scheme.bearerFormat) {
1051
+ sc.bearerFormat = scheme.bearerFormat;
1052
+ }
1053
+ break;
1054
+ case "apiKey":
1055
+ sc.in = scheme.in;
1056
+ sc.name = scheme.name;
1057
+ break;
1058
+ case "oauth2":
1059
+ case "openIdConnect":
1060
+ break;
1061
+ }
1062
+ out.push(sc);
1063
+ }
1064
+ return out;
1065
+ }
1066
+ /**
1067
+ * Collect parameters from an operation
1068
+ */
1069
+ collectParams(doc, op) {
1070
+ const pathParams = [];
1071
+ const queryParams = [];
1072
+ if (op.parameters) {
1073
+ for (const pr of op.parameters) {
1074
+ if (!pr || "$ref" in pr) continue;
1075
+ const p = pr;
1076
+ const schema = this.schemaConverter.schemaRefToIR(doc, p.schema);
1077
+ const param = {
1078
+ name: p.name,
1079
+ required: p.required || false,
1080
+ schema,
1081
+ description: p.description || ""
1082
+ };
1083
+ if (p.in === "path") {
1084
+ pathParams.push(param);
1085
+ } else if (p.in === "query") {
1086
+ queryParams.push(param);
1087
+ }
1088
+ }
1089
+ }
1090
+ pathParams.sort((a, b) => a.name.localeCompare(b.name));
1091
+ queryParams.sort((a, b) => a.name.localeCompare(b.name));
1092
+ return {
1093
+ pathParams,
1094
+ queryParams
1095
+ };
1096
+ }
1097
+ /**
1098
+ * Extract request body information with inline type extraction
1099
+ */
1100
+ /**
1101
+ * Find if a schema matches a component schema (useful after dereferencing)
1102
+ * Returns the component schema name if found, null otherwise
1103
+ */
1104
+ findMatchingComponentSchema(doc, schema) {
1105
+ if (!schema || !doc.components?.schemas) {
1106
+ return null;
1107
+ }
1108
+ if ("$ref" in schema && schema.$ref) {
1109
+ const ref = schema.$ref;
1110
+ if (ref.startsWith("#/components/schemas/")) {
1111
+ const name = ref.replace("#/components/schemas/", "");
1112
+ if (doc.components.schemas[name]) {
1113
+ return name;
1114
+ }
1115
+ }
1116
+ }
1117
+ const schemaIR = this.schemaConverter.schemaRefToIR(doc, schema);
1118
+ for (const [name, componentSchema] of Object.entries(doc.components.schemas)) {
1119
+ const componentIR = this.schemaConverter.schemaRefToIR(doc, componentSchema);
1120
+ if (this.compareSchemas(schemaIR, componentIR)) {
1121
+ return name;
1122
+ }
1123
+ }
1124
+ return null;
1125
+ }
1126
+ /**
1127
+ * Compare two IR schemas for structural equality
1128
+ */
1129
+ compareSchemas(schema1, schema2) {
1130
+ if (schema1.kind !== schema2.kind) {
1131
+ return false;
1132
+ }
1133
+ if (schema1.kind === IRSchemaKind.Ref && schema2.kind === IRSchemaKind.Ref) {
1134
+ return schema1.ref === schema2.ref;
1135
+ }
1136
+ if (schema1.kind === IRSchemaKind.Object && schema2.kind === IRSchemaKind.Object) {
1137
+ const props1 = schema1.properties || [];
1138
+ const props2 = schema2.properties || [];
1139
+ if (props1.length !== props2.length) {
1140
+ return false;
1141
+ }
1142
+ const props1Map = new Map(props1.map((p) => [
1143
+ p.name,
1144
+ {
1145
+ type: p.type,
1146
+ required: p.required
1147
+ }
1148
+ ]));
1149
+ const props2Map = new Map(props2.map((p) => [
1150
+ p.name,
1151
+ {
1152
+ type: p.type,
1153
+ required: p.required
1154
+ }
1155
+ ]));
1156
+ if (props1Map.size !== props2Map.size) {
1157
+ return false;
1158
+ }
1159
+ for (const [name, prop1] of props1Map) {
1160
+ const prop2 = props2Map.get(name);
1161
+ if (!prop2 || prop1.required !== prop2.required || !this.compareSchemas(prop1.type, prop2.type)) {
1162
+ return false;
1163
+ }
1164
+ }
1165
+ return true;
1166
+ }
1167
+ if (schema1.kind === IRSchemaKind.Array && schema2.kind === IRSchemaKind.Array) {
1168
+ if (!schema1.items || !schema2.items) {
1169
+ return schema1.items === schema2.items;
1170
+ }
1171
+ return this.compareSchemas(schema1.items, schema2.items);
1172
+ }
1173
+ return true;
1174
+ }
1175
+ /**
1176
+ * Extract model name from schema if it's a reference, otherwise return null
1177
+ * Handles direct refs and arrays of refs
1178
+ */
1179
+ extractModelNameFromSchema(schema) {
1180
+ if (schema.kind === IRSchemaKind.Ref && schema.ref) {
1181
+ return schema.ref;
1182
+ }
1183
+ if (schema.kind === IRSchemaKind.Array && schema.items) {
1184
+ if (schema.items.kind === IRSchemaKind.Ref && schema.items.ref) {
1185
+ return schema.items.ref;
1186
+ }
1187
+ }
1188
+ return null;
1189
+ }
1190
+ /**
1191
+ * Generate type name for request/response body
1192
+ * Prefers model name from schema reference, falls back to operation-based naming
1193
+ */
1194
+ generateTypeName(schema, tag, operationId, method, path7, suffix) {
1195
+ const modelName = this.extractModelNameFromSchema(schema);
1196
+ if (modelName) {
1197
+ return modelName;
1198
+ }
1199
+ const methodName = this.deriveMethodName(operationId, method, path7);
1200
+ return `${toPascalCase(tag)}${toPascalCase(methodName)}${suffix}`;
1201
+ }
1202
+ extractRequestBodyWithTypes(doc, op, tag, operationId, method, seenTypeNames) {
1203
+ const extractedTypes = [];
1204
+ if (!op.requestBody || "$ref" in op.requestBody) {
1205
+ return {
1206
+ requestBody: null,
1207
+ extractedTypes: []
1208
+ };
1209
+ }
1210
+ const rb = op.requestBody;
1211
+ if (rb.content?.["application/json"]) {
1212
+ const media = rb.content["application/json"];
1213
+ const schema = this.schemaConverter.schemaRefToIR(doc, media.schema);
1214
+ const componentSchemaName = this.findMatchingComponentSchema(doc, media.schema);
1215
+ if (componentSchemaName) {
1216
+ return {
1217
+ requestBody: {
1218
+ contentType: "application/json",
1219
+ typeTS: "",
1220
+ schema: {
1221
+ kind: IRSchemaKind.Ref,
1222
+ ref: componentSchemaName,
1223
+ nullable: false
1224
+ },
1225
+ required: rb.required || false
1226
+ },
1227
+ extractedTypes: []
1228
+ };
1229
+ }
1230
+ const typeName = this.generateTypeName(schema, tag, operationId, method, op.path, "RequestBody");
1231
+ if (schema.kind === IRSchemaKind.Object && !seenTypeNames.has(typeName)) {
1232
+ seenTypeNames.add(typeName);
1233
+ extractedTypes.push({
1234
+ name: typeName,
1235
+ schema,
1236
+ annotations: this.schemaConverter.extractAnnotations(media.schema)
1237
+ });
1238
+ return {
1239
+ requestBody: {
1240
+ contentType: "application/json",
1241
+ typeTS: "",
1242
+ schema: {
1243
+ kind: IRSchemaKind.Ref,
1244
+ ref: typeName,
1245
+ nullable: false
1246
+ },
1247
+ required: rb.required || false
1248
+ },
1249
+ extractedTypes
1250
+ };
1251
+ }
1252
+ return {
1253
+ requestBody: {
1254
+ contentType: "application/json",
1255
+ typeTS: "",
1256
+ schema,
1257
+ required: rb.required || false
1258
+ },
1259
+ extractedTypes: []
1260
+ };
1261
+ }
1262
+ const reqBody = this.extractRequestBody(doc, op);
1263
+ return {
1264
+ requestBody: reqBody,
1265
+ extractedTypes: []
1266
+ };
1267
+ }
1268
+ /**
1269
+ * Extract request body information (legacy method, kept for fallback)
1270
+ */
1271
+ extractRequestBody(doc, op) {
1272
+ if (!op.requestBody || "$ref" in op.requestBody) {
1273
+ return null;
1274
+ }
1275
+ const rb = op.requestBody;
1276
+ if (rb.content?.["application/json"]) {
1277
+ const media = rb.content["application/json"];
1278
+ return {
1279
+ contentType: "application/json",
1280
+ typeTS: "",
1281
+ schema: this.schemaConverter.schemaRefToIR(doc, media.schema),
1282
+ required: rb.required || false
1283
+ };
1284
+ }
1285
+ if (rb.content?.["application/x-www-form-urlencoded"]) {
1286
+ const media = rb.content["application/x-www-form-urlencoded"];
1287
+ return {
1288
+ contentType: "application/x-www-form-urlencoded",
1289
+ typeTS: "",
1290
+ schema: this.schemaConverter.schemaRefToIR(doc, media.schema),
1291
+ required: rb.required || false
1292
+ };
1293
+ }
1294
+ if (rb.content?.["multipart/form-data"]) {
1295
+ return {
1296
+ contentType: "multipart/form-data",
1297
+ typeTS: "",
1298
+ schema: {
1299
+ kind: IRSchemaKind.Unknown,
1300
+ nullable: false
1301
+ },
1302
+ required: rb.required || false
1303
+ };
1304
+ }
1305
+ if (rb.content) {
1306
+ const firstContentType = Object.keys(rb.content)[0];
1307
+ if (!firstContentType) {
1308
+ return null;
1309
+ }
1310
+ const media = rb.content[firstContentType];
1311
+ if (!media) {
1312
+ return null;
1313
+ }
1314
+ return {
1315
+ contentType: firstContentType,
1316
+ typeTS: "",
1317
+ schema: this.schemaConverter.schemaRefToIR(doc, media.schema),
1318
+ required: rb.required || false
1319
+ };
1320
+ }
1321
+ return null;
1322
+ }
1323
+ /**
1324
+ * Extract response information with inline type extraction
1325
+ */
1326
+ extractResponseWithTypes(doc, op, tag, operationId, method, seenTypeNames) {
1327
+ const extractedTypes = [];
1328
+ if (!op.responses) {
1329
+ return {
1330
+ response: {
1331
+ typeTS: "unknown",
1332
+ schema: {
1333
+ kind: IRSchemaKind.Unknown,
1334
+ nullable: false
1335
+ },
1336
+ description: "",
1337
+ isStreaming: false,
1338
+ contentType: ""
1339
+ },
1340
+ extractedTypes: []
1341
+ };
1342
+ }
1343
+ const tryCodes = [
1344
+ "200",
1345
+ "201"
1346
+ ];
1347
+ for (const code of tryCodes) {
1348
+ const response = op.responses[code];
1349
+ if (response && !("$ref" in response)) {
1350
+ const resp2 = response;
1351
+ if (resp2.content) {
1352
+ for (const [contentType, media] of Object.entries(resp2.content)) {
1353
+ const streaming = this.detectStreamingContentType(contentType);
1354
+ const schema = this.schemaConverter.schemaRefToIR(doc, media.schema);
1355
+ if (streaming.isStreaming) {
1356
+ return {
1357
+ response: {
1358
+ typeTS: "",
1359
+ schema,
1360
+ description: resp2.description || "",
1361
+ isStreaming: true,
1362
+ contentType,
1363
+ ...streaming.format && {
1364
+ streamingFormat: streaming.format
1365
+ }
1366
+ },
1367
+ extractedTypes: []
1368
+ };
1369
+ }
1370
+ if (contentType === "application/json") {
1371
+ const componentSchemaName = this.findMatchingComponentSchema(doc, media.schema);
1372
+ if (componentSchemaName) {
1373
+ return {
1374
+ response: {
1375
+ typeTS: "",
1376
+ schema: {
1377
+ kind: IRSchemaKind.Ref,
1378
+ ref: componentSchemaName,
1379
+ nullable: false
1380
+ },
1381
+ description: resp2.description || "",
1382
+ isStreaming: false,
1383
+ contentType
1384
+ },
1385
+ extractedTypes: []
1386
+ };
1387
+ }
1388
+ const typeName = this.generateTypeName(schema, tag, operationId, method, op.path, "Response");
1389
+ if (schema.kind === IRSchemaKind.Object && !seenTypeNames.has(typeName)) {
1390
+ seenTypeNames.add(typeName);
1391
+ extractedTypes.push({
1392
+ name: typeName,
1393
+ schema,
1394
+ annotations: this.schemaConverter.extractAnnotations(media.schema)
1395
+ });
1396
+ return {
1397
+ response: {
1398
+ typeTS: "",
1399
+ schema: {
1400
+ kind: IRSchemaKind.Ref,
1401
+ ref: typeName,
1402
+ nullable: false
1403
+ },
1404
+ description: resp2.description || "",
1405
+ isStreaming: false,
1406
+ contentType
1407
+ },
1408
+ extractedTypes
1409
+ };
1410
+ }
1411
+ return {
1412
+ response: {
1413
+ typeTS: "",
1414
+ schema,
1415
+ description: resp2.description || "",
1416
+ isStreaming: false,
1417
+ contentType
1418
+ },
1419
+ extractedTypes: []
1420
+ };
1421
+ }
1422
+ }
1423
+ const firstContentType = Object.keys(resp2.content)[0];
1424
+ if (!firstContentType) {
1425
+ return {
1426
+ response: {
1427
+ typeTS: "unknown",
1428
+ schema: {
1429
+ kind: IRSchemaKind.Unknown,
1430
+ nullable: false
1431
+ },
1432
+ description: resp2.description || "",
1433
+ isStreaming: false,
1434
+ contentType: ""
1435
+ },
1436
+ extractedTypes: []
1437
+ };
1438
+ }
1439
+ const firstMedia = resp2.content[firstContentType];
1440
+ if (!firstMedia) {
1441
+ return {
1442
+ response: {
1443
+ typeTS: "unknown",
1444
+ schema: {
1445
+ kind: IRSchemaKind.Unknown,
1446
+ nullable: false
1447
+ },
1448
+ description: resp2.description || "",
1449
+ isStreaming: false,
1450
+ contentType: ""
1451
+ },
1452
+ extractedTypes: []
1453
+ };
1454
+ }
1455
+ const firstSchema = this.schemaConverter.schemaRefToIR(doc, firstMedia.schema);
1456
+ const firstStreaming = this.detectStreamingContentType(firstContentType);
1457
+ return {
1458
+ response: {
1459
+ typeTS: "",
1460
+ schema: firstSchema,
1461
+ description: resp2.description || "",
1462
+ isStreaming: firstStreaming.isStreaming,
1463
+ contentType: firstContentType,
1464
+ streamingFormat: firstStreaming.format
1465
+ },
1466
+ extractedTypes: []
1467
+ };
1468
+ }
1469
+ return {
1470
+ response: {
1471
+ typeTS: "void",
1472
+ schema: {
1473
+ kind: IRSchemaKind.Unknown,
1474
+ nullable: false
1475
+ },
1476
+ description: resp2.description || "",
1477
+ isStreaming: false,
1478
+ contentType: ""
1479
+ },
1480
+ extractedTypes: []
1481
+ };
1482
+ }
1483
+ }
1484
+ const resp = this.extractResponse(doc, op);
1485
+ return {
1486
+ response: resp,
1487
+ extractedTypes: []
1488
+ };
1489
+ }
1490
+ /**
1491
+ * Extract response information (legacy method, kept for fallback)
1492
+ */
1493
+ extractResponse(doc, op) {
1494
+ if (!op.responses) {
1495
+ return {
1496
+ typeTS: "unknown",
1497
+ schema: {
1498
+ kind: IRSchemaKind.Unknown,
1499
+ nullable: false
1500
+ },
1501
+ description: "",
1502
+ isStreaming: false,
1503
+ contentType: ""
1504
+ };
1505
+ }
1506
+ const tryCodes = [
1507
+ "200",
1508
+ "201"
1509
+ ];
1510
+ for (const code of tryCodes) {
1511
+ const response = op.responses[code];
1512
+ if (response && !("$ref" in response)) {
1513
+ const resp = response;
1514
+ if (resp.content) {
1515
+ for (const [contentType, media2] of Object.entries(resp.content)) {
1516
+ const streaming2 = this.detectStreamingContentType(contentType);
1517
+ if (streaming2.isStreaming) {
1518
+ return {
1519
+ typeTS: "",
1520
+ schema: this.schemaConverter.schemaRefToIR(doc, media2.schema),
1521
+ description: resp.description || "",
1522
+ isStreaming: true,
1523
+ contentType,
1524
+ streamingFormat: streaming2.format
1525
+ };
1526
+ }
1527
+ }
1528
+ if (resp.content["application/json"]) {
1529
+ const media2 = resp.content["application/json"];
1530
+ return {
1531
+ typeTS: "",
1532
+ schema: this.schemaConverter.schemaRefToIR(doc, media2.schema),
1533
+ description: resp.description || "",
1534
+ isStreaming: false,
1535
+ contentType: "application/json"
1536
+ };
1537
+ }
1538
+ const [firstContentType, media] = Object.entries(resp.content)[0] ?? [];
1539
+ if (!firstContentType || !media) {
1540
+ return {
1541
+ typeTS: "void",
1542
+ schema: {
1543
+ kind: IRSchemaKind.Unknown,
1544
+ nullable: false
1545
+ },
1546
+ description: resp.description || "",
1547
+ isStreaming: false,
1548
+ contentType: ""
1549
+ };
1550
+ }
1551
+ const streaming = this.detectStreamingContentType(firstContentType);
1552
+ return {
1553
+ typeTS: "",
1554
+ schema: this.schemaConverter.schemaRefToIR(doc, media.schema),
1555
+ description: resp.description || "",
1556
+ isStreaming: streaming.isStreaming,
1557
+ contentType: firstContentType,
1558
+ streamingFormat: streaming.format
1559
+ };
1560
+ }
1561
+ return {
1562
+ typeTS: "void",
1563
+ schema: {
1564
+ kind: IRSchemaKind.Unknown,
1565
+ nullable: false
1566
+ },
1567
+ description: resp.description || "",
1568
+ isStreaming: false,
1569
+ contentType: ""
1570
+ };
1571
+ }
1572
+ }
1573
+ for (const [code, response] of Object.entries(op.responses)) {
1574
+ if (code.length === 3 && code[0] === "2" && response && !("$ref" in response)) {
1575
+ const resp = response;
1576
+ if (code === "204") {
1577
+ return {
1578
+ typeTS: "void",
1579
+ schema: {
1580
+ kind: IRSchemaKind.Unknown,
1581
+ nullable: false
1582
+ },
1583
+ description: resp.description || "",
1584
+ isStreaming: false,
1585
+ contentType: ""
1586
+ };
1587
+ }
1588
+ if (resp.content) {
1589
+ for (const [contentType, media2] of Object.entries(resp.content)) {
1590
+ const streaming2 = this.detectStreamingContentType(contentType);
1591
+ if (streaming2.isStreaming) {
1592
+ return {
1593
+ typeTS: "",
1594
+ schema: this.schemaConverter.schemaRefToIR(doc, media2.schema),
1595
+ description: resp.description || "",
1596
+ isStreaming: true,
1597
+ contentType,
1598
+ streamingFormat: streaming2.format
1599
+ };
1600
+ }
1601
+ }
1602
+ if (resp.content["application/json"]) {
1603
+ const media2 = resp.content["application/json"];
1604
+ return {
1605
+ typeTS: "",
1606
+ schema: this.schemaConverter.schemaRefToIR(doc, media2.schema),
1607
+ description: resp.description || "",
1608
+ isStreaming: false,
1609
+ contentType: "application/json"
1610
+ };
1611
+ }
1612
+ const [firstContentType, media] = Object.entries(resp.content)[0] ?? [];
1613
+ if (!firstContentType || !media) {
1614
+ return {
1615
+ typeTS: "void",
1616
+ schema: {
1617
+ kind: IRSchemaKind.Unknown,
1618
+ nullable: false
1619
+ },
1620
+ description: resp.description || "",
1621
+ isStreaming: false,
1622
+ contentType: ""
1623
+ };
1624
+ }
1625
+ const streaming = this.detectStreamingContentType(firstContentType);
1626
+ return {
1627
+ typeTS: "",
1628
+ schema: this.schemaConverter.schemaRefToIR(doc, media.schema),
1629
+ description: resp.description || "",
1630
+ isStreaming: streaming.isStreaming,
1631
+ contentType: firstContentType,
1632
+ streamingFormat: streaming.format
1633
+ };
1634
+ }
1635
+ }
1636
+ }
1637
+ return {
1638
+ typeTS: "unknown",
1639
+ schema: {
1640
+ kind: IRSchemaKind.Unknown,
1641
+ nullable: false
1642
+ },
1643
+ description: "",
1644
+ isStreaming: false,
1645
+ contentType: ""
1646
+ };
1647
+ }
1648
+ /**
1649
+ * Build structured models from components.schemas
1650
+ */
1651
+ buildStructuredModels(doc) {
1652
+ const out = [];
1653
+ if (!doc.components?.schemas) {
1654
+ return out;
1655
+ }
1656
+ const names = Object.keys(doc.components.schemas).sort();
1657
+ const seen = /* @__PURE__ */ new Set();
1658
+ for (const name of names) {
1659
+ seen.add(name);
1660
+ }
1661
+ for (const name of names) {
1662
+ const sr = doc.components.schemas[name];
1663
+ const schema = this.schemaConverter.schemaRefToIR(doc, sr);
1664
+ out.push({
1665
+ name,
1666
+ schema,
1667
+ annotations: this.schemaConverter.extractAnnotations(sr)
1668
+ });
1669
+ }
1670
+ return out;
1671
+ }
1672
+ /**
1673
+ * Filter unused ModelDefs
1674
+ */
1675
+ filterUnusedModelDefs(filteredIR, allModelDefs) {
1676
+ const modelDefMap = /* @__PURE__ */ new Map();
1677
+ for (const md of allModelDefs) {
1678
+ modelDefMap.set(md.name, md);
1679
+ }
1680
+ const referenced = /* @__PURE__ */ new Set();
1681
+ const visited = /* @__PURE__ */ new Set();
1682
+ const collectRefs = /* @__PURE__ */ __name((schema) => {
1683
+ if (schema.kind === "ref" && schema.ref) {
1684
+ const refName = schema.ref;
1685
+ referenced.add(refName);
1686
+ if (!visited.has(refName)) {
1687
+ visited.add(refName);
1688
+ const md = modelDefMap.get(refName);
1689
+ if (md) {
1690
+ collectRefs(md.schema);
1691
+ }
1692
+ }
1693
+ }
1694
+ if (schema.items) {
1695
+ collectRefs(schema.items);
1696
+ }
1697
+ if (schema.additionalProperties) {
1698
+ collectRefs(schema.additionalProperties);
1699
+ }
1700
+ if (schema.oneOf) {
1701
+ for (const sub of schema.oneOf) {
1702
+ collectRefs(sub);
1703
+ }
1704
+ }
1705
+ if (schema.anyOf) {
1706
+ for (const sub of schema.anyOf) {
1707
+ collectRefs(sub);
1708
+ }
1709
+ }
1710
+ if (schema.allOf) {
1711
+ for (const sub of schema.allOf) {
1712
+ collectRefs(sub);
1713
+ }
1714
+ }
1715
+ if (schema.not) {
1716
+ collectRefs(schema.not);
1717
+ }
1718
+ if (schema.properties) {
1719
+ for (const field of schema.properties) {
1720
+ collectRefs(field.type);
1721
+ }
1722
+ }
1723
+ }, "collectRefs");
1724
+ for (const service of filteredIR.services) {
1725
+ for (const op of service.operations) {
1726
+ for (const param of op.pathParams) {
1727
+ collectRefs(param.schema);
1728
+ }
1729
+ for (const param of op.queryParams) {
1730
+ collectRefs(param.schema);
1731
+ }
1732
+ if (op.requestBody) {
1733
+ collectRefs(op.requestBody.schema);
1734
+ }
1735
+ collectRefs(op.response.schema);
1736
+ }
1737
+ }
1738
+ return allModelDefs.filter((md) => referenced.has(md.name));
1739
+ }
1740
+ };
1741
+ IrBuilderService = _ts_decorate4([
1742
+ Injectable3(),
1743
+ _ts_metadata("design:type", Function),
1744
+ _ts_metadata("design:paramtypes", [
1745
+ typeof SchemaConverterService === "undefined" ? Object : SchemaConverterService
1746
+ ])
1747
+ ], IrBuilderService);
1748
+
1749
+ // src/openapi/openapi.service.ts
1750
+ import { Injectable as Injectable4, Logger as Logger2 } from "@nestjs/common";
1751
+ import SwaggerParser from "@apidevtools/swagger-parser";
1752
+ import * as fs2 from "fs";
1753
+ import * as path3 from "path";
1754
+ function _ts_decorate5(decorators, target, key, desc) {
1755
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1756
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1757
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1758
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
1759
+ }
1760
+ __name(_ts_decorate5, "_ts_decorate");
1761
+ var OpenApiService = class _OpenApiService {
1762
+ static {
1763
+ __name(this, "OpenApiService");
1764
+ }
1765
+ logger = new Logger2(_OpenApiService.name);
1766
+ /**
1767
+ * Load an OpenAPI document from a local file path or an HTTP(S) URL
1768
+ * Supports both OpenAPI 3.0 and 3.1
1769
+ */
1770
+ async loadDocument(input) {
1771
+ try {
1772
+ let url = null;
1773
+ try {
1774
+ url = new URL(input);
1775
+ } catch {
1776
+ }
1777
+ let api;
1778
+ if (url && (url.protocol === "http:" || url.protocol === "https:")) {
1779
+ this.logger.debug(`Loading OpenAPI spec from URL: ${input}`);
1780
+ const response = await fetch(input, {
1781
+ signal: AbortSignal.timeout(1e4)
1782
+ });
1783
+ if (!response.ok) {
1784
+ throw new Error(`Failed to fetch OpenAPI spec: HTTP ${response.status} ${response.statusText}`);
1785
+ }
1786
+ const documentText = await response.text();
1787
+ let documentJson;
1788
+ try {
1789
+ documentJson = JSON.parse(documentText);
1790
+ } catch {
1791
+ throw new Error("OpenAPI spec is not valid JSON");
1792
+ }
1793
+ try {
1794
+ const parsed = await SwaggerParser.parse(documentJson);
1795
+ const bundled = await SwaggerParser.bundle(parsed);
1796
+ api = bundled;
1797
+ } catch (error) {
1798
+ this.logger.debug(`Bundle failed: ${error instanceof Error ? error.message : String(error)}`);
1799
+ this.logger.debug("Attempting dereference directly");
1800
+ const parsed = await SwaggerParser.parse(documentJson);
1801
+ const dereferenced = await SwaggerParser.dereference(parsed);
1802
+ api = dereferenced;
1803
+ }
1804
+ } else {
1805
+ const filePath = path3.resolve(input);
1806
+ if (!fs2.existsSync(filePath)) {
1807
+ throw new Error(`OpenAPI spec file not found: ${filePath}`);
1808
+ }
1809
+ this.logger.debug(`Loading OpenAPI spec from file: ${filePath}`);
1810
+ try {
1811
+ api = await SwaggerParser.bundle(filePath);
1812
+ } catch (error) {
1813
+ this.logger.debug(`Bundle failed: ${error instanceof Error ? error.message : String(error)}`);
1814
+ this.logger.debug("Attempting dereference directly");
1815
+ api = await SwaggerParser.dereference(filePath);
1816
+ }
1817
+ }
1818
+ const version = detectOpenAPIVersion(api);
1819
+ if (!isSupportedVersion(version)) {
1820
+ throw new Error(`Unsupported OpenAPI version: ${api.openapi}. Only versions 3.0.x and 3.1.0 are supported.`);
1821
+ }
1822
+ this.logger.log(`Detected OpenAPI version: ${version} (${api.openapi})`);
1823
+ return api;
1824
+ } catch (error) {
1825
+ if (error instanceof Error) {
1826
+ throw new Error(`Failed to load OpenAPI document from ${input}: ${error.message}`);
1827
+ }
1828
+ throw error;
1829
+ }
1830
+ }
1831
+ /**
1832
+ * Validate an OpenAPI document
1833
+ */
1834
+ async validateDocument(input) {
1835
+ try {
1836
+ let url = null;
1837
+ try {
1838
+ url = new URL(input);
1839
+ } catch {
1840
+ }
1841
+ if (url && (url.protocol === "http:" || url.protocol === "https:")) {
1842
+ await SwaggerParser.validate(input);
1843
+ } else {
1844
+ const filePath = path3.resolve(input);
1845
+ if (!fs2.existsSync(filePath)) {
1846
+ throw new Error(`OpenAPI spec file not found: ${filePath}`);
1847
+ }
1848
+ await SwaggerParser.validate(filePath);
1849
+ }
1850
+ } catch (error) {
1851
+ if (error instanceof Error) {
1852
+ throw new Error(`Invalid OpenAPI document: ${error.message}`);
1853
+ }
1854
+ throw error;
1855
+ }
1856
+ }
1857
+ };
1858
+ OpenApiService = _ts_decorate5([
1859
+ Injectable4()
1860
+ ], OpenApiService);
1861
+
1862
+ // src/generator/generator.service.ts
1863
+ function _ts_decorate6(decorators, target, key, desc) {
1864
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1865
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1866
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1867
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
1868
+ }
1869
+ __name(_ts_decorate6, "_ts_decorate");
1870
+ function _ts_metadata2(k, v) {
1871
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1872
+ }
1873
+ __name(_ts_metadata2, "_ts_metadata");
1874
+ var GeneratorService = class _GeneratorService {
1875
+ static {
1876
+ __name(this, "GeneratorService");
1877
+ }
1878
+ irBuilder;
1879
+ openApiService;
1880
+ logger = new Logger3(_GeneratorService.name);
1881
+ generators = /* @__PURE__ */ new Map();
1882
+ constructor(irBuilder, openApiService) {
1883
+ this.irBuilder = irBuilder;
1884
+ this.openApiService = openApiService;
1885
+ }
1886
+ /**
1887
+ * Register a generator
1888
+ */
1889
+ register(generator) {
1890
+ this.generators.set(generator.getType(), generator);
1891
+ this.logger.debug(`Registered generator: ${generator.getType()}`);
1892
+ }
1893
+ /**
1894
+ * Get a generator by type
1895
+ */
1896
+ getGenerator(type) {
1897
+ return this.generators.get(type);
1898
+ }
1899
+ /**
1900
+ * Get all available generator types
1901
+ */
1902
+ getAvailableTypes() {
1903
+ return Array.from(this.generators.keys());
1904
+ }
1905
+ /**
1906
+ * Generate SDK for a client
1907
+ * TypeScript will narrow the client type based on the discriminated union
1908
+ */
1909
+ async generate(spec, client) {
1910
+ const generator = this.getGenerator(client.type);
1911
+ if (!generator) {
1912
+ throw new Error(`Unsupported client type: ${client.type}`);
1913
+ }
1914
+ const doc = await this.openApiService.loadDocument(spec);
1915
+ const fullIR = this.irBuilder.buildIR(doc);
1916
+ const filteredIR = this.irBuilder.filterIR(fullIR, client);
1917
+ await generator.generate(client, filteredIR);
1918
+ }
1919
+ };
1920
+ GeneratorService = _ts_decorate6([
1921
+ Injectable5(),
1922
+ _ts_metadata2("design:type", Function),
1923
+ _ts_metadata2("design:paramtypes", [
1924
+ typeof IrBuilderService === "undefined" ? Object : IrBuilderService,
1925
+ typeof OpenApiService === "undefined" ? Object : OpenApiService
1926
+ ])
1927
+ ], GeneratorService);
1928
+
1929
+ // src/generator/generator.module.ts
1930
+ import { Module as Module3 } from "@nestjs/common";
1931
+
1932
+ // src/openapi/openapi.module.ts
1933
+ import { Module as Module2 } from "@nestjs/common";
1934
+ function _ts_decorate7(decorators, target, key, desc) {
1935
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1936
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1937
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1938
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
1939
+ }
1940
+ __name(_ts_decorate7, "_ts_decorate");
1941
+ var OpenApiModule = class {
1942
+ static {
1943
+ __name(this, "OpenApiModule");
1944
+ }
1945
+ };
1946
+ OpenApiModule = _ts_decorate7([
1947
+ Module2({
1948
+ providers: [
1949
+ OpenApiService
1950
+ ],
1951
+ exports: [
1952
+ OpenApiService
1953
+ ]
1954
+ })
1955
+ ], OpenApiModule);
1956
+
1957
+ // src/generator/typescript/typescript-generator.service.ts
1958
+ import { Injectable as Injectable6, Logger as Logger4 } from "@nestjs/common";
1959
+ import * as fs4 from "fs";
1960
+ import * as path5 from "path";
1961
+ import * as Handlebars2 from "handlebars";
1962
+
1963
+ // src/generator/handlebars-helpers.ts
1964
+ import * as Handlebars from "handlebars";
1965
+ function registerCommonHandlebarsHelpers() {
1966
+ Handlebars.registerHelper("pascal", (str) => toPascalCase(str));
1967
+ Handlebars.registerHelper("camel", (str) => toCamelCase(str));
1968
+ Handlebars.registerHelper("kebab", (str) => toKebabCase(str));
1969
+ Handlebars.registerHelper("snake", (str) => toSnakeCase(str));
1970
+ Handlebars.registerHelper("serviceName", (tag) => toPascalCase(tag) + "Service");
1971
+ Handlebars.registerHelper("serviceProp", (tag) => toCamelCase(tag));
1972
+ Handlebars.registerHelper("fileBase", (tag) => toSnakeCase(tag).toLowerCase());
1973
+ Handlebars.registerHelper("eq", (a, b) => a === b);
1974
+ Handlebars.registerHelper("ne", (a, b) => a !== b);
1975
+ Handlebars.registerHelper("gt", (a, b) => a > b);
1976
+ Handlebars.registerHelper("lt", (a, b) => a < b);
1977
+ Handlebars.registerHelper("sub", (a, b) => a - b);
1978
+ Handlebars.registerHelper("subtract", (a, b) => a - b);
1979
+ Handlebars.registerHelper("len", (arr) => Array.isArray(arr) ? arr.length : 0);
1980
+ Handlebars.registerHelper("lte", (a, b) => a <= b);
1981
+ Handlebars.registerHelper("or", function(...args) {
1982
+ const options = args[args.length - 1];
1983
+ if (options && "fn" in options && options.fn) {
1984
+ return args.slice(0, -1).some((a) => a) ? options.fn(this) : options.inverse?.(this);
1985
+ }
1986
+ return args.slice(0, -1).some((a) => a);
1987
+ });
1988
+ Handlebars.registerHelper("and", function(...args) {
1989
+ const options = args[args.length - 1];
1990
+ if (options && "fn" in options && options.fn) {
1991
+ return args.slice(0, -1).every((a) => a) ? options.fn(this) : options.inverse?.(this);
1992
+ }
1993
+ return args.slice(0, -1).every((a) => a);
1994
+ });
1995
+ Handlebars.registerHelper("replace", (str, search, replace) => {
1996
+ if (typeof str !== "string") return str;
1997
+ return str.replace(new RegExp(search.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g"), replace);
1998
+ });
1999
+ Handlebars.registerHelper("index", (arr, idx) => arr?.[idx]);
2000
+ Handlebars.registerHelper("getServiceName", (tag) => {
2001
+ const parts = tag.split(".");
2002
+ return parts.length > 1 ? parts[1] : tag;
2003
+ });
2004
+ Handlebars.registerHelper("groupByNamespace", (services) => {
2005
+ const namespaces = {};
2006
+ for (const service of services) {
2007
+ const parts = service.tag.split(".");
2008
+ if (parts.length === 1) {
2009
+ if (!namespaces[""]) namespaces[""] = [];
2010
+ namespaces[""]?.push(service);
2011
+ } else {
2012
+ const namespace = parts[0];
2013
+ if (namespace && !namespaces[namespace]) {
2014
+ namespaces[namespace] = [];
2015
+ }
2016
+ if (namespace) {
2017
+ namespaces[namespace]?.push(service);
2018
+ }
2019
+ }
2020
+ }
2021
+ return namespaces;
2022
+ });
2023
+ Handlebars.registerHelper("getRootServices", (services) => {
2024
+ return services.filter((s) => !s.tag.includes("."));
2025
+ });
2026
+ Handlebars.registerHelper("dict", () => ({}));
2027
+ Handlebars.registerHelper("setVar", (name, value, options) => {
2028
+ if (options?.data?.root && typeof options.data.root === "object") {
2029
+ options.data.root[`_${name}`] = value;
2030
+ }
2031
+ return "";
2032
+ });
2033
+ Handlebars.registerHelper("getVar", (name, options) => {
2034
+ if (options?.data?.root && typeof options.data.root === "object") {
2035
+ return options.data.root[`_${name}`] || false;
2036
+ }
2037
+ return false;
2038
+ });
2039
+ Handlebars.registerHelper("set", (obj, key, value) => {
2040
+ if (obj && typeof obj === "object") {
2041
+ obj[key] = value;
2042
+ }
2043
+ return "";
2044
+ });
2045
+ Handlebars.registerHelper("hasKey", (obj, key) => {
2046
+ return obj && typeof obj === "object" && key in obj;
2047
+ });
2048
+ Handlebars.registerHelper("lookup", (obj, key) => {
2049
+ return obj && typeof obj === "object" ? obj[key] : void 0;
2050
+ });
2051
+ Handlebars.registerHelper("reMatch", (pattern, str) => {
2052
+ try {
2053
+ const regex = new RegExp(pattern);
2054
+ return regex.test(str);
2055
+ } catch {
2056
+ return false;
2057
+ }
2058
+ });
2059
+ }
2060
+ __name(registerCommonHandlebarsHelpers, "registerCommonHandlebarsHelpers");
2061
+
2062
+ // src/generator/typescript/helpers.ts
2063
+ function decodeHtmlEntities(str) {
2064
+ return str.replace(/&quot;/g, '"').replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&#x60;/g, "`").replace(/&#96;/g, "`").replace(/&amp;/g, "&");
2065
+ }
2066
+ __name(decodeHtmlEntities, "decodeHtmlEntities");
2067
+ function schemaToTSType(s, predefinedTypes, modelDefs, isSameFile = false) {
2068
+ let t;
2069
+ switch (s.kind) {
2070
+ case IRSchemaKind.String:
2071
+ if (s.format === "binary") {
2072
+ t = "Blob";
2073
+ } else {
2074
+ t = "string";
2075
+ }
2076
+ break;
2077
+ case IRSchemaKind.Number:
2078
+ case IRSchemaKind.Integer:
2079
+ t = "number";
2080
+ break;
2081
+ case IRSchemaKind.Boolean:
2082
+ t = "boolean";
2083
+ break;
2084
+ case IRSchemaKind.Null:
2085
+ t = "null";
2086
+ break;
2087
+ case IRSchemaKind.Ref:
2088
+ if (s.ref) {
2089
+ if (predefinedTypes?.some((pt) => pt.type === s.ref)) {
2090
+ t = s.ref;
2091
+ } else if (isSameFile && modelDefs) {
2092
+ const localType = modelDefs.find((md) => md.name === s.ref);
2093
+ if (localType) {
2094
+ t = s.ref;
2095
+ } else {
2096
+ t = "Schema." + s.ref;
2097
+ }
2098
+ } else {
2099
+ t = "Schema." + s.ref;
2100
+ }
2101
+ } else {
2102
+ t = "unknown";
2103
+ }
2104
+ break;
2105
+ case IRSchemaKind.Array:
2106
+ if (s.items) {
2107
+ const inner = schemaToTSType(s.items, predefinedTypes, modelDefs);
2108
+ if (inner.includes(" | ") || inner.includes(" & ")) {
2109
+ t = `(${inner})[]`;
2110
+ } else {
2111
+ t = `${inner}[]`;
2112
+ }
2113
+ } else {
2114
+ t = "unknown[]";
2115
+ }
2116
+ break;
2117
+ case IRSchemaKind.OneOf:
2118
+ if (s.oneOf) {
2119
+ const parts = s.oneOf.map((sub) => schemaToTSType(sub, predefinedTypes, modelDefs));
2120
+ t = parts.join(" | ");
2121
+ } else {
2122
+ t = "unknown";
2123
+ }
2124
+ break;
2125
+ case IRSchemaKind.AnyOf:
2126
+ if (s.anyOf) {
2127
+ const parts = s.anyOf.map((sub) => schemaToTSType(sub, predefinedTypes, modelDefs));
2128
+ t = parts.join(" | ");
2129
+ } else {
2130
+ t = "unknown";
2131
+ }
2132
+ break;
2133
+ case IRSchemaKind.AllOf:
2134
+ if (s.allOf) {
2135
+ const parts = s.allOf.map((sub) => schemaToTSType(sub, predefinedTypes, modelDefs));
2136
+ t = parts.join(" & ");
2137
+ } else {
2138
+ t = "unknown";
2139
+ }
2140
+ break;
2141
+ case IRSchemaKind.Enum:
2142
+ if (s.enumValues && s.enumValues.length > 0) {
2143
+ const vals = [];
2144
+ switch (s.enumBase) {
2145
+ case IRSchemaKind.Number:
2146
+ case IRSchemaKind.Integer:
2147
+ for (const v of s.enumValues) {
2148
+ vals.push(v);
2149
+ }
2150
+ break;
2151
+ case IRSchemaKind.Boolean:
2152
+ for (const v of s.enumValues) {
2153
+ if (v === "true" || v === "false") {
2154
+ vals.push(v);
2155
+ } else {
2156
+ vals.push(`"${v}"`);
2157
+ }
2158
+ }
2159
+ break;
2160
+ default:
2161
+ for (const v of s.enumValues) {
2162
+ vals.push(`"${v}"`);
2163
+ }
2164
+ }
2165
+ t = vals.join(" | ");
2166
+ } else {
2167
+ t = "unknown";
2168
+ }
2169
+ break;
2170
+ case IRSchemaKind.Object:
2171
+ if (!s.properties || s.properties.length === 0) {
2172
+ t = "Record<string, unknown>";
2173
+ } else {
2174
+ const parts = [];
2175
+ for (const f of s.properties) {
2176
+ const ft = schemaToTSType(f.type, predefinedTypes, modelDefs, isSameFile);
2177
+ if (f.required) {
2178
+ parts.push(`${f.name}: ${ft}`);
2179
+ } else {
2180
+ parts.push(`${f.name}?: ${ft}`);
2181
+ }
2182
+ }
2183
+ t = "{ " + parts.join("; ") + " }";
2184
+ }
2185
+ break;
2186
+ default:
2187
+ t = "unknown";
2188
+ }
2189
+ if (s.nullable && t !== "null") {
2190
+ t += " | null";
2191
+ }
2192
+ return decodeHtmlEntities(t);
2193
+ }
2194
+ __name(schemaToTSType, "schemaToTSType");
2195
+ function deriveMethodName(op) {
2196
+ const path7 = op.path;
2197
+ const hasID = path7.includes("{") && path7.includes("}");
2198
+ if (op.operationID) {
2199
+ return toCamelCase(op.operationID);
2200
+ }
2201
+ switch (op.method) {
2202
+ case "GET":
2203
+ return hasID ? "get" : "list";
2204
+ case "POST":
2205
+ return "create";
2206
+ case "PUT":
2207
+ case "PATCH":
2208
+ return "update";
2209
+ case "DELETE":
2210
+ return "delete";
2211
+ default:
2212
+ return op.method.toLowerCase();
2213
+ }
2214
+ }
2215
+ __name(deriveMethodName, "deriveMethodName");
2216
+ async function resolveMethodName(client, op) {
2217
+ if (client.operationIdParser) {
2218
+ try {
2219
+ const name = await client.operationIdParser(op.operationID, op.method, op.path);
2220
+ if (name) {
2221
+ return toCamelCase(name);
2222
+ }
2223
+ } catch {
2224
+ }
2225
+ }
2226
+ const defaultParsed = defaultParseOperationID(op.operationID);
2227
+ if (defaultParsed) {
2228
+ return toCamelCase(defaultParsed);
2229
+ }
2230
+ return deriveMethodName(op);
2231
+ }
2232
+ __name(resolveMethodName, "resolveMethodName");
2233
+ function defaultParseOperationID(opID) {
2234
+ if (!opID) {
2235
+ return "";
2236
+ }
2237
+ const idx = opID.indexOf("Controller_");
2238
+ if (idx >= 0) {
2239
+ return opID.substring(idx + "Controller_".length);
2240
+ }
2241
+ return opID;
2242
+ }
2243
+ __name(defaultParseOperationID, "defaultParseOperationID");
2244
+ function buildPathTemplate(op) {
2245
+ const path7 = op.path;
2246
+ let result = "`";
2247
+ for (let i = 0; i < path7.length; i++) {
2248
+ if (path7[i] === "{") {
2249
+ let j = i + 1;
2250
+ while (j < path7.length && path7[j] !== "}") {
2251
+ j++;
2252
+ }
2253
+ if (j < path7.length) {
2254
+ const name = path7.substring(i + 1, j);
2255
+ result += `\${encodeURIComponent(${name})}`;
2256
+ i = j;
2257
+ continue;
2258
+ }
2259
+ }
2260
+ result += path7[i];
2261
+ }
2262
+ result += "`";
2263
+ return result;
2264
+ }
2265
+ __name(buildPathTemplate, "buildPathTemplate");
2266
+ function buildQueryKeyBase(op) {
2267
+ const path7 = op.path;
2268
+ const parts = path7.split("/");
2269
+ const baseParts = [];
2270
+ for (const p of parts) {
2271
+ if (p === "") {
2272
+ continue;
2273
+ }
2274
+ if (p.startsWith("{") && p.endsWith("}")) {
2275
+ continue;
2276
+ }
2277
+ baseParts.push(p);
2278
+ }
2279
+ const base = baseParts.join("/");
2280
+ return `'${base}'`;
2281
+ }
2282
+ __name(buildQueryKeyBase, "buildQueryKeyBase");
2283
+ function orderPathParams(op) {
2284
+ const ordered = [];
2285
+ const index = /* @__PURE__ */ new Map();
2286
+ for (let i = 0; i < op.pathParams.length; i++) {
2287
+ const param = op.pathParams[i];
2288
+ if (param) {
2289
+ index.set(param.name, i);
2290
+ }
2291
+ }
2292
+ const path7 = op.path;
2293
+ for (let i = 0; i < path7.length; i++) {
2294
+ if (path7[i] === "{") {
2295
+ let j = i + 1;
2296
+ while (j < path7.length && path7[j] !== "}") {
2297
+ j++;
2298
+ }
2299
+ if (j < path7.length) {
2300
+ const name = path7.substring(i + 1, j);
2301
+ const idx = index.get(name);
2302
+ if (idx !== void 0) {
2303
+ const param = op.pathParams[idx];
2304
+ if (param) {
2305
+ ordered.push(param);
2306
+ }
2307
+ }
2308
+ i = j;
2309
+ continue;
2310
+ }
2311
+ }
2312
+ }
2313
+ return ordered;
2314
+ }
2315
+ __name(orderPathParams, "orderPathParams");
2316
+ function schemaToTSTypeWithSimpleTypes(s, modelDefs, predefinedTypes, isSameFile = false) {
2317
+ return schemaToTSType(s, predefinedTypes, modelDefs, isSameFile);
2318
+ }
2319
+ __name(schemaToTSTypeWithSimpleTypes, "schemaToTSTypeWithSimpleTypes");
2320
+ function buildMethodSignature(op, methodName, modelDefs, predefinedTypes, isSameFile = false) {
2321
+ const parts = [];
2322
+ for (const p of orderPathParams(op)) {
2323
+ parts.push(`${p.name}: ${schemaToTSTypeWithSimpleTypes(p.schema, modelDefs, predefinedTypes, isSameFile)}`);
2324
+ }
2325
+ if (op.queryParams.length > 0) {
2326
+ const queryType = toPascalCase(op.tag) + toPascalCase(methodName) + "Query";
2327
+ parts.push(`query?: Schema.${queryType}`);
2328
+ }
2329
+ if (op.requestBody) {
2330
+ const opt = op.requestBody.required === true ? "" : "?";
2331
+ parts.push(`body${opt}: ${schemaToTSTypeWithSimpleTypes(op.requestBody.schema, modelDefs, predefinedTypes, isSameFile)}`);
2332
+ }
2333
+ parts.push('init?: Omit<RequestInit, "method" | "body">');
2334
+ return parts;
2335
+ }
2336
+ __name(buildMethodSignature, "buildMethodSignature");
2337
+ function collectPredefinedTypesUsedInSchema(modelDefs, predefinedTypes) {
2338
+ if (!predefinedTypes || predefinedTypes.length === 0) {
2339
+ return [];
2340
+ }
2341
+ const usedTypes = /* @__PURE__ */ new Set();
2342
+ const resolveRef = /* @__PURE__ */ __name((ref) => {
2343
+ const modelDef = modelDefs.find((md) => md.name === ref);
2344
+ return modelDef ? modelDef.schema : null;
2345
+ }, "resolveRef");
2346
+ const checkSchema = /* @__PURE__ */ __name((schema) => {
2347
+ if (schema.kind === "ref" && schema.ref) {
2348
+ const isPredefined = predefinedTypes.some((pt) => pt.type === schema.ref);
2349
+ if (isPredefined) {
2350
+ usedTypes.add(schema.ref);
2351
+ } else {
2352
+ const resolved = resolveRef(schema.ref);
2353
+ if (resolved) {
2354
+ checkSchema(resolved);
2355
+ }
2356
+ }
2357
+ } else if (schema.kind === "array" && schema.items) {
2358
+ checkSchema(schema.items);
2359
+ } else if (schema.kind === "object" && schema.properties) {
2360
+ for (const prop of schema.properties) {
2361
+ checkSchema(prop.type);
2362
+ }
2363
+ } else if (schema.kind === "oneOf" && schema.oneOf) {
2364
+ for (const sub of schema.oneOf) {
2365
+ checkSchema(sub);
2366
+ }
2367
+ } else if (schema.kind === "anyOf" && schema.anyOf) {
2368
+ for (const sub of schema.anyOf) {
2369
+ checkSchema(sub);
2370
+ }
2371
+ } else if (schema.kind === "allOf" && schema.allOf) {
2372
+ for (const sub of schema.allOf) {
2373
+ checkSchema(sub);
2374
+ }
2375
+ }
2376
+ }, "checkSchema");
2377
+ for (const modelDef of modelDefs) {
2378
+ checkSchema(modelDef.schema);
2379
+ }
2380
+ return predefinedTypes.filter((pt) => usedTypes.has(pt.type));
2381
+ }
2382
+ __name(collectPredefinedTypesUsedInSchema, "collectPredefinedTypesUsedInSchema");
2383
+ function collectPredefinedTypesUsedInService(service, predefinedTypes, modelDefs) {
2384
+ if (!predefinedTypes || predefinedTypes.length === 0) {
2385
+ return [];
2386
+ }
2387
+ const usedTypes = /* @__PURE__ */ new Set();
2388
+ const resolveRef = /* @__PURE__ */ __name((ref) => {
2389
+ if (!modelDefs) return null;
2390
+ const modelDef = modelDefs.find((md) => md.name === ref);
2391
+ return modelDef ? modelDef.schema : null;
2392
+ }, "resolveRef");
2393
+ const checkSchema = /* @__PURE__ */ __name((schema) => {
2394
+ if (schema.kind === "ref" && schema.ref) {
2395
+ const isPredefined = predefinedTypes.some((pt) => pt.type === schema.ref);
2396
+ if (isPredefined) {
2397
+ usedTypes.add(schema.ref);
2398
+ } else {
2399
+ const resolved = resolveRef(schema.ref);
2400
+ if (resolved) {
2401
+ checkSchema(resolved);
2402
+ }
2403
+ }
2404
+ } else if (schema.kind === "array" && schema.items) {
2405
+ checkSchema(schema.items);
2406
+ } else if (schema.kind === "object" && schema.properties) {
2407
+ for (const prop of schema.properties) {
2408
+ checkSchema(prop.type);
2409
+ }
2410
+ } else if (schema.kind === "oneOf" && schema.oneOf) {
2411
+ for (const sub of schema.oneOf) {
2412
+ checkSchema(sub);
2413
+ }
2414
+ } else if (schema.kind === "anyOf" && schema.anyOf) {
2415
+ for (const sub of schema.anyOf) {
2416
+ checkSchema(sub);
2417
+ }
2418
+ } else if (schema.kind === "allOf" && schema.allOf) {
2419
+ for (const sub of schema.allOf) {
2420
+ checkSchema(sub);
2421
+ }
2422
+ }
2423
+ }, "checkSchema");
2424
+ for (const op of service.operations) {
2425
+ for (const param of op.pathParams) {
2426
+ checkSchema(param.schema);
2427
+ }
2428
+ }
2429
+ return predefinedTypes.filter((pt) => usedTypes.has(pt.type));
2430
+ }
2431
+ __name(collectPredefinedTypesUsedInService, "collectPredefinedTypesUsedInService");
2432
+ function queryKeyArgs(op) {
2433
+ const out = [];
2434
+ for (const p of orderPathParams(op)) {
2435
+ out.push(p.name);
2436
+ }
2437
+ if (op.queryParams.length > 0) {
2438
+ out.push("query");
2439
+ }
2440
+ if (op.requestBody) {
2441
+ out.push("body");
2442
+ }
2443
+ return out;
2444
+ }
2445
+ __name(queryKeyArgs, "queryKeyArgs");
2446
+ function buildQueryKeyReturnType(op, methodName, modelDefs, predefinedTypes, isSameFile = false) {
2447
+ const baseType = buildQueryKeyBase(op);
2448
+ const pathParamTypes = [];
2449
+ for (const p of orderPathParams(op)) {
2450
+ const type = schemaToTSTypeWithSimpleTypes(p.schema, modelDefs, predefinedTypes, isSameFile);
2451
+ pathParamTypes.push(type);
2452
+ }
2453
+ const hasOptionalQuery = op.queryParams.length > 0;
2454
+ const hasOptionalBody = op.requestBody && !op.requestBody.required;
2455
+ const hasRequiredBody = op.requestBody && op.requestBody.required;
2456
+ const buildTuple = /* @__PURE__ */ __name((types) => {
2457
+ const allTypes = [
2458
+ baseType,
2459
+ ...types
2460
+ ];
2461
+ return `readonly [${allTypes.join(", ")}]`;
2462
+ }, "buildTuple");
2463
+ if (!hasOptionalQuery && !hasOptionalBody) {
2464
+ if (hasRequiredBody) {
2465
+ if (!op.requestBody?.schema) {
2466
+ throw new Error("Request body schema is required");
2467
+ }
2468
+ const bodyType = schemaToTSTypeWithSimpleTypes(op.requestBody.schema, modelDefs, predefinedTypes, isSameFile);
2469
+ return buildTuple([
2470
+ ...pathParamTypes,
2471
+ bodyType
2472
+ ]);
2473
+ }
2474
+ return buildTuple(pathParamTypes);
2475
+ }
2476
+ const unionTypes = [];
2477
+ if (hasOptionalQuery && hasOptionalBody) {
2478
+ const queryType = `Schema.${toPascalCase(op.tag)}${toPascalCase(methodName)}Query`;
2479
+ const bodyType = schemaToTSTypeWithSimpleTypes(
2480
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2481
+ op.requestBody.schema,
2482
+ modelDefs,
2483
+ predefinedTypes,
2484
+ isSameFile
2485
+ );
2486
+ unionTypes.push(buildTuple(pathParamTypes));
2487
+ unionTypes.push(buildTuple([
2488
+ ...pathParamTypes,
2489
+ queryType
2490
+ ]));
2491
+ unionTypes.push(buildTuple([
2492
+ ...pathParamTypes,
2493
+ bodyType
2494
+ ]));
2495
+ unionTypes.push(buildTuple([
2496
+ ...pathParamTypes,
2497
+ queryType,
2498
+ bodyType
2499
+ ]));
2500
+ } else if (hasOptionalQuery) {
2501
+ const queryType = `Schema.${toPascalCase(op.tag)}${toPascalCase(methodName)}Query`;
2502
+ if (hasRequiredBody) {
2503
+ if (!op.requestBody?.schema) {
2504
+ throw new Error("Request body schema is required");
2505
+ }
2506
+ const bodyType = schemaToTSTypeWithSimpleTypes(op.requestBody.schema, modelDefs, predefinedTypes, isSameFile);
2507
+ unionTypes.push(buildTuple([
2508
+ ...pathParamTypes,
2509
+ bodyType
2510
+ ]));
2511
+ unionTypes.push(buildTuple([
2512
+ ...pathParamTypes,
2513
+ bodyType,
2514
+ queryType
2515
+ ]));
2516
+ } else {
2517
+ unionTypes.push(buildTuple(pathParamTypes));
2518
+ unionTypes.push(buildTuple([
2519
+ ...pathParamTypes,
2520
+ queryType
2521
+ ]));
2522
+ }
2523
+ } else if (hasOptionalBody) {
2524
+ if (!op.requestBody?.schema) {
2525
+ throw new Error("Request body schema is required");
2526
+ }
2527
+ const bodyType = schemaToTSTypeWithSimpleTypes(op.requestBody.schema, modelDefs, predefinedTypes, isSameFile);
2528
+ unionTypes.push(buildTuple(pathParamTypes));
2529
+ unionTypes.push(buildTuple([
2530
+ ...pathParamTypes,
2531
+ bodyType
2532
+ ]));
2533
+ }
2534
+ return unionTypes.join(" | ");
2535
+ }
2536
+ __name(buildQueryKeyReturnType, "buildQueryKeyReturnType");
2537
+ function hasOptionalQueryKeyParams(op) {
2538
+ return op.queryParams.length > 0 || op.requestBody !== null && !op.requestBody.required;
2539
+ }
2540
+ __name(hasOptionalQueryKeyParams, "hasOptionalQueryKeyParams");
2541
+ function quoteTSPropertyName(name) {
2542
+ let needsQuoting = false;
2543
+ for (const char of name) {
2544
+ if (!(char >= "a" && char <= "z" || char >= "A" && char <= "Z" || char >= "0" && char <= "9" || char === "_" || char === "$")) {
2545
+ needsQuoting = true;
2546
+ break;
2547
+ }
2548
+ }
2549
+ const firstChar = name[0];
2550
+ if (firstChar && firstChar >= "0" && firstChar <= "9") {
2551
+ needsQuoting = true;
2552
+ }
2553
+ if (needsQuoting) {
2554
+ return `"${name}"`;
2555
+ }
2556
+ return name;
2557
+ }
2558
+ __name(quoteTSPropertyName, "quoteTSPropertyName");
2559
+ function extractRefDependencies(schema, visited = /* @__PURE__ */ new Set()) {
2560
+ const deps = /* @__PURE__ */ new Set();
2561
+ if (schema.kind === "ref" && schema.ref) {
2562
+ const refName = schema.ref;
2563
+ if (!visited.has(refName)) {
2564
+ visited.add(refName);
2565
+ deps.add(refName);
2566
+ }
2567
+ } else if (schema.kind === "array" && schema.items) {
2568
+ const itemDeps = extractRefDependencies(schema.items, visited);
2569
+ itemDeps.forEach((dep) => deps.add(dep));
2570
+ } else if (schema.kind === "object" && schema.properties) {
2571
+ for (const prop of schema.properties) {
2572
+ const propDeps = extractRefDependencies(prop.type, visited);
2573
+ propDeps.forEach((dep) => deps.add(dep));
2574
+ }
2575
+ if (schema.additionalProperties) {
2576
+ const addlDeps = extractRefDependencies(schema.additionalProperties, visited);
2577
+ addlDeps.forEach((dep) => deps.add(dep));
2578
+ }
2579
+ } else if (schema.kind === "oneOf" && schema.oneOf) {
2580
+ for (const opt of schema.oneOf) {
2581
+ const optDeps = extractRefDependencies(opt, visited);
2582
+ optDeps.forEach((dep) => deps.add(dep));
2583
+ }
2584
+ } else if (schema.kind === "anyOf" && schema.anyOf) {
2585
+ for (const opt of schema.anyOf) {
2586
+ const optDeps = extractRefDependencies(opt, visited);
2587
+ optDeps.forEach((dep) => deps.add(dep));
2588
+ }
2589
+ } else if (schema.kind === "allOf" && schema.allOf) {
2590
+ for (const sch of schema.allOf) {
2591
+ const schDeps = extractRefDependencies(sch, visited);
2592
+ schDeps.forEach((dep) => deps.add(dep));
2593
+ }
2594
+ } else if (schema.kind === "not" && schema.not) {
2595
+ const notDeps = extractRefDependencies(schema.not, visited);
2596
+ notDeps.forEach((dep) => deps.add(dep));
2597
+ }
2598
+ return deps;
2599
+ }
2600
+ __name(extractRefDependencies, "extractRefDependencies");
2601
+ function sortModelDefsByDependencies(modelDefs) {
2602
+ const modelDefMap = /* @__PURE__ */ new Map();
2603
+ for (const md of modelDefs) {
2604
+ modelDefMap.set(md.name, md);
2605
+ }
2606
+ const dependencies = /* @__PURE__ */ new Map();
2607
+ for (const md of modelDefs) {
2608
+ const deps = extractRefDependencies(md.schema);
2609
+ const validDeps = /* @__PURE__ */ new Set();
2610
+ for (const dep of deps) {
2611
+ if (modelDefMap.has(dep)) {
2612
+ validDeps.add(dep);
2613
+ }
2614
+ }
2615
+ dependencies.set(md.name, validDeps);
2616
+ }
2617
+ const sorted = [];
2618
+ const inDegree = /* @__PURE__ */ new Map();
2619
+ const queue = [];
2620
+ for (const md of modelDefs) {
2621
+ inDegree.set(md.name, dependencies.get(md.name)?.size || 0);
2622
+ if (inDegree.get(md.name) === 0) {
2623
+ queue.push(md.name);
2624
+ }
2625
+ }
2626
+ while (queue.length > 0) {
2627
+ const current = queue.shift();
2628
+ if (!current) {
2629
+ continue;
2630
+ }
2631
+ const modelDef = modelDefMap.get(current);
2632
+ if (modelDef) {
2633
+ sorted.push(modelDef);
2634
+ }
2635
+ for (const md of modelDefs) {
2636
+ const deps = dependencies.get(md.name);
2637
+ if (deps?.has(current)) {
2638
+ const newInDegree = (inDegree.get(md.name) || 0) - 1;
2639
+ inDegree.set(md.name, newInDegree);
2640
+ if (newInDegree === 0) {
2641
+ queue.push(md.name);
2642
+ }
2643
+ }
2644
+ }
2645
+ }
2646
+ if (sorted.length < modelDefs.length) {
2647
+ const sortedNames = new Set(sorted.map((md) => md.name));
2648
+ for (const md of modelDefs) {
2649
+ if (!sortedNames.has(md.name)) {
2650
+ sorted.push(md);
2651
+ }
2652
+ }
2653
+ }
2654
+ return sorted;
2655
+ }
2656
+ __name(sortModelDefsByDependencies, "sortModelDefsByDependencies");
2657
+ function isStreamingOperation(op) {
2658
+ return op.response.isStreaming === true;
2659
+ }
2660
+ __name(isStreamingOperation, "isStreamingOperation");
2661
+ function getStreamingItemType(op) {
2662
+ const schema = op.response.schema;
2663
+ if (schema.kind === IRSchemaKind.Array && schema.items) {
2664
+ return schemaToTSType(schema.items);
2665
+ }
2666
+ if (op.response.streamingFormat === "sse") {
2667
+ return "string";
2668
+ }
2669
+ return schemaToTSType(schema);
2670
+ }
2671
+ __name(getStreamingItemType, "getStreamingItemType");
2672
+
2673
+ // src/generator/typescript/zod-schema-converter.ts
2674
+ function schemaToZodSchema(s, indent = "", modelDefs, useLocalSchemaTypes = false) {
2675
+ const nextIndent = indent + " ";
2676
+ let zod;
2677
+ switch (s.kind) {
2678
+ case IRSchemaKind.String:
2679
+ zod = "z.string()";
2680
+ if (s.format === "date" || s.format === "date-time") {
2681
+ zod = "z.iso.datetime()";
2682
+ } else if (s.format === "email") {
2683
+ zod = "z.email()";
2684
+ } else if (s.format === "uri" || s.format === "url") {
2685
+ zod = "z.url()";
2686
+ } else if (s.format === "uuid") {
2687
+ zod = "z.uuid()";
2688
+ }
2689
+ break;
2690
+ case IRSchemaKind.Number:
2691
+ zod = "z.number()";
2692
+ break;
2693
+ case IRSchemaKind.Integer:
2694
+ zod = "z.number().int()";
2695
+ break;
2696
+ case IRSchemaKind.Boolean:
2697
+ zod = "z.boolean()";
2698
+ break;
2699
+ case IRSchemaKind.Null:
2700
+ zod = "z.null()";
2701
+ break;
2702
+ case IRSchemaKind.Ref:
2703
+ if (s.ref) {
2704
+ zod = useLocalSchemaTypes ? `${s.ref}Schema` : `Schema.${s.ref}Schema`;
2705
+ } else {
2706
+ zod = "z.unknown()";
2707
+ }
2708
+ break;
2709
+ case IRSchemaKind.Array:
2710
+ if (s.items) {
2711
+ const itemsZod = schemaToZodSchema(s.items, nextIndent, modelDefs, useLocalSchemaTypes);
2712
+ zod = `${itemsZod}.array()`;
2713
+ } else {
2714
+ zod = "z.unknown().array()";
2715
+ }
2716
+ break;
2717
+ case IRSchemaKind.Object:
2718
+ if (!s.properties || s.properties.length === 0) {
2719
+ if (s.additionalProperties) {
2720
+ const valueZod = schemaToZodSchema(s.additionalProperties, nextIndent, modelDefs, useLocalSchemaTypes);
2721
+ zod = `z.record(z.string(), ${valueZod})`;
2722
+ } else {
2723
+ zod = "z.record(z.string(), z.unknown())";
2724
+ }
2725
+ } else {
2726
+ const props = [];
2727
+ for (const field of s.properties) {
2728
+ const fieldZod = schemaToZodSchema(field.type, nextIndent, modelDefs, useLocalSchemaTypes);
2729
+ const fieldName = quoteTSPropertyName(field.name);
2730
+ if (field.required) {
2731
+ props.push(`${nextIndent}${fieldName}: ${fieldZod}`);
2732
+ } else {
2733
+ props.push(`${nextIndent}${fieldName}: ${fieldZod}.optional()`);
2734
+ }
2735
+ }
2736
+ const objectZod = `z.object({
2737
+ ${props.join(",\n")}
2738
+ ${indent}})`;
2739
+ if (s.additionalProperties) {
2740
+ const valueZod = schemaToZodSchema(s.additionalProperties, nextIndent, modelDefs, useLocalSchemaTypes);
2741
+ zod = `${objectZod}.catchall(${valueZod})`;
2742
+ } else {
2743
+ zod = objectZod;
2744
+ }
2745
+ }
2746
+ break;
2747
+ case IRSchemaKind.Enum:
2748
+ if (s.enumValues && s.enumValues.length > 0) {
2749
+ const enumValues = s.enumValues.map((v) => {
2750
+ if (v === "true" || v === "false") {
2751
+ return v;
2752
+ }
2753
+ if (/^-?[0-9]+(\.[0-9]+)?$/.test(v)) {
2754
+ return v;
2755
+ }
2756
+ return JSON.stringify(v);
2757
+ });
2758
+ zod = `z.enum([${enumValues.join(", ")}])`;
2759
+ } else {
2760
+ zod = "z.string()";
2761
+ }
2762
+ break;
2763
+ case IRSchemaKind.OneOf:
2764
+ if (s.oneOf && s.oneOf.length > 0) {
2765
+ const options = s.oneOf.map((opt) => schemaToZodSchema(opt, nextIndent, modelDefs, useLocalSchemaTypes));
2766
+ zod = `z.union([${options.join(", ")}])`;
2767
+ } else {
2768
+ zod = "z.unknown()";
2769
+ }
2770
+ break;
2771
+ case IRSchemaKind.AnyOf:
2772
+ if (s.anyOf && s.anyOf.length > 0) {
2773
+ const options = s.anyOf.map((opt) => schemaToZodSchema(opt, nextIndent, modelDefs, useLocalSchemaTypes));
2774
+ zod = `z.union([${options.join(", ")}])`;
2775
+ } else {
2776
+ zod = "z.unknown()";
2777
+ }
2778
+ break;
2779
+ case IRSchemaKind.AllOf:
2780
+ if (s.allOf && s.allOf.length > 0) {
2781
+ const schemas = s.allOf.map((sch) => schemaToZodSchema(sch, nextIndent, modelDefs, useLocalSchemaTypes));
2782
+ zod = schemas.join(".and(") + ")".repeat(schemas.length - 1);
2783
+ } else {
2784
+ zod = "z.unknown()";
2785
+ }
2786
+ break;
2787
+ default:
2788
+ zod = "z.unknown()";
2789
+ }
2790
+ if (s.nullable && zod !== "z.null()") {
2791
+ zod = `${zod}.nullable()`;
2792
+ }
2793
+ return zod;
2794
+ }
2795
+ __name(schemaToZodSchema, "schemaToZodSchema");
2796
+
2797
+ // src/generator/typescript/prettier-formatter.ts
2798
+ import { exec } from "child_process";
2799
+ import { promisify } from "util";
2800
+ import * as path4 from "path";
2801
+ import * as fs3 from "fs";
2802
+ var execAsync = promisify(exec);
2803
+ async function formatWithPrettier(outDir, filesToFormat, logger) {
2804
+ const log = logger || console;
2805
+ try {
2806
+ try {
2807
+ await execAsync("npx --yes prettier --version", {
2808
+ cwd: outDir,
2809
+ maxBuffer: 10 * 1024 * 1024
2810
+ });
2811
+ } catch {
2812
+ log.warn?.("Prettier is not available. Skipping code formatting. Install prettier to enable formatting.");
2813
+ return;
2814
+ }
2815
+ const tsFilesToFormat = [];
2816
+ for (const file of filesToFormat) {
2817
+ if (file.endsWith(".ts") || file.endsWith(".tsx")) {
2818
+ const fullPath = path4.join(outDir, file);
2819
+ if (fs3.existsSync(fullPath)) {
2820
+ tsFilesToFormat.push(file);
2821
+ }
2822
+ }
2823
+ }
2824
+ if (tsFilesToFormat.length === 0) {
2825
+ log.debug?.("No TypeScript files to format.");
2826
+ return;
2827
+ }
2828
+ const escapedFiles = tsFilesToFormat.map((file) => {
2829
+ const escaped = file.replace(/\\/g, "/");
2830
+ return `"${escaped}"`;
2831
+ }).join(" ");
2832
+ const { stderr } = await execAsync(`npx --yes prettier --write --log-level=error ${escapedFiles}`, {
2833
+ cwd: outDir,
2834
+ maxBuffer: 10 * 1024 * 1024
2835
+ });
2836
+ if (stderr && !stderr.includes("warning")) {
2837
+ log.warn?.(stderr);
2838
+ }
2839
+ } catch (error) {
2840
+ const errorMessage = error instanceof Error ? error.message : String(error);
2841
+ log.warn?.(`Failed to format code with Prettier: ${errorMessage}. Generated code will not be formatted.`);
2842
+ }
2843
+ }
2844
+ __name(formatWithPrettier, "formatWithPrettier");
2845
+
2846
+ // src/generator/typescript/typescript-generator.service.ts
2847
+ function _ts_decorate8(decorators, target, key, desc) {
2848
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2849
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
2850
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2851
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
2852
+ }
2853
+ __name(_ts_decorate8, "_ts_decorate");
2854
+ function _ts_metadata3(k, v) {
2855
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
2856
+ }
2857
+ __name(_ts_metadata3, "_ts_metadata");
2858
+ var TypeScriptGeneratorService = class _TypeScriptGeneratorService {
2859
+ static {
2860
+ __name(this, "TypeScriptGeneratorService");
2861
+ }
2862
+ configService;
2863
+ logger = new Logger4(_TypeScriptGeneratorService.name);
2864
+ constructor(configService) {
2865
+ this.configService = configService;
2866
+ }
2867
+ getType() {
2868
+ return "typescript";
2869
+ }
2870
+ async generate(client, ir) {
2871
+ if (!client.defaultBaseURL && ir.openApiDocument) {
2872
+ const doc = ir.openApiDocument;
2873
+ const servers = doc.servers;
2874
+ if (Array.isArray(servers) && servers.length > 0) {
2875
+ client.defaultBaseURL = servers[0]?.url || "";
2876
+ }
2877
+ }
2878
+ if (!client.srcDir) {
2879
+ client.srcDir = "src";
2880
+ }
2881
+ const srcDirPath = client.srcDir;
2882
+ const srcDir = path5.join(client.outDir, srcDirPath);
2883
+ const servicesDir = path5.join(srcDir, "services");
2884
+ await fs4.promises.mkdir(servicesDir, {
2885
+ recursive: true
2886
+ });
2887
+ const processedIR = await this.preprocessIR(client, ir);
2888
+ this.registerHandlebarsHelpers(client);
2889
+ const generatedTypeScriptFiles = [];
2890
+ generatedTypeScriptFiles.push(...await this.generateClient(client, processedIR, srcDir));
2891
+ generatedTypeScriptFiles.push(...await this.generateAuthStrategies(client, processedIR, srcDir));
2892
+ generatedTypeScriptFiles.push(...await this.generateIndex(client, processedIR, srcDir));
2893
+ generatedTypeScriptFiles.push(...await this.generateUtils(client, processedIR, srcDir));
2894
+ generatedTypeScriptFiles.push(...await this.generateServices(client, processedIR, servicesDir));
2895
+ generatedTypeScriptFiles.push(...await this.generateSchema(client, processedIR, srcDir));
2896
+ generatedTypeScriptFiles.push(...await this.generateZodSchema(client, processedIR, srcDir));
2897
+ await this.generatePackageJson(client);
2898
+ await this.generateTsConfig(client);
2899
+ generatedTypeScriptFiles.push(...await this.generateTsupConfig(client));
2900
+ await this.generateReadme(client, processedIR);
2901
+ const shouldFormat = client.formatCode !== false;
2902
+ if (shouldFormat) {
2903
+ await this.generatePrettierConfig(client);
2904
+ this.logger.debug("Formatting generated TypeScript files with Prettier...", client.packageName);
2905
+ await formatWithPrettier(client.outDir, generatedTypeScriptFiles, this.logger);
2906
+ } else {
2907
+ this.logger.debug("Code formatting is disabled for this client.", client.packageName);
2908
+ }
2909
+ }
2910
+ /**
2911
+ * Pre-process IR to resolve method names and add cached values
2912
+ */
2913
+ async preprocessIR(client, ir) {
2914
+ const processedServices = await Promise.all(ir.services.map(async (service) => ({
2915
+ ...service,
2916
+ operations: await Promise.all(service.operations.map(async (op) => {
2917
+ const methodName = await resolveMethodName(client, op);
2918
+ return {
2919
+ ...op,
2920
+ _resolvedMethodName: methodName
2921
+ };
2922
+ }))
2923
+ })));
2924
+ return {
2925
+ ...ir,
2926
+ services: processedServices
2927
+ };
2928
+ }
2929
+ registerHandlebarsHelpers(_client) {
2930
+ registerCommonHandlebarsHelpers();
2931
+ Handlebars2.registerHelper("methodName", (op) => {
2932
+ return op._resolvedMethodName || deriveMethodName(op);
2933
+ });
2934
+ Handlebars2.registerHelper("queryTypeName", (op) => {
2935
+ const methodName = op._resolvedMethodName || deriveMethodName(op);
2936
+ return toPascalCase(op.tag) + toPascalCase(methodName) + "Query";
2937
+ });
2938
+ Handlebars2.registerHelper("pathTemplate", (op) => {
2939
+ const result = buildPathTemplate(op);
2940
+ return new Handlebars2.SafeString(result);
2941
+ });
2942
+ Handlebars2.registerHelper("queryKeyBase", (op) => {
2943
+ const result = buildQueryKeyBase(op);
2944
+ return new Handlebars2.SafeString(result);
2945
+ });
2946
+ Handlebars2.registerHelper("pathParamsInOrder", (op) => orderPathParams(op));
2947
+ Handlebars2.registerHelper("methodSignature", (op, options) => {
2948
+ const methodName = op._resolvedMethodName || deriveMethodName(op);
2949
+ const modelDefs = options?.data?.root?.IR?.modelDefs || [];
2950
+ const predefinedTypes = options?.data?.root?.PredefinedTypes || [];
2951
+ const isSameFile = options?.data?.root?.isSameFile || false;
2952
+ const signature = buildMethodSignature(op, methodName, modelDefs, predefinedTypes, isSameFile);
2953
+ return signature.map((s) => new Handlebars2.SafeString(s));
2954
+ });
2955
+ Handlebars2.registerHelper("methodSignatureNoInit", (op, options) => {
2956
+ const methodName = op._resolvedMethodName || deriveMethodName(op);
2957
+ const modelDefs = options?.data?.root?.IR?.modelDefs || [];
2958
+ const predefinedTypes = options?.data?.root?.PredefinedTypes || [];
2959
+ const isSameFile = options?.data?.root?.isSameFile || false;
2960
+ const parts = buildMethodSignature(op, methodName, modelDefs, predefinedTypes, isSameFile);
2961
+ return parts.slice(0, -1);
2962
+ });
2963
+ Handlebars2.registerHelper("queryKeyArgs", (op) => {
2964
+ const args = queryKeyArgs(op);
2965
+ return args.map((arg) => new Handlebars2.SafeString(arg));
2966
+ });
2967
+ Handlebars2.registerHelper("queryKeyReturnType", (op, options) => {
2968
+ const methodName = op._resolvedMethodName || deriveMethodName(op);
2969
+ const modelDefs = options?.data?.root?.IR?.modelDefs || [];
2970
+ const predefinedTypes = options?.data?.root?.PredefinedTypes || [];
2971
+ const isSameFile = options?.data?.root?.isSameFile || false;
2972
+ const returnType = buildQueryKeyReturnType(op, methodName, modelDefs, predefinedTypes, isSameFile);
2973
+ return new Handlebars2.SafeString(returnType);
2974
+ });
2975
+ Handlebars2.registerHelper("hasOptionalQueryKeyParams", (op) => {
2976
+ return hasOptionalQueryKeyParams(op);
2977
+ });
2978
+ Handlebars2.registerHelper("hasAnyOptionalQueryKeyParams", (service) => {
2979
+ return service.operations.some((op) => hasOptionalQueryKeyParams(op));
2980
+ });
2981
+ Handlebars2.registerHelper("tsType", (x, options) => {
2982
+ if (x && typeof x === "object" && "kind" in x) {
2983
+ const predefinedTypes = options?.data?.root?.PredefinedTypes || [];
2984
+ const modelDefs = options?.data?.root?.IR?.modelDefs || [];
2985
+ const isSameFile = options?.data?.root?.isSameFile || false;
2986
+ return schemaToTSType(x, predefinedTypes, modelDefs, isSameFile);
2987
+ }
2988
+ return "unknown";
2989
+ });
2990
+ Handlebars2.registerHelper("stripSchemaNs", (s) => s.replace(/^Schema\./, ""));
2991
+ Handlebars2.registerHelper("tsTypeStripNs", (x, options) => {
2992
+ const predefinedTypes = options?.data?.root?.PredefinedTypes || [];
2993
+ const modelDefs = options?.data?.root?.IR?.modelDefs || [];
2994
+ const isSameFile = options?.data?.root?.isSameFile || false;
2995
+ if (x && typeof x === "object" && "kind" in x) {
2996
+ const typeStr = schemaToTSType(x, predefinedTypes, modelDefs, isSameFile);
2997
+ const stripped = typeStr.replace(/Schema\./g, "");
2998
+ return new Handlebars2.SafeString(stripped);
2999
+ }
3000
+ if (typeof x === "string") {
3001
+ if (x.startsWith("Schema.")) {
3002
+ const typeName = x.replace(/^Schema\./, "");
3003
+ const predefinedType = predefinedTypes.find((pt) => pt.type === typeName);
3004
+ if (predefinedType) {
3005
+ return new Handlebars2.SafeString(typeName);
3006
+ }
3007
+ return new Handlebars2.SafeString(typeName);
3008
+ }
3009
+ return new Handlebars2.SafeString(x);
3010
+ }
3011
+ return "unknown";
3012
+ });
3013
+ Handlebars2.registerHelper("isPredefinedType", (typeName, options) => {
3014
+ const predefinedTypes = options?.data?.root?.PredefinedTypes || [];
3015
+ return predefinedTypes.some((pt) => pt.type === typeName);
3016
+ });
3017
+ Handlebars2.registerHelper("getPredefinedType", (typeName, options) => {
3018
+ const predefinedTypes = options?.data?.root?.PredefinedTypes || [];
3019
+ return predefinedTypes.find((pt) => pt.type === typeName);
3020
+ });
3021
+ Handlebars2.registerHelper("groupByPackage", (predefinedTypes) => {
3022
+ const grouped = {};
3023
+ for (const pt of predefinedTypes || []) {
3024
+ if (!grouped[pt.package]) {
3025
+ grouped[pt.package] = {
3026
+ package: pt.package,
3027
+ types: []
3028
+ };
3029
+ }
3030
+ grouped[pt.package]?.types.push(pt.type);
3031
+ }
3032
+ return Object.values(grouped);
3033
+ });
3034
+ Handlebars2.registerHelper("getServicePredefinedTypes", (service, options) => {
3035
+ const predefinedTypes = options?.data?.root?.PredefinedTypes || [];
3036
+ const modelDefs = options?.data?.root?.IR?.modelDefs || [];
3037
+ return collectPredefinedTypesUsedInService(service, predefinedTypes, modelDefs);
3038
+ });
3039
+ Handlebars2.registerHelper("getSchemaPredefinedTypes", (options) => {
3040
+ const predefinedTypes = options?.data?.root?.PredefinedTypes || [];
3041
+ const modelDefs = options?.data?.root?.IR?.modelDefs || [];
3042
+ return collectPredefinedTypesUsedInSchema(modelDefs, predefinedTypes);
3043
+ });
3044
+ Handlebars2.registerHelper("joinTypes", (types) => {
3045
+ return types.join(", ");
3046
+ });
3047
+ Handlebars2.registerHelper("uniquePackages", (predefinedTypes) => {
3048
+ const packages = /* @__PURE__ */ new Set();
3049
+ for (const pt of predefinedTypes || []) {
3050
+ if (pt.package) {
3051
+ packages.add(pt.package);
3052
+ }
3053
+ }
3054
+ return Array.from(packages);
3055
+ });
3056
+ Handlebars2.registerHelper("isPredefinedPackage", (packageName, predefinedTypes) => {
3057
+ if (!predefinedTypes) return false;
3058
+ return predefinedTypes.some((pt) => pt.package === packageName);
3059
+ });
3060
+ Handlebars2.registerHelper("getAllDependencies", (client) => {
3061
+ const deps = {
3062
+ "@blimu/fetch": "^0.4.0",
3063
+ zod: "^4.3.5"
3064
+ };
3065
+ if (client.predefinedTypes) {
3066
+ for (const pt of client.predefinedTypes) {
3067
+ if (pt.package && !deps[pt.package]) {
3068
+ deps[pt.package] = client.dependencies?.[pt.package] || "*";
3069
+ }
3070
+ }
3071
+ }
3072
+ if (client.dependencies) {
3073
+ for (const [pkg, version] of Object.entries(client.dependencies)) {
3074
+ if (pkg !== "zod" && !deps[pkg]) {
3075
+ deps[pkg] = version;
3076
+ }
3077
+ }
3078
+ }
3079
+ return deps;
3080
+ });
3081
+ Handlebars2.registerHelper("decodeHtml", (str) => {
3082
+ if (typeof str !== "string") return str;
3083
+ let decoded = str.replace(/&amp;#x60;/g, "&#x60;").replace(/&amp;#96;/g, "&#96;").replace(/&amp;quot;/g, "&quot;").replace(/&amp;lt;/g, "&lt;").replace(/&amp;gt;/g, "&gt;");
3084
+ decoded = decoded.replace(/&quot;/g, '"').replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&#x60;/g, "`").replace(/&#96;/g, "`").replace(/&amp;/g, "&");
3085
+ return new Handlebars2.SafeString(decoded);
3086
+ });
3087
+ Handlebars2.registerHelper("quotePropName", (name) => quoteTSPropertyName(name));
3088
+ Handlebars2.registerHelper("zodSchema", (x, options) => {
3089
+ if (x && typeof x === "object" && "kind" in x) {
3090
+ const modelDefs = options?.data?.root?.IR?.modelDefs || [];
3091
+ const useLocalSchemaTypes = options?.data?.root?._templateName === "schema.zod.ts.hbs";
3092
+ return new Handlebars2.SafeString(schemaToZodSchema(x, "", modelDefs, useLocalSchemaTypes));
3093
+ }
3094
+ return "z.unknown()";
3095
+ });
3096
+ Handlebars2.registerHelper("isStreaming", (op) => {
3097
+ return isStreamingOperation(op);
3098
+ });
3099
+ Handlebars2.registerHelper("hasBearerScheme", (schemes) => {
3100
+ if (!Array.isArray(schemes)) return false;
3101
+ return schemes.some((s) => s.type === "http" && s.scheme === "bearer");
3102
+ });
3103
+ Handlebars2.registerHelper("hasApiKeyScheme", (schemes) => {
3104
+ if (!Array.isArray(schemes)) return false;
3105
+ return schemes.some((s) => s.type === "apiKey");
3106
+ });
3107
+ Handlebars2.registerHelper("serviceUsesSchema", (service) => {
3108
+ if (!service || !service.operations || !Array.isArray(service.operations)) {
3109
+ return false;
3110
+ }
3111
+ return service.operations.some((op) => {
3112
+ if (op.response?.schema) {
3113
+ const schema = op.response.schema;
3114
+ if (schema.kind === "ref" || schema.kind === "object" || schema.kind === "array" || schema.kind === "oneOf" || schema.kind === "anyOf" || schema.kind === "allOf") {
3115
+ return true;
3116
+ }
3117
+ }
3118
+ if (op.requestBody?.schema) {
3119
+ const schema = op.requestBody.schema;
3120
+ if (schema.kind === "ref" || schema.kind === "object" || schema.kind === "array" || schema.kind === "oneOf" || schema.kind === "anyOf" || schema.kind === "allOf") {
3121
+ return true;
3122
+ }
3123
+ }
3124
+ if (op.queryParams && op.queryParams.length > 0) {
3125
+ return true;
3126
+ }
3127
+ return false;
3128
+ });
3129
+ });
3130
+ Handlebars2.registerHelper("streamingItemType", (op) => {
3131
+ return new Handlebars2.SafeString(getStreamingItemType(op));
3132
+ });
3133
+ }
3134
+ async renderTemplate(templateName, data, outputPath, client) {
3135
+ const overridePath = client.templates?.[templateName];
3136
+ if (overridePath) {
3137
+ this.logger.debug(`Using template override for ${templateName}: ${overridePath}`);
3138
+ try {
3139
+ await fs4.promises.access(overridePath, fs4.constants.R_OK);
3140
+ const templateContent2 = await fs4.promises.readFile(overridePath, "utf-8");
3141
+ const template2 = Handlebars2.compile(templateContent2);
3142
+ const contextWithTemplate2 = {
3143
+ ...data,
3144
+ _templateName: templateName
3145
+ };
3146
+ const rendered2 = template2(contextWithTemplate2);
3147
+ await fs4.promises.writeFile(outputPath, rendered2, "utf-8");
3148
+ return;
3149
+ } catch (error) {
3150
+ const errorMsg = error instanceof Error ? error.message : String(error);
3151
+ this.logger.error(`Template override file not found or not readable: ${overridePath}. Error: ${errorMsg}`);
3152
+ throw new Error(`Template override file not found or not readable: ${overridePath}`);
3153
+ }
3154
+ }
3155
+ const possiblePaths = [
3156
+ path5.join(__dirname, "generator/typescript/templates", templateName),
3157
+ path5.join(__dirname, "templates", templateName),
3158
+ path5.join(__dirname, "../typescript/templates", templateName),
3159
+ path5.join(process.cwd(), "src/generator/typescript/templates", templateName),
3160
+ path5.join(process.cwd(), "packages/codegen/src/generator/typescript/templates", templateName)
3161
+ ];
3162
+ let templatePath = null;
3163
+ for (const possiblePath of possiblePaths) {
3164
+ try {
3165
+ await fs4.promises.access(possiblePath);
3166
+ templatePath = possiblePath;
3167
+ break;
3168
+ } catch {
3169
+ this.logger.debug(`Template not found at: ${possiblePath}`);
3170
+ }
3171
+ }
3172
+ if (!templatePath) {
3173
+ this.logger.error(`Template not found: ${templateName}`);
3174
+ this.logger.error(`Checked paths: ${possiblePaths.join(", ")}`);
3175
+ this.logger.error(`__dirname: ${__dirname}`);
3176
+ throw new Error(`Template not found: ${templateName}`);
3177
+ }
3178
+ const templateContent = await fs4.promises.readFile(templatePath, "utf-8");
3179
+ const template = Handlebars2.compile(templateContent);
3180
+ const contextWithTemplate = {
3181
+ ...data,
3182
+ _templateName: templateName
3183
+ };
3184
+ const rendered = template(contextWithTemplate);
3185
+ const outputDir = path5.dirname(outputPath);
3186
+ await fs4.promises.mkdir(outputDir, {
3187
+ recursive: true
3188
+ });
3189
+ await fs4.promises.writeFile(outputPath, rendered, "utf-8");
3190
+ }
3191
+ async generateClient(client, ir, srcDir) {
3192
+ const clientPath = path5.join(srcDir, "client.ts");
3193
+ if (this.configService.shouldExcludeFile(client, clientPath)) {
3194
+ return [];
3195
+ }
3196
+ try {
3197
+ await this.renderTemplate("client.ts.hbs", {
3198
+ Client: client,
3199
+ IR: ir
3200
+ }, clientPath, client);
3201
+ return [
3202
+ path5.relative(client.outDir, clientPath)
3203
+ ];
3204
+ } catch (error) {
3205
+ const errorMsg = error instanceof Error ? error.message : String(error);
3206
+ this.logger.warn(`Template client.ts.hbs not found: ${errorMsg}, using placeholder`);
3207
+ const content = `// Generated client - template rendering to be implemented`;
3208
+ await fs4.promises.writeFile(clientPath, content, "utf-8");
3209
+ return [
3210
+ path5.relative(client.outDir, clientPath)
3211
+ ];
3212
+ }
3213
+ }
3214
+ async generateAuthStrategies(client, ir, srcDir) {
3215
+ const authStrategiesPath = path5.join(srcDir, "auth-strategies.ts");
3216
+ if (this.configService.shouldExcludeFile(client, authStrategiesPath)) {
3217
+ return [];
3218
+ }
3219
+ try {
3220
+ await this.renderTemplate("auth-strategies.ts.hbs", {
3221
+ Client: client,
3222
+ IR: ir
3223
+ }, authStrategiesPath, client);
3224
+ return [
3225
+ path5.relative(client.outDir, authStrategiesPath)
3226
+ ];
3227
+ } catch {
3228
+ this.logger.warn(`Template auth-strategies.ts.hbs not found, using placeholder`);
3229
+ const content = `// Generated auth-strategies - template rendering to be implemented`;
3230
+ await fs4.promises.writeFile(authStrategiesPath, content, "utf-8");
3231
+ return [
3232
+ path5.relative(client.outDir, authStrategiesPath)
3233
+ ];
3234
+ }
3235
+ }
3236
+ async generateIndex(client, ir, srcDir) {
3237
+ const indexPath = path5.join(srcDir, "index.ts");
3238
+ if (this.configService.shouldExcludeFile(client, indexPath)) {
3239
+ return [];
3240
+ }
3241
+ try {
3242
+ await fs4.promises.access(indexPath);
3243
+ this.logger.debug(`index.ts already exists at ${indexPath}, skipping generation to preserve customizations`);
3244
+ return [];
3245
+ } catch {
3246
+ }
3247
+ try {
3248
+ await this.renderTemplate("index.ts.hbs", {
3249
+ Client: client,
3250
+ IR: ir
3251
+ }, indexPath, client);
3252
+ return [
3253
+ path5.relative(client.outDir, indexPath)
3254
+ ];
3255
+ } catch {
3256
+ this.logger.warn(`Template index.ts.hbs not found, using placeholder`);
3257
+ const content = `// Generated index - template rendering to be implemented`;
3258
+ await fs4.promises.writeFile(indexPath, content, "utf-8");
3259
+ return [
3260
+ path5.relative(client.outDir, indexPath)
3261
+ ];
3262
+ }
3263
+ }
3264
+ async generateUtils(client, ir, srcDir) {
3265
+ const utilsPath = path5.join(srcDir, "utils.ts");
3266
+ if (this.configService.shouldExcludeFile(client, utilsPath)) {
3267
+ return [];
3268
+ }
3269
+ try {
3270
+ await this.renderTemplate("utils.ts.hbs", {
3271
+ Client: client,
3272
+ IR: ir
3273
+ }, utilsPath, client);
3274
+ return [
3275
+ path5.relative(client.outDir, utilsPath)
3276
+ ];
3277
+ } catch {
3278
+ this.logger.warn(`Template utils.ts.hbs not found, using placeholder`);
3279
+ const content = `// Generated utils - template rendering to be implemented`;
3280
+ await fs4.promises.writeFile(utilsPath, content, "utf-8");
3281
+ return [
3282
+ path5.relative(client.outDir, utilsPath)
3283
+ ];
3284
+ }
3285
+ }
3286
+ async generateServices(client, ir, servicesDir) {
3287
+ const generatedFiles = [];
3288
+ for (const service of ir.services) {
3289
+ const servicePath = path5.join(servicesDir, `${toSnakeCase(service.tag).toLowerCase()}.ts`);
3290
+ if (this.configService.shouldExcludeFile(client, servicePath)) {
3291
+ continue;
3292
+ }
3293
+ try {
3294
+ await this.renderTemplate("service.ts.hbs", {
3295
+ Client: client,
3296
+ Service: service,
3297
+ IR: ir,
3298
+ PredefinedTypes: client.predefinedTypes || [],
3299
+ isSameFile: false
3300
+ }, servicePath, client);
3301
+ generatedFiles.push(path5.relative(client.outDir, servicePath));
3302
+ } catch {
3303
+ this.logger.warn(`Template service.ts.hbs not found, using placeholder`);
3304
+ const content = `// Generated service ${service.tag} - template rendering to be implemented`;
3305
+ await fs4.promises.writeFile(servicePath, content, "utf-8");
3306
+ generatedFiles.push(path5.relative(client.outDir, servicePath));
3307
+ }
3308
+ }
3309
+ return generatedFiles;
3310
+ }
3311
+ async generateSchema(client, ir, srcDir) {
3312
+ const schemaPath = path5.join(srcDir, "schema.ts");
3313
+ if (this.configService.shouldExcludeFile(client, schemaPath)) {
3314
+ return [];
3315
+ }
3316
+ try {
3317
+ await this.renderTemplate("schema.ts.hbs", {
3318
+ Client: client,
3319
+ IR: ir,
3320
+ PredefinedTypes: client.predefinedTypes || [],
3321
+ isSameFile: true
3322
+ }, schemaPath, client);
3323
+ return [
3324
+ path5.relative(client.outDir, schemaPath)
3325
+ ];
3326
+ } catch (error) {
3327
+ const errorMsg = error instanceof Error ? error.message : String(error);
3328
+ this.logger.warn(`Template schema.ts.hbs error: ${errorMsg}, using placeholder`);
3329
+ const content = `// Generated schema - template rendering to be implemented`;
3330
+ await fs4.promises.writeFile(schemaPath, content, "utf-8");
3331
+ return [
3332
+ path5.relative(client.outDir, schemaPath)
3333
+ ];
3334
+ }
3335
+ }
3336
+ async generateZodSchema(client, ir, srcDir) {
3337
+ const zodSchemaPath = path5.join(srcDir, "schema.zod.ts");
3338
+ if (this.configService.shouldExcludeFile(client, zodSchemaPath)) {
3339
+ return [];
3340
+ }
3341
+ try {
3342
+ const sortedModelDefs = sortModelDefsByDependencies(ir.modelDefs);
3343
+ const sortedIR = {
3344
+ ...ir,
3345
+ modelDefs: sortedModelDefs
3346
+ };
3347
+ await this.renderTemplate("schema.zod.ts.hbs", {
3348
+ Client: client,
3349
+ IR: sortedIR
3350
+ }, zodSchemaPath, client);
3351
+ return [
3352
+ path5.relative(client.outDir, zodSchemaPath)
3353
+ ];
3354
+ } catch (error) {
3355
+ const errorMsg = error instanceof Error ? error.message : String(error);
3356
+ this.logger.warn(`Template schema.zod.ts.hbs error: ${errorMsg}, using placeholder`);
3357
+ const content = `// Generated Zod schemas - template rendering to be implemented`;
3358
+ await fs4.promises.writeFile(zodSchemaPath, content, "utf-8");
3359
+ return [
3360
+ path5.relative(client.outDir, zodSchemaPath)
3361
+ ];
3362
+ }
3363
+ }
3364
+ async generatePackageJson(client) {
3365
+ const packageJsonPath = path5.join(client.outDir, "package.json");
3366
+ if (this.configService.shouldExcludeFile(client, packageJsonPath)) {
3367
+ return;
3368
+ }
3369
+ try {
3370
+ await this.renderTemplate("package.json.hbs", {
3371
+ Client: client
3372
+ }, packageJsonPath, client);
3373
+ } catch {
3374
+ this.logger.warn(`Template package.json.hbs not found, using fallback`);
3375
+ const content = JSON.stringify({
3376
+ name: client.packageName,
3377
+ version: "0.0.1",
3378
+ main: "dist/index.js",
3379
+ types: "dist/index.d.ts"
3380
+ }, null, 2);
3381
+ await fs4.promises.writeFile(packageJsonPath, content, "utf-8");
3382
+ }
3383
+ }
3384
+ async generateTsConfig(client) {
3385
+ const tsConfigPath = path5.join(client.outDir, "tsconfig.json");
3386
+ if (this.configService.shouldExcludeFile(client, tsConfigPath)) {
3387
+ return;
3388
+ }
3389
+ try {
3390
+ await this.renderTemplate("tsconfig.json.hbs", {
3391
+ Client: client
3392
+ }, tsConfigPath, client);
3393
+ } catch {
3394
+ this.logger.warn(`Template tsconfig.json.hbs not found, using fallback`);
3395
+ const srcDir = client.srcDir || "src";
3396
+ const content = JSON.stringify({
3397
+ compilerOptions: {
3398
+ target: "ES2020",
3399
+ module: "commonjs",
3400
+ lib: [
3401
+ "ES2020"
3402
+ ],
3403
+ declaration: true,
3404
+ outDir: "./dist",
3405
+ rootDir: `./${srcDir}`,
3406
+ strict: true,
3407
+ esModuleInterop: true,
3408
+ skipLibCheck: true,
3409
+ forceConsistentCasingInFileNames: true
3410
+ },
3411
+ include: [
3412
+ `${srcDir}/**/*`
3413
+ ]
3414
+ }, null, 2);
3415
+ const outputDir = path5.dirname(tsConfigPath);
3416
+ await fs4.promises.mkdir(outputDir, {
3417
+ recursive: true
3418
+ });
3419
+ await fs4.promises.writeFile(tsConfigPath, content, "utf-8");
3420
+ }
3421
+ }
3422
+ async generateTsupConfig(client) {
3423
+ const tsupConfigPath = path5.join(client.outDir, "tsup.config.ts");
3424
+ if (this.configService.shouldExcludeFile(client, tsupConfigPath)) {
3425
+ return [];
3426
+ }
3427
+ try {
3428
+ await this.renderTemplate("tsup.config.ts.hbs", {
3429
+ Client: client
3430
+ }, tsupConfigPath, client);
3431
+ } catch {
3432
+ this.logger.warn(`Template tsup.config.ts.hbs not found, using fallback`);
3433
+ const srcDir = client.srcDir || "src";
3434
+ const content = `import { defineConfig } from "tsup";
5
3435
 
6
3436
  export default defineConfig({
7
- entry: ["${e.srcDir||"src"}/**/*.ts"],
3437
+ entry: ["${srcDir}/index.ts"],
8
3438
  format: ["cjs", "esm"],
9
3439
  dts: {
10
3440
  resolve: true,
@@ -16,7 +3446,208 @@ export default defineConfig({
16
3446
  tsconfig: "./tsconfig.json",
17
3447
  external: [],
18
3448
  });
19
- `;await w.promises.writeFile(t,o,"utf-8")}return["tsup.config.ts"]}async generateReadme(e,t){let r=g.join(e.outDir,"README.md");if(!this.configService.shouldExcludeFile(e,r))try{await this.renderTemplate("README.md.hbs",{Client:e,IR:t},r,e)}catch{this.logger.warn("Template README.md.hbs not found, using fallback");let o=`# ${e.name}
3449
+ `;
3450
+ await fs4.promises.writeFile(tsupConfigPath, content, "utf-8");
3451
+ }
3452
+ return [
3453
+ "tsup.config.ts"
3454
+ ];
3455
+ }
3456
+ async generateReadme(client, ir) {
3457
+ const readmePath = path5.join(client.outDir, "README.md");
3458
+ if (this.configService.shouldExcludeFile(client, readmePath)) {
3459
+ return;
3460
+ }
3461
+ try {
3462
+ await this.renderTemplate("README.md.hbs", {
3463
+ Client: client,
3464
+ IR: ir
3465
+ }, readmePath, client);
3466
+ } catch {
3467
+ this.logger.warn(`Template README.md.hbs not found, using fallback`);
3468
+ const content = `# ${client.name}
3469
+
3470
+ Generated SDK from OpenAPI specification.`;
3471
+ await fs4.promises.writeFile(readmePath, content, "utf-8");
3472
+ }
3473
+ }
3474
+ async generatePrettierConfig(client) {
3475
+ const prettierConfigPath = path5.join(client.outDir, ".prettierrc");
3476
+ if (this.configService.shouldExcludeFile(client, prettierConfigPath)) {
3477
+ return;
3478
+ }
3479
+ try {
3480
+ await this.renderTemplate(".prettierrc.hbs", {
3481
+ Client: client
3482
+ }, prettierConfigPath, client);
3483
+ } catch {
3484
+ this.logger.warn(`Template .prettierrc.hbs not found, using fallback`);
3485
+ const content = JSON.stringify({
3486
+ semi: true,
3487
+ trailingComma: "es5",
3488
+ singleQuote: true,
3489
+ printWidth: 80,
3490
+ tabWidth: 2,
3491
+ useTabs: false
3492
+ }, null, 2);
3493
+ await fs4.promises.writeFile(prettierConfigPath, content, "utf-8");
3494
+ }
3495
+ }
3496
+ };
3497
+ TypeScriptGeneratorService = _ts_decorate8([
3498
+ Injectable6(),
3499
+ _ts_metadata3("design:type", Function),
3500
+ _ts_metadata3("design:paramtypes", [
3501
+ typeof ConfigService === "undefined" ? Object : ConfigService
3502
+ ])
3503
+ ], TypeScriptGeneratorService);
3504
+
3505
+ // src/generator/generator.module.ts
3506
+ function _ts_decorate9(decorators, target, key, desc) {
3507
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3508
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
3509
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
3510
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
3511
+ }
3512
+ __name(_ts_decorate9, "_ts_decorate");
3513
+ function _ts_metadata4(k, v) {
3514
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
3515
+ }
3516
+ __name(_ts_metadata4, "_ts_metadata");
3517
+ var GeneratorModule = class {
3518
+ static {
3519
+ __name(this, "GeneratorModule");
3520
+ }
3521
+ generatorService;
3522
+ typeScriptGenerator;
3523
+ constructor(generatorService, typeScriptGenerator) {
3524
+ this.generatorService = generatorService;
3525
+ this.typeScriptGenerator = typeScriptGenerator;
3526
+ this.generatorService.register(this.typeScriptGenerator);
3527
+ }
3528
+ };
3529
+ GeneratorModule = _ts_decorate9([
3530
+ Module3({
3531
+ imports: [
3532
+ OpenApiModule,
3533
+ ConfigModule
3534
+ ],
3535
+ providers: [
3536
+ GeneratorService,
3537
+ IrBuilderService,
3538
+ SchemaConverterService,
3539
+ TypeScriptGeneratorService
3540
+ ],
3541
+ exports: [
3542
+ GeneratorService,
3543
+ IrBuilderService,
3544
+ SchemaConverterService
3545
+ ]
3546
+ }),
3547
+ _ts_metadata4("design:type", Function),
3548
+ _ts_metadata4("design:paramtypes", [
3549
+ typeof GeneratorService === "undefined" ? Object : GeneratorService,
3550
+ typeof TypeScriptGeneratorService === "undefined" ? Object : TypeScriptGeneratorService
3551
+ ])
3552
+ ], GeneratorModule);
20
3553
 
21
- Generated SDK from OpenAPI specification.`;await w.promises.writeFile(r,o,"utf-8")}}async generatePrettierConfig(e){let t=g.join(e.outDir,".prettierrc");if(!this.configService.shouldExcludeFile(e,t))try{await this.renderTemplate(".prettierrc.hbs",{Client:e},t,e)}catch{this.logger.warn("Template .prettierrc.hbs not found, using fallback");let n=JSON.stringify({semi:!0,trailingComma:"es5",singleQuote:!0,printWidth:80,tabWidth:2,useTabs:!1},null,2);await w.promises.writeFile(t,n,"utf-8")}}};R=it([ct(),He("design:type",Function),He("design:paramtypes",[typeof k>"u"?Object:k])],R);function pt(s,e,t,r){var n=arguments.length,o=n<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(s,e,t,r);else for(var i=s.length-1;i>=0;i--)(a=s[i])&&(o=(n<3?a(o):n>3?a(e,t,o):a(e,t))||o);return n>3&&o&&Object.defineProperty(e,t,o),o}p(pt,"_ts_decorate");function $e(s,e){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(s,e)}p($e,"_ts_metadata");var Y=class{static{p(this,"GeneratorModule")}generatorService;typeScriptGenerator;constructor(e,t){this.generatorService=e,this.typeScriptGenerator=t,this.generatorService.register(this.typeScriptGenerator)}};Y=pt([lt({imports:[M,B],providers:[j,x,v,R],exports:[j,x,v]}),$e("design:type",Function),$e("design:paramtypes",[typeof j>"u"?Object:j,typeof R>"u"?Object:R])],Y);import*as E from"path";import*as De from"fs";import{exec as ut}from"child_process";import{promisify as mt}from"util";var dt=mt(ut);async function mn(s,e){let t,r;if(typeof s=="string"){let u=E.isAbsolute(s)?s:E.resolve(s);t=await W(u),r=e?.baseDir??E.dirname(u)}else t=s,r=e?.baseDir;let n=new k,o=new P,a=new v,i=new x(a),c=new j(i,o),f=new R(n);c.register(f);for(let u of t.clients){if(e?.client&&u.name!==e.client)continue;let b=r?E.resolve(r,u.outDir):E.resolve(u.outDir);await De.promises.mkdir(b,{recursive:!0});let d={...u,outDir:b};await ht(n,d),await c.generate(t.spec,d),await gt(n,d)}}p(mn,"generate");async function ht(s,e){let t=s.getPreCommand(e);t.length!==0&&await Ae(t,e.outDir,"pre-command")}p(ht,"executePreCommands");async function gt(s,e){let t=s.getPostCommand(e);t.length!==0&&await Ae(t,e.outDir,"post-command")}p(gt,"executePostCommands");async function Ae(s,e,t){try{let[r,...n]=s,{stdout:o,stderr:a}=await dt(`${r} ${n.map(i=>`"${i}"`).join(" ")}`,{cwd:e,maxBuffer:10*1024*1024});o&&console.debug(`[${t}] ${o}`),a&&!a.includes("warning")&&console.warn(`[${t}] ${a}`)}catch(r){let n=r instanceof Error?r.message:String(r);throw new Error(`${t} failed: ${n}`)}}p(Ae,"executeCommand");async function dn(s){return W(s)}p(dn,"loadConfig");export{Ne as ClientSchema,B as ConfigModule,J as ConfigSchema,k as ConfigService,Y as GeneratorModule,j as GeneratorService,l as IRSchemaKind,M as OpenApiModule,P as OpenApiService,qe as PredefinedTypeSchema,ie as TYPESCRIPT_TEMPLATE_NAMES,Fe as TypeScriptClientSchema,At as defineConfig,mn as generate,dn as loadConfig,W as loadMjsConfig};
3554
+ // src/api/generate.ts
3555
+ import * as path6 from "path";
3556
+ import * as fs5 from "fs";
3557
+ import { exec as exec2 } from "child_process";
3558
+ import { promisify as promisify2 } from "util";
3559
+ var execAsync2 = promisify2(exec2);
3560
+ async function generate(configOrPath, options) {
3561
+ let config;
3562
+ let baseDir;
3563
+ if (typeof configOrPath === "string") {
3564
+ const configPath = path6.isAbsolute(configOrPath) ? configOrPath : path6.resolve(configOrPath);
3565
+ config = await loadMjsConfig(configPath);
3566
+ baseDir = options?.baseDir ?? path6.dirname(configPath);
3567
+ } else {
3568
+ config = configOrPath;
3569
+ baseDir = options?.baseDir;
3570
+ }
3571
+ const configService = new ConfigService();
3572
+ const openApiService = new OpenApiService();
3573
+ const schemaConverter = new SchemaConverterService();
3574
+ const irBuilder = new IrBuilderService(schemaConverter);
3575
+ const generatorService = new GeneratorService(irBuilder, openApiService);
3576
+ const typeScriptGenerator = new TypeScriptGeneratorService(configService);
3577
+ generatorService.register(typeScriptGenerator);
3578
+ for (const client of config.clients) {
3579
+ if (options?.client && client.name !== options.client) {
3580
+ continue;
3581
+ }
3582
+ const resolvedOutDir = baseDir ? path6.resolve(baseDir, client.outDir) : path6.resolve(client.outDir);
3583
+ await fs5.promises.mkdir(resolvedOutDir, {
3584
+ recursive: true
3585
+ });
3586
+ const clientWithResolvedPath = {
3587
+ ...client,
3588
+ outDir: resolvedOutDir
3589
+ };
3590
+ await executePreCommands(configService, clientWithResolvedPath);
3591
+ await generatorService.generate(config.spec, clientWithResolvedPath);
3592
+ await executePostCommands(configService, clientWithResolvedPath);
3593
+ }
3594
+ }
3595
+ __name(generate, "generate");
3596
+ async function executePreCommands(configService, client) {
3597
+ const command = configService.getPreCommand(client);
3598
+ if (command.length === 0) {
3599
+ return;
3600
+ }
3601
+ await executeCommand(command, client.outDir, "pre-command");
3602
+ }
3603
+ __name(executePreCommands, "executePreCommands");
3604
+ async function executePostCommands(configService, client) {
3605
+ const command = configService.getPostCommand(client);
3606
+ if (command.length === 0) {
3607
+ return;
3608
+ }
3609
+ await executeCommand(command, client.outDir, "post-command");
3610
+ }
3611
+ __name(executePostCommands, "executePostCommands");
3612
+ async function executeCommand(command, cwd, label) {
3613
+ try {
3614
+ const [cmd, ...args] = command;
3615
+ const { stdout, stderr } = await execAsync2(`${cmd} ${args.map((a) => `"${a}"`).join(" ")}`, {
3616
+ cwd,
3617
+ maxBuffer: 10 * 1024 * 1024
3618
+ });
3619
+ if (stdout) {
3620
+ console.debug(`[${label}] ${stdout}`);
3621
+ }
3622
+ if (stderr && !stderr.includes("warning")) {
3623
+ console.warn(`[${label}] ${stderr}`);
3624
+ }
3625
+ } catch (error) {
3626
+ const errorMessage = error instanceof Error ? error.message : String(error);
3627
+ throw new Error(`${label} failed: ${errorMessage}`);
3628
+ }
3629
+ }
3630
+ __name(executeCommand, "executeCommand");
3631
+ async function loadConfig(configPath) {
3632
+ return loadMjsConfig(configPath);
3633
+ }
3634
+ __name(loadConfig, "loadConfig");
3635
+ export {
3636
+ ClientSchema,
3637
+ ConfigModule,
3638
+ ConfigSchema,
3639
+ ConfigService,
3640
+ GeneratorModule,
3641
+ GeneratorService,
3642
+ IRSchemaKind,
3643
+ OpenApiModule,
3644
+ OpenApiService,
3645
+ PredefinedTypeSchema,
3646
+ TYPESCRIPT_TEMPLATE_NAMES,
3647
+ TypeScriptClientSchema,
3648
+ defineConfig,
3649
+ generate,
3650
+ loadConfig,
3651
+ loadMjsConfig
3652
+ };
22
3653
  //# sourceMappingURL=index.mjs.map