@ahoo-wang/fetcher-generator 2.12.1 → 2.12.2
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/aggregate/aggregateResolver.d.ts.map +1 -1
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/client/apiClientGenerator.d.ts.map +1 -1
- package/dist/index.cjs +7 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +234 -234
- package/dist/index.js.map +1 -1
- package/dist/utils/operations.d.ts +5 -1
- package/dist/utils/operations.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aggregateResolver.d.ts","sourceRoot":"","sources":["../../src/aggregate/aggregateResolver.ts"],"names":[],"mappings":"AAaA,OAAO,EACL,OAAO,EACP,SAAS,EAKV,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAEL,wBAAwB,EAGzB,MAAM,aAAa,CAAC;AAGrB,OAAO,
|
|
1
|
+
{"version":3,"file":"aggregateResolver.d.ts","sourceRoot":"","sources":["../../src/aggregate/aggregateResolver.ts"],"names":[],"mappings":"AAaA,OAAO,EACL,OAAO,EACP,SAAS,EAKV,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAEL,wBAAwB,EAGzB,MAAM,aAAa,CAAC;AAGrB,OAAO,EAQL,eAAe,EAChB,MAAM,UAAU,CAAC;AAOlB;;;GAGG;AACH,qBAAa,iBAAiB;IAUhB,OAAO,CAAC,QAAQ,CAAC,OAAO;IATpC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAGzB;IAEF;;;OAGG;gBAC0B,OAAO,EAAE,OAAO;IAK7C;;;OAGG;IACH,OAAO,CAAC,KAAK;IAUb;;;OAGG;IACH,OAAO,IAAI,wBAAwB;IAoBnC;;;;OAIG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,eAAe,EAAE,eAAe;IAoEvD;;;OAGG;IACH,KAAK,CAAC,SAAS,EAAE,SAAS;IAqB1B;;;OAGG;IACH,MAAM,CAAC,SAAS,EAAE,SAAS;IAkD3B;;;OAGG;IACH,MAAM,CAAC,SAAS,EAAE,SAAS,GAAG,IAAI;CA4BnC"}
|
package/dist/cli.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("commander"),a=require("./index.cjs");require("@ahoo-wang/fetcher");require("yaml");require("fs");require("path");class f{getTimestamp(){return new Date().toTimeString().slice(0,8)}info(e,...t){const o=this.getTimestamp();t.length>0?console.log(`[${o}] ℹ️ ${e}`,...t):console.log(`[${o}] ℹ️ ${e}`)}success(e,...t){const o=this.getTimestamp();t.length>0?console.log(`[${o}] ✅ ${e}`,...t):console.log(`[${o}] ✅ ${e}`)}error(e,...t){const o=this.getTimestamp();t.length>0?console.error(`[${o}] ❌ ${e}`,...t):console.error(`[${o}] ❌ ${e}`)}progress(e,t=0,...o){const i=this.getTimestamp(),r=" ".repeat(t);o.length>0?console.log(`[${i}] 🔄 ${r}${e}`,...o):console.log(`[${i}] 🔄 ${r}${e}`)}progressWithCount(e,t,o,i=0,...r){const s=this.getTimestamp(),p=" ".repeat(i),l=`[${e}/${t}]`;r.length>0?console.log(`[${s}] 🔄 ${p}${l} ${o}`,...r):console.log(`[${s}] 🔄 ${p}${l} ${o}`)}}function h(n){if(!n)return!1;try{const e=new URL(n);return e.protocol==="http:"||e.protocol==="https:"}catch{return n.length>0}}async function $(n){const e=new f;process.on("SIGINT",()=>{e.error("Generation interrupted by user"),process.exit(130)}),h(n.input)||(e.error("Invalid input: must be a valid file path or HTTP/HTTPS URL"),process.exit(2));try{e.info("Starting code generation...");const t={inputPath:n.input,outputDir:n.output,configPath:n.config,tsConfigFilePath:n.tsConfigFilePath,logger:e};await new a.CodeGenerator(t).generate(),e.success(`Code generation completed successfully! Files generated in: ${n.output}`)}catch(t){e.error(`Error during code generation: ${t}`),process.exit(1)}}const d="2.12.
|
|
2
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("commander"),a=require("./index.cjs");require("@ahoo-wang/fetcher");require("yaml");require("fs");require("path");class f{getTimestamp(){return new Date().toTimeString().slice(0,8)}info(e,...t){const o=this.getTimestamp();t.length>0?console.log(`[${o}] ℹ️ ${e}`,...t):console.log(`[${o}] ℹ️ ${e}`)}success(e,...t){const o=this.getTimestamp();t.length>0?console.log(`[${o}] ✅ ${e}`,...t):console.log(`[${o}] ✅ ${e}`)}error(e,...t){const o=this.getTimestamp();t.length>0?console.error(`[${o}] ❌ ${e}`,...t):console.error(`[${o}] ❌ ${e}`)}progress(e,t=0,...o){const i=this.getTimestamp(),r=" ".repeat(t);o.length>0?console.log(`[${i}] 🔄 ${r}${e}`,...o):console.log(`[${i}] 🔄 ${r}${e}`)}progressWithCount(e,t,o,i=0,...r){const s=this.getTimestamp(),p=" ".repeat(i),l=`[${e}/${t}]`;r.length>0?console.log(`[${s}] 🔄 ${p}${l} ${o}`,...r):console.log(`[${s}] 🔄 ${p}${l} ${o}`)}}function h(n){if(!n)return!1;try{const e=new URL(n);return e.protocol==="http:"||e.protocol==="https:"}catch{return n.length>0}}async function $(n){const e=new f;process.on("SIGINT",()=>{e.error("Generation interrupted by user"),process.exit(130)}),h(n.input)||(e.error("Invalid input: must be a valid file path or HTTP/HTTPS URL"),process.exit(2));try{e.info("Starting code generation...");const t={inputPath:n.input,outputDir:n.output,configPath:n.config,tsConfigFilePath:n.tsConfigFilePath,logger:e};await new a.CodeGenerator(t).generate(),e.success(`Code generation completed successfully! Files generated in: ${n.output}`)}catch(t){e.error(`Error during code generation: ${t}`),process.exit(1)}}const d="2.12.2",m={version:d};function u(){return c.program.name("fetcher-generator").description("OpenAPI Specification TypeScript code generator for Wow").version(m.version,"-v, --version"),c.program.command("generate").description("Generate TypeScript code from OpenAPI specification").requiredOption("-i, --input <file>","Input OpenAPI specification file path or URL (http/https)").option("-o, --output <path>","Output directory path","src/generated").option("-c, --config <file>","Configuration file path",a.DEFAULT_CONFIG_PATH).option("-t, --ts-config-file-path <file>","TypeScript configuration file path").action($),c.program}function g(){u().parse()}g();exports.runCLI=g;exports.setupCLI=u;
|
|
3
3
|
//# sourceMappingURL=cli.cjs.map
|
package/dist/cli.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apiClientGenerator.d.ts","sourceRoot":"","sources":["../../src/client/apiClientGenerator.ts"],"names":[],"mappings":"AA2BA,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"apiClientGenerator.d.ts","sourceRoot":"","sources":["../../src/client/apiClientGenerator.ts"],"names":[],"mappings":"AA2BA,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAgChE;;;GAGG;AACH,qBAAa,kBAAmB,YAAW,SAAS;aAUtB,OAAO,EAAE,eAAe;IATpD,OAAO,CAAC,2BAA2B,CAAsB;IACzD,OAAO,CAAC,iBAAiB,CAAuB;IAEhD,OAAO,CAAC,QAAQ,CAAC,0BAA0B,CAAqB;IAEhE;;;OAGG;gBACyB,OAAO,EAAE,eAAe;IAMpD;;;OAGG;IACH,QAAQ;IAgBR;;;;OAIG;IACH,OAAO,CAAC,kBAAkB;IAoB1B;;;;OAIG;IACH,OAAO,CAAC,mBAAmB;IAU3B;;;;OAIG;IACH,OAAO,CAAC,iBAAiB;IAyBzB;;;;;OAKG;IACH,OAAO,CAAC,aAAa;IAarB;;;;;OAKG;IACH,OAAO,CAAC,kBAAkB;IAuD1B;;;;;;OAMG;IACH,OAAO,CAAC,iBAAiB;IA6DzB;;;;;OAKG;IACH,OAAO,CAAC,uBAAuB;IAiC/B;;;;;OAKG;IACH,OAAO,CAAC,iBAAiB;IA0DzB;;;;;;OAMG;IACH,OAAO,CAAC,gBAAgB;IAkDxB;;;;OAIG;IACH,OAAO,CAAC,eAAe;IAmCvB,OAAO,CAAC,eAAe;IAIvB;;;;OAIG;IACH,OAAO,CAAC,cAAc;IAoCtB,OAAO,CAAC,cAAc;CAUvB"}
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const x=require("ts-morph"),h=require("@ahoo-wang/fetcher"),
|
|
2
|
-
`){if(!Array.isArray(n))return;const t=n.filter(o=>typeof o=="string"&&o.length>0);return t.length>0?t.join(e):void 0}function v(n,e){const t=
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const x=require("ts-morph"),h=require("@ahoo-wang/fetcher"),j=require("@ahoo-wang/fetcher-wow"),Q=require("yaml"),me=require("fs"),_=require("path");function C(n){return n.$ref.split("/").pop()}function I(n,e){const t=C(n);return e.schemas?.[t]}function J(n,e){const t=C(n);return e.requestBodies?.[t]}function V(n,e){const t=C(n);return e.parameters?.[t]}function $(n,e){return{key:C(n),schema:I(n,e)}}const fe=/[-_\s.]+/;function w(n){return n.split(fe)}function H(n){return Array.isArray(n)?n.flatMap(e=>G(w(e))):G(w(n))}function G(n){return n.flatMap(e=>{if(e.length===0)return[];const t=[];let o="";for(let r=0;r<e.length;r++){const i=e[r],s=/[A-Z]/.test(i),a=r>0&&/[a-z]/.test(e[r-1]);s&&a&&o?(t.push(o),o=i):o+=i}return o&&t.push(o),t})}function S(n){return n===""||n.length===0?"":H(n).filter(t=>t.length>0).map(t=>{const o=t.charAt(0),r=t.slice(1);return(/[a-zA-Z]/.test(o)?o.toUpperCase():o)+r.toLowerCase()}).join("")}function y(n){const e=S(n);return e.charAt(0).toLowerCase()+e.slice(1)}function he(n){return n===""||Array.isArray(n)&&n.length===0?"":H(n).filter(t=>t.length>0).map(t=>t.toUpperCase()).join("_")}function g(n){return!!(n&&typeof n=="object"&&"$ref"in n)}function q(n,e){if(e&&!g(e)&&e.content)return e.content[n]?.schema}function K(n){return q(h.ContentTypeValues.APPLICATION_JSON,n)}function ye(n){return q(h.ContentTypeValues.TEXT_EVENT_STREAM,n)}function xe(n){return q("*/*",n)}const Ae=["string","number","integer","boolean","null"];function Y(n){return Array.isArray(n)?!0:Ae.includes(n)}function W(n){return Array.isArray(n.enum)&&n.enum.length>0}function R(n){return n.type==="object"&&!!n.properties}function O(n){return n.type==="array"&&!!n.items}function Pe(n){return Array.isArray(n.anyOf)&&n.anyOf.length>0}function Ce(n){return Array.isArray(n.oneOf)&&n.oneOf.length>0}function D(n){return Array.isArray(n.allOf)&&n.allOf.length>0}function k(n){return Pe(n)||Ce(n)||D(n)}function $e(n){return n.includes("|")||n.includes("&")?`(${n})[]`:`${n}[]`}function Te(n){return n.type==="object"&&!n.properties&&n.additionalProperties!==void 0}function Ie(n){return n.type!=="object"?!1:n.properties?Object.keys(n.properties).length===0:!0}function E(n){if(Array.isArray(n))return n.map(e=>E(e)).join(" | ");switch(n){case"string":return"string";case"number":case"integer":return"number";case"boolean":return"boolean";case"null":return"null";default:return"any"}}function X(n){const e=[];for(const[t,o]of Object.entries(n))Z(o).forEach(r=>{e.push({method:r.method,operation:r.operation,path:t})});return e.sort((t,o)=>t.operation.operationId.localeCompare(o.method))}function Z(n){return[{method:"get",operation:n.get},{method:"put",operation:n.put},{method:"post",operation:n.post},{method:"delete",operation:n.delete},{method:"options",operation:n.options},{method:"head",operation:n.head},{method:"patch",operation:n.patch},{method:"trace",operation:n.trace}].filter(({operation:e})=>e!==void 0)}function M(n){return n.responses[200]}function L(n){const e=M(n);return K(e)}function ve(n,e){return n.parameters?n.parameters.map(t=>g(t)?V(t,e):t).filter(t=>t.in==="path"):[]}const Se="string";function ee(n){return!n.schema||g(n.schema)||!n.schema.type||!Y(n.schema.type)?Se:E(n.schema.type)}function te(n){return n.startsWith("http://")||n.startsWith("https://")?Ee(n):Re(n)}async function Ee(n){return await(await fetch(n)).text()}function Re(n){return new Promise((e,t)=>{me.readFile(n,"utf-8",(o,r)=>{o?t(o):e(r)})})}async function we(n){const e=await te(n);switch(ne(e)){case"json":return JSON.parse(e);case"yaml":return Q.parse(e);default:throw new Error(`Unsupported file format: ${n}`)}}async function Oe(n){const e=await te(n);switch(ne(e)){case"json":return JSON.parse(e);case"yaml":return Q.parse(e);default:throw new Error(`Unsupported file format: ${n}`)}}function ne(n){const e=n.trimStart();if(e.startsWith("{")||e.startsWith("["))return"json";if(e.startsWith("-")||e.startsWith("%YAML"))return"yaml";try{return JSON.parse(e),"json"}catch{if(e.length>0)return"yaml"}throw new Error("Unable to infer file format")}const oe="types.ts",De="@";function Ne(n){return h.combineURLs(n.path,oe)}function re(n,e,t){const o=h.combineURLs(e,t),r=n.getSourceFile(o);return r||n.createSourceFile(o,"",{overwrite:!0})}function A(n,e,t){let o=n.getImportDeclaration(r=>r.getModuleSpecifierValue()===e);o||(o=n.addImportDeclaration({moduleSpecifier:e})),t.forEach(r=>{o.getNamedImports().some(s=>s.getName()===r)||o.addNamedImport(r)})}function f(n,e,t){if(t.path.startsWith(De)){A(n,t.path,[t.name]);return}const o=n.getDirectoryPath(),r=_.join(e,t.path,oe);let i=_.relative(o,r);i=i.replace(/\.ts$/,""),i.startsWith(".")||(i="./"+i),A(n,i,[t.name])}function be(n,e,t,o){n.path!==o.path&&f(e,t,o)}function ie(n,e=`
|
|
2
|
+
`){if(!Array.isArray(n))return;const t=n.filter(o=>typeof o=="string"&&o.length>0);return t.length>0?t.join(e):void 0}function v(n,e){const t=ie(e);t&&n.addJsDoc(t)}function se(n,e){const t=[n.title,n.description];return e&&t.push(`- key: ${e}`),n.format&&t.push(`- format: ${n.format}`),B(t,n,"default"),B(t,n,"example"),qe(t,n),Me(t,n),Fe(t,n),t}function z(n,e,t){const o=se(e,t);v(n,o)}function B(n,e,t){const o=e[t];if(o){if(typeof o!="object"){n.push(`- ${t}: \`${o}\``);return}n.push(`- ${t}: `),n.push("```json"),n.push(JSON.stringify(o)),n.push("```")}}function qe(n,e){const t=["- Numeric Constraints"];e.minimum!==void 0&&t.push(` - minimum: ${e.minimum}`),e.maximum!==void 0&&t.push(` - maximum: ${e.maximum}`),e.exclusiveMinimum!==void 0&&t.push(` - exclusiveMinimum: ${e.exclusiveMinimum}`),e.exclusiveMaximum!==void 0&&t.push(` - exclusiveMaximum: ${e.exclusiveMaximum}`),e.multipleOf!==void 0&&t.push(` - multipleOf: ${e.multipleOf}`),t.length!==1&&n.push(...t)}function Me(n,e){const t=["- String Constraints"];e.minLength!==void 0&&t.push(` - minLength: ${e.minLength}`),e.maxLength!==void 0&&t.push(` - maxLength: ${e.maxLength}`),e.pattern!==void 0&&t.push(` - pattern: ${e.pattern}`),t.length!==1&&n.push(...t)}function Fe(n,e){const t=["- Array Constraints"];e.minItems!==void 0&&t.push(` - minItems: ${e.minItems}`),e.maxItems!==void 0&&t.push(` - maxItems: ${e.maxItems}`),e.uniqueItems!==void 0&&t.push(` - uniqueItems: ${e.uniqueItems}`),t.length!==1&&n.push(...t)}function je(n){const e=n.split(".");return e.length!=2||e[0].length===0||e[1].length===0?null:e}function _e(n){const e=je(n.name);return e?{tag:n,contextAlias:e[0],aggregateName:e[1]}:null}function Ge(n){const e=n?.map(o=>_e(o)).filter(o=>o!==null);if(!e)return new Map;const t=new Map;return e.forEach(o=>{t.set(o.tag.name,{aggregate:o,commands:new Map,events:new Map})}),t}function We(n){if(!n)return null;const e=n.split(".");return e.length!=3?null:e[2]}const ke="#/components/responses/wow.CommandOk",Le="#/components/parameters/wow.id";class ze{constructor(e){this.openAPI=e,this.aggregates=Ge(e.tags),this.build()}aggregates;build(){const e=X(this.openAPI.paths);for(const t of e)this.commands(t.path,t),this.state(t.operation),this.events(t.operation),this.fields(t.operation)}resolve(){const e=new Map;for(const t of this.aggregates.values()){if(!t.state||!t.fields)continue;const o=t.aggregate.contextAlias;let r=e.get(o);r||(r=new Set,e.set(o,r)),r.add(t)}return e}commands(e,t){const o=t.operation;if(o.operationId==="wow.command.send")return;const r=We(o.operationId);if(!r)return;const i=M(o);if(!i||!g(i)||i.$ref!==ke||!o.requestBody)return;const s=o.parameters??[],a=s.filter(d=>g(d)&&d.$ref===Le).at(0),c=s.filter(d=>!g(d)&&d.in==="path");if(a){const d=V(a,this.openAPI.components);c.push(d)}const l=o.requestBody.content[h.ContentTypeValues.APPLICATION_JSON].schema,p=$(l,this.openAPI.components);p.schema.title=p.schema.title||o.summary,p.schema.description=p.schema.description||o.description;const de={name:r,method:t.method,path:e,pathParameters:c,summary:o.summary,description:o.description,schema:p,operation:o};o.tags?.forEach(d=>{const F=this.aggregates.get(d);F&&F.commands.set(r,de)})}state(e){if(!e.operationId?.endsWith(".snapshot_state.single"))return;const t=L(e);if(!g(t))return;const o=$(t,this.openAPI.components);e.tags?.forEach(r=>{const i=this.aggregates.get(r);i&&(i.state=o)})}events(e){if(!this.openAPI.components||!e.operationId?.endsWith(".event.list_query"))return;const t=L(e);if(g(t))return;const o=t?.items;if(!g(o))return;const i=I(o,this.openAPI.components).properties.body.items.anyOf.map(s=>{const a=s.title,c=s.properties.name.const,u=s.properties.body,l=$(u,this.openAPI.components);return l.schema.title=l.schema.title||s.title,{title:a,name:c,schema:l}});e.tags?.forEach(s=>{const a=this.aggregates.get(s);a&&i.forEach(c=>{a.events.set(c.name,c)})})}fields(e){if(!this.openAPI.components||!e.operationId?.endsWith(".snapshot.count"))return;const o=J(e.requestBody,this.openAPI.components).content[h.ContentTypeValues.APPLICATION_JSON].schema,i=I(o,this.openAPI.components).properties?.field,s=$(i,this.openAPI.components);e.tags?.forEach(a=>{const c=this.aggregates.get(a);c&&(c.fields=s)})}}const P="@ahoo-wang/fetcher-wow",Be={"wow.command.CommandResult":"CommandResult","wow.command.CommandResultArray":"CommandResultArray","wow.MessageHeaderSqlType":"MessageHeaderSqlType","wow.api.BindingError":"BindingError","wow.api.DefaultErrorInfo":"ErrorInfo","wow.api.RecoverableType":"RecoverableType","wow.api.command.DefaultDeleteAggregate":"DeleteAggregate","wow.api.command.DefaultRecoverAggregate":"RecoverAggregate","wow.api.messaging.FunctionInfoData":"FunctionInfo","wow.api.messaging.FunctionKind":"FunctionKind","wow.api.modeling.AggregateId":"AggregateId","wow.api.query.Condition":"Condition","wow.api.query.ConditionOptions":"ConditionOptions","wow.api.query.ListQuery":"ListQuery","wow.api.query.Operator":"Operator","wow.api.query.PagedQuery":"PagedQuery","wow.api.query.Pagination":"Pagination","wow.api.query.Projection":"Projection","wow.api.query.Sort":"FieldSort","wow.api.query.Sort.Direction":"SortDirection","wow.command.CommandStage":"CommandStage","wow.command.SimpleWaitSignal":"WaitSignal","wow.configuration.Aggregate":"Aggregate","wow.configuration.BoundedContext":"BoundedContext","wow.configuration.WowMetadata":"WowMetadata","wow.modeling.DomainEvent":"DomainEvent","wow.openapi.BatchResult":"BatchResult","wow.messaging.CompensationTarget":"CompensationTarget"};function m(n){if(!n)return{name:"",path:"/"};const e=Be[n];if(e)return{name:e,path:P};const t=n.split(".");let o=-1;for(let c=0;c<t.length;c++)if(t[c]&&/^[A-Z]/.test(t[c])){o=c;break}const r=t.slice(0,o),i=r.length>0?`/${r.join("/")}`:"/",s=t.slice(o);return{name:S(s),path:i}}function T(n){const e=C(n);return m(e)}class Ue{constructor(e,t,o,r){this.modelInfo=e,this.sourceFile=t,this.keySchema=o,this.outputDir=r}generate(){const e=this.process();e&&z(e,this.keySchema.schema,this.keySchema.key)}process(){const{schema:e}=this.keySchema;return W(e)?this.processEnum(e):R(e)?this.processInterface(e):O(e)?this.processArray(e):D(e)?this.processIntersection(e):k(e)?this.processComposition(e):this.processTypeAlias(e)}resolveReference(e){const t=T(e);return be(this.modelInfo,this.sourceFile,this.outputDir,t),t}resolveAdditionalProperties(e){return e.additionalProperties===void 0||e.additionalProperties===!1?"":e.additionalProperties===!0?"[key: string]: any":`[key: string]: ${this.resolveType(e.additionalProperties)}`}resolvePropertyDefinitions(e){const{properties:t}=e;return Object.entries(t).map(([o,r])=>{const i=this.resolveType(r);if(!g(r)){const s=se(r),a=ie(s,`
|
|
3
3
|
* `);if(a)return`
|
|
4
4
|
/**
|
|
5
5
|
* ${a}
|
|
@@ -8,14 +8,14 @@
|
|
|
8
8
|
`}return`${o}: ${i}`})}resolveObjectType(e){const t=[];if(R(e)){const r=this.resolvePropertyDefinitions(e);t.push(...r)}const o=this.resolveAdditionalProperties(e);return o&&t.push(o),t.length===0?"Record<string, any>":`{
|
|
9
9
|
${t.join(`;
|
|
10
10
|
`)};
|
|
11
|
-
}`}resolveMapValueType(e){return e.additionalProperties===void 0||e.additionalProperties===!1||e.additionalProperties===!0?"any":this.resolveType(e.additionalProperties)}resolveType(e){if(
|
|
11
|
+
}`}resolveMapValueType(e){return e.additionalProperties===void 0||e.additionalProperties===!1||e.additionalProperties===!0?"any":this.resolveType(e.additionalProperties)}resolveType(e){if(g(e))return this.resolveReference(e).name;if(Te(e))return`Record<string,${this.resolveMapValueType(e)}>`;if(e.const)return`'${e.const}'`;if(W(e))return e.enum.map(t=>`'${t}'`).join(" | ");if(k(e)){const o=(e.oneOf||e.anyOf||e.allOf||[]).map(i=>this.resolveType(i)),r=D(e)?" & ":" | ";return`(${o.join(r)})`}if(O(e)){const t=this.resolveType(e.items);return $e(t)}return e.type==="object"?this.resolveObjectType(e):e.type?E(e.type):"any"}processEnum(e){return this.sourceFile.addEnum({name:this.modelInfo.name,isExported:!0,members:e.enum.filter(t=>typeof t=="string"&&t.length>0).map(t=>({name:he(t),initializer:`'${t}'`}))})}addPropertyToInterface(e,t,o){const r=this.resolveType(o);let i=e.getProperty(t);i?i.setType(r):i=e.addProperty({name:t,type:r}),g(o)||z(i,o)}processInterface(e){const t=this.sourceFile.addInterface({name:this.modelInfo.name,isExported:!0}),o=e.properties||{};return Object.entries(o).forEach(([r,i])=>{this.addPropertyToInterface(t,r,i)}),e.additionalProperties&&t.addIndexSignature({keyName:"key",keyType:"string",returnType:this.resolveType(e.additionalProperties===!0?{}:e.additionalProperties)}).addJsDoc("Additional properties"),t}processArray(e){const t=this.resolveType(e.items);return this.sourceFile.addTypeAlias({name:this.modelInfo.name,type:`Array<${t}>`,isExported:!0})}processComposition(e){return this.sourceFile.addTypeAlias({name:this.modelInfo.name,type:this.resolveType(e),isExported:!0})}processIntersection(e){const t=this.sourceFile.addInterface({name:this.modelInfo.name,isExported:!0});return e.allOf.forEach(o=>{if(g(o)){const r=this.resolveType(o);t.addExtends(r);return}R(o)&&Object.entries(o.properties).forEach(([r,i])=>{this.addPropertyToInterface(t,r,i)})}),t}processTypeAlias(e){return this.sourceFile.addTypeAlias({name:this.modelInfo.name,type:this.resolveType(e),isExported:!0})}}class Qe{constructor(e){this.context=e}getOrCreateSourceFile(e){const t=Ne(e);return this.context.getOrCreateSourceFile(t)}generate(){const e=this.context.openAPI.components?.schemas;if(!e){this.context.logger.info("No schemas found in OpenAPI specification");return}const t=this.stateAggregatedTypeNames(),o=this.filterSchemas(e,t);this.context.logger.progress(`Generating models for ${o.length} schemas`),o.forEach((r,i)=>{this.context.logger.progressWithCount(i+1,o.length,`Processing schema: ${r.key}`,2),this.generateKeyedSchema(r)}),this.context.logger.success("Model generation completed")}filterSchemas(e,t){return Object.entries(e).map(([o,r])=>({key:o,schema:r})).filter(o=>!this.isWowSchema(o.key,t))}isWowSchema(e,t){if(e!=="wow.api.query.PagedList"&&e.startsWith("wow.api.query.")&&e.endsWith("PagedList"))return!1;if(e.startsWith("wow.")||e.endsWith("AggregatedCondition")||e.endsWith("AggregatedDomainEventStream")||e.endsWith("AggregatedDomainEventStreamPagedList")||e.endsWith("AggregatedDomainEventStreamServerSentEventNonNullData")||e.endsWith("AggregatedListQuery")||e.endsWith("AggregatedPagedQuery")||e.endsWith("AggregatedSingleQuery"))return!0;const o=m(e);return t.has(o.name)}aggregatedSchemaSuffix=["MaterializedSnapshot","MaterializedSnapshotPagedList","MaterializedSnapshotServerSentEventNonNullData","PagedList","ServerSentEventNonNullData","Snapshot","StateEvent"];stateAggregatedTypeNames(){const e=new Set;for(const t of this.context.contextAggregates.values())for(const o of t)this.aggregatedSchemaSuffix.forEach(r=>{const i=m(o.state.key),s=S(i.name)+r;e.add(s)});return e}generateKeyedSchema(e){const t=m(e.key),o=this.getOrCreateSourceFile(t);new Ue(t,o,e,this.context.outputDir).generate()}}const Je="@ahoo-wang/fetcher-decorator",Ve=["type ApiMetadata","type ApiMetadataCapable","type ParameterRequest","api","get","post","put","patch","del","request","attribute","path","autoGeneratedError"],He={type:"Promise<Response>",metadata:"{resultExtractor: ResultExtractors.Response }"},U={type:"Promise<string>",metadata:"{resultExtractor: ResultExtractors.Text }"},N=`{
|
|
12
12
|
headers: { Accept: ContentTypeValues.TEXT_EVENT_STREAM },
|
|
13
13
|
resultExtractor: JsonEventStreamResultExtractor,
|
|
14
|
-
}`;function se(n){A(n,Qe,Je)}function ae(n,e,t=[]){return e.addClass({name:n,isExported:!0,decorators:[{name:"api",arguments:t}]})}function ce(n,e){n.addImplements("ApiMetadataCapable"),n.addConstructor({parameters:[{name:"apiMetadata",type:"ApiMetadata",hasQuestionToken:e===void 0,scope:x.Scope.Public,isReadonly:!0,initializer:e}]})}const He="@ahoo-wang/fetcher-eventstream";function ge(n){A(n,He,["JsonEventStreamResultExtractor","type JsonServerSentEventStream"])}function Ke(n){let e=0,t=0;return n.commands.forEach(o=>{o.path.startsWith(_.ResourceAttributionPathSpec.TENANT)&&(e+=1),o.path.startsWith(_.ResourceAttributionPathSpec.OWNER)&&(t+=1)}),e===0&&t===0?"ResourceAttributionPathSpec.NONE":e>t?"ResourceAttributionPathSpec.TENANT":"ResourceAttributionPathSpec.OWNER"}function ue(n,e,t,o){const r=`${t.contextAlias}/${t.aggregateName}/${o}.ts`;return oe(n,e,r)}function Ye(n,e){return`${S(n.aggregateName)}${e}`}function M(n){return n==="delete"?"del":n}const Xe="x-fetcher-method";function Ze(n,e){const t=n[Xe];if(t)return t;if(!n.operationId)return;const o=w(n.operationId);for(let r=o.length-1;r>=0;r--){const i=y(o.slice(r));if(!e(i))return i}return y(o)}class et{constructor(e){this.context=e,this.apiMetadataCtorInitializer=this.context.currentContextAlias?`{basePath:'${this.context.currentContextAlias}'}`:void 0}defaultParameterRequestType="ParameterRequest";defaultReturnType=Ve;apiMetadataCtorInitializer;generate(){this.context.logger.info("Starting API client generation");const e=this.resolveApiTags();this.context.logger.info(`Resolved ${e.size} API client tags: ${Array.from(e.keys()).join(", ")}`);const t=this.groupOperations(e);this.context.logger.info(`Grouped operations into ${t.size} tag groups`),this.generateApiClients(e,t),this.context.logger.success("API client generation completed")}generateApiClients(e,t){this.context.logger.info(`Generating ${t.size} API client classes`);let o=0;for(const[r,i]of t){o++,this.context.logger.progressWithCount(o,t.size,`Generating API client for tag: ${r}`);const s=e.get(r);this.generateApiClient(s,i)}}createApiClientFile(e){let t=e.path;return this.context.currentContextAlias&&(t=h.combineURLs(this.context.currentContextAlias,t)),t=h.combineURLs(t,`${e.name}ApiClient.ts`),this.context.logger.info(`Creating API client file: ${t}`),this.context.getOrCreateSourceFile(t)}generateApiClient(e,t){const o=m(e.name);this.context.logger.info(`Generating API client class: ${o.name}ApiClient with ${t.size} operations`);const r=this.createApiClientFile(o);se(r),ge(r);const i=ae(o.name+"ApiClient",r);v(i,[e.description]),ce(i,this.apiMetadataCtorInitializer),this.context.logger.info(`Processing ${t.size} operations for ${o.name}ApiClient`),t.forEach(s=>{this.processOperation(e,r,i,s)}),this.context.logger.success(`Completed API client: ${o.name}ApiClient`)}getMethodName(e,t){const o=Ze(t,r=>e.getMethod(r)!==void 0);if(!o)throw new Error(`Unable to resolve method name for apiClientClass:${e.getName()}.`);return o}resolveRequestType(e,t){if(!t.requestBody)return this.context.logger.info(`No request body found for operation ${t.operationId}, using default: ${this.defaultParameterRequestType}`),this.defaultParameterRequestType;let o;if(u(t.requestBody)?(this.context.logger.info(`Extracting request body from reference for operation: ${t.operationId}`),o=V(t.requestBody,this.context.openAPI.components)):o=t.requestBody,!o)return this.context.logger.info(`Request body extraction failed for operation ${t.operationId}, using default: ${this.defaultParameterRequestType}`),this.defaultParameterRequestType;if(o.content["multipart/form-data"])return this.context.logger.info(`Detected multipart/form-data content for operation ${t.operationId}, using ParameterRequest<FormData>`),"ParameterRequest<FormData>";if(o.content["application/json"]){const r=o.content["application/json"].schema;if(u(r)){const i=T(r);this.context.logger.info(`Adding import for request body model: ${i.name} from ${i.path}`),f(e,this.context.outputDir,i);const s=`ParameterRequest<${i.name}>`;return this.context.logger.info(`Resolved request type for operation ${t.operationId}: ${s}`),s}}return this.context.logger.info(`Using default request type for operation ${t.operationId}: ${this.defaultParameterRequestType}`),this.defaultParameterRequestType}resolveParameters(e,t,o){const r=Ie(o,this.context.openAPI.components).filter(a=>!this.context.isIgnoreApiClientPathParameters(e.name,a.name));this.context.logger.info(`Found ${r.length} path parameters for operation ${o.operationId}`);const i=r.map(a=>{const c=Z(a);return this.context.logger.info(`Adding path parameter: ${a.name} (type: ${c})`),{name:a.name,type:c,hasQuestionToken:!1,decorators:[{name:"path",arguments:[`'${a.name}'`]}]}}),s=this.resolveRequestType(t,o);return this.context.logger.info(`Adding httpRequest parameter: ${s}`),i.push({name:"httpRequest",hasQuestionToken:s===this.defaultParameterRequestType,type:`${s}`,decorators:[{name:"request",arguments:[]}]}),this.context.logger.info("Adding attributes parameter: Record<string, any>"),i.push({name:"attributes",hasQuestionToken:!0,type:"Record<string, any>",decorators:[{name:"attribute",arguments:[]}]}),i}resolveSchemaReturnType(e,t){const o="Promise<any>";if(u(t)){const r=T(t);this.context.logger.info(`Adding import for response model: ${r.name} from ${r.path}`),f(e,this.context.outputDir,r);const i=`Promise<${r.name}>`;return this.context.logger.info(`Resolved reference return type: ${i}`),i}if(!t.type)return this.context.logger.info(`Schema has no type, using default return type: ${o}`),o;if(X(t.type)){const i=`Promise<${E(t.type)}>`;return this.context.logger.info(`Resolved primitive return type: ${i}`),i}return this.context.logger.info(`Using default return type: ${o}`),o}resolveReturnType(e,t){const o=F(t);if(!o)return this.context.logger.info(`No OK response found for operation ${t.operationId}, using default return type: ${this.defaultReturnType.type}`),this.defaultReturnType;const r=Y(o)||ye(o);if(r){const s=this.resolveSchemaReturnType(e,r);return this.context.logger.info(`Resolved JSON/wildcard response return type for operation ${t.operationId}: ${s}`),{type:s,metadata:s===Q.type?Q.metadata:void 0}}const i=he(o);if(i){if(u(i)){const a=I(i,this.context.openAPI.components);if(O(a)&&u(a.items)){const c=T(a.items);this.context.logger.info(`Adding import for event stream model: ${c.name} from ${c.path}`),f(e,this.context.outputDir,c);const l=`Promise<JsonServerSentEventStream<${c.name.includes("ServerSentEvent")?`${c.name}['data']`:c.name}>>`;return this.context.logger.info(`Resolved event stream return type for operation ${t.operationId}: ${l}`),{type:l,metadata:b}}}const s="Promise<JsonServerSentEventStream<any>>";return this.context.logger.info(`Resolved generic event stream return type for operation ${t.operationId}: ${s}`),{type:s,metadata:b}}return this.context.logger.info(`Using default return type for operation ${t.operationId}: ${this.defaultReturnType.type}`),this.defaultReturnType}processOperation(e,t,o,r){this.context.logger.info(`Processing operation: ${r.operation.operationId} (${r.method} ${r.path})`);const i=this.getMethodName(o,r.operation);this.context.logger.info(`Generated method name: ${i}`);const s=this.resolveParameters(e,t,r.operation),a=this.resolveReturnType(t,r.operation),c=a.metadata?{name:M(r.method),arguments:[`'${r.path}'`,a.metadata]}:{name:M(r.method),arguments:[`'${r.path}'`]};this.context.logger.info(`Creating method with ${s.length} parameters, return type: ${a.type}`);const g=o.addMethod({name:i,decorators:[c],parameters:s,returnType:a.type,statements:[`throw autoGeneratedError(${s.map(l=>l.name).join(",")});`]});v(g,[r.operation.summary,r.operation.description,`- operationId: \`${r.operation.operationId}\``,`- path: \`${r.path}\``]),this.context.logger.success(`Operation method generated: ${i}`)}groupOperations(e){this.context.logger.info("Grouping operations by API client tags");const t=new Map;let o=0;for(const[r,i]of Object.entries(this.context.openAPI.paths)){const s=N(i).filter(a=>{if(!a.operation.operationId)return!1;const c=a.operation.tags;return!c||c.length==0?!1:c.every(g=>e.has(g))});this.context.logger.info(`Path ${r}: found ${s.length} valid operations`);for(const a of s)a.operation.tags.forEach(c=>{const g={...a,path:r};t.has(c)||t.set(c,new Set),t.get(c).add(g),o++})}return this.context.logger.info(`Grouped ${o} operations into ${t.size} tag groups`),t}shouldIgnoreTag(e){return e==="wow"||e==="Actuator"||this.isAggregateTag(e)}resolveApiTags(){this.context.logger.info("Resolving API client tags from OpenAPI specification");const e=new Map,t=this.context.openAPI.tags?.length||0;for(const r of Object.values(this.context.openAPI.paths))N(r).forEach(i=>{i.operation.tags?.forEach(s=>{!this.shouldIgnoreTag(s)&&!e.has(s)&&e.set(s,{name:s,description:""})})});let o=0;return this.context.openAPI.tags?.forEach(r=>{this.shouldIgnoreTag(r.name)?this.context.logger.info(`Excluded tag: ${r.name} (wow/Actuator/aggregate)`):(e.set(r.name,r),o++,this.context.logger.info(`Included API client tag: ${r.name}`))}),this.context.logger.info(`Resolved ${o} API client tags from ${t} total tags`),e}isAggregateTag(e){for(const t of this.context.contextAggregates.values())for(const o of t)if(o.aggregate.tag.name===e)return!0;return!1}}class tt{constructor(e){this.context=e}commandEndpointPathsName="COMMAND_ENDPOINT_PATHS";defaultCommandClientOptionsName="DEFAULT_COMMAND_CLIENT_OPTIONS";generate(){const e=Array.from(this.context.contextAggregates.values()).reduce((o,r)=>o+r.size,0);this.context.logger.info("--- Generating Command Clients ---"),this.context.logger.progress(`Generating command clients for ${e} aggregates`);let t=0;for(const[,o]of this.context.contextAggregates)o.forEach(r=>{t++,this.context.logger.progressWithCount(t,e,`Processing command client for aggregate: ${r.aggregate.aggregateName}`),this.processAggregate(r)});this.context.logger.success("Command client generation completed")}processAggregate(e){this.context.logger.info(`Processing command client for aggregate: ${e.aggregate.aggregateName} in context: ${e.aggregate.contextAlias}`);const t=ue(this.context.project,this.context.outputDir,e.aggregate,"commandClient");this.context.logger.info(`Processing command endpoint paths for ${e.commands.size} commands`),this.processCommandEndpointPaths(t,e),this.context.logger.info(`Creating default command client options: ${this.defaultCommandClientOptionsName}`),t.addVariableStatement({declarationKind:x.VariableDeclarationKind.Const,declarations:[{name:this.defaultCommandClientOptionsName,type:"ApiMetadata",initializer:`{
|
|
14
|
+
}`;function ae(n){A(n,Je,Ve)}function ce(n,e,t=[]){return e.addClass({name:n,isExported:!0,decorators:[{name:"api",arguments:t}]})}function ge(n,e){n.addImplements("ApiMetadataCapable"),n.addConstructor({parameters:[{name:"apiMetadata",type:"ApiMetadata",hasQuestionToken:e===void 0,scope:x.Scope.Public,isReadonly:!0,initializer:e}]})}const Ke="@ahoo-wang/fetcher-eventstream";function ue(n){A(n,Ke,["JsonEventStreamResultExtractor","type JsonServerSentEventStream"])}function Ye(n){let e=0,t=0;return n.commands.forEach(o=>{o.path.startsWith(j.ResourceAttributionPathSpec.TENANT)&&(e+=1),o.path.startsWith(j.ResourceAttributionPathSpec.OWNER)&&(t+=1)}),e===0&&t===0?"ResourceAttributionPathSpec.NONE":e>t?"ResourceAttributionPathSpec.TENANT":"ResourceAttributionPathSpec.OWNER"}function le(n,e,t,o){const r=`${t.contextAlias}/${t.aggregateName}/${o}.ts`;return re(n,e,r)}function Xe(n,e){return`${S(n.aggregateName)}${e}`}function b(n){return n==="delete"?"del":n}const Ze="x-fetcher-method";function et(n,e){const t=n[Ze];if(t)return t;if(!n.operationId)return;const o=w(n.operationId);for(let r=o.length-1;r>=0;r--){const i=y(o.slice(r));if(!e(i))return i}return y(o)}class tt{constructor(e){this.context=e,this.apiMetadataCtorInitializer=this.context.currentContextAlias?`{basePath:'${this.context.currentContextAlias}'}`:void 0}defaultParameterRequestType="ParameterRequest";defaultReturnType=He;apiMetadataCtorInitializer;generate(){this.context.logger.info("Starting API client generation");const e=this.resolveApiTags();this.context.logger.info(`Resolved ${e.size} API client tags: ${Array.from(e.keys()).join(", ")}`);const t=this.groupOperations(e);this.context.logger.info(`Grouped operations into ${t.size} tag groups`),this.generateApiClients(e,t),this.context.logger.success("API client generation completed")}generateApiClients(e,t){this.context.logger.info(`Generating ${t.size} API client classes`);let o=0;for(const[r,i]of t){o++,this.context.logger.progressWithCount(o,t.size,`Generating API client for tag: ${r}`);const s=e.get(r);this.generateApiClient(s,i)}}createApiClientFile(e){let t=e.path;return this.context.currentContextAlias&&(t=h.combineURLs(this.context.currentContextAlias,t)),t=h.combineURLs(t,`${e.name}ApiClient.ts`),this.context.logger.info(`Creating API client file: ${t}`),this.context.getOrCreateSourceFile(t)}generateApiClient(e,t){const o=m(e.name);this.context.logger.info(`Generating API client class: ${o.name}ApiClient with ${t.size} operations`);const r=this.createApiClientFile(o);ae(r),ue(r);const i=ce(o.name+"ApiClient",r);v(i,[e.description]),ge(i,this.apiMetadataCtorInitializer),this.context.logger.info(`Processing ${t.size} operations for ${o.name}ApiClient`),t.forEach(s=>{this.processOperation(e,r,i,s)}),this.context.logger.success(`Completed API client: ${o.name}ApiClient`)}getMethodName(e,t){const o=et(t,r=>e.getMethod(r)!==void 0);if(!o)throw new Error(`Unable to resolve method name for apiClientClass:${e.getName()}.`);return o}resolveRequestType(e,t){if(!t.requestBody)return this.context.logger.info(`No request body found for operation ${t.operationId}, using default: ${this.defaultParameterRequestType}`),this.defaultParameterRequestType;let o;if(g(t.requestBody)?(this.context.logger.info(`Extracting request body from reference for operation: ${t.operationId}`),o=J(t.requestBody,this.context.openAPI.components)):o=t.requestBody,!o)return this.context.logger.info(`Request body extraction failed for operation ${t.operationId}, using default: ${this.defaultParameterRequestType}`),this.defaultParameterRequestType;if(o.content["multipart/form-data"])return this.context.logger.info(`Detected multipart/form-data content for operation ${t.operationId}, using ParameterRequest<FormData>`),"ParameterRequest<FormData>";if(o.content["application/json"]){const r=o.content["application/json"].schema;if(g(r)){const i=T(r);this.context.logger.info(`Adding import for request body model: ${i.name} from ${i.path}`),f(e,this.context.outputDir,i);const s=`ParameterRequest<${i.name}>`;return this.context.logger.info(`Resolved request type for operation ${t.operationId}: ${s}`),s}}return this.context.logger.info(`Using default request type for operation ${t.operationId}: ${this.defaultParameterRequestType}`),this.defaultParameterRequestType}resolveParameters(e,t,o){const r=ve(o,this.context.openAPI.components).filter(a=>!this.context.isIgnoreApiClientPathParameters(e.name,a.name));this.context.logger.info(`Found ${r.length} path parameters for operation ${o.operationId}`);const i=r.map(a=>{const c=ee(a);return this.context.logger.info(`Adding path parameter: ${a.name} (type: ${c})`),{name:a.name,type:c,hasQuestionToken:!1,decorators:[{name:"path",arguments:[`'${a.name}'`]}]}}),s=this.resolveRequestType(t,o);return this.context.logger.info(`Adding httpRequest parameter: ${s}`),i.push({name:"httpRequest",hasQuestionToken:s===this.defaultParameterRequestType,type:`${s}`,decorators:[{name:"request",arguments:[]}]}),this.context.logger.info("Adding attributes parameter: Record<string, any>"),i.push({name:"attributes",hasQuestionToken:!0,type:"Record<string, any>",decorators:[{name:"attribute",arguments:[]}]}),i}resolveSchemaReturnType(e,t){const o="Promise<any>";if(g(t)){const r=T(t);this.context.logger.info(`Adding import for response model: ${r.name} from ${r.path}`),f(e,this.context.outputDir,r);const i=`Promise<${r.name}>`;return this.context.logger.info(`Resolved reference return type: ${i}`),i}if(!t.type)return this.context.logger.info(`Schema has no type, using default return type: ${o}`),o;if(Y(t.type)){const i=`Promise<${E(t.type)}>`;return this.context.logger.info(`Resolved primitive return type: ${i}`),i}return this.context.logger.info(`Using default return type: ${o}`),o}resolveReturnType(e,t){const o=M(t);if(!o)return this.context.logger.info(`No OK response found for operation ${t.operationId}, using default return type: ${this.defaultReturnType.type}`),this.defaultReturnType;const r=K(o)||xe(o);if(r){const s=this.resolveSchemaReturnType(e,r);return this.context.logger.info(`Resolved JSON/wildcard response return type for operation ${t.operationId}: ${s}`),{type:s,metadata:s===U.type?U.metadata:void 0}}const i=ye(o);if(i){if(g(i)){const a=I(i,this.context.openAPI.components);if(O(a)&&g(a.items)){const c=T(a.items);this.context.logger.info(`Adding import for event stream model: ${c.name} from ${c.path}`),f(e,this.context.outputDir,c);const l=`Promise<JsonServerSentEventStream<${c.name.includes("ServerSentEvent")?`${c.name}['data']`:c.name}>>`;return this.context.logger.info(`Resolved event stream return type for operation ${t.operationId}: ${l}`),{type:l,metadata:N}}}const s="Promise<JsonServerSentEventStream<any>>";return this.context.logger.info(`Resolved generic event stream return type for operation ${t.operationId}: ${s}`),{type:s,metadata:N}}return this.context.logger.info(`Using default return type for operation ${t.operationId}: ${this.defaultReturnType.type}`),this.defaultReturnType}processOperation(e,t,o,r){this.context.logger.info(`Processing operation: ${r.operation.operationId} (${r.method} ${r.path})`);const i=this.getMethodName(o,r.operation);this.context.logger.info(`Generated method name: ${i}`);const s=this.resolveParameters(e,t,r.operation),a=this.resolveReturnType(t,r.operation),c=a.metadata?{name:b(r.method),arguments:[`'${r.path}'`,a.metadata]}:{name:b(r.method),arguments:[`'${r.path}'`]};this.context.logger.info(`Creating method with ${s.length} parameters, return type: ${a.type}`);const u=o.addMethod({name:i,decorators:[c],parameters:s,returnType:a.type,statements:[`throw autoGeneratedError(${s.map(l=>l.name).join(",")});`]});v(u,[r.operation.summary,r.operation.description,`- operationId: \`${r.operation.operationId}\``,`- path: \`${r.path}\``]),this.context.logger.success(`Operation method generated: ${i}`)}groupOperations(e){this.context.logger.info("Grouping operations by API client tags");const t=new Map,o=X(this.context.openAPI.paths).filter(i=>{if(!i.operation.operationId)return!1;const s=i.operation.tags;return!s||s.length==0?!1:s.every(a=>e.has(a))});let r=0;for(const i of o)i.operation.tags.forEach(s=>{t.has(s)||t.set(s,new Set),t.get(s).add(i),r++});return this.context.logger.info(`Grouped ${r} operations into ${t.size} tag groups`),t}shouldIgnoreTag(e){return e==="wow"||e==="Actuator"||this.isAggregateTag(e)}resolveApiTags(){this.context.logger.info("Resolving API client tags from OpenAPI specification");const e=new Map,t=this.context.openAPI.tags?.length||0;for(const r of Object.values(this.context.openAPI.paths))Z(r).forEach(i=>{i.operation.tags?.forEach(s=>{!this.shouldIgnoreTag(s)&&!e.has(s)&&e.set(s,{name:s,description:""})})});let o=0;return this.context.openAPI.tags?.forEach(r=>{this.shouldIgnoreTag(r.name)?this.context.logger.info(`Excluded tag: ${r.name} (wow/Actuator/aggregate)`):(e.set(r.name,r),o++,this.context.logger.info(`Included API client tag: ${r.name}`))}),this.context.logger.info(`Resolved ${o} API client tags from ${t} total tags`),e}isAggregateTag(e){for(const t of this.context.contextAggregates.values())for(const o of t)if(o.aggregate.tag.name===e)return!0;return!1}}class nt{constructor(e){this.context=e}commandEndpointPathsName="COMMAND_ENDPOINT_PATHS";defaultCommandClientOptionsName="DEFAULT_COMMAND_CLIENT_OPTIONS";generate(){const e=Array.from(this.context.contextAggregates.values()).reduce((o,r)=>o+r.size,0);this.context.logger.info("--- Generating Command Clients ---"),this.context.logger.progress(`Generating command clients for ${e} aggregates`);let t=0;for(const[,o]of this.context.contextAggregates)o.forEach(r=>{t++,this.context.logger.progressWithCount(t,e,`Processing command client for aggregate: ${r.aggregate.aggregateName}`),this.processAggregate(r)});this.context.logger.success("Command client generation completed")}processAggregate(e){this.context.logger.info(`Processing command client for aggregate: ${e.aggregate.aggregateName} in context: ${e.aggregate.contextAlias}`);const t=le(this.context.project,this.context.outputDir,e.aggregate,"commandClient");this.context.logger.info(`Processing command endpoint paths for ${e.commands.size} commands`),this.processCommandEndpointPaths(t,e),this.context.logger.info(`Creating default command client options: ${this.defaultCommandClientOptionsName}`),t.addVariableStatement({declarationKind:x.VariableDeclarationKind.Const,declarations:[{name:this.defaultCommandClientOptionsName,type:"ApiMetadata",initializer:`{
|
|
15
15
|
basePath: '${e.aggregate.contextAlias}'
|
|
16
|
-
}`}],isExported:!1}),this.context.logger.info(`Adding imports from ${P}: CommandRequest, CommandResult, CommandResultEventStream, DeleteAggregate, RecoverAggregate`),t.addImportDeclaration({moduleSpecifier:P,namedImports:["CommandRequest","CommandResult","CommandResultEventStream","DeleteAggregate","RecoverAggregate"],isTypeOnly:!0}),this.context.logger.info("Adding import from @ahoo-wang/fetcher-eventstream: JsonEventStreamResultExtractor"),
|
|
16
|
+
}`}],isExported:!1}),this.context.logger.info(`Adding imports from ${P}: CommandRequest, CommandResult, CommandResultEventStream, DeleteAggregate, RecoverAggregate`),t.addImportDeclaration({moduleSpecifier:P,namedImports:["CommandRequest","CommandResult","CommandResultEventStream","DeleteAggregate","RecoverAggregate"],isTypeOnly:!0}),this.context.logger.info("Adding import from @ahoo-wang/fetcher-eventstream: JsonEventStreamResultExtractor"),ue(t),this.context.logger.info("Adding import from @ahoo-wang/fetcher: ContentTypeValues"),A(t,"@ahoo-wang/fetcher",["ContentTypeValues"]),this.context.logger.info("Adding imports from @ahoo-wang/fetcher-decorator: ApiMetadata types and decorators"),ae(t),this.context.logger.info("Generating standard command client class"),this.processCommandClient(t,e),this.context.logger.info("Generating stream command client class"),this.processCommandClient(t,e,!0),this.context.logger.success(`Command client generation completed for aggregate: ${e.aggregate.aggregateName}`)}processCommandEndpointPaths(e,t){this.context.logger.info(`Creating command endpoint paths enum: ${this.commandEndpointPathsName}`);const o=e.addEnum({name:this.commandEndpointPathsName});t.commands.forEach(r=>{this.context.logger.info(`Adding command endpoint: ${r.name.toUpperCase()} = '${r.path}'`),o.addMember({name:r.name.toUpperCase(),initializer:`'${r.path}'`})}),this.context.logger.success(`Command endpoint paths enum created with ${t.commands.size} entries`)}getEndpointPath(e){return`${this.commandEndpointPathsName}.${e.name.toUpperCase()}`}processCommandClient(e,t,o=!1){let r="CommandClient",i=[],s="Promise<CommandResult>";o&&(r="Stream"+r,i=["''",N],s="Promise<CommandResultEventStream>");const a=Xe(t.aggregate,r),c=ce(a,e,i);ge(c,this.defaultCommandClientOptionsName),t.commands.forEach(u=>{this.processCommandMethod(t,e,c,u,s)})}resolveParameters(e,t,o){const r=m(o.schema.key);this.context.logger.info(`Adding import for command model: ${r.name} from path: ${r.path}`),f(t,this.context.outputDir,r);const i=o.pathParameters.filter(s=>!this.context.isIgnoreCommandClientPathParameters(e.name,s.name)).map(s=>{const a=ee(s);return this.context.logger.info(`Adding path parameter: ${s.name} (type: ${a})`),{name:s.name,type:a,hasQuestionToken:!1,decorators:[{name:"path",arguments:[`'${s.name}'`]}]}});return this.context.logger.info(`Adding command request parameter: commandRequest (type: CommandRequest<${r.name}>)`),i.push({name:"commandRequest",hasQuestionToken:Ie(o.schema.schema),type:`CommandRequest<${r.name}>`,decorators:[{name:"request",arguments:[]}]}),this.context.logger.info("Adding attributes parameter: attributes (type: Record<string, any>)"),i.push({name:"attributes",hasQuestionToken:!0,type:"Record<string, any>",decorators:[{name:"attribute",arguments:[]}]}),i}processCommandMethod(e,t,o,r,i){this.context.logger.info(`Generating command method: ${y(r.name)} for command: ${r.name}`),this.context.logger.info(`Command method details: HTTP ${r.method}, path: ${r.path}, return type: ${i}`);const s=this.resolveParameters(e.aggregate.tag,t,r),a=o.addMethod({name:y(r.name),decorators:[{name:b(r.method),arguments:[`${this.getEndpointPath(r)}`]}],parameters:s,returnType:i,statements:[`throw autoGeneratedError(${s.map(c=>c.name).join(",")});`]});this.context.logger.info(`Adding JSDoc documentation for method: ${y(r.name)}`),v(a,[r.summary,r.description,`- operationId: \`${r.operation.operationId}\``,`- path: \`${r.path}\``]),this.context.logger.success(`Command method generated: ${y(r.name)}`)}}class ot{constructor(e){this.context=e}generate(){const e=Array.from(this.context.contextAggregates.values()).reduce((o,r)=>o+r.size,0);this.context.logger.info("--- Generating Query Clients ---"),this.context.logger.progress(`Generating query clients for ${e} aggregates`);let t=0;for(const[,o]of this.context.contextAggregates)o.forEach(r=>{t++,this.context.logger.progressWithCount(t,e,`Processing query client for aggregate: ${r.aggregate.aggregateName}`),this.processQueryClient(r)});this.context.logger.success("Query client generation completed")}createClientFilePath(e,t){return le(this.context.project,this.context.outputDir,e,t)}processQueryClient(e){const t=this.createClientFilePath(e.aggregate,"queryClient");this.context.logger.info(`Processing query client for aggregate: ${e.aggregate.aggregateName} in context: ${e.aggregate.contextAlias}`),this.context.logger.info(`Adding imports from ${P}: QueryClientFactory, QueryClientOptions, ResourceAttributionPathSpec`),t.addImportDeclaration({moduleSpecifier:P,namedImports:["QueryClientFactory","QueryClientOptions","ResourceAttributionPathSpec"]});const o="DEFAULT_QUERY_CLIENT_OPTIONS";this.context.logger.info(`Creating default query client options: ${o}`),t.addVariableStatement({declarationKind:x.VariableDeclarationKind.Const,declarations:[{name:o,type:"QueryClientOptions",initializer:`{
|
|
17
17
|
contextAlias: '${e.aggregate.contextAlias}',
|
|
18
18
|
aggregateName: '${e.aggregate.aggregateName}',
|
|
19
|
-
resourceAttribution: ${
|
|
20
|
-
}`}],isExported:!1});const r=[];this.context.logger.info(`Processing ${e.events.size} domain events for aggregate: ${e.aggregate.aggregateName}`);for(const l of e.events.values()){const p=m(l.schema.key);this.context.logger.info(`Adding import for event model: ${p.name} from path: ${p.path}`),f(t,this.context.outputDir,p),r.push(p)}const i="DOMAIN_EVENT_TYPES",s=r.map(l=>l.name).join(" | ");this.context.logger.info(`Creating domain event types union: ${i} = ${s}`),t.addTypeAlias({name:i,type:s});const a=`${y(e.aggregate.aggregateName)}QueryClientFactory`,c=m(e.state.key),
|
|
19
|
+
resourceAttribution: ${Ye(e)},
|
|
20
|
+
}`}],isExported:!1});const r=[];this.context.logger.info(`Processing ${e.events.size} domain events for aggregate: ${e.aggregate.aggregateName}`);for(const l of e.events.values()){const p=m(l.schema.key);this.context.logger.info(`Adding import for event model: ${p.name} from path: ${p.path}`),f(t,this.context.outputDir,p),r.push(p)}const i="DOMAIN_EVENT_TYPES",s=r.map(l=>l.name).join(" | ");this.context.logger.info(`Creating domain event types union: ${i} = ${s}`),t.addTypeAlias({name:i,type:s});const a=`${y(e.aggregate.aggregateName)}QueryClientFactory`,c=m(e.state.key),u=m(e.fields.key);this.context.logger.info(`Adding import for state model: ${c.name} from path: ${c.path}`),f(t,this.context.outputDir,c),this.context.logger.info(`Adding import for fields model: ${u.name} from path: ${u.path}`),f(t,this.context.outputDir,u),this.context.logger.info(`Creating query client factory: ${a}`),t.addVariableStatement({declarationKind:x.VariableDeclarationKind.Const,declarations:[{name:a,initializer:`new QueryClientFactory<${c.name}, ${u.name} | string, ${i}>(${o})`}],isExported:!0}),this.context.logger.success(`Query client generation completed for aggregate: ${e.aggregate.aggregateName}`)}}class rt{constructor(e){this.context=e,this.queryClientGenerator=new ot(e),this.commandClientGenerator=new nt(e),this.apiClientGenerator=new tt(e)}queryClientGenerator;commandClientGenerator;apiClientGenerator;generate(){this.context.logger.info("--- Generating Clients ---"),this.context.logger.progress(`Generating clients for ${this.context.contextAggregates.size} bounded contexts`);let e=0;for(const[t]of this.context.contextAggregates)e++,this.context.logger.progressWithCount(e,this.context.contextAggregates.size,`Processing bounded context: ${t}`,1),this.processBoundedContext(t);this.queryClientGenerator.generate(),this.commandClientGenerator.generate(),this.apiClientGenerator.generate(),this.context.logger.success("Client generation completed")}processBoundedContext(e){const t=`${e}/boundedContext.ts`;this.context.logger.info(`Creating bounded context file: ${t}`);const o=this.context.getOrCreateSourceFile(t);this.context.logger.info(`Adding bounded context alias constant: BOUNDED_CONTEXT_ALIAS = '${e}'`),o.addStatements(`export const BOUNDED_CONTEXT_ALIAS = '${e}';`),this.context.logger.success(`Bounded context file created successfully: ${t}`)}}class it{project;openAPI;outputDir;contextAggregates;logger;config;defaultIgnorePathParameters=["tenantId","ownerId"];currentContextAlias;constructor(e){this.project=e.project,this.openAPI=e.openAPI,this.outputDir=e.outputDir,this.contextAggregates=e.contextAggregates,this.logger=e.logger,this.config=e.config??{},this.currentContextAlias=this.openAPI.info["x-wow-context-alias"]}getOrCreateSourceFile(e){return re(this.project,this.outputDir,e)}isIgnoreApiClientPathParameters(e,t){return(this.config.apiClients?.[e]?.ignorePathParameters??this.defaultIgnorePathParameters).includes(t)}isIgnoreCommandClientPathParameters(e,t){return this.defaultIgnorePathParameters.includes(t)}}const pe="./fetcher-generator.config.json";class st{constructor(e){this.options=e,this.project=new x.Project(e),this.options.logger.info("Project instance created with tsConfigFilePath: ",this.options.tsConfigFilePath)}project;async generate(){this.options.logger.info("Starting code generation from OpenAPI specification"),this.options.logger.info(`Input path: ${this.options.inputPath}`),this.options.logger.info(`Output directory: ${this.options.outputDir}`),this.options.logger.info("Parsing OpenAPI specification");const e=await we(this.options.inputPath);this.options.logger.info("OpenAPI specification parsed successfully"),this.options.logger.info("Resolving bounded context aggregates");const o=new ze(e).resolve();this.options.logger.info(`Resolved ${o.size} bounded context aggregates`);const r=this.options.configPath??pe;let i={};try{this.options.logger.info("Parsing configuration file:",r),i=await Oe(r)}catch(u){this.options.logger.info("Configuration file parsing failed ",u)}const s=new it({openAPI:e,project:this.project,outputDir:this.options.outputDir,contextAggregates:o,logger:this.options.logger,config:i});this.options.logger.info("Generating models"),new Qe(s).generate(),this.options.logger.info("Models generated successfully"),this.options.logger.info("Generating clients"),new rt(s).generate(),this.options.logger.info("Clients generated successfully"),this.options.logger.info("Generating index files"),this.generateIndex(),this.options.logger.info("Index files generated successfully"),this.options.logger.info("Optimizing source files"),this.optimizeSourceFiles(),this.options.logger.info("Source files optimized successfully"),this.options.logger.info("Saving project to disk"),await this.project.save(),this.options.logger.info("Code generation completed successfully")}generateIndex(){this.options.logger.info(`Generating index files for output directory: ${this.options.outputDir}`);const e=this.project.getDirectory(this.options.outputDir);if(!e){this.options.logger.info("Output directory not found, skipping index generation");return}this.processDirectory(e),this.generateIndexForDirectory(e),this.options.logger.info("Index file generation completed")}processDirectory(e){const t=e.getDirectories();this.options.logger.info(`Processing ${t.length} subdirectories`);for(const o of t)this.options.logger.info(`Processing subdirectory: ${o.getPath()}`),this.generateIndexForDirectory(o),this.processDirectory(o)}generateIndexForDirectory(e){const t=e.getPath();this.options.logger.info(`Generating index for directory: ${t}`);const o=e.getSourceFiles().filter(a=>a.getBaseName().endsWith(".ts")&&a.getBaseName()!=="index.ts"),r=e.getDirectories();if(this.options.logger.info(`Found ${o.length} TypeScript files and ${r.length} subdirectories in ${t}`),o.length===0&&r.length===0){this.options.logger.info(`No files or subdirectories to export in ${t}, skipping index generation`);return}const i=`${t}/index.ts`;this.options.logger.info(`Creating/updating index file: ${i}`);const s=this.project.getSourceFile(i)||this.project.createSourceFile(i,"",{overwrite:!0});s.removeText();for(const a of o){const c=`./${a.getBaseNameWithoutExtension()}`;this.options.logger.info(`Adding export for file: ${c}`),s.addExportDeclaration({moduleSpecifier:c,isTypeOnly:!1,namedExports:[]})}for(const a of r){const c=`./${a.getBaseName()}`;this.options.logger.info(`Adding export for subdirectory: ${c}`),s.addExportDeclaration({moduleSpecifier:c,isTypeOnly:!1,namedExports:[]})}this.options.logger.info(`Index file generated for ${t} with ${o.length+r.length} exports`)}optimizeSourceFiles(){const e=this.project.getSourceFiles();this.options.logger.info(`Optimizing ${e.length} source files`),e.forEach((t,o)=>{this.options.logger.info(`Optimizing file ${o+1}/${e.length}`),t.formatText(),t.organizeImports(),t.fixMissingImports()}),this.options.logger.info("All source files optimized")}}exports.CodeGenerator=st;exports.DEFAULT_CONFIG_PATH=pe;
|
|
21
21
|
//# sourceMappingURL=index.cjs.map
|