@@ -15,12 +15,14 @@ import * as fs from 'fs';
1515import * as p from 'path' ;
1616
1717import { parseApiSpec } from './apiSpec' ;
18- import { Components , parseRefs } from './ref' ;
18+ import { getRefs } from './ref' ;
1919import { convertRoutesToOpenAPI } from './openapi' ;
2020import type { Route } from './route' ;
2121import type { Schema } from './ir' ;
2222import { Project } from './project' ;
2323import { KNOWN_IMPORTS } from './knownImports' ;
24+ import { findSymbolInitializer } from './resolveInit' ;
25+ import { parseCodecInitializer } from './codec' ;
2426
2527const app = command ( {
2628 name : 'api-ts' ,
@@ -130,7 +132,7 @@ const app = command({
130132 process . exit ( 1 ) ;
131133 }
132134
133- const components : Components = { } ;
135+ const components : Record < string , Schema > = { } ;
134136 const queue : Schema [ ] = apiSpec . flatMap ( ( route ) => {
135137 return [
136138 ...route . parameters . map ( ( p ) => p . schema ) ,
@@ -140,13 +142,33 @@ const app = command({
140142 } ) ;
141143 let schema : Schema | undefined ;
142144 while ( ( ( schema = queue . pop ( ) ) , schema !== undefined ) ) {
143- const newComponents = parseRefs ( project . right , schema ) ;
144- for ( const [ name , schema ] of Object . entries ( newComponents ) ) {
145- if ( components [ name ] !== undefined ) {
145+ const refs = getRefs ( schema ) ;
146+ for ( const ref of refs ) {
147+ if ( components [ ref . name ] !== undefined ) {
146148 continue ;
147149 }
148- components [ name ] = schema ;
149- queue . push ( schema ) ;
150+ const sourceFile = project . right . get ( ref . location ) ;
151+ if ( sourceFile === undefined ) {
152+ console . error ( `Could not find source file '${ ref . location } '` ) ;
153+ process . exit ( 1 ) ;
154+ }
155+ const initE = findSymbolInitializer ( project . right , sourceFile , ref . name ) ;
156+ if ( E . isLeft ( initE ) ) {
157+ console . error (
158+ `Could not find symbol '${ ref . name } ' in '${ ref . location } ': ${ initE . left } ` ,
159+ ) ;
160+ process . exit ( 1 ) ;
161+ }
162+ const [ newSourceFile , init ] = initE . right ;
163+ const codecE = parseCodecInitializer ( project . right , newSourceFile , init ) ;
164+ if ( E . isLeft ( codecE ) ) {
165+ console . error (
166+ `Could not parse codec '${ ref . name } ' in '${ ref . location } ': ${ codecE . left } ` ,
167+ ) ;
168+ process . exit ( 1 ) ;
169+ }
170+ components [ ref . name ] = codecE . right ;
171+ queue . push ( codecE . right ) ;
150172 }
151173 }
152174
0 commit comments