1- import SwaggerParser , { type resolve } from "@apidevtools/swagger-parser" ;
1+ import type { resolve } from "@apidevtools/swagger-parser" ;
22import { consola } from "consola" ;
33import lodash from "lodash" ;
44import * as typescript from "typescript" ;
@@ -11,7 +11,6 @@ import { CodeGenConfig } from "./configuration.js";
1111import { SchemaComponentsMap } from "./schema-components-map.js" ;
1212import { SchemaParserFabric } from "./schema-parser/schema-parser-fabric.js" ;
1313import { SchemaRoutes } from "./schema-routes/schema-routes.js" ;
14- import { SchemaWalker } from "./schema-walker.js" ;
1514import { SwaggerSchemaResolver } from "./swagger-schema-resolver.js" ;
1615import { TemplatesWorker } from "./templates-worker.js" ;
1716import { JavascriptTranslator } from "./translators/javascript.js" ;
@@ -45,24 +44,17 @@ export class CodeGenProcess {
4544 fileSystem : FileSystem ;
4645 codeFormatter : CodeFormatter ;
4746 templatesWorker : TemplatesWorker ;
48- schemaWalker : SchemaWalker ;
4947 javascriptTranslator : JavascriptTranslator ;
50- swaggerParser : SwaggerParser ;
5148 swaggerRefs : Awaited < ReturnType < typeof resolve > > | undefined | null ;
5249
5350 constructor ( config : Partial < GenerateApiConfiguration [ "config" ] > ) {
5451 this . config = new CodeGenConfig ( config ) ;
55- this . swaggerParser = new SwaggerParser ( ) ;
5652 this . fileSystem = new FileSystem ( ) ;
5753 this . swaggerSchemaResolver = new SwaggerSchemaResolver (
5854 this . config ,
5955 this . fileSystem ,
6056 ) ;
61- this . schemaWalker = new SchemaWalker (
62- this . config ,
63- this . swaggerSchemaResolver ,
64- ) ;
65- this . schemaComponentsMap = new SchemaComponentsMap ( this . config , this ) ;
57+ this . schemaComponentsMap = new SchemaComponentsMap ( this . config ) ;
6658 this . typeNameFormatter = new TypeNameFormatter ( this . config ) ;
6759 this . templatesWorker = new TemplatesWorker (
6860 this . config ,
@@ -75,11 +67,9 @@ export class CodeGenProcess {
7567 this . templatesWorker ,
7668 this . schemaComponentsMap ,
7769 this . typeNameFormatter ,
78- this . schemaWalker ,
7970 ) ;
8071 this . schemaRoutes = new SchemaRoutes (
8172 this . config ,
82- this ,
8373 this . schemaParserFabric ,
8474 this . schemaComponentsMap ,
8575 this . templatesWorker ,
@@ -99,73 +89,35 @@ export class CodeGenProcess {
9989 templatesToRender : this . templatesWorker . getTemplates ( this . config ) ,
10090 } ) ;
10191
102- const swagger = await this . swaggerSchemaResolver . create ( ) ;
103-
104- this . swaggerSchemaResolver . fixSwaggerSchema ( swagger ) ;
105-
106- try {
107- this . swaggerRefs = await this . swaggerParser . resolve (
108- this . config . url || this . config . input || ( this . config . spec as any ) ,
109- {
110- continueOnError : true ,
111- mutateInputSchema : true ,
112- validate : {
113- schema : false ,
114- spec : false ,
115- } ,
116- resolve : {
117- external : true ,
118- http : {
119- ...this . config . requestOptions ,
120- headers : Object . assign (
121- { } ,
122- this . config . authorizationToken
123- ? {
124- Authorization : this . config . authorizationToken ,
125- }
126- : { } ,
127- this . config . requestOptions ?. headers ?? { } ,
128- ) ,
129- } ,
130- } ,
131- } ,
132- ) ;
133- this . swaggerRefs . set ( "fixed-swagger-schema" , swagger . usageSchema as any ) ;
134- this . swaggerRefs . set (
135- "original-swagger-schema" ,
136- swagger . originalSchema as any ,
137- ) ;
138- } catch ( e ) {
139- consola . error ( e ) ;
140- }
92+ const resolvedSwaggerSchema = await this . swaggerSchemaResolver . create ( ) ;
14193
14294 this . config . update ( {
143- swaggerSchema : swagger . usageSchema ,
144- originalSchema : swagger . originalSchema ,
95+ resolvedSwaggerSchema : resolvedSwaggerSchema ,
96+ swaggerSchema : resolvedSwaggerSchema . usageSchema ,
97+ originalSchema : resolvedSwaggerSchema . originalSchema ,
14598 } ) ;
14699
147- this . schemaWalker . addSchema ( "$usage" , swagger . usageSchema ) ;
148- this . schemaWalker . addSchema ( "$original" , swagger . originalSchema ) ;
149-
150100 consola . info ( "start generating your typescript api" ) ;
151101
152102 this . config . update (
153- this . config . hooks . onInit ( this . config , this ) || this . config ,
103+ this . config . hooks . onInit ?. ( this . config , this ) || this . config ,
154104 ) ;
155105
156106 this . schemaComponentsMap . clear ( ) ;
157107
158- lodash . each ( swagger . usageSchema . components , ( component , componentName ) =>
159- lodash . each ( component , ( rawTypeData , typeName ) => {
160- this . schemaComponentsMap . createComponent (
161- this . schemaComponentsMap . createRef ( [
162- "components" ,
163- componentName ,
164- typeName ,
165- ] ) ,
166- rawTypeData ,
167- ) ;
168- } ) ,
108+ lodash . each (
109+ resolvedSwaggerSchema . usageSchema . components ,
110+ ( component , componentName ) =>
111+ lodash . each ( component , ( rawTypeData , typeName ) => {
112+ this . schemaComponentsMap . createComponent (
113+ this . schemaComponentsMap . createRef ( [
114+ "components" ,
115+ componentName ,
116+ typeName ,
117+ ] ) ,
118+ rawTypeData ,
119+ ) ;
120+ } ) ,
169121 ) ;
170122
171123 // Set all discriminators at the top
@@ -190,13 +142,10 @@ export class CodeGenProcess {
190142 return parsed ;
191143 } ) ;
192144
193- this . schemaRoutes . attachSchema ( {
194- usageSchema : swagger . usageSchema ,
195- parsedSchemas,
196- } ) ;
145+ this . schemaRoutes . attachSchema ( resolvedSwaggerSchema , parsedSchemas ) ;
197146
198147 const rawConfiguration = {
199- apiConfig : this . createApiConfig ( swagger . usageSchema ) ,
148+ apiConfig : this . createApiConfig ( resolvedSwaggerSchema . usageSchema ) ,
200149 config : this . config ,
201150 modelTypes : this . collectModelTypes ( ) ,
202151 hasSecurityRoutes : this . schemaRoutes . hasSecurityRoutes ,
@@ -214,7 +163,7 @@ export class CodeGenProcess {
214163 } ;
215164
216165 const configuration =
217- this . config . hooks . onPrepareConfig ( rawConfiguration ) || rawConfiguration ;
166+ this . config . hooks . onPrepareConfig ?. ( rawConfiguration ) || rawConfiguration ;
218167
219168 if ( this . fileSystem . pathIsExist ( this . config . output ) ) {
220169 if ( this . config . cleanOutput ) {
0 commit comments